Commit add23d97 by Erik Sundell Committed by Daniel Lee

stackdriver: load time series meta data for group by dropdown

parent 5763d3ca
......@@ -103,6 +103,7 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
params.Add("interval.startTime", startTime.UTC().Format(time.RFC3339))
params.Add("interval.endTime", endTime.UTC().Format(time.RFC3339))
params.Add("filter", "metric.type=\""+metricType+"\"")
params.Add("view", query.Model.Get("view").MustString())
setAggParams(&params, query)
if setting.Env == setting.DEV {
......
......@@ -11,17 +11,16 @@ export default class StackdriverDatasource {
this.id = instanceSettings.id;
}
async query(options) {
async getTimeSeries(options) {
const queries = options.targets.filter(target => !target.hide).map(t => ({
refId: t.refId,
datasourceId: this.id,
metricType: t.metricType,
primaryAggregation: t.aggregation.crossSeriesReducer,
groupBys: t.aggregation.groupBys,
primaryAggregation: 'REDUCE_MEAN', //t.aggregation.crossSeriesReducer,
// groupBys: t.aggregation.groupBys,
view: t.view || 'FULL',
}));
const result = [];
const { data } = await this.backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
......@@ -31,7 +30,12 @@ export default class StackdriverDatasource {
queries,
},
});
return data;
}
async query(options) {
const result = [];
const data = await this.getTimeSeries(options);
if (data.results) {
Object['values'](data.results).forEach(queryRes => {
if (!queryRes.series) {
......
......@@ -2,8 +2,8 @@
<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label width-9">Metric Type</span>
<gf-form-dropdown model="ctrl.target.metricType" get-options="ctrl.getMetricTypes($query)" class="min-width-20"
disabled type="text" allow-custom="true" lookup-text="true" css-class="min-width-12" on-change="ctrl.refresh()"></gf-form-dropdown>
<gf-form-dropdown model="ctrl.target.metricType" get-options="ctrl.getMetricTypes($query)" class="min-width-20" disabled
type="text" allow-custom="true" lookup-text="true" css-class="min-width-12" on-change="ctrl.onMetricTypeChange()"></gf-form-dropdown>
</div>
<div class="gf-form gf-form--grow">
<div class="gf-form-label gf-form-label--grow"></div>
......@@ -30,8 +30,8 @@
<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label width-9">Project</span>
<input class="gf-form-input" disabled type="text" ng-model='ctrl.target.project.name' get-options="ctrl.getProjects()"
css-class="min-width-12" />
<input class="gf-form-input" disabled type="text" ng-model='ctrl.target.project.name' get-options="ctrl.getProjects()" css-class="min-width-12"
/>
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword" ng-click="ctrl.showHelp = !ctrl.showHelp">
......
......@@ -2,10 +2,16 @@ import _ from 'lodash';
import { QueryCtrl } from 'app/plugins/sdk';
import appEvents from 'app/core/app_events';
export interface LabelType {
key: string;
value: string;
}
export interface QueryMeta {
rawQuery: string;
rawQueryString: string;
metricLabels: any;
metricLabels: LabelType[];
resourceLabels: LabelType[];
}
export class StackdriverQueryCtrl extends QueryCtrl {
static templateUrl = 'partials/query.editor.html';
......@@ -59,18 +65,22 @@ export class StackdriverQueryCtrl extends QueryCtrl {
showLastQuery: boolean;
lastQueryMeta: QueryMeta;
lastQueryError?: string;
metricLabels: LabelType[];
resourceLabels: LabelType[];
/** @ngInject */
constructor($scope, $injector, private uiSegmentSrv) {
constructor($scope, $injector, private uiSegmentSrv, private timeSrv) {
super($scope, $injector);
_.defaultsDeep(this.target, this.defaults);
this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
this.getCurrentProject().then(this.getMetricTypes.bind(this));
this.getCurrentProject()
.then(this.getMetricTypes.bind(this))
.then(this.getLabels.bind(this));
this.groupBySegments = _.map(this.target.aggregation.groupBys, groupBy => {
this.groupBySegments = this.target.aggregation.groupBys.map(groupBy => {
return uiSegmentSrv.getSegmentForValue(groupBy);
});
this.ensurePlusButton(this.groupBySegments);
......@@ -116,12 +126,47 @@ export class StackdriverQueryCtrl extends QueryCtrl {
}
}
async getLabels() {
const data = await this.datasource.getTimeSeries({
targets: [
{
refId: this.target.refId,
datasourceId: this.datasource.id,
metricType: this.target.metricType,
aggregation: {
crossSeriesReducer: 'REDUCE_NONE',
},
view: 'HEADERS',
},
],
range: this.timeSrv.timeRange(),
});
this.metricLabels = data.results[this.target.refId].meta.metricLabels;
this.resourceLabels = data.results[this.target.refId].meta.resourceLabels;
}
async onMetricTypeChange() {
this.refresh();
this.getLabels();
}
getGroupBys() {
const segments = _.map(Object.keys(this.lastQueryMeta.metricLabels), (label: string) => {
return this.uiSegmentSrv.newSegment({ value: label, expandable: false });
const metricLabels = Object.keys(this.metricLabels).map(l => {
return this.uiSegmentSrv.newSegment({
value: `metric.label.${l}`,
expandable: false,
});
});
const resourceLabels = Object.keys(this.resourceLabels).map(l => {
return this.uiSegmentSrv.newSegment({
value: `resource.label.${l}`,
expandable: false,
});
});
return Promise.resolve(segments);
return Promise.resolve([...metricLabels, ...resourceLabels]);
}
groupByChanged(segment, index) {
......@@ -136,6 +181,7 @@ export class StackdriverQueryCtrl extends QueryCtrl {
[]
);
this.ensurePlusButton(this.groupBySegments);
this.refresh();
}
ensurePlusButton(segments) {
......
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