2020-11-19 06:17:00 -06:00
|
|
|
package mathexp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Sum(v *data.Field) *float64 {
|
|
|
|
var sum float64
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
if f, ok := v.At(i).(*float64); ok {
|
2020-12-07 14:07:18 -06:00
|
|
|
if f == nil || math.IsNaN(*f) {
|
2020-11-19 06:17:00 -06:00
|
|
|
nan := math.NaN()
|
|
|
|
return &nan
|
|
|
|
}
|
|
|
|
sum += *f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &sum
|
|
|
|
}
|
|
|
|
|
|
|
|
func Avg(v *data.Field) *float64 {
|
|
|
|
sum := Sum(v)
|
|
|
|
f := *sum / float64(v.Len())
|
|
|
|
return &f
|
|
|
|
}
|
|
|
|
|
|
|
|
func Min(fv *data.Field) *float64 {
|
|
|
|
var f float64
|
|
|
|
if fv.Len() == 0 {
|
|
|
|
nan := math.NaN()
|
|
|
|
return &nan
|
|
|
|
}
|
|
|
|
for i := 0; i < fv.Len(); i++ {
|
|
|
|
if v, ok := fv.At(i).(*float64); ok {
|
2020-12-07 14:07:18 -06:00
|
|
|
if v == nil || math.IsNaN(*v) {
|
2020-11-19 06:17:00 -06:00
|
|
|
nan := math.NaN()
|
|
|
|
return &nan
|
|
|
|
}
|
|
|
|
if i == 0 || *v < f {
|
|
|
|
f = *v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &f
|
|
|
|
}
|
|
|
|
|
|
|
|
func Max(fv *data.Field) *float64 {
|
|
|
|
var f float64
|
|
|
|
if fv.Len() == 0 {
|
|
|
|
nan := math.NaN()
|
|
|
|
return &nan
|
|
|
|
}
|
|
|
|
for i := 0; i < fv.Len(); i++ {
|
|
|
|
if v, ok := fv.At(i).(*float64); ok {
|
2020-12-07 14:07:18 -06:00
|
|
|
if v == nil || math.IsNaN(*v) {
|
2020-11-19 06:17:00 -06:00
|
|
|
nan := math.NaN()
|
|
|
|
return &nan
|
|
|
|
}
|
|
|
|
if i == 0 || *v > f {
|
|
|
|
f = *v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &f
|
|
|
|
}
|
|
|
|
|
|
|
|
func Count(fv *data.Field) *float64 {
|
|
|
|
f := float64(fv.Len())
|
|
|
|
return &f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce turns the Series into a Number based on the given reduction function
|
2020-12-11 05:59:12 -06:00
|
|
|
func (s Series) Reduce(refID, rFunc string) (Number, error) {
|
2020-11-19 06:17:00 -06:00
|
|
|
var l data.Labels
|
|
|
|
if s.GetLabels() != nil {
|
|
|
|
l = s.GetLabels().Copy()
|
|
|
|
}
|
2020-12-11 05:59:12 -06:00
|
|
|
number := NewNumber(refID, l)
|
2020-11-19 06:17:00 -06:00
|
|
|
var f *float64
|
|
|
|
fVec := s.Frame.Fields[1]
|
|
|
|
switch rFunc {
|
|
|
|
case "sum":
|
|
|
|
f = Sum(fVec)
|
|
|
|
case "mean":
|
|
|
|
f = Avg(fVec)
|
|
|
|
case "min":
|
|
|
|
f = Min(fVec)
|
|
|
|
case "max":
|
|
|
|
f = Max(fVec)
|
|
|
|
case "count":
|
|
|
|
f = Count(fVec)
|
|
|
|
default:
|
|
|
|
return number, fmt.Errorf("reduction %v not implemented", rFunc)
|
|
|
|
}
|
|
|
|
number.SetValue(f)
|
|
|
|
|
|
|
|
return number, nil
|
|
|
|
}
|