Commit 251bb09a by Erik Sundell

stackdriver: convert most common stackdriver units to grafana units if possible

parent e2bda4d3
......@@ -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',
};
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,
});
});
});
......
......@@ -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');
......
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