Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

timeShift function: adequately set QueryFrom/QueryTo on output series #1946

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions expr/func_timeshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions expr/func_timeshift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down