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"
|
2016-10-06 05:51:45 -05:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/interval"
|
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-03-08 00:02:49 -06:00
|
|
|
func (query *Query) Build(queryContext plugins.DataQuery) (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-03-08 00:02:49 -06:00
|
|
|
calculator := interval.NewCalculator(interval.CalculatorOptions{})
|
|
|
|
i := calculator.Calculate(*queryContext.TimeRange, query.Interval)
|
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-03-08 00:02:49 -06:00
|
|
|
res = strings.ReplaceAll(res, "$interval", i.Text)
|
|
|
|
res = strings.ReplaceAll(res, "$__interval_ms", strconv.FormatInt(i.Milliseconds(), 10))
|
|
|
|
res = strings.ReplaceAll(res, "$__interval", i.Text)
|
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-03-18 07:02:37 -05:00
|
|
|
func isTimeRangeNumeric(tr *plugins.DataTimeRange) bool {
|
|
|
|
if _, err := strconv.ParseInt(tr.From, 10, 64); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, err := strconv.ParseInt(tr.To, 10, 64); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
func (query *Query) renderTimeFilter(queryContext plugins.DataQuery) string {
|
2021-03-18 07:02:37 -05:00
|
|
|
// If from expressions
|
|
|
|
if isTimeRangeNumeric(queryContext.TimeRange) {
|
|
|
|
from, to, err := epochMStoInfluxTime(queryContext.TimeRange)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Sprintf(" time > %s and time < %s ", from, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
// on error fallback to original time range processing.
|
|
|
|
glog.Warn("failed to parse expected time range in query, falling back to non-expression time range processing", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// else from dashboard alerting
|
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
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
func (query *Query) renderSelectors(queryContext plugins.DataQuery) 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-03-08 00:02:49 -06:00
|
|
|
func (query *Query) renderGroupBy(queryContext plugins.DataQuery) 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
|
|
|
|
|
|
|
func epochMStoInfluxTime(tr *plugins.DataTimeRange) (string, string, error) {
|
|
|
|
from, err := strconv.ParseInt(tr.From, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
to, err := strconv.ParseInt(tr.To, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%dms", from), fmt.Sprintf("%dms", to), nil
|
|
|
|
}
|