2016-10-05 03:56:34 -05:00
|
|
|
package influxdb
|
|
|
|
|
2016-10-05 13:36:05 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-23 03:24:47 -05:00
|
|
|
"regexp"
|
2017-01-11 05:10:26 -06:00
|
|
|
"strconv"
|
2016-10-05 13:36:05 -05:00
|
|
|
"strings"
|
2021-07-19 04:32:33 -05:00
|
|
|
"time"
|
2016-10-06 05:51:45 -05:00
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2021-09-07 02:35:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/intervalv2"
|
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 (
|
2018-04-27 15:14:36 -05:00
|
|
|
regexpOperatorPattern = regexp.MustCompile(`^\/.*\/$`)
|
|
|
|
regexpMeasurementPattern = regexp.MustCompile(`^\/.*\/$`)
|
2016-11-10 03:41:00 -06:00
|
|
|
)
|
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
func (query *Query) Build(queryContext *backend.QueryDataRequest) (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)
|
2018-12-21 09:38:53 -06:00
|
|
|
res += query.renderTz()
|
2017-01-11 05:10:26 -06:00
|
|
|
}
|
|
|
|
|
2021-09-07 02:35:37 -05:00
|
|
|
intervalText := intervalv2.FormatDuration(query.Interval)
|
2021-09-06 02:33:07 -05:00
|
|
|
intervalMs := int64(query.Interval / time.Millisecond)
|
2017-01-11 05:10:26 -06:00
|
|
|
|
2020-09-22 09:22:19 -05:00
|
|
|
res = strings.ReplaceAll(res, "$timeFilter", query.renderTimeFilter(queryContext))
|
2021-09-06 02:33:07 -05:00
|
|
|
res = strings.ReplaceAll(res, "$interval", intervalText)
|
|
|
|
res = strings.ReplaceAll(res, "$__interval_ms", strconv.FormatInt(intervalMs, 10))
|
|
|
|
res = strings.ReplaceAll(res, "$__interval", intervalText)
|
2021-07-19 04:32:33 -05:00
|
|
|
|
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 += " "
|
|
|
|
}
|
|
|
|
|
2020-09-22 09:22:19 -05:00
|
|
|
// If the operator is missing we fall back to sensible defaults
|
2016-11-10 03:41:00 -06:00
|
|
|
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
|
2020-07-16 07:39:01 -05:00
|
|
|
switch tag.Operator {
|
|
|
|
case "=~", "!~":
|
2016-11-10 03:41:00 -06:00
|
|
|
textValue = tag.Value
|
2020-07-16 07:39:01 -05:00
|
|
|
case "<", ">":
|
2016-11-17 08:47:15 -06:00
|
|
|
textValue = tag.Value
|
2020-07-16 07:39:01 -05:00
|
|
|
default:
|
2020-09-22 09:22:19 -05:00
|
|
|
textValue = fmt.Sprintf("'%s'", strings.ReplaceAll(tag.Value, `\`, `\\`))
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
func (query *Query) renderTimeFilter(queryContext *backend.QueryDataRequest) string {
|
|
|
|
from, to := epochMStoInfluxTime(&queryContext.Queries[0].TimeRange)
|
|
|
|
return fmt.Sprintf("time > %s and time < %s", from, to)
|
2016-10-06 07:16:26 -05:00
|
|
|
}
|
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
func (query *Query) renderSelectors(queryContext *backend.QueryDataRequest) 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
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
func (query *Query) renderGroupBy(queryContext *backend.QueryDataRequest) 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" {
|
2020-09-22 09:22:19 -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
|
|
|
}
|
2018-12-21 09:38:53 -06:00
|
|
|
|
|
|
|
func (query *Query) renderTz() string {
|
|
|
|
tz := query.Tz
|
|
|
|
if tz == "" {
|
|
|
|
return ""
|
|
|
|
}
|
2019-04-23 03:24:47 -05:00
|
|
|
return fmt.Sprintf(" tz('%s')", tz)
|
2018-12-21 09:38:53 -06:00
|
|
|
}
|
2021-03-18 07:02:37 -05:00
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
func epochMStoInfluxTime(tr *backend.TimeRange) (string, string) {
|
|
|
|
from := tr.From.UnixNano() / int64(time.Millisecond)
|
|
|
|
to := tr.To.UnixNano() / int64(time.Millisecond)
|
2021-03-18 07:02:37 -05:00
|
|
|
|
2021-07-19 04:32:33 -05:00
|
|
|
return fmt.Sprintf("%dms", from), fmt.Sprintf("%dms", to)
|
2021-03-18 07:02:37 -05:00
|
|
|
}
|