2016-10-05 03:56:34 -05:00
|
|
|
package influxdb
|
|
|
|
|
2016-10-05 13:36:05 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2016-10-05 03:56:34 -05:00
|
|
|
|
|
|
|
type QueryBuild struct{}
|
|
|
|
|
2016-10-05 13:36:05 -05:00
|
|
|
func renderTags(query *Query) []string {
|
|
|
|
var res []string
|
|
|
|
for i, tag := range query.Tags {
|
|
|
|
str := ""
|
|
|
|
|
|
|
|
if i > 0 {
|
|
|
|
if tag.Condition == "" {
|
|
|
|
str += "AND"
|
|
|
|
} else {
|
|
|
|
str += tag.Condition
|
|
|
|
}
|
|
|
|
str += " "
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, fmt.Sprintf(`%s"%s" %s '%s'`, str, tag.Key, tag.Operator, tag.Value))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2016-10-05 03:56:34 -05:00
|
|
|
func (*QueryBuild) Build(query *Query) (string, error) {
|
2016-10-05 13:36:05 -05:00
|
|
|
res := "SELECT "
|
|
|
|
|
|
|
|
var selectors []string
|
|
|
|
for _, sel := range query.Selects {
|
|
|
|
|
|
|
|
stk := ""
|
|
|
|
for _, s := range *sel {
|
|
|
|
stk = s.Render(stk)
|
|
|
|
}
|
|
|
|
selectors = append(selectors, stk)
|
|
|
|
}
|
|
|
|
res += strings.Join(selectors, ", ")
|
|
|
|
|
2016-10-05 13:57:28 -05:00
|
|
|
policy := ""
|
|
|
|
if query.Policy != "" {
|
|
|
|
policy = `"` + query.Policy + `".`
|
|
|
|
}
|
|
|
|
res += fmt.Sprintf(` FROM %s"%s"`, policy, query.Measurement)
|
2016-10-05 13:36:05 -05:00
|
|
|
|
|
|
|
res += " WHERE "
|
|
|
|
conditions := renderTags(query)
|
|
|
|
res += strings.Join(conditions, " ")
|
|
|
|
if len(conditions) > 0 {
|
|
|
|
res += " AND "
|
|
|
|
}
|
|
|
|
|
|
|
|
res += "$timeFilter"
|
|
|
|
|
|
|
|
var groupBy []string
|
|
|
|
for _, group := range query.GroupBy {
|
|
|
|
groupBy = append(groupBy, group.Render(""))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(groupBy) > 0 {
|
|
|
|
res += " GROUP BY " + strings.Join(groupBy, " ")
|
|
|
|
}
|
2016-10-05 03:56:34 -05:00
|
|
|
|
2016-10-05 13:36:05 -05:00
|
|
|
return res, nil
|
2016-10-05 03:56:34 -05:00
|
|
|
}
|