Commit b124ba9a by Marcus Efraimsson Committed by GitHub

Merge pull request #13726 from mtanda/cw_unit_override

Allow unit overwrite if cloudwatch/stackdriver datasource response doesn't include unit
parents 5bd11744 b2932058
......@@ -137,7 +137,11 @@ export default class CloudWatchDatasource {
if (res.results) {
_.forEach(res.results, queryRes => {
_.forEach(queryRes.series, series => {
data.push({ target: series.name, datapoints: series.points, unit: queryRes.meta.unit || 'none' });
const s = { target: series.name, datapoints: series.points } as any;
if (queryRes.meta.unit) {
s.unit = queryRes.meta.unit;
}
data.push(s);
});
});
}
......
......@@ -89,7 +89,7 @@ export default class StackdriverDatasource {
}
resolvePanelUnitFromTargets(targets: any[]) {
let unit = 'none';
let unit;
if (targets.length > 0 && targets.every(t => t.unit === targets[0].unit)) {
if (stackdriverUnitMappings.hasOwnProperty(targets[0].unit)) {
unit = stackdriverUnitMappings[targets[0].unit];
......@@ -109,13 +109,16 @@ export default class StackdriverDatasource {
const unit = this.resolvePanelUnitFromTargets(options.targets);
queryRes.series.forEach(series => {
result.push({
let timeSerie: any = {
target: series.name,
datapoints: series.points,
refId: queryRes.refId,
meta: queryRes.meta,
unit,
});
};
if (unit) {
timeSerie = { ...timeSerie, unit };
}
result.push(timeSerie);
});
});
}
......
......@@ -235,8 +235,8 @@ describe('StackdriverDataSource', () => {
beforeEach(() => {
res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }]);
});
it('should return none', () => {
expect(res).toEqual('none');
it('should return undefined', () => {
expect(res).toBeUndefined();
});
});
describe('and the stackdriver unit has a corresponding grafana unit', () => {
......@@ -262,16 +262,16 @@ describe('StackdriverDataSource', () => {
beforeEach(() => {
res = ds.resolvePanelUnitFromTargets([{ unit: 'megaseconds' }, { unit: 'megaseconds' }]);
});
it('should return the default value - none', () => {
expect(res).toEqual('none');
it('should return the default value of undefined', () => {
expect(res).toBeUndefined();
});
});
describe('and all target units are not the same', () => {
beforeEach(() => {
res = ds.resolvePanelUnitFromTargets([{ unit: 'bit' }, { unit: 'min' }]);
});
it('should return the default value - none', () => {
expect(res).toEqual('none');
it('should return the default value of undefined', () => {
expect(res).toBeUndefined();
});
});
});
......
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