-
Notifications
You must be signed in to change notification settings - Fork 143
/
datapoint.go
157 lines (133 loc) · 3.87 KB
/
datapoint.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2020 The PipeCD 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 insight
import (
"fmt"
"time"
"github.com/pipe-cd/pipecd/pkg/model"
)
// DeployFrequency represents a data point that shows the deployment frequency metrics.
type DeployFrequency struct {
Timestamp int64 `json:"timestamp"`
DeployCount float32 `json:"deploy_count"`
}
func (d *DeployFrequency) GetTimestamp() int64 {
return d.Timestamp
}
func (d *DeployFrequency) Value() float32 {
return d.DeployCount
}
func (d *DeployFrequency) Merge(point DataPoint) error {
if point == nil {
return nil
}
df, ok := point.(*DeployFrequency)
if !ok {
return fmt.Errorf("can not cast to DataPoint to DeployFrequency, %v", point)
}
if df.Timestamp != d.Timestamp {
return fmt.Errorf("mismatch timestamp. want: %d, acutual: %d", d.Timestamp, df.Timestamp)
}
d.DeployCount += df.DeployCount
return nil
}
// ChangeFailureRate represents a data point that shows the change failure rate metrics.
type ChangeFailureRate struct {
Timestamp int64 `json:"timestamp"`
Rate float32 `json:"rate"`
SuccessCount int64 `json:"success_count"`
FailureCount int64 `json:"failure_count"`
}
func (c *ChangeFailureRate) GetTimestamp() int64 {
return c.Timestamp
}
func (c *ChangeFailureRate) Value() float32 {
return c.Rate
}
func (c *ChangeFailureRate) Merge(point DataPoint) error {
if point == nil {
return nil
}
cfr, ok := point.(*ChangeFailureRate)
if !ok {
return fmt.Errorf("can not cast to DataPoint to ChangeFailureRate, %v", point)
}
if cfr.Timestamp != c.Timestamp {
return fmt.Errorf("mismatch timestamp. want: %d, acutual: %d", c.Timestamp, cfr.Timestamp)
}
c.FailureCount += cfr.FailureCount
c.SuccessCount += cfr.SuccessCount
c.Rate = float32(c.FailureCount) / float32(c.FailureCount+c.SuccessCount)
return nil
}
type DataPoint interface {
// Value gets data for model.InsightDataPoint.
Value() float32
// Timestamp gets timestamp.
GetTimestamp() int64
// Merge merges other DataPoint.
Merge(point DataPoint) error
}
// ToDataPoints converts a list of concrete points into the list of DataPoints
func ToDataPoints(i interface{}) ([]DataPoint, error) {
switch dps := i.(type) {
case []*DeployFrequency:
dataPoints := make([]DataPoint, len(dps))
for j, dp := range dps {
dataPoints[j] = dp
}
return dataPoints, nil
case []*ChangeFailureRate:
dataPoints := make([]DataPoint, len(dps))
for j, dp := range dps {
dataPoints[j] = dp
}
return dataPoints, nil
default:
return nil, fmt.Errorf("cannot convert to DataPoints: %v", dps)
}
}
// UpdateDataPoint sets data point
func UpdateDataPoint(dp []DataPoint, point DataPoint, timestamp int64) ([]DataPoint, error) {
latestData := dp[len(dp)-1]
if timestamp < latestData.GetTimestamp() {
return nil, fmt.Errorf("invalid timestamp")
}
if timestamp == latestData.GetTimestamp() {
err := latestData.Merge(point)
if err != nil {
return nil, err
}
dp[len(dp)-1] = latestData
} else {
dp = append(dp, point)
}
return dp, nil
}
func extractDataPoints(dp []DataPoint, from, to time.Time) ([]*model.InsightDataPoint, error) {
var result []*model.InsightDataPoint
for _, d := range dp {
ts := d.GetTimestamp()
if ts > to.Unix() {
break
}
if ts >= from.Unix() {
result = append(result, &model.InsightDataPoint{
Timestamp: ts,
Value: d.Value(),
})
}
}
return result, nil
}