From 9a83a285d91413339bad0fdce15ae643e91f09bd Mon Sep 17 00:00:00 2001 From: Chris Goller Date: Mon, 11 Dec 2017 09:49:24 -0600 Subject: [PATCH] Fix template rendering to ignore template if not in query --- influx/templates.go | 6 +++ influx/templates_test.go | 98 ++++++++++++++++++++++++++++++++++------ 2 files changed, 89 insertions(+), 15 deletions(-) diff --git a/influx/templates.go b/influx/templates.go index 8c0ad0e28..4dfe76fe1 100644 --- a/influx/templates.go +++ b/influx/templates.go @@ -38,6 +38,12 @@ func RenderTemplate(query string, t chronograf.TemplateVar, now time.Time) (stri if len(t.Values) == 0 { return query, nil } + + // we only need to render the template if the template exists in the query + if !strings.Contains(query, t.Var) { + return query, nil + } + switch t.Values[0].Type { case "tagKey", "fieldKey", "measurement", "database": return strings.Replace(query, t.Var, `"`+t.Values[0].Value+`"`, -1), nil diff --git a/influx/templates_test.go b/influx/templates_test.go index 482a16dc5..c1e4d6277 100644 --- a/influx/templates_test.go +++ b/influx/templates_test.go @@ -62,10 +62,10 @@ func TestTemplateReplace(t *testing.T) { }, { name: "select with parameters and aggregates", - query: `SELECT mean($field) FROM "cpu" WHERE $tag = $value GROUP BY $tag`, + query: `SELECT mean(:field:) FROM "cpu" WHERE :tag: = :value: GROUP BY :tag:`, vars: []chronograf.TemplateVar{ chronograf.TemplateVar{ - Var: "$value", + Var: ":value:", Values: []chronograf.TemplateValue{ { Type: "tagValue", @@ -74,7 +74,7 @@ func TestTemplateReplace(t *testing.T) { }, }, chronograf.TemplateVar{ - Var: "$tag", + Var: ":tag:", Values: []chronograf.TemplateValue{ { Type: "tagKey", @@ -83,7 +83,7 @@ func TestTemplateReplace(t *testing.T) { }, }, chronograf.TemplateVar{ - Var: "$field", + Var: ":field:", Values: []chronograf.TemplateValue{ { Type: "fieldKey", @@ -96,25 +96,25 @@ func TestTemplateReplace(t *testing.T) { }, { name: "Non-existant parameters", - query: `SELECT $field FROM "cpu"`, - want: `SELECT $field FROM "cpu"`, + query: `SELECT :field: FROM "cpu"`, + want: `SELECT :field: FROM "cpu"`, }, { name: "var without a value", - query: `SELECT $field FROM "cpu"`, + query: `SELECT :field: FROM "cpu"`, vars: []chronograf.TemplateVar{ chronograf.TemplateVar{ - Var: "$field", + Var: ":field:", }, }, - want: `SELECT $field FROM "cpu"`, + want: `SELECT :field: FROM "cpu"`, }, { name: "var with unknown type", - query: `SELECT $field FROM "cpu"`, + query: `SELECT :field: FROM "cpu"`, vars: []chronograf.TemplateVar{ chronograf.TemplateVar{ - Var: "$field", + Var: ":field:", Values: []chronograf.TemplateValue{ { Type: "who knows?", @@ -123,7 +123,7 @@ func TestTemplateReplace(t *testing.T) { }, }, }, - want: `SELECT $field FROM "cpu"`, + want: `SELECT :field: FROM "cpu"`, }, { name: "auto group by", @@ -224,6 +224,71 @@ func TestTemplateReplace(t *testing.T) { }, want: `SELECT mean(usage_idle) FROM "cpu" WHERE time > now() - 1h GROUP BY time(93s)`, }, + { + name: "no template variables specified", + query: `SELECT mean(usage_idle) FROM "cpu" WHERE time > :dashboardTime: GROUP BY :interval:`, + want: `SELECT mean(usage_idle) FROM "cpu" WHERE time > :dashboardTime: GROUP BY :interval:`, + }, + { + name: "auto group by failing condition", + query: `SELECT mean(usage_idle) FROM "cpu" WHERE time > :dashboardTime: GROUP BY :interval:`, + vars: []chronograf.TemplateVar{ + { + Var: ":interval:", + Values: []chronograf.TemplateValue{ + { + Value: "115", + Type: "resolution", + }, + { + Value: "3", + Type: "pointsPerPixel", + }, + }, + }, + { + Var: ":dashboardTime:", + Values: []chronograf.TemplateValue{ + { + Value: "now() - 1h", + Type: "constant", + Selected: true, + }, + }, + }, + }, + want: `SELECT mean(usage_idle) FROM "cpu" WHERE time > now() - 1h GROUP BY time(93s)`, + }, + { + name: "query with no template variables contained should return query", + query: `SHOW DATABASES`, + vars: []chronograf.TemplateVar{ + { + Var: ":interval:", + Values: []chronograf.TemplateValue{ + { + Value: "115", + Type: "resolution", + }, + { + Value: "3", + Type: "pointsPerPixel", + }, + }, + }, + { + Var: ":dashboardTime:", + Values: []chronograf.TemplateValue{ + { + Value: "now() - 1h", + Type: "constant", + Selected: true, + }, + }, + }, + }, + want: `SHOW DATABASES`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -392,11 +457,16 @@ func Test_RenderTemplate(t *testing.T) { want: "SELECT mean(usage_idle) FROM cpu WHERE time > '1985-10-25T00:01:00Z' and time < '1985-10-25T00:02:00Z' GROUP BY time(179ms)", }, { - name: "absolute time with nano seconds and zero duraiton", + name: "absolute time with nano seconds and zero duration", query: "SELECT mean(usage_idle) FROM cpu WHERE time > '2017-07-24T15:33:42.994Z' and time < '2017-07-24T15:33:42.994Z' GROUP BY :interval:", resolution: 1000, want: "SELECT mean(usage_idle) FROM cpu WHERE time > '2017-07-24T15:33:42.994Z' and time < '2017-07-24T15:33:42.994Z' GROUP BY time(1ms)", }, + { + name: "query should be returned if there are no template variables", + query: "SHOW DATABASES", + want: "SHOW DATABASES", + }, } for _, tt := range gbvTests { @@ -426,5 +496,3 @@ func Test_RenderTemplate(t *testing.T) { }) } } - -// SELECT mean("numSeries") AS "mean_numSeries" FROM "_internal"."monitor"."database" WHERE time > now() - 1h GROUP BY :interval: FILL(null);SELECT mean("numSeries") AS "mean_numSeries_shifted__1__h" FROM "_internal"."monitor"."database" WHERE time > now() - 1h - 1h AND time < now() - 1h GROUP BY :interval: FILL(null)