diff --git a/server/datastore/mysql/labels.go b/server/datastore/mysql/labels.go index 1bea42c1f3..6b9f07d097 100644 --- a/server/datastore/mysql/labels.go +++ b/server/datastore/mysql/labels.go @@ -509,6 +509,7 @@ func (ds *Datastore) ListHostsInLabel(ctx context.Context, filter fleet.TeamFilt JOIN hosts h ON (lm.host_id = h.id) LEFT JOIN host_seen_times hst ON (h.id=hst.host_id) LEFT JOIN host_disks hd ON (h.id=hd.host_id) + %s %s ` failingPoliciesSelect := `, @@ -525,7 +526,12 @@ func (ds *Datastore) ListHostsInLabel(ctx context.Context, filter fleet.TeamFilt failingPoliciesJoin = "" } - query := fmt.Sprintf(queryFmt, failingPoliciesSelect, failingPoliciesJoin) + displayNameJoin := "" + if opt.ListOptions.OrderKey == "display_name" { + displayNameJoin = ` JOIN host_display_names hdn ON h.id = hdn.host_id ` + } + + query := fmt.Sprintf(queryFmt, failingPoliciesSelect, failingPoliciesJoin, displayNameJoin) query, params := ds.applyHostLabelFilters(filter, lid, query, opt) @@ -553,8 +559,15 @@ func (ds *Datastore) applyHostLabelFilters(filter fleet.TeamFilter, lid uint, qu func (ds *Datastore) CountHostsInLabel(ctx context.Context, filter fleet.TeamFilter, lid uint, opt fleet.HostListOptions) (int, error) { query := `SELECT count(*) FROM label_membership lm JOIN hosts h ON (lm.host_id = h.id) - LEFT JOIN host_seen_times hst ON (h.id=hst.host_id)` + LEFT JOIN host_seen_times hst ON (h.id=hst.host_id) + %s` + displayNameJoin := "" + if opt.ListOptions.OrderKey == "display_name" { + displayNameJoin = ` JOIN host_display_names hdn ON h.id = hdn.host_id ` + } + + query = fmt.Sprintf(query, displayNameJoin) query, params := ds.applyHostLabelFilters(filter, lid, query, opt) var count int diff --git a/server/service/integration_core_test.go b/server/service/integration_core_test.go index d78c03faa4..9c1f9d6c5e 100644 --- a/server/service/integration_core_test.go +++ b/server/service/integration_core_test.go @@ -2859,10 +2859,27 @@ func (s *integrationTestSuite) TestLabels() { s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d/hosts", lbl2.ID), nil, http.StatusOK, &listHostsResp) assert.Len(t, listHostsResp.Hosts, len(hosts)) + // list hosts in label searching by display_name + s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d/hosts", lbl2.ID), nil, http.StatusOK, &listHostsResp, "order_key", "display_name", "order_direction", "desc") + assert.Len(t, listHostsResp.Hosts, len(hosts)) + // first in the list is the last one, as the names are ordered with the index + // of creation, and vice-versa + assert.Equal(t, hosts[len(hosts)-1].ID, listHostsResp.Hosts[0].ID) + assert.Equal(t, hosts[0].ID, listHostsResp.Hosts[len(hosts)-1].ID) + + // count hosts in label order by display_name + var countResp countHostsResponse + s.DoJSON("GET", "/api/latest/fleet/hosts/count", nil, http.StatusOK, &countResp, "label_id", fmt.Sprint(lbl2.ID), "order_key", "display_name", "order_direction", "desc") + assert.Equal(t, len(hosts), countResp.Count) + // lists hosts in label without hosts s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d/hosts", lbl1.ID), nil, http.StatusOK, &listHostsResp) assert.Len(t, listHostsResp.Hosts, 0) + // count hosts in label + s.DoJSON("GET", "/api/latest/fleet/hosts/count", nil, http.StatusOK, &countResp, "label_id", fmt.Sprint(lbl1.ID)) + assert.Equal(t, 0, countResp.Count) + // lists hosts in invalid label s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d/hosts", lbl2.ID+1), nil, http.StatusOK, &listHostsResp) assert.Len(t, listHostsResp.Hosts, 0)