Commit ed6d3bf6 by Erik Sundell

stackdriver: WIP - implement stackdriver style auto alignment period. also…

stackdriver: WIP - implement stackdriver style auto alignment period. also return the used alignment period and display it in the query editor
parent af9033f3
......@@ -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(&params, query)
setAggParams(&params, 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)
......
......@@ -28,15 +28,19 @@
<div class="gf-form-label gf-form-label--grow"></div>
</div>
</div>
<div class="gf-form offset-width-9">
<label class="gf-form-label query-keyword width-12">Alignment Period</label>
</div>
<div class="gf-form-inline">
<div class="gf-form">
<label class="gf-form-label query-keyword width-9">Alignment Period</label>
<div class="gf-form-select-wrapper gf-form-select-wrapper--caret-indent">
<select class="gf-form-input width-14" ng-model="target.aggregation.alignmentPeriod" ng-options="f.value as f.text for f in alignmentPeriods"
<select class="gf-form-input width-12" ng-model="target.aggregation.alignmentPeriod" ng-options="f.value as f.text for f in alignmentPeriods"
ng-change="refresh()"></select>
</div>
</div>
<div class="gf-form gf-form--grow">
<div class="gf-form-label gf-form-label--grow"></div>
</div>
<div class="gf-form gf-form--grow">
<label ng-if="alignmentPeriod" class="gf-form-label gf-form-label--grow">
{{formatAlignmentText()}}
</label>
</div>
</div>
\ No newline at end of file
......@@ -41,7 +41,7 @@
<div class="gf-form-label gf-form-label--grow"></div>
</div>
</div>
<stackdriver-aggregation target="ctrl.target" refresh="ctrl.refresh()"></stackdriver-aggregation>
<stackdriver-aggregation target="ctrl.target" alignment-period="ctrl.lastQueryMeta.alignmentPeriod" refresh="ctrl.refresh()"></stackdriver-aggregation>
<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label query-keyword width-9">Alias By</span>
......
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);
......
......@@ -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[] };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment