grafana/pkg/tsdb/influxdb/flux/macros.go
Ryan McKinley 5f1f820bb9
Influx: Support flux in the influx datasource (#25308)
* add flux 
* add token to datasource config editor
* add backend for flux
* make the interpolated query available in query inspector
* go mod tidy
* Chore: fixes a couple of strict null errors in influxdb plugin

Co-authored-by: kyle <kyle@grafana.com>
Co-authored-by: Lukas Siatka <lukasz.siatka@grafana.com>
2020-06-10 15:26:24 -04:00

46 lines
1.2 KiB
Go

package flux
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
const variableFilter = `(?m)([a-zA-Z]+)\.([a-zA-Z]+)`
// Interpolate processes macros
func Interpolate(query QueryModel) (string, error) {
flux := query.RawQuery
variableFilterExp, err := regexp.Compile(variableFilter)
matches := variableFilterExp.FindAllStringSubmatch(flux, -1)
if matches != nil {
timeRange := query.TimeRange
from := timeRange.From.UTC().Format(time.RFC3339)
to := timeRange.To.UTC().Format(time.RFC3339)
for _, match := range matches {
switch match[2] {
case "timeRangeStart":
flux = strings.ReplaceAll(flux, match[0], from)
case "timeRangeStop":
flux = strings.ReplaceAll(flux, match[0], to)
case "windowPeriod":
flux = strings.ReplaceAll(flux, match[0], query.Interval.String())
case "bucket":
flux = strings.ReplaceAll(flux, match[0], "\""+query.Options.Bucket+"\"")
case "defaultBucket":
flux = strings.ReplaceAll(flux, match[0], "\""+query.Options.DefaultBucket+"\"")
case "organization":
flux = strings.ReplaceAll(flux, match[0], "\""+query.Options.Organization+"\"")
}
}
}
backend.Logger.Info(fmt.Sprintf("%s => %v", flux, query.Options))
return flux, err
}