2016-10-05 03:56:34 -05:00
|
|
|
package influxdb
|
|
|
|
|
2016-10-05 13:36:05 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-01-11 05:10:26 -06:00
|
|
|
"strconv"
|
2016-10-05 13:36:05 -05:00
|
|
|
"strings"
|
2017-01-11 05:10:26 -06:00
|
|
|
"time"
|
2016-10-06 05:51:45 -05:00
|
|
|
|
2016-11-10 03:41:00 -06:00
|
|
|
"regexp"
|
|
|
|
|
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-11-10 03:41:00 -06:00
|
|
|
var (
|
2016-11-14 01:47:45 -06:00
|
|
|
regexpOperatorPattern *regexp.Regexp = regexp.MustCompile(`^\/.*\/$`)
|
|
|
|
regexpMeasurementPattern *regexp.Regexp = regexp.MustCompile(`^\/.*\/$`)
|
2016-11-10 03:41:00 -06:00
|
|
|
)
|
|
|
|
|
2017-09-20 11:31:34 -05:00
|
|
|
func (query *Query) Build(queryContext *tsdb.TsdbQuery) (string, error) {
|
2017-01-11 05:10:26 -06:00
|
|
|
var res string
|
|
|
|
|
2016-11-10 07:16:18 -06:00
|
|
|
if query.UseRawQuery && query.RawQuery != "" {
|
2017-01-11 05:10:26 -06:00
|
|
|
res = query.RawQuery
|
|
|
|
} else {
|
|
|
|
res = query.renderSelectors(queryContext)
|
|
|
|
res += query.renderMeasurement()
|
|
|
|
res += query.renderWhereClause()
|
|
|
|
res += query.renderTimeFilter(queryContext)
|
|
|
|
res += query.renderGroupBy(queryContext)
|
|
|
|
}
|
|
|
|
|
2017-11-15 04:22:00 -06:00
|
|
|
calculator := tsdb.NewIntervalCalculator(&tsdb.IntervalOptions{})
|
|
|
|
interval := calculator.Calculate(queryContext.TimeRange, query.Interval)
|
2017-01-11 05:10:26 -06:00
|
|
|
|
2017-04-27 01:51:43 -05:00
|
|
|
res = strings.Replace(res, "$timeFilter", query.renderTimeFilter(queryContext), -1)
|
|
|
|
res = strings.Replace(res, "$interval", interval.Text, -1)
|
|
|
|
res = strings.Replace(res, "$__interval_ms", strconv.FormatInt(interval.Value.Nanoseconds()/int64(time.Millisecond), 10), -1)
|
|
|
|
res = strings.Replace(res, "$__interval", interval.Text, -1)
|
2017-01-11 05:10:26 -06:00
|
|
|
return res, nil
|
|
|
|
}
|
2016-10-11 11:29:09 -05:00
|
|
|
|
2016-11-10 07:38:06 -06:00
|
|
|
func (query *Query) renderTags() []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 += " "
|
|
|
|
}
|
|
|
|
|
2016-11-10 03:41:00 -06:00
|
|
|
//If the operator is missing we fall back to sensible defaults
|
|
|
|
if tag.Operator == "" {
|
|
|
|
if regexpOperatorPattern.Match([]byte(tag.Value)) {
|
|
|
|
tag.Operator = "=~"
|
|
|
|
} else {
|
|
|
|
tag.Operator = "="
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// quote value unless regex or number
|
2018-04-22 13:51:58 -05:00
|
|
|
var textValue string
|
2016-11-07 07:26:20 -06:00
|
|
|
if tag.Operator == "=~" || tag.Operator == "!~" {
|
2016-11-10 03:41:00 -06:00
|
|
|
textValue = tag.Value
|
2016-11-17 08:47:15 -06:00
|
|
|
} else if tag.Operator == "<" || tag.Operator == ">" {
|
|
|
|
textValue = tag.Value
|
2016-11-07 07:26:20 -06:00
|
|
|
} else {
|
2018-02-19 03:00:09 -06:00
|
|
|
textValue = fmt.Sprintf("'%s'", strings.Replace(tag.Value, `\`, `\\`, -1))
|
2016-11-07 07:26:20 -06:00
|
|
|
}
|
|
|
|
|
2016-11-10 03:41:00 -06:00
|
|
|
res = append(res, fmt.Sprintf(`%s"%s" %s %s`, str, tag.Key, tag.Operator, textValue))
|
2016-10-05 13:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-09-20 11:31:34 -05:00
|
|
|
func (query *Query) renderTimeFilter(queryContext *tsdb.TsdbQuery) 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
|
|
|
}
|
|
|
|
|
2017-09-20 11:31:34 -05:00
|
|
|
func (query *Query) renderSelectors(queryContext *tsdb.TsdbQuery) 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-13 04:42:51 -05:00
|
|
|
stk = s.Render(query, 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-11-10 07:38:06 -06:00
|
|
|
func (query *Query) renderMeasurement() string {
|
2018-04-22 13:51:58 -05:00
|
|
|
var policy string
|
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-11-14 01:47:45 -06:00
|
|
|
|
|
|
|
measurement := query.Measurement
|
|
|
|
|
|
|
|
if !regexpMeasurementPattern.Match([]byte(measurement)) {
|
|
|
|
measurement = fmt.Sprintf(`"%s"`, measurement)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(` FROM %s%s`, policy, measurement)
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|
2016-10-05 13:36:05 -05:00
|
|
|
|
2016-11-10 07:38:06 -06:00
|
|
|
func (query *Query) renderWhereClause() string {
|
2016-10-06 07:16:26 -05:00
|
|
|
res := " WHERE "
|
2016-11-10 07:38:06 -06:00
|
|
|
conditions := query.renderTags()
|
2016-10-05 13:36:05 -05:00
|
|
|
if len(conditions) > 0 {
|
2017-08-31 02:33:03 -05:00
|
|
|
if len(conditions) > 1 {
|
|
|
|
res += "(" + strings.Join(conditions, " ") + ")"
|
|
|
|
} else {
|
|
|
|
res += conditions[0]
|
|
|
|
}
|
2016-10-05 13:36:05 -05:00
|
|
|
res += " AND "
|
|
|
|
}
|
|
|
|
|
2016-10-06 07:16:26 -05:00
|
|
|
return res
|
|
|
|
}
|
2016-10-05 13:36:05 -05:00
|
|
|
|
2017-09-20 11:31:34 -05:00
|
|
|
func (query *Query) renderGroupBy(queryContext *tsdb.TsdbQuery) 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-13 04:42:51 -05:00
|
|
|
groupBy += group.Render(query, 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
|
|
|
}
|