From ed6d3bf6ed16f880eff8f60c8016ec557da1a3dc Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Wed, 26 Sep 2018 17:50:08 +0200 Subject: [PATCH] stackdriver: WIP - implement stackdriver style auto alignment period. also return the used alignment period and display it in the query editor --- pkg/tsdb/stackdriver/stackdriver.go | 28 +++++++++++++++++-- .../partials/query.aggregation.html | 16 +++++++---- .../stackdriver/partials/query.editor.html | 2 +- .../stackdriver/query_aggregation_ctrl.ts | 11 ++++++++ .../datasource/stackdriver/query_ctrl.ts | 1 + 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/pkg/tsdb/stackdriver/stackdriver.go b/pkg/tsdb/stackdriver/stackdriver.go index 5e65e0ae527..e6f9ddf6c56 100644 --- a/pkg/tsdb/stackdriver/stackdriver.go +++ b/pkg/tsdb/stackdriver/stackdriver.go @@ -116,6 +116,8 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd return nil, err } + durationSeconds := int(endTime.Sub(startTime).Seconds()) + for _, query := range tsdbQuery.Queries { var target string @@ -145,7 +147,7 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd params.Add("interval.endTime", endTime.UTC().Format(time.RFC3339)) params.Add("filter", strings.Trim(fmt.Sprintf(`metric.type="%s" %s`, metricType, filterString), " ")) params.Add("view", query.Model.Get("view").MustString()) - setAggParams(¶ms, query) + setAggParams(¶ms, query, durationSeconds) if setting.Env == setting.DEV { slog.Debug("Stackdriver request", "params", params) @@ -171,7 +173,7 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd return stackdriverQueries, nil } -func setAggParams(params *url.Values, query *tsdb.Query) { +func setAggParams(params *url.Values, query *tsdb.Query, durationSeconds int) { primaryAggregation := query.Model.Get("primaryAggregation").MustString() perSeriesAligner := query.Model.Get("perSeriesAligner").MustString() alignmentPeriod := query.Model.Get("alignmentPeriod").MustString() @@ -185,10 +187,21 @@ func setAggParams(params *url.Values, query *tsdb.Query) { } if alignmentPeriod == "grafana-auto" || alignmentPeriod == "" { - alignmentPeriodValue := int(math.Max(float64(query.IntervalMs), 60.0)) + alignmentPeriodValue := int(math.Max(float64(query.IntervalMs)/1000, 60.0)) alignmentPeriod = "+" + strconv.Itoa(alignmentPeriodValue) + "s" } + if alignmentPeriod == "stackdriver-auto" { + alignmentPeriodValue := int(math.Max(float64(durationSeconds), 60.0)) + if alignmentPeriodValue <= 60*60*5 { + alignmentPeriod = "+60s" + } else if alignmentPeriodValue <= 60*60*23 { + alignmentPeriod = "+300s" + } else { + alignmentPeriod = "+3600s" + } + } + re := regexp.MustCompile("[0-9]+") seconds, err := strconv.ParseInt(re.FindString(alignmentPeriod), 10, 64) if err != nil || seconds > 3600 { @@ -218,6 +231,15 @@ func (e *StackdriverExecutor) executeQuery(ctx context.Context, query *Stackdriv req.URL.RawQuery = query.Params.Encode() queryResult.Meta.Set("rawQuery", req.URL.RawQuery) + alignmentPeriod, ok := req.URL.Query()["aggregation.alignmentPeriod"] + + if ok { + re := regexp.MustCompile("[0-9]+") + seconds, err := strconv.ParseInt(re.FindString(alignmentPeriod[0]), 10, 64) + if err == nil { + queryResult.Meta.Set("alignmentPeriod", seconds) + } + } span, ctx := opentracing.StartSpanFromContext(ctx, "stackdriver query") span.SetTag("target", query.Target) diff --git a/public/app/plugins/datasource/stackdriver/partials/query.aggregation.html b/public/app/plugins/datasource/stackdriver/partials/query.aggregation.html index d0eb33f649c..1f1386741e3 100755 --- a/public/app/plugins/datasource/stackdriver/partials/query.aggregation.html +++ b/public/app/plugins/datasource/stackdriver/partials/query.aggregation.html @@ -28,15 +28,19 @@
-
- +
+
+
+
-
+
-
-
-
+
+
\ No newline at end of file diff --git a/public/app/plugins/datasource/stackdriver/partials/query.editor.html b/public/app/plugins/datasource/stackdriver/partials/query.editor.html index 6f3fb216d24..48ef31c2b43 100755 --- a/public/app/plugins/datasource/stackdriver/partials/query.editor.html +++ b/public/app/plugins/datasource/stackdriver/partials/query.editor.html @@ -41,7 +41,7 @@
- +
Alias By diff --git a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts index f2e6fef79e6..30dd4fa5c54 100644 --- a/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_aggregation_ctrl.ts @@ -1,6 +1,8 @@ import angular from 'angular'; import _ from 'lodash'; import * as options from './constants'; +import * as options from './constants'; +import kbn from 'app/core/utils/kbn'; export class StackdriverAggregation { constructor() { @@ -10,6 +12,7 @@ export class StackdriverAggregation { restrict: 'E', scope: { target: '=', + alignmentPeriod: '<', refresh: '&', }, }; @@ -24,6 +27,7 @@ export class StackdriverAggregationCtrl { $scope.alignmentPeriods = options.alignmentPeriods; $scope.onAlignmentChange = this.onAlignmentChange.bind(this); $scope.onAggregationChange = this.onAggregationChange.bind(this); + $scope.formatAlignmentText = this.formatAlignmentText.bind(this); $scope.$on('metricTypeChanged', this.setAlignOptions.bind(this)); } @@ -77,6 +81,13 @@ export class StackdriverAggregationCtrl { this.$scope.target.aggregation.crossSeriesReducer = newValue ? newValue.value : ''; } } + + formatAlignmentText() { + const selectedAlignment = this.$scope.alignOptions.find( + ap => ap.value === this.$scope.target.aggregation.perSeriesAligner + ); + return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`; + } } angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation); diff --git a/public/app/plugins/datasource/stackdriver/query_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_ctrl.ts index b9eefcf3789..6ef8bb8ea0e 100644 --- a/public/app/plugins/datasource/stackdriver/query_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_ctrl.ts @@ -5,6 +5,7 @@ import { FilterSegments, DefaultRemoveFilterValue } from './filter_segments'; import './query_aggregation_ctrl'; export interface QueryMeta { + alignmentPeriod: string; rawQuery: string; rawQueryString: string; metricLabels: { [key: string]: string[] };