| 1 | package newrelic_platform_go |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | ) |
| 6 | |
| 7 | type IMetrica interface { |
| 8 | GetValue() (float64, error) |
| 9 | GetName() string |
| 10 | GetUnits() string |
| 11 | } |
| 12 | |
| 13 | type MetricaValue interface{} |
| 14 | |
| 15 | type SimpleMetricaValue float64 |
| 16 | |
| 17 | type AggregatedMetricaValue struct { |
| 18 | Min float64 `json:"min"` |
| 19 | Max float64 `json:"max"` |
| 20 | Total float64 `json:"total"` |
| 21 | Count int `json:"count"` |
| 22 | SumOfSquares float64 `json:"sum_of_squares"` |
| 23 | } |
| 24 | |
| 25 | func NewAggregatedMetricaValue(existValue float64, newValue float64) *AggregatedMetricaValue { |
| 26 | v := &AggregatedMetricaValue{ |
| 27 | Min: math.Min(newValue, existValue), |
| 28 | Max: math.Max(newValue, existValue), |
| 29 | Total: newValue + existValue, |
| 30 | Count: 2, |
| 31 | SumOfSquares: newValue*newValue + existValue*existValue, |
| 32 | } |
| 33 | return v |
| 34 | } |
| 35 | |
| 36 | func (aggregatedValue *AggregatedMetricaValue) Aggregate(newValue float64) { |
| 37 | aggregatedValue.Min = math.Min(newValue, aggregatedValue.Min) |
| 38 | aggregatedValue.Max = math.Max(newValue, aggregatedValue.Max) |
| 39 | aggregatedValue.Total += newValue |
| 40 | aggregatedValue.Count++ |
| 41 | aggregatedValue.SumOfSquares += newValue * newValue |
| 42 | } |