Scale/offset
This commit is contained in:
80
pkg/timeseries/agg_functions.go
Normal file
80
pkg/timeseries/agg_functions.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package timeseries
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func AggAvg(points []TimePoint) *float64 {
|
||||
sum := AggSum(points)
|
||||
avg := *sum / float64(len(points))
|
||||
return &avg
|
||||
}
|
||||
|
||||
func AggSum(points []TimePoint) *float64 {
|
||||
var sum float64 = 0
|
||||
for _, p := range points {
|
||||
if p.Value != nil {
|
||||
sum += *p.Value
|
||||
}
|
||||
}
|
||||
return &sum
|
||||
}
|
||||
|
||||
func AggMax(points []TimePoint) *float64 {
|
||||
var max *float64 = nil
|
||||
for _, p := range points {
|
||||
if p.Value != nil {
|
||||
if max == nil {
|
||||
max = p.Value
|
||||
} else if *p.Value > *max {
|
||||
max = p.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
func AggMin(points []TimePoint) *float64 {
|
||||
var min *float64 = nil
|
||||
for _, p := range points {
|
||||
if p.Value != nil {
|
||||
if min == nil {
|
||||
min = p.Value
|
||||
} else if *p.Value < *min {
|
||||
min = p.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
func AggCount(points []TimePoint) *float64 {
|
||||
count := float64(len(points))
|
||||
return &count
|
||||
}
|
||||
|
||||
func AggFirst(points []TimePoint) *float64 {
|
||||
return points[0].Value
|
||||
}
|
||||
|
||||
func AggLast(points []TimePoint) *float64 {
|
||||
return points[len(points)-1].Value
|
||||
}
|
||||
|
||||
func AggMedian(points []TimePoint) *float64 {
|
||||
values := make([]float64, 0)
|
||||
for _, p := range points {
|
||||
if p.Value != nil {
|
||||
values = append(values, *p.Value)
|
||||
}
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
values = sort.Float64Slice(values)
|
||||
medianIndex := int(math.Floor(float64(len(values)) / 2))
|
||||
median := values[medianIndex]
|
||||
return &median
|
||||
}
|
||||
Reference in New Issue
Block a user