Commit 42f18928 by Sven Klemm

Add $__timeGroupAlias to postgres macros

parent bd77541e
...@@ -369,7 +369,7 @@ ...@@ -369,7 +369,7 @@
{ {
"alias": "", "alias": "",
"format": "time_series", "format": "time_series",
"rawSql": "SELECT $__timeGroup(time, '5m') AS time, avg(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1", "rawSql": "SELECT $__timeGroupAlias(time, '5m'), avg(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1",
"refId": "A" "refId": "A"
} }
], ],
...@@ -452,7 +452,7 @@ ...@@ -452,7 +452,7 @@
{ {
"alias": "", "alias": "",
"format": "time_series", "format": "time_series",
"rawSql": "SELECT $__timeGroup(time, '5m', NULL) AS time, avg(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1", "rawSql": "SELECT $__timeGroupAlias(time, '5m', NULL), avg(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1",
"refId": "A" "refId": "A"
} }
], ],
...@@ -535,7 +535,7 @@ ...@@ -535,7 +535,7 @@
{ {
"alias": "", "alias": "",
"format": "time_series", "format": "time_series",
"rawSql": "SELECT $__timeGroup(time, '5m', 10.0) AS time, avg(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1", "rawSql": "SELECT $__timeGroupAlias(time, '5m', 10.0), avg(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1",
"refId": "A" "refId": "A"
} }
], ],
...@@ -618,7 +618,7 @@ ...@@ -618,7 +618,7 @@
{ {
"alias": "", "alias": "",
"format": "time_series", "format": "time_series",
"rawSql": "SELECT $__timeGroup(time, '$summarize') AS time, avg(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1", "rawSql": "SELECT $__timeGroupAlias(time, '$summarize'), avg(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1",
"refId": "A" "refId": "A"
} }
], ],
...@@ -701,7 +701,7 @@ ...@@ -701,7 +701,7 @@
{ {
"alias": "", "alias": "",
"format": "time_series", "format": "time_series",
"rawSql": "SELECT $__timeGroup(time, '$summarize', NULL) AS time, sum(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1", "rawSql": "SELECT $__timeGroupAlias(time, '$summarize', NULL), sum(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1",
"refId": "A" "refId": "A"
} }
], ],
...@@ -784,7 +784,7 @@ ...@@ -784,7 +784,7 @@
{ {
"alias": "", "alias": "",
"format": "time_series", "format": "time_series",
"rawSql": "SELECT $__timeGroup(time, '$summarize', 100.0) AS time, sum(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1", "rawSql": "SELECT $__timeGroupAlias(time, '$summarize', 100.0), sum(value) as value FROM metric WHERE $__timeFilter(time) GROUP BY 1 ORDER BY 1",
"refId": "A" "refId": "A"
} }
], ],
...@@ -956,7 +956,7 @@ ...@@ -956,7 +956,7 @@
{ {
"alias": "", "alias": "",
"format": "time_series", "format": "time_series",
"rawSql": "SELECT \n $__timeGroup(time, '$summarize') AS time, \n avg(\"valueOne\") as \"valueOne\", \n avg(\"valueTwo\") as \"valueTwo\" \nFROM\n metric_values \nWHERE\n $__timeFilter(time) AND\n measurement in($metric)\nGROUP BY 1\nORDER BY 1", "rawSql": "SELECT \n $__timeGroupAlias(time, '$summarize'), \n avg(\"valueOne\") as \"valueOne\", \n avg(\"valueTwo\") as \"valueTwo\" \nFROM\n metric_values \nWHERE\n $__timeFilter(time) AND\n measurement in($metric)\nGROUP BY 1\nORDER BY 1",
"refId": "A" "refId": "A"
} }
], ],
...@@ -2352,5 +2352,6 @@ ...@@ -2352,5 +2352,6 @@
"timezone": "", "timezone": "",
"title": "Datasource tests - Postgres (unittest)", "title": "Datasource tests - Postgres (unittest)",
"uid": "vHQdlVziz", "uid": "vHQdlVziz",
"version": 17 "version": 1
} }
...@@ -110,6 +110,12 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string, ...@@ -110,6 +110,12 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
} }
} }
return fmt.Sprintf("floor(extract(epoch from %s)/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil return fmt.Sprintf("floor(extract(epoch from %s)/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil
case "__timeGroupAlias":
tg, err := m.evaluateMacro("__timeGroup", args)
if err == nil {
return tg + " AS \"time\"", err
}
return "", err
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)
......
...@@ -50,18 +50,24 @@ func TestMacroEngine(t *testing.T) { ...@@ -50,18 +50,24 @@ func TestMacroEngine(t *testing.T) {
Convey("interpolate __timeGroup function", func() { Convey("interpolate __timeGroup function", func() {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')") sql, err := engine.Interpolate(query, timeRange, "$__timeGroup(time_column,'5m')")
So(err, ShouldBeNil)
sql2, err := engine.Interpolate(query, timeRange, "$__timeGroupAlias(time_column,'5m')")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(sql, ShouldEqual, "GROUP BY floor(extract(epoch from time_column)/300)*300") So(sql, ShouldEqual, "floor(extract(epoch from time_column)/300)*300")
So(sql2, ShouldEqual, sql+" AS \"time\"")
}) })
Convey("interpolate __timeGroup function with spaces between args", func() { Convey("interpolate __timeGroup function with spaces between args", func() {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')") sql, err := engine.Interpolate(query, timeRange, "$__timeGroup(time_column , '5m')")
So(err, ShouldBeNil)
sql2, err := engine.Interpolate(query, timeRange, "$__timeGroupAlias(time_column , '5m')")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(sql, ShouldEqual, "GROUP BY floor(extract(epoch from time_column)/300)*300") So(sql, ShouldEqual, "floor(extract(epoch from time_column)/300)*300")
So(sql2, ShouldEqual, sql+" AS \"time\"")
}) })
Convey("interpolate __timeTo function", func() { Convey("interpolate __timeTo function", func() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment