From c93d020190c959e7a35af1e5eee18ee4a1f60672 Mon Sep 17 00:00:00 2001 From: Michael Desa Date: Thu, 22 Feb 2018 13:17:49 -0500 Subject: [PATCH] Create new point template var template value Previously users specify `resolution` and `pixelsPerPoint`. This has been change and the front end will now need to specify `points` which is the number of points it'd like to get back from the query. --- influx/templates.go | 30 ++++++++++++++++++++++++++++++ influx/templates_test.go | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/influx/templates.go b/influx/templates.go index 4dfe76fe1..6402fbba3 100644 --- a/influx/templates.go +++ b/influx/templates.go @@ -58,6 +58,20 @@ func RenderTemplate(query string, t chronograf.TemplateVar, now time.Time) (stri tv[t.Values[i].Type] = t.Values[i].Value } + if pts, ok := tv["points"]; ok { + points, err := strconv.ParseInt(pts, 0, 64) + if err != nil { + return "", err + } + + dur, err := ParseTime(query, now) + if err != nil { + return "", err + } + interval := AutoInterval(points, dur) + return strings.Replace(query, t.Var, interval, -1), nil + } + if res, ok := tv["resolution"]; ok { resolution, err := strconv.ParseInt(res, 0, 64) if err != nil { @@ -82,6 +96,22 @@ func RenderTemplate(query string, t chronograf.TemplateVar, now time.Time) (stri return query, nil } +func AutoInterval(points int64, duration time.Duration) string { + // The function is: ((total_seconds * millisecond_converstion) / group_by) = pixels / 3 + // Number of points given the pixels + pixels := float64(points) + msPerPixel := float64(duration/time.Millisecond) / pixels + secPerPixel := float64(duration/time.Second) / pixels + if secPerPixel < 1.0 { + if msPerPixel < 1.0 { + msPerPixel = 1.0 + } + return strconv.FormatInt(int64(msPerPixel), 10) + "ms" + } + // If groupby is more than 1 second round to the second + return strconv.FormatInt(int64(secPerPixel), 10) + "s" +} + func AutoGroupBy(resolution, pixelsPerPoint int64, duration time.Duration) string { // The function is: ((total_seconds * millisecond_converstion) / group_by) = pixels / 3 // Number of points given the pixels diff --git a/influx/templates_test.go b/influx/templates_test.go index c1e4d6277..6c1e74c67 100644 --- a/influx/templates_test.go +++ b/influx/templates_test.go @@ -125,6 +125,38 @@ func TestTemplateReplace(t *testing.T) { }, want: `SELECT :field: FROM "cpu"`, }, + { + name: "auto interval", + query: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by time(:interval:)`, + vars: []chronograf.TemplateVar{ + { + Var: ":interval:", + Values: []chronograf.TemplateValue{ + { + Value: "333", + Type: "points", + }, + }, + }, + }, + want: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by time(46702s)`, + }, + { + name: "auto interval", + query: `SELECT derivative(mean(usage_idle),:interval:) from "cpu" where time > now() - 4320h group by time(:interval:)`, + vars: []chronograf.TemplateVar{ + { + Var: ":interval:", + Values: []chronograf.TemplateValue{ + { + Value: "333", + Type: "points", + }, + }, + }, + }, + want: `SELECT derivative(mean(usage_idle),46702s) from "cpu" where time > now() - 4320h group by time(46702s)`, + }, { name: "auto group by", query: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by :interval:`, @@ -133,7 +165,7 @@ func TestTemplateReplace(t *testing.T) { Var: ":interval:", Values: []chronograf.TemplateValue{ { - Value: "1000", + Value: "999", Type: "resolution", }, { @@ -143,7 +175,7 @@ func TestTemplateReplace(t *testing.T) { }, }, }, - want: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by time(46655s)`, + want: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by time(46702s)`, }, { name: "auto group by without duration", @@ -153,7 +185,7 @@ func TestTemplateReplace(t *testing.T) { Var: ":interval:", Values: []chronograf.TemplateValue{ { - Value: "1000", + Value: "999", Type: "resolution", }, { @@ -163,7 +195,7 @@ func TestTemplateReplace(t *testing.T) { }, }, }, - want: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by time(46655s)`, + want: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by time(46702s)`, }, { name: "auto group by with :dashboardTime:",