From af802dc15fc712c1d6115e3e785e47fc3c83901f Mon Sep 17 00:00:00 2001 From: Zach Wasserman Date: Tue, 20 Apr 2021 13:35:15 -0700 Subject: [PATCH] Add host and user counts to list teams response (#668) --- server/datastore/mysql/teams.go | 5 ++++- server/kolide/teams.go | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/server/datastore/mysql/teams.go b/server/datastore/mysql/teams.go index 72bdba605e..f9f0e123d6 100644 --- a/server/datastore/mysql/teams.go +++ b/server/datastore/mysql/teams.go @@ -81,7 +81,10 @@ func (d *Datastore) SaveTeam(team *kolide.Team) (*kolide.Team, error) { // kolide.ListOptions func (d *Datastore) ListTeams(opt kolide.ListOptions) ([]*kolide.Team, error) { query := ` - SELECT * FROM teams + SELECT *, + (SELECT count(*) FROM user_teams WHERE team_id = id) AS user_count, + (SELECT count(*) FROM hosts WHERE team_id = id) AS host_count + FROM teams WHERE TRUE ` query, params := searchLike(query, nil, opt.MatchQuery, teamSearchColumns...) diff --git a/server/kolide/teams.go b/server/kolide/teams.go index b1bc6e8565..496a81b617 100644 --- a/server/kolide/teams.go +++ b/server/kolide/teams.go @@ -54,8 +54,12 @@ type Team struct { // Derived from JOINs + // UserCount is the count of users with explicit roles on this team. + UserCount int `json:"user_count" db:"user_count"` // Users is the users that have a role on this team. Users []TeamUser `json:"users,omitempty"` + // UserCount is the count of hosts assigned to this team. + HostCount int `json:"host_count" db:"host_count"` // Hosts are the hosts assigned to the team. Hosts []Host `json:"hosts,omitempty"` }