diff --git a/pkg/timeseries/agg_functions.go b/pkg/timeseries/agg_functions.go index 66c655e..8acb164 100644 --- a/pkg/timeseries/agg_functions.go +++ b/pkg/timeseries/agg_functions.go @@ -9,17 +9,33 @@ type AggregationFunc = func(points []TimePoint) *float64 func AggAvg(points []TimePoint) *float64 { sum := AggSum(points) - avg := *sum / float64(len(points)) + // Do not include nulls in the average + nonNullLength := 0 + for _, p := range points { + if p.Value != nil { + nonNullLength++ + } + } + if sum == nil || nonNullLength == 0 { + return nil + } + avg := *sum / float64(nonNullLength) return &avg } func AggSum(points []TimePoint) *float64 { var sum float64 = 0 + isNil := true for _, p := range points { if p.Value != nil { + isNil = false sum += *p.Value } } + // Skip empty frames + if isNil { + return nil + } return &sum }