2016-10-05 03:56:34 -05:00
|
|
|
package influxdb
|
|
|
|
|
2016-10-05 13:36:05 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-10-06 05:51:45 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
2016-10-05 13:36:05 -05:00
|
|
|
)
|
2016-10-05 03:56:34 -05:00
|
|
|
|
2016-10-06 07:16:26 -05:00
|
|
|
type QueryBuilder struct{}
|
2016-10-05 03:56:34 -05:00
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
func (qb *QueryBuilder) Build(query *Query, queryContext *tsdb.QueryContext) (string, error) {
|
|
|
|
res := qb.renderSelectors(query, queryContext)
|
|
|
|
res += qb.renderMeasurement(query)
|
|
|
|
res += qb.renderWhereClause(query)
|
|
|
|
res += qb.renderTimeFilter(query, queryContext)
|
|
|
|
res += qb.renderGroupBy(query, queryContext)
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *QueryBuilder) renderTags(query *Query) []string {
|
2016-10-05 13:36:05 -05:00
|
|
|
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-10 04:58:06 -05:00
|
|
|
func (qb *QueryBuilder) renderTimeFilter(query *Query, queryContext *tsdb.QueryContext) string {
|
2016-10-07 04:33:17 -05:00
|
|
|
from := "now() - " + queryContext.TimeRange.From
|
2016-10-07 04:23:37 -05:00
|
|
|
to := ""
|
|
|
|
|
2016-10-07 04:33:17 -05:00
|
|
|
if queryContext.TimeRange.To != "now" && queryContext.TimeRange.To != "" {
|
|
|
|
to = " and time < now() - " + strings.Replace(queryContext.TimeRange.To, "now-", "", 1)
|
2016-10-07 04:23:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("time > %s%s", from, to)
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
func (qb *QueryBuilder) renderSelectors(query *Query, queryContext *tsdb.QueryContext) string {
|
2016-10-05 13:36:05 -05:00
|
|
|
res := "SELECT "
|
|
|
|
|
|
|
|
var selectors []string
|
|
|
|
for _, sel := range query.Selects {
|
|
|
|
|
|
|
|
stk := ""
|
|
|
|
for _, s := range *sel {
|
2016-10-10 00:12:21 -05:00
|
|
|
stk = s.Render(queryContext, stk)
|
2016-10-05 13:36:05 -05:00
|
|
|
}
|
|
|
|
selectors = append(selectors, stk)
|
|
|
|
}
|
|
|
|
|
2016-10-06 07:16:26 -05:00
|
|
|
return res + strings.Join(selectors, ", ")
|
|
|
|
}
|
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
func (qb *QueryBuilder) renderMeasurement(query *Query) string {
|
2016-10-05 13:57:28 -05:00
|
|
|
policy := ""
|
2016-10-06 05:51:45 -05:00
|
|
|
if query.Policy == "" || query.Policy == "default" {
|
|
|
|
policy = ""
|
|
|
|
} else {
|
2016-10-05 13:57:28 -05:00
|
|
|
policy = `"` + query.Policy + `".`
|
|
|
|
}
|
2016-10-06 07:16:26 -05:00
|
|
|
return fmt.Sprintf(` FROM %s"%s"`, policy, query.Measurement)
|
|
|
|
}
|
2016-10-05 13:36:05 -05:00
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
func (qb *QueryBuilder) renderWhereClause(query *Query) string {
|
2016-10-06 07:16:26 -05:00
|
|
|
res := " WHERE "
|
2016-10-10 04:58:06 -05:00
|
|
|
conditions := qb.renderTags(query)
|
2016-10-05 13:36:05 -05:00
|
|
|
res += strings.Join(conditions, " ")
|
|
|
|
if len(conditions) > 0 {
|
|
|
|
res += " AND "
|
|
|
|
}
|
|
|
|
|
2016-10-06 07:16:26 -05:00
|
|
|
return res
|
|
|
|
}
|
2016-10-05 13:36:05 -05:00
|
|
|
|
2016-10-10 04:58:06 -05:00
|
|
|
func (qb *QueryBuilder) renderGroupBy(query *Query, queryContext *tsdb.QueryContext) string {
|
2016-10-07 08:09:54 -05:00
|
|
|
groupBy := ""
|
|
|
|
for i, group := range query.GroupBy {
|
|
|
|
if i == 0 {
|
|
|
|
groupBy += " GROUP BY"
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 && group.Type != "fill" {
|
2016-10-10 02:52:53 -05:00
|
|
|
groupBy += ", " //fill is so very special. fill is a creep, fill is a weirdo
|
2016-10-07 08:09:54 -05:00
|
|
|
} else {
|
|
|
|
groupBy += " "
|
|
|
|
}
|
2016-10-05 13:36:05 -05:00
|
|
|
|
2016-10-10 00:12:21 -05:00
|
|
|
groupBy += group.Render(queryContext, "")
|
2016-10-05 13:36:05 -05:00
|
|
|
}
|
2016-10-05 03:56:34 -05:00
|
|
|
|
2016-10-07 08:09:54 -05:00
|
|
|
return groupBy
|
2016-10-05 03:56:34 -05:00
|
|
|
}
|