diff --git a/expr/func_timeshift.go b/expr/func_timeshift.go index afa7d40a4f..abda4a66d6 100644 --- a/expr/func_timeshift.go +++ b/expr/func_timeshift.go @@ -71,6 +71,8 @@ func (s *FuncTimeShift) Exec(dataMap DataMap) ([]models.Series, error) { } serie.Target = newName(serie.Target) + serie.QueryFrom = uint32(int(serie.QueryFrom) + negativeOffset) + serie.QueryTo = uint32(int(serie.QueryTo) + negativeOffset) serie.QueryPatt = newName(serie.QueryPatt) serie.Tags = serie.CopyTagsWith("timeShift", s.timeShift) serie.Datapoints = out diff --git a/expr/func_timeshift_test.go b/expr/func_timeshift_test.go index 2af3184a41..e84918f2e8 100644 --- a/expr/func_timeshift_test.go +++ b/expr/func_timeshift_test.go @@ -45,7 +45,6 @@ func TestTimeShiftSingle(t *testing.T) { Datapoints: shiftInput(a, -offset), // shift forward to avoid underflow }, } - inputCopy := models.SeriesCopy(input) // to later verify that it is unchanged testTimeShift( "identity", @@ -64,12 +63,6 @@ func TestTimeShiftSingle(t *testing.T) { true, false, ) - - t.Run("DidNotModifyInput", func(t *testing.T) { - if err := equalOutput(inputCopy, input, nil, nil); err != nil { - t.Fatal("Input was modified: ", err) - } - }) } func TestTimeShiftMultiple(t *testing.T) { @@ -141,6 +134,15 @@ func TestTimeShiftPositive(t *testing.T) { } func testTimeShift(name string, in []models.Series, out []models.Series, t *testing.T, expectedOffset int, shift string, resetEnd, alignDST bool) { + for i := range in { + in[i].QueryFrom = in[i].Datapoints[0].Ts + in[i].QueryTo = in[i].Datapoints[len(in[i].Datapoints)-1].Ts + out[i].QueryFrom = uint32(int(in[i].QueryFrom) - expectedOffset) + out[i].QueryTo = uint32(int(in[i].QueryTo) - expectedOffset) + } + + inputCopy := models.SeriesCopy(in) // to later verify that it is unchanged + f := NewTimeShift() f.(*FuncTimeShift).in = NewMock(in) f.(*FuncTimeShift).timeShift = shift @@ -164,6 +166,12 @@ func testTimeShift(name string, in []models.Series, out []models.Series, t *test t.Fatalf("case %q: Expected context offset = %d, got %d", name, expectedOffset, actualOffset) } + t.Run("DidNotModifyInput", func(t *testing.T) { + if err := equalOutput(inputCopy, in, nil, nil); err != nil { + t.Fatal("Input was modified: ", err) + } + }) + got, err := f.Exec(make(map[Req][]models.Series)) if err := equalOutput(out, got, nil, err); err != nil { t.Fatal("Failed test:", name, err)