Add host and user counts to list teams response (#668)

This commit is contained in:
Zach Wasserman 2021-04-20 13:35:15 -07:00 committed by GitHub
parent 9ade086448
commit af802dc15f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View file

@ -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...)

View file

@ -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"`
}