/
orgmetric.go
131 lines (108 loc) · 4.15 KB
/
orgmetric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Copyright 2024 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"context"
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
)
type OrgMetricsService struct {
pb.UnimplementedOrgMetricsServiceServer
*service
uc *biz.OrgMetricsUseCase
}
func NewOrgMetricsService(uc *biz.OrgMetricsUseCase, opts ...NewOpt) *OrgMetricsService {
return &OrgMetricsService{
service: newService(opts...),
uc: uc,
}
}
func (s *OrgMetricsService) Totals(ctx context.Context, req *pb.OrgMetricsServiceTotalsRequest) (*pb.OrgMetricsServiceTotalsResponse, error) {
currentOrg, err := requireCurrentOrg(ctx)
if err != nil {
return nil, err
}
// totals
// TODO: Merge it to a single request
totals, err := s.uc.RunsTotal(ctx, currentOrg.ID, *req.TimeWindow.ToDuration())
if err != nil {
return nil, handleUseCaseErr(err, s.log)
}
totalsByStatus, err := s.uc.RunsTotalByStatus(ctx, currentOrg.ID, *req.TimeWindow.ToDuration())
if err != nil {
return nil, handleUseCaseErr(err, s.log)
}
totalsByRunnerType, err := s.uc.RunsTotalByRunnerType(ctx, currentOrg.ID, *req.TimeWindow.ToDuration())
if err != nil {
return nil, handleUseCaseErr(err, s.log)
}
return &pb.OrgMetricsServiceTotalsResponse{Result: &pb.OrgMetricsServiceTotalsResponse_Result{
RunsTotal: totals,
RunsTotalByStatus: totalByStatusToPb(totalsByStatus),
RunsTotalByRunnerType: totalByRunnerTypeToPb(totalsByRunnerType),
}}, nil
}
func (s *OrgMetricsService) TopWorkflowsByRunsCount(ctx context.Context, req *pb.TopWorkflowsByRunsCountRequest) (*pb.TopWorkflowsByRunsCountResponse, error) {
currentOrg, err := requireCurrentOrg(ctx)
if err != nil {
return nil, err
}
res, err := s.uc.TopWorkflowsByRunsCount(ctx, currentOrg.ID, int(req.GetNumWorkflows()), *req.TimeWindow.ToDuration())
if err != nil {
return nil, handleUseCaseErr(err, s.log)
}
var result = []*pb.TopWorkflowsByRunsCountResponse_TotalByStatus{}
for _, r := range res {
result = append(result, &pb.TopWorkflowsByRunsCountResponse_TotalByStatus{
Workflow: bizWorkflowToPb(r.Workflow),
RunsTotalByStatus: totalByStatusToPb(r.ByStatus),
})
}
return &pb.TopWorkflowsByRunsCountResponse{Result: result}, nil
}
func (s *OrgMetricsService) DailyRunsCount(ctx context.Context, req *pb.DailyRunsCountRequest) (*pb.DailyRunsCountResponse, error) {
org, err := requireCurrentOrg(ctx)
if err != nil {
return nil, err
}
metricsByDay, err := s.uc.DailyRunsCount(ctx, org.ID, req.WorkflowId, *req.TimeWindow.ToDuration())
if err != nil {
return nil, handleUseCaseErr(err, s.log)
}
var res = make([]*pb.DailyRunsCountResponse_TotalByDay, 0, len(metricsByDay))
for _, m := range metricsByDay {
for _, stateMetrics := range m.Totals {
res = append(res, &pb.DailyRunsCountResponse_TotalByDay{
Date: m.Date.Format("2006-01-02"),
Metrics: &pb.MetricsStatusCount{Status: bizWorkflowRunStatusToPb(biz.WorkflowRunStatus(stateMetrics.Status)), Count: stateMetrics.Count},
})
}
}
return &pb.DailyRunsCountResponse{Result: res}, nil
}
func totalByStatusToPb(in map[string]int32) []*pb.MetricsStatusCount {
resp := make([]*pb.MetricsStatusCount, 0, len(in))
for k, v := range in {
resp = append(resp, &pb.MetricsStatusCount{Status: bizWorkflowRunStatusToPb(biz.WorkflowRunStatus(k)), Count: v})
}
return resp
}
func totalByRunnerTypeToPb(in map[string]int32) []*pb.MetricsRunnerCount {
resp := make([]*pb.MetricsRunnerCount, 0, len(in))
for k, v := range in {
resp = append(resp, &pb.MetricsRunnerCount{RunnerType: bizRunnerToPb(k), Count: v})
}
return resp
}