Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding date filter to GetNodesCounts API #3586

Merged
merged 5 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion api/external/cfgmgmt/cfgmgmt.pb.policy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions api/external/cfgmgmt/cfgmgmt.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 36 additions & 18 deletions api/external/cfgmgmt/request/stats.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/external/cfgmgmt/request/stats.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ option go_package = "github.com/chef/automate/api/external/cfgmgmt/request";
message NodesCounts {
// List of filters to be applied to the node count results.
repeated string filter = 1;
// Earliest node check-in.
string start = 2;
// Latest node check-in
string end = 3;
}

message RunsCounts {
Expand Down
50 changes: 33 additions & 17 deletions api/interservice/cfgmgmt/request/stats.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/interservice/cfgmgmt/request/stats.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ option go_package = "github.com/chef/automate/api/interservice/cfgmgmt/request";

message NodesCounts {
repeated string filter = 1;
string start = 2;
string end = 3;
}

message RunsCounts {
Expand Down
14 changes: 14 additions & 0 deletions components/automate-gateway/api/cfgmgmt.pb.swagger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions components/automate-gateway/handler/cfgmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ func (s *CfgMgmtServer) GetNodesCounts(ctx context.Context, request *cfgReq.Node

cfgMgmtRequest := &cmsReq.NodesCounts{
Filter: request.Filter,
Start: request.Start,
End: request.End,
}

cfgmgmtNodesCounts, err := s.cfgMgmtClient.GetNodesCounts(ctx, cfgMgmtRequest)
Expand Down
4 changes: 2 additions & 2 deletions components/config-mgmt-service/backend/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Client interface {
NodeExists(nodeID string, projectFilters map[string][]string) (bool, error)
// @params (page, perPage, sortField, ascending, filters, startDate, endDate)
GetNodes(int, int, string, bool, map[string][]string, string, string) ([]Node, error)
// @params (filters)
GetNodesCounts(map[string][]string) (NodesCounts, error)
// @params (filters, startDate, endDate)
GetNodesCounts(map[string][]string, string, string) (NodesCounts, error)
// @params (node_id, page, per_page, filters, start, end)
GetRuns(string, int, int, map[string][]string, string, string) ([]AbridgedConverge, error)
// @params (filters nodeID start end)
Expand Down
13 changes: 10 additions & 3 deletions components/config-mgmt-service/backend/elastic/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,26 @@ func (es Backend) GetNodes(page int, perPage int, sortField string,
}

// GetNodesCounts - get the number of successful, failure, and missing nodes
func (es Backend) GetNodesCounts(filters map[string][]string) (backend.NodesCounts, error) {
func (es Backend) GetNodesCounts(filters map[string][]string,
startDate string, endDate string) (backend.NodesCounts, error) {
var ns = *new(backend.NodesCounts)

// Adding the exists = true filter to the list of filters, because nodes
// have documents that persist in elasticsearch to hold historical data
// even after the node no longer exists
filters["exists"] = []string{"true"}

boolQuery := newBoolQueryFromFilters(filters)
mainQuery := newBoolQueryFromFilters(filters)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change with this PR. The rest is passing the start and end date to this function.


rangeQuery, ok := newRangeQuery(startDate, endDate, NodeCheckin)

if ok {
mainQuery = mainQuery.Must(rangeQuery)
}

searchTerm := "status"

statusNodesBuckets, err := es.getAggregationBucket(boolQuery, IndexNodeState, searchTerm)
statusNodesBuckets, err := es.getAggregationBucket(mainQuery, IndexNodeState, searchTerm)
if err != nil {
return ns, err
}
Expand Down
3 changes: 2 additions & 1 deletion components/config-mgmt-service/backend/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func (m Backend) NodeExists(id string, filters map[string][]string) (bool, error
return false, nil
}

func (m Backend) GetNodesCounts(filters map[string][]string) (backend.NodesCounts, error) {
func (m Backend) GetNodesCounts(filters map[string][]string,
startDate string, endDate string) (backend.NodesCounts, error) {
ns := backend.NodesCounts{}
return ns, nil
}
Expand Down
2 changes: 1 addition & 1 deletion components/config-mgmt-service/backend/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestNodeExists(t *testing.T) {
func TestGetNodesCounts(t *testing.T) {
expected := *new(backend.NodesCounts)
filterMap := make(map[string][]string)
nodeState, err := subject.New().GetNodesCounts(filterMap)
nodeState, err := subject.New().GetNodesCounts(filterMap, "", "")
assert.Nil(t, err)
assert.Equal(t, nodeState, expected)
}
Expand Down
8 changes: 7 additions & 1 deletion components/config-mgmt-service/grpcserver/cfg_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,18 @@ func (s *CfgMgmtServer) GetNodesCounts(ctx context.Context,
return nodesCounts, errors.GrpcErrorFromErr(codes.InvalidArgument, err)
}

// Date Range
if !params.ValidateDateTimeRange(request.GetStart(), request.GetEnd()) {
return nodesCounts, status.Errorf(codes.InvalidArgument,
"Invalid start/end time. (format: YYYY-MM-DD'T'HH:mm:ssZ)")
}

filters, err = filterByProjects(ctx, filters)
if err != nil {
return nodesCounts, errors.GrpcErrorFromErr(codes.Internal, err)
}

state, err := s.client.GetNodesCounts(filters)
state, err := s.client.GetNodesCounts(filters, request.GetStart(), request.GetEnd())
if err != nil {
return nodesCounts, errors.GrpcErrorFromErr(codes.Internal, err)
}
Expand Down
Loading