-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmetrica.go
42 lines (35 loc) · 1.09 KB
/
metrica.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
package newrelic_platform_go
import (
"math"
)
type IMetrica interface {
GetValue() (float64, error)
GetName() string
GetUnits() string
}
type MetricaValue interface{}
type SimpleMetricaValue float64
type AggregatedMetricaValue struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
Total float64 `json:"total"`
Count int `json:"count"`
SumOfSquares float64 `json:"sum_of_squares"`
}
func NewAggregatedMetricaValue(existValue float64, newValue float64) *AggregatedMetricaValue {
v := &AggregatedMetricaValue{
Min: math.Min(newValue, existValue),
Max: math.Max(newValue, existValue),
Total: newValue + existValue,
Count: 2,
SumOfSquares: newValue*newValue + existValue*existValue,
}
return v
}
func (aggregatedValue *AggregatedMetricaValue) Aggregate(newValue float64) {
aggregatedValue.Min = math.Min(newValue, aggregatedValue.Min)
aggregatedValue.Max = math.Max(newValue, aggregatedValue.Max)
aggregatedValue.Total += newValue
aggregatedValue.Count++
aggregatedValue.SumOfSquares += newValue * newValue
}