-
Notifications
You must be signed in to change notification settings - Fork 209
Create GetInsightData for deploy frequency #1188
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
Changes from all commits
a34eb41
79393b0
6d0a32a
4344548
54d2c7a
6c1965f
8681e87
74cb3bd
efd3f84
79061bd
651c401
4939e23
fbbd84c
bb63a36
127d2a3
33abb45
e19da30
98c92e7
06e9695
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1311,6 +1311,91 @@ func (a *WebAPI) ListAPIKeys(ctx context.Context, req *webservice.ListAPIKeysReq | |
| } | ||
|
|
||
| // GetInsightData returns the accumulated insight data. | ||
| func (a *WebAPI) GetInsightData(_ context.Context, _ *webservice.GetInsightDataRequest) (*webservice.GetInsightDataResponse, error) { | ||
| func (a *WebAPI) GetInsightData(ctx context.Context, req *webservice.GetInsightDataRequest) (*webservice.GetInsightDataResponse, error) { | ||
| claims, err := rpcauth.ExtractClaims(ctx) | ||
| if err != nil { | ||
| a.logger.Error("failed to authenticate the current user", zap.Error(err)) | ||
| return nil, err | ||
| } | ||
|
|
||
| switch req.MetricsKind { | ||
| case model.InsightMetricsKind_DEPLOYMENT_FREQUENCY: | ||
| return a.getInsightDataForDeployFrequency(ctx, claims.Role.ProjectId, req) | ||
| } | ||
| return nil, status.Error(codes.Unimplemented, "") | ||
| } | ||
|
|
||
| // getInsightDataForDeployFrequency returns the accumulated insight data for deploy frequency. | ||
| // This function is temporary implementation for front end. | ||
| func (a *WebAPI) getInsightDataForDeployFrequency(ctx context.Context, projectID string, req *webservice.GetInsightDataRequest) (*webservice.GetInsightDataResponse, error) { | ||
| counts := make([]*model.InsightDataPoint, req.DataPointCount) | ||
|
|
||
| var movePoint func(time.Time, int) time.Time | ||
| var start time.Time | ||
| // To prevent heavy loading | ||
| // - Support only daily | ||
| // - DataPointCount needs to be less than or equal to 7 | ||
| switch req.Step { | ||
| case model.InsightStep_DAILY: | ||
| if req.DataPointCount > 7 { | ||
| return nil, status.Error(codes.InvalidArgument, "DataPointCount needs to be less than or equal to 7") | ||
| } | ||
| movePoint = func(from time.Time, i int) time.Time { | ||
| return from.AddDate(0, 0, i) | ||
| } | ||
| rangeFrom := time.Unix(req.RangeFrom, 0) | ||
| start = time.Date(rangeFrom.Year(), rangeFrom.Month(), rangeFrom.Day(), 0, 0, 0, 0, time.UTC) | ||
| default: | ||
| return nil, status.Error(codes.InvalidArgument, "Invalid step") | ||
| } | ||
|
|
||
| for i := 0; i < int(req.DataPointCount); i++ { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree :) |
||
| target := movePoint(start, i) | ||
|
|
||
| filters := []datastore.ListFilter{ | ||
| { | ||
| Field: "ProjectId", | ||
| Operator: "==", | ||
| Value: projectID, | ||
| }, | ||
| { | ||
| Field: "CreatedAt", | ||
| Operator: ">=", | ||
| Value: target.Unix(), | ||
| }, | ||
| { | ||
| Field: "CreatedAt", | ||
| Operator: "<", | ||
| Value: movePoint(target, 1).Unix(), // target's finish time on unix time | ||
| }, | ||
| } | ||
|
|
||
| if req.ApplicationId != "" { | ||
| filters = append(filters, datastore.ListFilter{ | ||
| Field: "ApplicationId", | ||
| Operator: "==", | ||
| Value: req.ApplicationId, | ||
| }) | ||
| } | ||
|
|
||
| pageSize := 50 | ||
| deployments, err := a.deploymentStore.ListDeployments(ctx, datastore.ListOptions{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| PageSize: pageSize, | ||
| Filters: filters, | ||
| }) | ||
| if err != nil { | ||
| a.logger.Error("failed to get deployments", zap.Error(err)) | ||
| return nil, status.Error(codes.Internal, "Failed to get deployments") | ||
| } | ||
|
|
||
| counts[i] = &model.InsightDataPoint{ | ||
| Timestamp: target.Unix(), | ||
| Value: float32(len(deployments)), | ||
| } | ||
| } | ||
|
|
||
| return &webservice.GetInsightDataResponse{ | ||
| UpdatedAt: time.Now().Unix(), | ||
| DataPoints: counts, | ||
| }, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍