mirror of
https://github.com/fleetdm/fleet
synced 2026-05-21 16:08:47 +00:00
Use id instead of seen time/created_at to sort hosts (#3482)
* Use id instead of seen time/created_at to sort hosts * Add test for ordering by id
This commit is contained in:
parent
5dc93a8c7a
commit
06a64cdd2c
3 changed files with 9 additions and 1 deletions
1
changes/use-index-for-sorting-hosts
Normal file
1
changes/use-index-for-sorting-hosts
Normal file
|
|
@ -0,0 +1 @@
|
|||
* Improve performance of sorting hosts in certain scenarios.
|
||||
|
|
@ -816,7 +816,7 @@ func (d *Datastore) SearchHosts(ctx context.Context, filter fleet.TeamFilter, qu
|
|||
args = append(args, in)
|
||||
sqlb.WriteString(" id NOT IN (?) AND ")
|
||||
sqlb.WriteString(d.whereFilterHostsByTeams(filter, "h"))
|
||||
sqlb.WriteString(` ORDER BY COALESCE(hst.seen_time, h.created_at) DESC LIMIT 10`)
|
||||
sqlb.WriteString(` ORDER BY h.id DESC LIMIT 10`)
|
||||
|
||||
sql, args, err := sqlx.In(sqlb.String(), args...)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -1017,6 +1017,13 @@ func testHostsSearch(t *testing.T, ds *Datastore) {
|
|||
assert.Nil(t, err)
|
||||
require.Len(t, hosts, 1)
|
||||
assert.Equal(t, hosts[0].ID, h2.ID)
|
||||
|
||||
// sorted by ids desc
|
||||
filter = fleet.TeamFilter{User: userObs, IncludeObserver: true}
|
||||
hits, err = ds.SearchHosts(context.Background(), filter, "")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, hits, 3)
|
||||
assert.Equal(t, []uint{h3.ID, h2.ID, h1.ID}, []uint{hits[0].ID, hits[1].ID, hits[2].ID})
|
||||
}
|
||||
|
||||
func testHostsSearchLimit(t *testing.T, ds *Datastore) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue