diff --git a/server/kolide/hosts.go b/server/kolide/hosts.go index 5aab473faf..9ef1101620 100644 --- a/server/kolide/hosts.go +++ b/server/kolide/hosts.go @@ -20,6 +20,7 @@ type HostStore interface { type HostService interface { ListHosts(ctx context.Context) ([]*Host, error) GetHost(ctx context.Context, id uint) (*Host, error) + HostStatus(ctx context.Context, host Host) string DeleteHost(ctx context.Context, id uint) error } diff --git a/server/service/endpoint_hosts.go b/server/service/endpoint_hosts.go index cc5250f261..dc6e904dcc 100644 --- a/server/service/endpoint_hosts.go +++ b/server/service/endpoint_hosts.go @@ -6,6 +6,11 @@ import ( "golang.org/x/net/context" ) +type hostResponse struct { + kolide.Host + Status string `json:"status"` +} + //////////////////////////////////////////////////////////////////////////////// // Get Host //////////////////////////////////////////////////////////////////////////////// @@ -15,8 +20,8 @@ type getHostRequest struct { } type getHostResponse struct { - Host *kolide.Host `json:"host"` - Err error `json:"error,omitempty"` + Host *hostResponse `json:"host"` + Err error `json:"error,omitempty"` } func (r getHostResponse) error() error { return r.Err } @@ -28,7 +33,7 @@ func makeGetHostEndpoint(svc kolide.Service) endpoint.Endpoint { if err != nil { return getHostResponse{Err: err}, nil } - return getHostResponse{host, nil}, nil + return getHostResponse{&hostResponse{*host, svc.HostStatus(ctx, *host)}, nil}, nil } } @@ -37,8 +42,8 @@ func makeGetHostEndpoint(svc kolide.Service) endpoint.Endpoint { //////////////////////////////////////////////////////////////////////////////// type listHostsResponse struct { - Hosts []kolide.Host `json:"hosts"` - Err error `json:"error,omitempty"` + Hosts []hostResponse `json:"hosts"` + Err error `json:"error,omitempty"` } func (r listHostsResponse) error() error { return r.Err } @@ -50,9 +55,9 @@ func makeListHostsEndpoint(svc kolide.Service) endpoint.Endpoint { return listHostsResponse{Err: err}, nil } - resp := listHostsResponse{Hosts: []kolide.Host{}} + resp := listHostsResponse{Hosts: []hostResponse{}} for _, host := range hosts { - resp.Hosts = append(resp.Hosts, *host) + resp.Hosts = append(resp.Hosts, hostResponse{*host, svc.HostStatus(ctx, *host)}) } return resp, nil } diff --git a/server/service/service_hosts.go b/server/service/service_hosts.go index adcb638f41..77d7e386d5 100644 --- a/server/service/service_hosts.go +++ b/server/service/service_hosts.go @@ -1,6 +1,8 @@ package service import ( + "time" + "github.com/kolide/kolide-ose/server/kolide" "golang.org/x/net/context" ) @@ -13,6 +15,14 @@ func (svc service) GetHost(ctx context.Context, id uint) (*kolide.Host, error) { return svc.ds.Host(id) } +func (svc service) HostStatus(ctx context.Context, host kolide.Host) string { + if host.UpdatedAt.Add(30 * time.Minute).Before(svc.clock.Now()) { + return "offline" + } else { + return "online" + } +} + func (svc service) DeleteHost(ctx context.Context, id uint) error { host, err := svc.ds.Host(id) if err != nil {