mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 10:03:33 -06:00
* 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>
46 lines
1.2 KiB
Go
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
|
|
}
|