Commit 140205a7 by Daniel Lee Committed by GitHub

Merge pull request #14381 from grafana/davkal/fix-prometheus-transformation-error

Prometheus: Make result transformer more robust for empty responses
parents dbb34020 c0c06e4d
......@@ -16,7 +16,7 @@ export class ResultTransformer {
options.valueWithRefId
),
];
} else if (options.format === 'heatmap') {
} else if (prometheusResult && options.format === 'heatmap') {
let seriesList = [];
prometheusResult.sort(sortSeriesByLabel);
for (const metricData of prometheusResult) {
......@@ -24,7 +24,7 @@ export class ResultTransformer {
}
seriesList = this.transformToHistogramOverTime(seriesList);
return seriesList;
} else {
} else if (prometheusResult) {
const seriesList = [];
for (const metricData of prometheusResult) {
if (response.data.data.resultType === 'matrix') {
......@@ -82,7 +82,7 @@ export class ResultTransformer {
let i, j;
const metricLabels = {};
if (md.length === 0) {
if (!md || md.length === 0) {
return table;
}
......
......@@ -10,6 +10,31 @@ describe('Prometheus Result Transformer', () => {
ctx.resultTransformer = new ResultTransformer(ctx.templateSrv);
});
describe('When nothing is returned', () => {
test('should return empty series', () => {
const response = {
status: 'success',
data: {
resultType: '',
result: null,
},
};
const series = ctx.resultTransformer.transform({ data: response }, {});
expect(series).toEqual([]);
});
test('should return empty table', () => {
const response = {
status: 'success',
data: {
resultType: '',
result: null,
},
};
const table = ctx.resultTransformer.transform({ data: response }, { format: 'table' });
expect(table).toMatchObject([{ type: 'table', rows: [] }]);
});
});
describe('When resultFormat is table', () => {
const response = {
status: 'success',
......
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