From 251bb09aeddf3c191762651a945b784cf27ad305 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Thu, 27 Sep 2018 14:03:52 +0200 Subject: [PATCH] stackdriver: convert most common stackdriver units to grafana units if possible --- .../plugins/datasource/stackdriver/constants.ts | 16 ++++++++++++++++ .../plugins/datasource/stackdriver/datasource.ts | 14 ++++++++++++++ .../plugins/datasource/stackdriver/query_ctrl.ts | 5 ++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/stackdriver/constants.ts b/public/app/plugins/datasource/stackdriver/constants.ts index 64674a5eb3f..30fec1396f1 100644 --- a/public/app/plugins/datasource/stackdriver/constants.ts +++ b/public/app/plugins/datasource/stackdriver/constants.ts @@ -253,3 +253,19 @@ export const alignmentPeriods = [ { text: '1d', value: '+86400s' }, { text: '1w', value: '+604800s' }, ]; + +export const stackdriverUnitMappings = { + bit: 'bits', + By: 'bytes', + s: 's', + min: 'm', + h: 'h', + d: 'd', + us: 'µs', + ms: 'ms', + ns: 'ns', + percent: 'percent', + MiBy: 'mbytes', + 'By/s': 'Bps', + GBy: 'decgbytes', +}; diff --git a/public/app/plugins/datasource/stackdriver/datasource.ts b/public/app/plugins/datasource/stackdriver/datasource.ts index 15f003f4c74..82cca4fcfb0 100644 --- a/public/app/plugins/datasource/stackdriver/datasource.ts +++ b/public/app/plugins/datasource/stackdriver/datasource.ts @@ -1,3 +1,4 @@ +import { stackdriverUnitMappings } from './constants'; /** @ngInject */ export default class StackdriverDatasource { id: number; @@ -85,6 +86,16 @@ export default class StackdriverDatasource { return interpolatedGroupBys; } + resolveUnit(targets: any[]) { + let unit = 'none'; + if (targets.length > 0 && targets.every(t => t.unit === targets[0].unit)) { + if (stackdriverUnitMappings.hasOwnProperty(targets[0].unit)) { + unit = stackdriverUnitMappings[targets[0].unit]; + } + } + return unit; + } + async query(options) { const result = []; const data = await this.getTimeSeries(options); @@ -93,12 +104,15 @@ export default class StackdriverDatasource { if (!queryRes.series) { return; } + + const unit = this.resolveUnit(options.targets); queryRes.series.forEach(series => { result.push({ target: series.name, datapoints: series.points, refId: queryRes.refId, meta: queryRes.meta, + unit, }); }); }); diff --git a/public/app/plugins/datasource/stackdriver/query_ctrl.ts b/public/app/plugins/datasource/stackdriver/query_ctrl.ts index 6ef8bb8ea0e..3b97f074e3e 100644 --- a/public/app/plugins/datasource/stackdriver/query_ctrl.ts +++ b/public/app/plugins/datasource/stackdriver/query_ctrl.ts @@ -19,6 +19,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { id: string; name: string; }; + unit: string; metricType: string; service: string; refId: string; @@ -47,6 +48,7 @@ export class StackdriverQueryCtrl extends QueryCtrl { metricType: this.defaultDropdownValue, service: this.defaultServiceValue, metric: '', + unit: '', aggregation: { crossSeriesReducer: 'REDUCE_MEAN', alignmentPeriod: 'auto', @@ -221,7 +223,8 @@ export class StackdriverQueryCtrl extends QueryCtrl { setMetricType() { this.target.metricType = this.metricType; - const { valueType, metricKind } = this.metricDescriptors.find(m => m.type === this.target.metricType); + const { valueType, metricKind, unit } = this.metricDescriptors.find(m => m.type === this.target.metricType); + this.target.unit = unit; this.target.valueType = valueType; this.target.metricKind = metricKind; this.$scope.$broadcast('metricTypeChanged');