grafana/pkg/tsdb/influxdb/query.go
ying-jeanne c989f0fbbe
SQL: Migrate to use SDK contracts (#36635)
* convert SQLs to use sdk contracts

* make draft

* postgres

* intermedia

* get datasourceinfo filled at the beginning of the service

* move the interval into package because of cyclict  import and fix all postgres tests

* fix mysql test

* fix mssql

* fix the test for pr https://github.com/grafana/grafana/issues/35839

* fix some issue about intervalv2 package

* update sql test

* wire migration for SQLs

* add sqls to the background process

* make it register instead of register and start

* revert formatting

* fix tests

* fix linter

* remove integration test

* Postgres test fix

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
2021-09-07 09:35:37 +02:00

168 lines
3.9 KiB
Go

package influxdb
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/tsdb/intervalv2"
)
var (
regexpOperatorPattern = regexp.MustCompile(`^\/.*\/$`)
regexpMeasurementPattern = regexp.MustCompile(`^\/.*\/$`)
)
func (query *Query) Build(queryContext *backend.QueryDataRequest) (string, error) {
var res string
if query.UseRawQuery && query.RawQuery != "" {
res = query.RawQuery
} else {
res = query.renderSelectors(queryContext)
res += query.renderMeasurement()
res += query.renderWhereClause()
res += query.renderTimeFilter(queryContext)
res += query.renderGroupBy(queryContext)
res += query.renderTz()
}
intervalText := intervalv2.FormatDuration(query.Interval)
intervalMs := int64(query.Interval / time.Millisecond)
res = strings.ReplaceAll(res, "$timeFilter", query.renderTimeFilter(queryContext))
res = strings.ReplaceAll(res, "$interval", intervalText)
res = strings.ReplaceAll(res, "$__interval_ms", strconv.FormatInt(intervalMs, 10))
res = strings.ReplaceAll(res, "$__interval", intervalText)
return res, nil
}
func (query *Query) renderTags() []string {
var res []string
for i, tag := range query.Tags {
str := ""
if i > 0 {
if tag.Condition == "" {
str += "AND"
} else {
str += tag.Condition
}
str += " "
}
// 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
var textValue string
switch tag.Operator {
case "=~", "!~":
textValue = tag.Value
case "<", ">":
textValue = tag.Value
default:
textValue = fmt.Sprintf("'%s'", strings.ReplaceAll(tag.Value, `\`, `\\`))
}
res = append(res, fmt.Sprintf(`%s"%s" %s %s`, str, tag.Key, tag.Operator, textValue))
}
return res
}
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)
}
func (query *Query) renderSelectors(queryContext *backend.QueryDataRequest) string {
res := "SELECT "
var selectors []string
for _, sel := range query.Selects {
stk := ""
for _, s := range *sel {
stk = s.Render(query, queryContext, stk)
}
selectors = append(selectors, stk)
}
return res + strings.Join(selectors, ", ")
}
func (query *Query) renderMeasurement() string {
var policy string
if query.Policy == "" || query.Policy == "default" {
policy = ""
} else {
policy = `"` + query.Policy + `".`
}
measurement := query.Measurement
if !regexpMeasurementPattern.Match([]byte(measurement)) {
measurement = fmt.Sprintf(`"%s"`, measurement)
}
return fmt.Sprintf(` FROM %s%s`, policy, measurement)
}
func (query *Query) renderWhereClause() string {
res := " WHERE "
conditions := query.renderTags()
if len(conditions) > 0 {
if len(conditions) > 1 {
res += "(" + strings.Join(conditions, " ") + ")"
} else {
res += conditions[0]
}
res += " AND "
}
return res
}
func (query *Query) renderGroupBy(queryContext *backend.QueryDataRequest) string {
groupBy := ""
for i, group := range query.GroupBy {
if i == 0 {
groupBy += " GROUP BY"
}
if i > 0 && group.Type != "fill" {
groupBy += ", " // fill is so very special. fill is a creep, fill is a weirdo
} else {
groupBy += " "
}
groupBy += group.Render(query, queryContext, "")
}
return groupBy
}
func (query *Query) renderTz() string {
tz := query.Tz
if tz == "" {
return ""
}
return fmt.Sprintf(" tz('%s')", tz)
}
func epochMStoInfluxTime(tr *backend.TimeRange) (string, string) {
from := tr.From.UnixNano() / int64(time.Millisecond)
to := tr.To.UnixNano() / int64(time.Millisecond)
return fmt.Sprintf("%dms", from), fmt.Sprintf("%dms", to)
}