fleet/server/service/endpoint_targets.go

100 lines
2.6 KiB
Go
Raw Normal View History

2016-11-02 14:59:53 +00:00
package service
import (
"context"
"time"
2016-11-02 14:59:53 +00:00
"github.com/go-kit/kit/endpoint"
"github.com/fleetdm/fleet/server/kolide"
2016-11-02 14:59:53 +00:00
)
////////////////////////////////////////////////////////////////////////////////
// Search Targets
2016-11-02 14:59:53 +00:00
////////////////////////////////////////////////////////////////////////////////
type searchTargetsRequest struct {
Query string `json:"query"`
Selected struct {
Labels []uint `json:"labels"`
Hosts []uint `json:"hosts"`
} `json:"selected"`
}
type hostSearchResult struct {
HostResponse
2016-11-02 14:59:53 +00:00
DisplayText string `json:"display_text"`
}
type labelSearchResult struct {
kolide.Label
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"`
}
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 kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(searchTargetsRequest)
results, err := svc.SearchTargets(ctx, req.Query, req.Selected.Hosts, req.Selected.Labels)
if err != nil {
return searchTargetsResponse{Err: err}, nil
}
targets := &targetsData{
Hosts: []hostSearchResult{},
Labels: []labelSearchResult{},
}
for _, host := range results.Hosts {
targets.Hosts = append(targets.Hosts,
hostSearchResult{
HostResponse{
Host: host,
Status: host.Status(time.Now()),
},
2016-11-02 14:59:53 +00:00
host.HostName,
},
)
}
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
},
)
}
metrics, err := svc.CountHostsInTargets(ctx, req.Selected.Hosts, req.Selected.Labels)
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
}
}