2016-11-02 14:59:53 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2017-01-04 21:16:17 +00:00
|
|
|
"time"
|
|
|
|
|
2016-11-02 14:59:53 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/kolide"
|
2016-11-02 14:59:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-11-16 21:07:50 +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 {
|
2020-03-31 23:14:26 +00:00
|
|
|
HostResponse
|
2016-11-02 14:59:53 +00:00
|
|
|
DisplayText string `json:"display_text"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type labelSearchResult struct {
|
|
|
|
kolide.Label
|
2020-05-21 18:11:02 +00:00
|
|
|
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 {
|
2016-12-13 23:59:59 +00:00
|
|
|
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{
|
2020-03-31 23:14:26 +00:00
|
|
|
HostResponse{
|
2017-01-04 21:16:17 +00:00
|
|
|
Host: host,
|
2017-04-18 17:39:50 +00:00
|
|
|
Status: host.Status(time.Now()),
|
2017-01-04 21:16:17 +00:00
|
|
|
},
|
2016-11-02 14:59:53 +00:00
|
|
|
host.HostName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, label := range results.Labels {
|
|
|
|
targets.Labels = append(targets.Labels,
|
|
|
|
labelSearchResult{
|
2020-05-21 18:11:02 +00:00
|
|
|
Label: label,
|
|
|
|
DisplayText: label.Name,
|
|
|
|
Count: label.HostCount,
|
2016-11-02 14:59:53 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-12-06 17:37:22 +00:00
|
|
|
metrics, err := svc.CountHostsInTargets(ctx, req.Selected.Hosts, req.Selected.Labels)
|
2016-11-18 17:23:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return searchTargetsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:59:53 +00:00
|
|
|
return searchTargetsResponse{
|
2016-12-13 23:59:59 +00:00
|
|
|
Targets: targets,
|
|
|
|
TargetsCount: metrics.TotalHosts,
|
|
|
|
TargetsOnline: metrics.OnlineHosts,
|
|
|
|
TargetsOffline: metrics.OfflineHosts,
|
|
|
|
TargetsMissingInAction: metrics.MissingInActionHosts,
|
2016-11-02 14:59:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|