diff --git a/pkg/tsdb/graphite/graphite.go b/pkg/tsdb/graphite/graphite.go index 89cea55938f..3d6af5beb5b 100644 --- a/pkg/tsdb/graphite/graphite.go +++ b/pkg/tsdb/graphite/graphite.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "path" + "regexp" "strings" "golang.org/x/net/context/ctxhttp" @@ -49,9 +50,9 @@ func (e *GraphiteExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, for _, query := range queries { if fullTarget, err := query.Model.Get("targetFull").String(); err == nil { - formData["target"] = []string{fullTarget} + formData["target"] = []string{fixIntervalFormat(fullTarget)} } else { - formData["target"] = []string{query.Model.Get("target").MustString()} + formData["target"] = []string{fixIntervalFormat(query.Model.Get("target").MustString())} } } @@ -141,3 +142,17 @@ func formatTimeRange(input string) string { } return strings.Replace(strings.Replace(input, "m", "min", -1), "M", "mon", -1) } + +func fixIntervalFormat(target string) string { + rMinute := regexp.MustCompile(`'(\d+)m'`) + rMin := regexp.MustCompile("m") + target = rMinute.ReplaceAllStringFunc(target, func(m string) string { + return rMin.ReplaceAllString(m, "min") + }) + rMonth := regexp.MustCompile(`'(\d+)M'`) + rMon := regexp.MustCompile("M") + target = rMonth.ReplaceAllStringFunc(target, func(M string) string { + return rMon.ReplaceAllString(M, "mon") + }) + return target +} diff --git a/pkg/tsdb/graphite/graphite_test.go b/pkg/tsdb/graphite/graphite_test.go index 7a3bba9035b..c1a2736293b 100644 --- a/pkg/tsdb/graphite/graphite_test.go +++ b/pkg/tsdb/graphite/graphite_test.go @@ -1 +1,61 @@ package graphite + +import ( + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestGraphiteFunctions(t *testing.T) { + Convey("Testing Graphite Functions", t, func() { + + Convey("formatting time range for now", func() { + + timeRange := formatTimeRange("now") + So(timeRange, ShouldEqual, "now") + + }) + + Convey("formatting time range for now-1m", func() { + + timeRange := formatTimeRange("now-1m") + So(timeRange, ShouldEqual, "now-1min") + + }) + + Convey("formatting time range for now-1M", func() { + + timeRange := formatTimeRange("now-1M") + So(timeRange, ShouldEqual, "now-1mon") + + }) + + Convey("fix interval format in query for 1m", func() { + + timeRange := fixIntervalFormat("aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1m'), 4)") + So(timeRange, ShouldEqual, "aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1min'), 4)") + + }) + + Convey("fix interval format in query for 1M", func() { + + timeRange := fixIntervalFormat("aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1M'), 4)") + So(timeRange, ShouldEqual, "aliasByNode(hitcount(averageSeries(app.grafana.*.dashboards.views.count), '1mon'), 4)") + + }) + + Convey("should not override query for 1M", func() { + + timeRange := fixIntervalFormat("app.grafana.*.dashboards.views.1M.count") + So(timeRange, ShouldEqual, "app.grafana.*.dashboards.views.1M.count") + + }) + + Convey("should not override query for 1m", func() { + + timeRange := fixIntervalFormat("app.grafana.*.dashboards.views.1m.count") + So(timeRange, ShouldEqual, "app.grafana.*.dashboards.views.1m.count") + + }) + + }) +}