Chore: Add last downsampling function to Resample expression (#57379)

This commit is contained in:
Yuriy Tseretyan
2022-10-21 10:03:43 -04:00
committed by GitHub
parent cd0e0fcc53
commit 245c1ee3d3
3 changed files with 37 additions and 0 deletions

View File

@@ -54,6 +54,8 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin
default:
return s, fmt.Errorf("upsampling %v not implemented", upsampler)
}
} else if len(vals) == 1 {
value = vals[0]
} else { // downsampling
fVec := data.NewField("", s.GetLabels(), vals)
ff := Float64Field(*fVec)
@@ -67,6 +69,8 @@ func (s Series) Resample(refID string, interval time.Duration, downsampler strin
tmp = Min(&ff)
case "max":
tmp = Max(&ff)
case "last":
tmp = Last(&ff)
default:
return s, fmt.Errorf("downsampling %v not implemented", downsampler)
}

View File

@@ -245,6 +245,38 @@ func TestResampleSeries(t *testing.T) {
time.Unix(10, 0), nil,
}),
},
{
name: "resample series: downsampling (last / pad )",
interval: time.Second * 3,
downsampler: "last",
upsampler: "pad",
timeRange: backend.TimeRange{
From: time.Unix(0, 0),
To: time.Unix(11, 0),
},
seriesToResample: makeSeries("", nil, tp{
time.Unix(0, 0), float64Pointer(0),
}, tp{
time.Unix(2, 0), float64Pointer(2),
}, tp{
time.Unix(4, 0), float64Pointer(3),
}, tp{
time.Unix(6, 0), float64Pointer(4),
}, tp{
time.Unix(8, 0), float64Pointer(0),
}, tp{
time.Unix(10, 0), float64Pointer(1),
}),
series: makeSeries("", nil, tp{
time.Unix(0, 0), float64Pointer(0),
}, tp{
time.Unix(3, 0), float64Pointer(2),
}, tp{
time.Unix(6, 0), float64Pointer(4),
}, tp{
time.Unix(9, 0), float64Pointer(0),
}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {