From d7ccf98b1b3e0d80a1154ebf5cc61187f838a3be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Mon, 19 Aug 2019 14:04:16 +0200 Subject: [PATCH] Prometheus: Prevents panel editor crash when switching to Prometheus datasource (#18616) * Fix: Fixes panel editor crash when switching to Promehteus Fixes: #18600 * Refactor: Adds tests --- .../datasource/prometheus/query_hints.test.ts | 44 +++++++++++++++++++ .../datasource/prometheus/query_hints.ts | 2 +- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 public/app/plugins/datasource/prometheus/query_hints.test.ts diff --git a/public/app/plugins/datasource/prometheus/query_hints.test.ts b/public/app/plugins/datasource/prometheus/query_hints.test.ts new file mode 100644 index 00000000000..2c1a22914a4 --- /dev/null +++ b/public/app/plugins/datasource/prometheus/query_hints.test.ts @@ -0,0 +1,44 @@ +import { getQueryHints } from './query_hints'; + +describe('getQueryHints', () => { + describe('when called without datapoints in series', () => { + it('then it should use rows instead and return correct hint', () => { + const series = [ + { + fields: [ + { + name: 'Some Name', + }, + ], + rows: [[1], [2]], + }, + ]; + + const result = getQueryHints('up', series); + expect(result).toEqual([ + { + fix: { action: { query: 'up', type: 'ADD_RATE' }, label: 'Fix by adding rate().' }, + label: 'Time series is monotonically increasing.', + type: 'APPLY_RATE', + }, + ]); + }); + }); + + describe('when called without datapoints and rows in series', () => { + it('then it should use an empty array and return null', () => { + const series = [ + { + fields: [ + { + name: 'Some Name', + }, + ], + }, + ]; + + const result = getQueryHints('up', series); + expect(result).toEqual(null); + }); + }); +}); diff --git a/public/app/plugins/datasource/prometheus/query_hints.ts b/public/app/plugins/datasource/prometheus/query_hints.ts index cc0f13ce166..8b47ef70f82 100644 --- a/public/app/plugins/datasource/prometheus/query_hints.ts +++ b/public/app/plugins/datasource/prometheus/query_hints.ts @@ -29,7 +29,7 @@ export function getQueryHints(query: string, series?: any[], datasource?: any): // Check for monotonicity on series (table results are being ignored here) if (series && series.length > 0) { series.forEach(s => { - const datapoints: number[][] = s.datapoints; + const datapoints: number[][] = s.datapoints || s.rows || []; if (query.indexOf('rate(') === -1 && datapoints.length > 1) { let increasing = false; const nonNullData = datapoints.filter(dp => dp[0] !== null);