Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a34eb41
Create GetInsightData for deploy frequency
sanposhiho Dec 4, 2020
79393b0
Add test for getInsightDataForDeployFrequency
sanposhiho Dec 4, 2020
6d0a32a
Rename projectId to projectID
sanposhiho Dec 4, 2020
4344548
Add assert in test for response
sanposhiho Dec 4, 2020
54d2c7a
Add test to check error pattern
sanposhiho Dec 4, 2020
6c1965f
Remove empty line
sanposhiho Dec 4, 2020
8681e87
Fix to catch invalid step
sanposhiho Dec 4, 2020
74cb3bd
Fix to use zap.NewNop()
sanposhiho Dec 4, 2020
efd3f84
Rename accumulateFrom to start
sanposhiho Dec 4, 2020
79061bd
Fix to specify timezone UTC
sanposhiho Dec 4, 2020
651c401
Fix to reset to zero in weekly and daily
sanposhiho Dec 4, 2020
4939e23
Add pageSize opts
sanposhiho Dec 4, 2020
fbbd84c
Support only daily to prevent heavy loading
sanposhiho Dec 4, 2020
bb63a36
Remove unuse deps
sanposhiho Dec 4, 2020
127d2a3
Add comment for support
sanposhiho Dec 4, 2020
33abb45
Fix to specify page on ListDeployments option
sanposhiho Dec 6, 2020
e19da30
Go fmted
sanposhiho Dec 6, 2020
98c92e7
Fix not to get all data with page
sanposhiho Dec 7, 2020
06e9695
Remove empty line
sanposhiho Dec 7, 2020
674ccd4
Add getInsightDataForChangeFailureRate and change function architecture
sanposhiho Dec 4, 2020
e390ea2
Fix devide with float32
sanposhiho Dec 4, 2020
1ce5fbb
Fix to use page and pagesize
sanposhiho Dec 6, 2020
95ae396
Remove page specification on list option
sanposhiho Dec 7, 2020
6c11bd3
Rename function name
sanposhiho Dec 7, 2020
0fd6e5f
Rename test function name
sanposhiho Dec 7, 2020
e10356e
Merge branch 'master' into create-insight-data-for-change-failure-rate
sanposhiho Dec 7, 2020
aaf525c
Corrected for incorrect handling of conflicts
sanposhiho Dec 7, 2020
057bb86
Define getInsightDataForEachKind to fix deeply nested
sanposhiho Dec 8, 2020
605d33a
Fix to devision 0
sanposhiho Dec 9, 2020
d5581fe
Fix to 0 when deployment count is 0
sanposhiho Dec 9, 2020
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
198 changes: 152 additions & 46 deletions pkg/app/api/grpcapi/web_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,16 +1318,10 @@ func (a *WebAPI) GetInsightData(ctx context.Context, req *webservice.GetInsightD
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, "")
return a.getInsightData(ctx, claims.Role.ProjectId, req)
}

// 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) {
func (a *WebAPI) getInsightData(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
Expand All @@ -1350,52 +1344,164 @@ func (a *WebAPI) getInsightDataForDeployFrequency(ctx context.Context, projectID
}

for i := 0; i < int(req.DataPointCount); i++ {
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,
})
targetRangeFrom := movePoint(start, i)
targetRangeTo := movePoint(targetRangeFrom, 1)

var getInsightDataForEachKind func(context.Context, string, string, time.Time, time.Time) (*model.InsightDataPoint, error)
switch req.MetricsKind {
case model.InsightMetricsKind_DEPLOYMENT_FREQUENCY:
getInsightDataForEachKind = a.getInsightDataForDeployFrequency
case model.InsightMetricsKind_CHANGE_FAILURE_RATE:
getInsightDataForEachKind = a.getInsightDataForChangeFailureRate
default:
return nil, status.Error(codes.Unimplemented, "")
}

pageSize := 50
deployments, err := a.deploymentStore.ListDeployments(ctx, datastore.ListOptions{
PageSize: pageSize,
Filters: filters,
})
count, err := getInsightDataForEachKind(ctx, projectID, req.ApplicationId, targetRangeFrom, targetRangeTo)
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 nil, err
}
counts[i] = count
}

return &webservice.GetInsightDataResponse{
UpdatedAt: time.Now().Unix(),
DataPoints: counts,
}, nil
}

// getInsightDataForDeployFrequency accumulate insight data in target range for deploy frequency.
// This function is temporary implementation for front end.
func (a *WebAPI) getInsightDataForDeployFrequency(
ctx context.Context,
projectID string,
applicationID string,
targetRangeFrom time.Time,
targetRangeTo time.Time) (*model.InsightDataPoint, error) {
filters := []datastore.ListFilter{
{
Field: "ProjectId",
Operator: "==",
Value: projectID,
},
{
Field: "CreatedAt",
Operator: ">=",
Value: targetRangeFrom.Unix(),
},
{
Field: "CreatedAt",
Operator: "<",
Value: targetRangeTo.Unix(), // target's finish time on unix time
},
}

if applicationID != "" {
filters = append(filters, datastore.ListFilter{
Field: "ApplicationId",
Operator: "==",
Value: applicationID,
})
}

pageSize := 50
deployments, err := a.deploymentStore.ListDeployments(ctx, datastore.ListOptions{
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")
}

return &model.InsightDataPoint{
Timestamp: targetRangeFrom.Unix(),
Value: float32(len(deployments)),
}, nil
}

// getInsightDataForChangeFailureRate accumulate insight data in target range for change failure rate
// This function is temporary implementation for front end.
func (a *WebAPI) getInsightDataForChangeFailureRate(
ctx context.Context,
projectID string,
applicationID string,
targetRangeFrom time.Time,
targetRangeTo time.Time) (*model.InsightDataPoint, error) {

commonFilters := []datastore.ListFilter{
{
Field: "ProjectId",
Operator: "==",
Value: projectID,
},
{
Field: "CreatedAt",
Operator: ">=",
Value: targetRangeFrom.Unix(),
},
{
Field: "CreatedAt",
Operator: "<",
Value: targetRangeTo.Unix(), // target's finish time on unix time
},
}

if applicationID != "" {
commonFilters = append(commonFilters, datastore.ListFilter{
Field: "ApplicationId",
Operator: "==",
Value: applicationID,
})
}

filterForSuccessDeploy := []datastore.ListFilter{
{
Field: "Status",
Operator: "==",
Value: model.DeploymentStatus_DEPLOYMENT_SUCCESS,
},
}

filterForFailureDeploy := []datastore.ListFilter{
{
Field: "Status",
Operator: "==",
Value: model.DeploymentStatus_DEPLOYMENT_FAILURE,
},
}

pageSize := 50
successDeployments, err := a.deploymentStore.ListDeployments(ctx, datastore.ListOptions{
PageSize: pageSize,
Filters: append(filterForSuccessDeploy, commonFilters...),
})
if err != nil {
a.logger.Error("failed to get deployments", zap.Error(err))
return nil, status.Error(codes.Internal, "Failed to get deployments")
}

failureDeployments, err := a.deploymentStore.ListDeployments(ctx, datastore.ListOptions{
PageSize: pageSize,
Filters: append(filterForFailureDeploy, commonFilters...),
})
if err != nil {
a.logger.Error("failed to get deployments", zap.Error(err))
return nil, status.Error(codes.Internal, "Failed to get deployments")
}

successCount := len(successDeployments)
failureCount := len(failureDeployments)

var changeFailureRate float32
if successCount+failureCount != 0 {
changeFailureRate = float32(failureCount) / float32(successCount+failureCount)
} else {
changeFailureRate = 0
}

return &model.InsightDataPoint{
Timestamp: targetRangeFrom.Unix(),
Value: changeFailureRate,
}, nil
}
Loading