mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
86 lines
1.5 KiB
Go
86 lines
1.5 KiB
Go
package conditions
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
"gopkg.in/guregu/null.v3"
|
|
)
|
|
|
|
type QueryReducer interface {
|
|
Reduce(timeSeries *tsdb.TimeSeries) null.Float
|
|
}
|
|
|
|
type SimpleReducer struct {
|
|
Type string
|
|
}
|
|
|
|
func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
|
|
if len(series.Points) == 0 {
|
|
return null.FloatFromPtr(nil)
|
|
}
|
|
|
|
value := float64(0)
|
|
allNull := true
|
|
|
|
switch s.Type {
|
|
case "avg":
|
|
for _, point := range series.Points {
|
|
if point[0].Valid {
|
|
value += point[0].Float64
|
|
allNull = false
|
|
}
|
|
}
|
|
value = value / float64(len(series.Points))
|
|
case "sum":
|
|
for _, point := range series.Points {
|
|
if point[0].Valid {
|
|
value += point[0].Float64
|
|
allNull = false
|
|
}
|
|
}
|
|
case "min":
|
|
value = math.MaxFloat64
|
|
for _, point := range series.Points {
|
|
if point[0].Valid {
|
|
allNull = false
|
|
if value > point[0].Float64 {
|
|
value = point[0].Float64
|
|
}
|
|
}
|
|
}
|
|
case "max":
|
|
value = -math.MaxFloat64
|
|
for _, point := range series.Points {
|
|
if point[0].Valid {
|
|
allNull = false
|
|
if value < point[0].Float64 {
|
|
value = point[0].Float64
|
|
}
|
|
}
|
|
}
|
|
case "count":
|
|
value = float64(len(series.Points))
|
|
allNull = false
|
|
case "last":
|
|
points := series.Points
|
|
for i := len(points) - 1; i >= 0; i-- {
|
|
if points[i][0].Valid {
|
|
value = points[i][0].Float64
|
|
allNull = false
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if allNull {
|
|
return null.FloatFromPtr(nil)
|
|
}
|
|
|
|
return null.FloatFrom(value)
|
|
}
|
|
|
|
func NewSimpleReducer(typ string) *SimpleReducer {
|
|
return &SimpleReducer{Type: typ}
|
|
}
|