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"
|
2023-01-30 02:38:51 -06:00
|
|
|
|
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)
|
2022-02-09 12:26:16 -06:00
|
|
|
res += query.renderOrderByTime()
|
|
|
|
res += query.renderLimit()
|
|
|
|
res += query.renderSlimit()
|
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 {
|
2023-01-23 10:44:27 -06:00
|
|
|
res := make([]string, 0, len(query.Tags))
|
2016-10-05 13:36:05 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-02 08:05:24 -06:00
|
|
|
escapedKey := fmt.Sprintf(`"%s"`, tag.Key)
|
|
|
|
|
|
|
|
if strings.HasSuffix(tag.Key, "::tag") {
|
|
|
|
escapedKey = fmt.Sprintf(`"%s"::tag`, strings.TrimSuffix(tag.Key, "::tag"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(tag.Key, "::field") {
|
|
|
|
escapedKey = fmt.Sprintf(`"%s"::field`, strings.TrimSuffix(tag.Key, "::field"))
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, fmt.Sprintf(`%s%s %s %s`, str, escapedKey, 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)
|
2022-07-07 12:55:51 -05:00
|
|
|
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 "
|
|
|
|
|
2023-01-23 10:44:27 -06:00
|
|
|
selectors := make([]string, 0, len(query.Selects))
|
2016-10-05 13:36:05 -05:00
|
|
|
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
|
|
|
|
2022-02-09 12:26:16 -06:00
|
|
|
func (query *Query) renderOrderByTime() string {
|
|
|
|
orderByTime := query.OrderByTime
|
|
|
|
if orderByTime == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(" ORDER BY time %s", orderByTime)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-02-09 12:26:16 -06:00
|
|
|
func (query *Query) renderLimit() string {
|
|
|
|
limit := query.Limit
|
|
|
|
if limit == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(" limit %s", limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (query *Query) renderSlimit() string {
|
|
|
|
slimit := query.Slimit
|
|
|
|
if slimit == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(" slimit %s", slimit)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|