Expressions: Use enumerations rather than strings (#83741)
This commit is contained in:
@@ -7,8 +7,23 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
)
|
||||
|
||||
// The upsample function
|
||||
// +enum
|
||||
type Upsampler string
|
||||
|
||||
const (
|
||||
// Use the last seen value
|
||||
UpsamplerPad Upsampler = "pad"
|
||||
|
||||
// backfill
|
||||
UpsamplerBackfill Upsampler = "backfilling"
|
||||
|
||||
// Do not fill values (nill)
|
||||
UpsamplerFillNA Upsampler = "fillna"
|
||||
)
|
||||
|
||||
// Resample turns the Series into a Number based on the given reduction function
|
||||
func (s Series) Resample(refID string, interval time.Duration, downsampler string, upsampler string, from, to time.Time) (Series, error) {
|
||||
func (s Series) Resample(refID string, interval time.Duration, downsampler ReducerID, upsampler Upsampler, from, to time.Time) (Series, error) {
|
||||
newSeriesLength := int(float64(to.Sub(from).Nanoseconds()) / float64(interval.Nanoseconds()))
|
||||
if newSeriesLength <= 0 {
|
||||
return s, fmt.Errorf("the series cannot be sampled further; the time range is shorter than the interval")
|
||||
@@ -37,19 +52,19 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin
|
||||
var value *float64
|
||||
if len(vals) == 0 { // upsampling
|
||||
switch upsampler {
|
||||
case "pad":
|
||||
case UpsamplerPad:
|
||||
if lastSeen != nil {
|
||||
value = lastSeen
|
||||
} else {
|
||||
value = nil
|
||||
}
|
||||
case "backfilling":
|
||||
case UpsamplerBackfill:
|
||||
if sIdx == s.Len() { // no vals left
|
||||
value = nil
|
||||
} else {
|
||||
_, value = s.GetPoint(sIdx)
|
||||
}
|
||||
case "fillna":
|
||||
case UpsamplerFillNA:
|
||||
value = nil
|
||||
default:
|
||||
return s, fmt.Errorf("upsampling %v not implemented", upsampler)
|
||||
@@ -61,15 +76,15 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin
|
||||
ff := Float64Field(*fVec)
|
||||
var tmp *float64
|
||||
switch downsampler {
|
||||
case "sum":
|
||||
case ReducerSum:
|
||||
tmp = Sum(&ff)
|
||||
case "mean":
|
||||
case ReducerMean:
|
||||
tmp = Avg(&ff)
|
||||
case "min":
|
||||
case ReducerMin:
|
||||
tmp = Min(&ff)
|
||||
case "max":
|
||||
case ReducerMax:
|
||||
tmp = Max(&ff)
|
||||
case "last":
|
||||
case ReducerLast:
|
||||
tmp = Last(&ff)
|
||||
default:
|
||||
return s, fmt.Errorf("downsampling %v not implemented", downsampler)
|
||||
|
||||
@@ -13,8 +13,8 @@ func TestResampleSeries(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
interval time.Duration
|
||||
downsampler string
|
||||
upsampler string
|
||||
downsampler ReducerID
|
||||
upsampler Upsampler
|
||||
timeRange backend.TimeRange
|
||||
seriesToResample Series
|
||||
series Series
|
||||
|
||||
Reference in New Issue
Block a user