fleet/server/service/endpoint_targets.go

119 lines
3.1 KiB
Go
Raw Normal View History

2016-11-02 14:59:53 +00:00
package service
import (
"context"
"time"
2021-06-26 04:46:51 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/go-kit/kit/endpoint"
2016-11-02 14:59:53 +00:00
)
////////////////////////////////////////////////////////////////////////////////
// Search Targets
2016-11-02 14:59:53 +00:00
////////////////////////////////////////////////////////////////////////////////
type searchTargetsRequest struct {
// MatchQuery is the query SQL
MatchQuery string `json:"query"`
// QueryID is the ID of a saved query to run (used to determine if this is a
// query that observers can run).
QueryID *uint `json:"query_id"`
Selected fleet.HostTargets `json:"selected"`
2016-11-02 14:59:53 +00:00
}
type hostSearchResult struct {
HostResponse
2016-11-02 14:59:53 +00:00
DisplayText string `json:"display_text"`
}
type labelSearchResult struct {
*fleet.Label
DisplayText string `json:"display_text"`
Count int `json:"count"`
2016-11-02 14:59:53 +00:00
}
type teamSearchResult struct {
*fleet.Team
DisplayText string `json:"display_text"`
Count int `json:"count"`
}
2016-11-02 14:59:53 +00:00
type targetsData struct {
Hosts []hostSearchResult `json:"hosts"`
Labels []labelSearchResult `json:"labels"`
Teams []teamSearchResult `json:"teams"`
2016-11-02 14:59:53 +00:00
}
type searchTargetsResponse struct {
Targets *targetsData `json:"targets,omitempty"`
TargetsCount uint `json:"targets_count"`
TargetsOnline uint `json:"targets_online"`
TargetsOffline uint `json:"targets_offline"`
TargetsMissingInAction uint `json:"targets_missing_in_action"`
Err error `json:"error,omitempty"`
2016-11-02 14:59:53 +00:00
}
func (r searchTargetsResponse) error() error { return r.Err }
func makeSearchTargetsEndpoint(svc fleet.Service) endpoint.Endpoint {
2016-11-02 14:59:53 +00:00
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(searchTargetsRequest)
results, err := svc.SearchTargets(ctx, req.MatchQuery, req.QueryID, req.Selected)
2016-11-02 14:59:53 +00:00
if err != nil {
return searchTargetsResponse{Err: err}, nil
}
targets := &targetsData{
Hosts: []hostSearchResult{},
Labels: []labelSearchResult{},
Teams: []teamSearchResult{},
2016-11-02 14:59:53 +00:00
}
for _, host := range results.Hosts {
targets.Hosts = append(targets.Hosts,
hostSearchResult{
HostResponse{
Host: host,
Status: host.Status(time.Now()),
},
host.Hostname,
2016-11-02 14:59:53 +00:00
},
)
}
for _, label := range results.Labels {
targets.Labels = append(targets.Labels,
labelSearchResult{
Label: label,
DisplayText: label.Name,
Count: label.HostCount,
2016-11-02 14:59:53 +00:00
},
)
}
for _, team := range results.Teams {
targets.Teams = append(targets.Teams,
teamSearchResult{
Team: team,
DisplayText: team.Name,
Count: team.HostCount,
},
)
}
metrics, err := svc.CountHostsInTargets(ctx, req.QueryID, req.Selected)
if err != nil {
return searchTargetsResponse{Err: err}, nil
}
2016-11-02 14:59:53 +00:00
return searchTargetsResponse{
Targets: targets,
TargetsCount: metrics.TotalHosts,
TargetsOnline: metrics.OnlineHosts,
TargetsOffline: metrics.OfflineHosts,
TargetsMissingInAction: metrics.MissingInActionHosts,
2016-11-02 14:59:53 +00:00
}, nil
}
}