fix the GET /fleet_maintained_apps endpoint query param and count value (#22857)

relates to #22826

fix to the `GET /fleet_maintained_apps` endpoint so it returns correct
results is given a `query` query param. Also the total count takes into
account the query in the request.
This commit is contained in:
Gabriel Hernandez 2024-10-11 15:07:02 +01:00 committed by GitHub
parent 6a25d6917a
commit 199f4c4683
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -130,11 +130,6 @@ WHERE NOT EXISTS (
)
)`
// build the count statement before adding the pagination constraints to the
// default stmt.
dbReader := ds.reader(ctx)
getAppsCountStmt := fmt.Sprintf(`SELECT COUNT(DISTINCT s.id) FROM (%s) AS s`, stmt)
args := []any{teamID, teamID}
if match := opt.MatchQuery; match != "" {
@ -143,6 +138,16 @@ WHERE NOT EXISTS (
args = append(args, match)
}
// perform a second query to grab the counts. Build the count statement before
// adding the pagination constraints to the stmt but after including the
// MatchQuery option sql.
dbReader := ds.reader(ctx)
getAppsCountStmt := fmt.Sprintf(`SELECT COUNT(DISTINCT s.id) FROM (%s) AS s`, stmt)
var counts int
if err := sqlx.GetContext(ctx, dbReader, &counts, getAppsCountStmt, args...); err != nil {
return nil, nil, ctxerr.Wrap(ctx, err, "get fleet maintained apps count")
}
stmtPaged, args := appendListOptionsWithCursorToSQL(stmt, args, &opt)
var avail []fleet.MaintainedApp
@ -150,12 +155,6 @@ WHERE NOT EXISTS (
return nil, nil, ctxerr.Wrap(ctx, err, "selecting available fleet managed apps")
}
// perform a second query to grab the counts
var counts int
if err := sqlx.GetContext(ctx, dbReader, &counts, getAppsCountStmt, args...); err != nil {
return nil, nil, ctxerr.Wrap(ctx, err, "get fleet maintained apps count")
}
meta := &fleet.PaginationMetadata{HasPreviousResults: opt.Page > 0, TotalResults: uint(counts)}
if len(avail) > int(opt.PerPage) {
meta.HasNextResults = true