mirror of
https://github.com/grafana/grafana.git
synced 2025-09-25 13:13:51 +08:00
[postgres] fix timeGroup macro rounding (#12468)
* fix timeGroup tests to check for correct grouping * do explicit floor rounding in $__timeGroup macro * fix typo in comments
This commit is contained in:

committed by
Torkel Ödegaard

parent
c03d527d25
commit
c2c22c142b
@ -109,7 +109,7 @@ func (m *PostgresMacroEngine) evaluateMacro(name string, args []string) (string,
|
|||||||
m.Query.Model.Set("fillValue", floatVal)
|
m.Query.Model.Set("fillValue", floatVal)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("(extract(epoch from %s)/%v)::bigint*%v AS time", args[0], interval.Seconds(), interval.Seconds()), nil
|
return fmt.Sprintf("floor(extract(epoch from %s)/%v)*%v AS time", args[0], interval.Seconds(), interval.Seconds()), nil
|
||||||
case "__unixEpochFilter":
|
case "__unixEpochFilter":
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return "", fmt.Errorf("missing time column argument for macro %v", name)
|
return "", fmt.Errorf("missing time column argument for macro %v", name)
|
||||||
|
@ -53,7 +53,7 @@ func TestMacroEngine(t *testing.T) {
|
|||||||
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
|
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
So(sql, ShouldEqual, "GROUP BY (extract(epoch from time_column)/300)::bigint*300 AS time")
|
So(sql, ShouldEqual, "GROUP BY floor(extract(epoch from time_column)/300)*300 AS time")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("interpolate __timeGroup function with spaces between args", func() {
|
Convey("interpolate __timeGroup function with spaces between args", func() {
|
||||||
@ -61,7 +61,7 @@ func TestMacroEngine(t *testing.T) {
|
|||||||
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
|
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
So(sql, ShouldEqual, "GROUP BY (extract(epoch from time_column)/300)::bigint*300 AS time")
|
So(sql, ShouldEqual, "GROUP BY floor(extract(epoch from time_column)/300)*300 AS time")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("interpolate __timeTo function", func() {
|
Convey("interpolate __timeTo function", func() {
|
||||||
|
@ -189,21 +189,23 @@ func TestPostgres(t *testing.T) {
|
|||||||
So(queryResult.Error, ShouldBeNil)
|
So(queryResult.Error, ShouldBeNil)
|
||||||
|
|
||||||
points := queryResult.Series[0].Points
|
points := queryResult.Series[0].Points
|
||||||
So(len(points), ShouldEqual, 6)
|
// without fill this should result in 4 buckets
|
||||||
|
So(len(points), ShouldEqual, 4)
|
||||||
|
|
||||||
dt := fromStart
|
dt := fromStart
|
||||||
|
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
aValue := points[i][0].Float64
|
aValue := points[i][0].Float64
|
||||||
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
||||||
So(aValue, ShouldEqual, 15)
|
So(aValue, ShouldEqual, 15)
|
||||||
So(aTime, ShouldEqual, dt)
|
So(aTime, ShouldEqual, dt)
|
||||||
|
So(aTime.Unix()%300, ShouldEqual, 0)
|
||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(5 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
// adjust for 5 minute gap
|
// adjust for 10 minute gap between first and second set of points
|
||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(10 * time.Minute)
|
||||||
for i := 3; i < 6; i++ {
|
for i := 2; i < 4; i++ {
|
||||||
aValue := points[i][0].Float64
|
aValue := points[i][0].Float64
|
||||||
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
||||||
So(aValue, ShouldEqual, 20)
|
So(aValue, ShouldEqual, 20)
|
||||||
@ -239,7 +241,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
|
|
||||||
dt := fromStart
|
dt := fromStart
|
||||||
|
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
aValue := points[i][0].Float64
|
aValue := points[i][0].Float64
|
||||||
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
||||||
So(aValue, ShouldEqual, 15)
|
So(aValue, ShouldEqual, 15)
|
||||||
@ -247,17 +249,23 @@ func TestPostgres(t *testing.T) {
|
|||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(5 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check for NULL values inserted by fill
|
||||||
|
So(points[2][0].Valid, ShouldBeFalse)
|
||||||
So(points[3][0].Valid, ShouldBeFalse)
|
So(points[3][0].Valid, ShouldBeFalse)
|
||||||
|
|
||||||
// adjust for 5 minute gap
|
// adjust for 10 minute gap between first and second set of points
|
||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(10 * time.Minute)
|
||||||
for i := 4; i < 7; i++ {
|
for i := 4; i < 6; i++ {
|
||||||
aValue := points[i][0].Float64
|
aValue := points[i][0].Float64
|
||||||
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
|
||||||
So(aValue, ShouldEqual, 20)
|
So(aValue, ShouldEqual, 20)
|
||||||
So(aTime, ShouldEqual, dt)
|
So(aTime, ShouldEqual, dt)
|
||||||
dt = dt.Add(5 * time.Minute)
|
dt = dt.Add(5 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check for NULL values inserted by fill
|
||||||
|
So(points[6][0].Valid, ShouldBeFalse)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When doing a metric query using timeGroup with float fill enabled", func() {
|
Convey("When doing a metric query using timeGroup with float fill enabled", func() {
|
||||||
|
Reference in New Issue
Block a user