Move template variables into chronograf.go

pull/10616/head
Chris Goller 2017-04-19 11:18:23 -05:00
parent 5538cfa963
commit 4ba46c8c0d
3 changed files with 106 additions and 112 deletions

View File

@ -123,15 +123,46 @@ type Range struct {
Lower int64 `json:"lower"` // Lower is the lower bound
}
// TemplateValue is a value use to replace a template in an InfluxQL query
type TemplateValue struct {
Value string `json:"value"`
Type string `json:"type"`
}
// TemplateVar is a named variable within an InfluxQL query to be replaced with Values
type TemplateVar struct {
Var string `json:"tempVar"`
Values []TemplateValue `json:"values"`
}
// String converts the template variable into a correct InfluxQL string based
// on its type
func (t TemplateVar) String() string {
if len(t.Values) == 0 {
return ""
}
switch t.Values[0].Type {
case "tagKey", "fieldKey":
return `"` + t.Values[0].Value + `"`
case "tagValue":
return `'` + t.Values[0].Value + `'`
case "csv":
return t.Values[0].Value
default:
return ""
}
}
// Query retrieves a Response from a TimeSeries.
type Query struct {
Command string `json:"query"` // Command is the query itself
DB string `json:"db,omitempty"` // DB is optional and if empty will not be used.
RP string `json:"rp,omitempty"` // RP is a retention policy and optional; if empty will not be used.
Wheres []string `json:"wheres,omitempty"` // Wheres restricts the query to certain attributes
GroupBys []string `json:"groupbys,omitempty"` // GroupBys collate the query by these tags
Label string `json:"label,omitempty"` // Label is the Y-Axis label for the data
Range *Range `json:"range,omitempty"` // Range is the default Y-Axis range for the data
Command string `json:"query"` // Command is the query itself
DB string `json:"db,omitempty"` // DB is optional and if empty will not be used.
RP string `json:"rp,omitempty"` // RP is a retention policy and optional; if empty will not be used.
TemplateVars []TemplateVar `json:"tempVars"` // TemplateVars are template variables to replace within an InfluxQL query
Wheres []string `json:"wheres,omitempty"` // Wheres restricts the query to certain attributes
GroupBys []string `json:"groupbys,omitempty"` // GroupBys collate the query by these tags
Label string `json:"label,omitempty"` // Label is the Y-Axis label for the data
Range *Range `json:"range,omitempty"` // Range is the default Y-Axis range for the data
}
// DashboardQuery includes state for the query builder. This is a transition

View File

@ -1,46 +1,15 @@
package influx
import "strings"
import (
"strings"
// TemplateValue is a value use to replace a template in an InfluxQL query
type TemplateValue struct {
Value string `json:"value"`
Type string `json:"type"`
}
// TemplateVar is a named variable within an InfluxQL query to be replaced with Values
type TemplateVar struct {
Var string `json:"tempVar"`
Values []TemplateValue `json:"values"`
}
// String converts the template variable into a correct InfluxQL string based
// on its type
func (t TemplateVar) String() string {
if len(t.Values) == 0 {
return ""
}
switch t.Values[0].Type {
case "tagKey", "fieldKey":
return `"` + t.Values[0].Value + `"`
case "tagValue":
return `'` + t.Values[0].Value + `'`
case "csv":
return t.Values[0].Value
default:
return ""
}
}
// TemplateVars are template variables to replace within an InfluxQL query
type TemplateVars struct {
Vars []TemplateVar `json:"tempVars"`
}
"github.com/influxdata/chronograf"
)
// TemplateReplace replaces templates with values within the query string
func TemplateReplace(query string, templates TemplateVars) string {
func TemplateReplace(query string, templates []chronograf.TemplateVar) string {
replacements := []string{}
for _, v := range templates.Vars {
for _, v := range templates {
newVal := v.String()
if newVal != "" {
replacements = append(replacements, v.Var, newVal)

View File

@ -2,54 +2,54 @@ package influx
import (
"testing"
"github.com/influxdata/chronograf"
)
func TestTemplateReplace(t *testing.T) {
tests := []struct {
name string
query string
vars TemplateVars
vars []chronograf.TemplateVar
want string
}{
{
name: "select with parameters",
query: "$METHOD field1, $field FROM $measurement WHERE temperature > $temperature",
vars: TemplateVars{
Vars: []TemplateVar{
{
Var: "$temperature",
Values: []TemplateValue{
{
Type: "csv",
Value: "10",
},
vars: []chronograf.TemplateVar{
{
Var: "$temperature",
Values: []chronograf.TemplateValue{
{
Type: "csv",
Value: "10",
},
},
{
Var: "$field",
Values: []TemplateValue{
{
Type: "fieldKey",
Value: "field2",
},
},
{
Var: "$field",
Values: []chronograf.TemplateValue{
{
Type: "fieldKey",
Value: "field2",
},
},
{
Var: "$METHOD",
Values: []TemplateValue{
{
Type: "csv",
Value: "SELECT",
},
},
{
Var: "$METHOD",
Values: []chronograf.TemplateValue{
{
Type: "csv",
Value: "SELECT",
},
},
{
Var: "$measurement",
Values: []TemplateValue{
{
Type: "csv",
Value: `"cpu"`,
},
},
{
Var: "$measurement",
Values: []chronograf.TemplateValue{
{
Type: "csv",
Value: `"cpu"`,
},
},
},
@ -59,33 +59,31 @@ func TestTemplateReplace(t *testing.T) {
{
name: "select with parameters and aggregates",
query: `SELECT mean($field) FROM "cpu" WHERE $tag = $value GROUP BY $tag`,
vars: TemplateVars{
Vars: []TemplateVar{
{
Var: "$value",
Values: []TemplateValue{
{
Type: "tagValue",
Value: "howdy.com",
},
vars: []chronograf.TemplateVar{
{
Var: "$value",
Values: []chronograf.TemplateValue{
{
Type: "tagValue",
Value: "howdy.com",
},
},
{
Var: "$tag",
Values: []TemplateValue{
{
Type: "tagKey",
Value: "host",
},
},
{
Var: "$tag",
Values: []chronograf.TemplateValue{
{
Type: "tagKey",
Value: "host",
},
},
{
Var: "$field",
Values: []TemplateValue{
{
Type: "fieldKey",
Value: "field",
},
},
{
Var: "$field",
Values: []chronograf.TemplateValue{
{
Type: "fieldKey",
Value: "field",
},
},
},
@ -100,11 +98,9 @@ func TestTemplateReplace(t *testing.T) {
{
name: "var without a value",
query: `SELECT $field FROM "cpu"`,
vars: TemplateVars{
Vars: []TemplateVar{
{
Var: "$field",
},
vars: []chronograf.TemplateVar{
{
Var: "$field",
},
},
want: `SELECT $field FROM "cpu"`,
@ -112,15 +108,13 @@ func TestTemplateReplace(t *testing.T) {
{
name: "var with unknown type",
query: `SELECT $field FROM "cpu"`,
vars: TemplateVars{
Vars: []TemplateVar{
{
Var: "$field",
Values: []TemplateValue{
{
Type: "who knows?",
Value: "field",
},
vars: []chronograf.TemplateVar{
{
Var: "$field",
Values: []chronograf.TemplateValue{
{
Type: "who knows?",
Value: "field",
},
},
},