Updated ListQueries method in DB layer

This commit is contained in:
Juan Fernandez 2023-07-07 07:36:09 -04:00
parent 390e0565d0
commit de9a1540d0
No known key found for this signature in database
2 changed files with 13 additions and 2 deletions

View File

@ -358,12 +358,20 @@ func (ds *Datastore) ListQueries(ctx context.Context, opt fleet.ListQueryOptions
if opt.OnlyObserverCanRun { if opt.OnlyObserverCanRun {
sql += " AND q.observer_can_run=true" sql += " AND q.observer_can_run=true"
} }
args := []interface{}{false, aggregatedStatsTypeQuery}
whereClause := " AND team_id_char = ''"
if opt.TeamID != nil {
args = append(args, fmt.Sprint(*opt.TeamID))
whereClause = " AND team_id_char = ?"
}
sql += whereClause
sql = appendListOptionsToSQL(sql, &opt.ListOptions) sql = appendListOptionsToSQL(sql, &opt.ListOptions)
results := []*fleet.Query{} results := []*fleet.Query{}
if err := sqlx.SelectContext(ctx, ds.reader(ctx), &results, sql, false, aggregatedStatsTypeQuery); err != nil { if err := sqlx.SelectContext(ctx, ds.reader(ctx), &results, sql, args...); err != nil {
fmt.Println(err)
return nil, ctxerr.Wrap(ctx, err, "listing queries") return nil, ctxerr.Wrap(ctx, err, "listing queries")
} }

View File

@ -761,6 +761,9 @@ func (l ListOptions) UsesCursorPagination() bool {
type ListQueryOptions struct { type ListQueryOptions struct {
ListOptions ListOptions
// TeamID which team the queries belong to. If teamID is nil, then it is assumed the 'global'
// team
TeamID *uint
OnlyObserverCanRun bool OnlyObserverCanRun bool
} }