grafana/pkg/tsdb/influxdb/flux/macros.go
ismail simsek b0dfeb1911
Chore: Clean up intervalv2 functions (#82074)
* clean up intervalv2 functions

* use roundInterval from grafana-plugin-sdk-go

* use from grafana-plugin-sdk-go

* have intervalv2 in publicdashboards and remove tsdb/intervalv2

* legacydata cleanup

* remove unused variables

* Update pkg/tsdb/legacydata/interval/interval.go

Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com>

---------

Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com>
2024-02-09 11:39:21 +01:00

60 lines
1.8 KiB
Go

package flux
import (
"regexp"
"strconv"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
)
// $__interval_ms is the exact value in milliseconds
// $__interval is rounded to nice whole values
// v.windowPeriod is the exact value string-formatted
func interpolateInterval(flux string, interval time.Duration) string {
intervalMs := int64(interval / time.Millisecond)
intervalText := gtime.FormatInterval(interval)
flux = strings.ReplaceAll(flux, "$__interval_ms", strconv.FormatInt(intervalMs, 10))
flux = strings.ReplaceAll(flux, "$__interval", intervalText)
return flux
}
var fluxVariableFilterExp = regexp.MustCompile(`(?m)([a-zA-Z]+)\.([a-zA-Z]+)`)
func interpolateFluxSpecificVariables(query queryModel) string {
flux := query.RawQuery
matches := fluxVariableFilterExp.FindAllStringSubmatch(flux, -1)
if matches != nil {
timeRange := query.TimeRange
from := timeRange.From.UTC().Format(time.RFC3339Nano)
to := timeRange.To.UTC().Format(time.RFC3339Nano)
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+"\"")
}
}
}
return flux
}
func interpolate(query queryModel) string {
flux := interpolateFluxSpecificVariables(query)
flux = interpolateInterval(flux, query.Interval)
return flux
}