mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Extracted common code for diff calculation
This commit is contained in:
parent
3c2f6094b2
commit
28eaac3a9c
@ -95,55 +95,9 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "diff":
|
case "diff":
|
||||||
var (
|
allNull, value = calculateDiff(series, allNull, value, diff)
|
||||||
points = series.Points
|
|
||||||
first float64
|
|
||||||
i int
|
|
||||||
)
|
|
||||||
// get the newest point
|
|
||||||
for i = len(points) - 1; i >= 0; i-- {
|
|
||||||
if points[i][0].Valid {
|
|
||||||
allNull = false
|
|
||||||
first = points[i][0].Float64
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// get the oldest point
|
|
||||||
points = points[0:i]
|
|
||||||
for i := 0; i < len(points); i++ {
|
|
||||||
if points[i][0].Valid {
|
|
||||||
allNull = false
|
|
||||||
value = first - points[i][0].Float64
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "percent_diff":
|
case "percent_diff":
|
||||||
var (
|
allNull, value = calculateDiff(series, allNull, value, percentDiff)
|
||||||
points = series.Points
|
|
||||||
first float64
|
|
||||||
i int
|
|
||||||
)
|
|
||||||
// get the newest point
|
|
||||||
for i = len(points) - 1; i >= 0; i-- {
|
|
||||||
if points[i][0].Valid {
|
|
||||||
allNull = false
|
|
||||||
first = points[i][0].Float64
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if i >= 1 {
|
|
||||||
// get the oldest point
|
|
||||||
points = points[0:i]
|
|
||||||
for i := 0; i < len(points); i++ {
|
|
||||||
if points[i][0].Valid {
|
|
||||||
allNull = false
|
|
||||||
val := (first - points[i][0].Float64) / points[i][0].Float64 * 100
|
|
||||||
value = math.Abs(val)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "count_non_null":
|
case "count_non_null":
|
||||||
for _, v := range series.Points {
|
for _, v := range series.Points {
|
||||||
if v[0].Valid {
|
if v[0].Valid {
|
||||||
@ -166,3 +120,40 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
|
|||||||
func NewSimpleReducer(typ string) *SimpleReducer {
|
func NewSimpleReducer(typ string) *SimpleReducer {
|
||||||
return &SimpleReducer{Type: typ}
|
return &SimpleReducer{Type: typ}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func calculateDiff(series *tsdb.TimeSeries, allNull bool, value float64, fn func(float64, float64) float64) (bool, float64) {
|
||||||
|
var (
|
||||||
|
points = series.Points
|
||||||
|
first float64
|
||||||
|
i int
|
||||||
|
)
|
||||||
|
// get the newest point
|
||||||
|
for i = len(points) - 1; i >= 0; i-- {
|
||||||
|
if points[i][0].Valid {
|
||||||
|
allNull = false
|
||||||
|
first = points[i][0].Float64
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i >= 1 {
|
||||||
|
// get the oldest point
|
||||||
|
points = points[0:i]
|
||||||
|
for i := 0; i < len(points); i++ {
|
||||||
|
if points[i][0].Valid {
|
||||||
|
allNull = false
|
||||||
|
val := fn(first, points[i][0].Float64)
|
||||||
|
value = math.Abs(val)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return allNull, value
|
||||||
|
}
|
||||||
|
|
||||||
|
var diff = func(newest, oldest float64) float64 {
|
||||||
|
return newest - oldest
|
||||||
|
}
|
||||||
|
|
||||||
|
var percentDiff = func(newest, oldest float64) float64 {
|
||||||
|
return (newest - oldest) / oldest * 100
|
||||||
|
}
|
||||||
|
@ -143,6 +143,18 @@ func TestSimpleReducer(t *testing.T) {
|
|||||||
So(result, ShouldEqual, float64(10))
|
So(result, ShouldEqual, float64(10))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("diff with only nulls", func() {
|
||||||
|
reducer := NewSimpleReducer("diff")
|
||||||
|
series := &tsdb.TimeSeries{
|
||||||
|
Name: "test time serie",
|
||||||
|
}
|
||||||
|
|
||||||
|
series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 1))
|
||||||
|
series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2))
|
||||||
|
|
||||||
|
So(reducer.Reduce(series).Valid, ShouldEqual, false)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("percent_diff one point", func() {
|
Convey("percent_diff one point", func() {
|
||||||
result := testReducer("percent_diff", 40)
|
result := testReducer("percent_diff", 40)
|
||||||
So(result, ShouldEqual, float64(0))
|
So(result, ShouldEqual, float64(0))
|
||||||
|
Loading…
Reference in New Issue
Block a user