From e3dbef755ae418d2f8ff1356a2bd85eb137dae45 Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Thu, 5 Apr 2018 20:53:22 +0200 Subject: [PATCH 1/2] influxdb: Check before assuming first column to be 'time' When running queries like 'SHOW [...] CARDINALITY' introduced with InfluxDB 1.4 there is only a single column 'count'. However, the data source always assumed 'time' which threw an error because the code couldn't find a non-time column to display. This change actually checks this assumption and thereby fixes displaying the result in a Singlestat in Table mode (closes #11476). --- .../datasource/influxdb/influx_series.ts | 10 ++++++-- .../influxdb/specs/influx_series.jest.ts | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/influx_series.ts b/public/app/plugins/datasource/influxdb/influx_series.ts index a177ef3bb73..89e2d01b85b 100644 --- a/public/app/plugins/datasource/influxdb/influx_series.ts +++ b/public/app/plugins/datasource/influxdb/influx_series.ts @@ -151,11 +151,17 @@ export default class InfluxSeries { _.each(this.series, (series, seriesIndex) => { if (seriesIndex === 0) { - table.columns.push({ text: 'Time', type: 'time' }); + j = 0; + // Check that the first column is indeed 'time' + if (series.columns[0] === 'time') { + // Push this now before the tags and with the right type + table.columns.push({ text: 'Time', type: 'time' }); + j++; + } _.each(_.keys(series.tags), function(key) { table.columns.push({ text: key }); }); - for (j = 1; j < series.columns.length; j++) { + for (; j < series.columns.length; j++) { table.columns.push({ text: series.columns[j] }); } } diff --git a/public/app/plugins/datasource/influxdb/specs/influx_series.jest.ts b/public/app/plugins/datasource/influxdb/specs/influx_series.jest.ts index 8ae4a335828..8c8fee9ab9f 100644 --- a/public/app/plugins/datasource/influxdb/specs/influx_series.jest.ts +++ b/public/app/plugins/datasource/influxdb/specs/influx_series.jest.ts @@ -195,10 +195,34 @@ describe('when generating timeseries from influxdb response', function() { expect(table.type).toBe('table'); expect(table.columns.length).toBe(5); + expect(table.columns[0].text).toEqual('Time'); expect(table.rows[0]).toEqual([1431946625000, 'Africa', 'server2', 23, 10]); }); }); + describe('given table response from SHOW CARDINALITY', function() { + var options = { + alias: '', + series: [ + { + name: 'cpu', + columns: ['count'], + values: [[37]], + }, + ], + }; + + it('should return table', function() { + var series = new InfluxSeries(options); + var table = series.getTable(); + + expect(table.type).toBe('table'); + expect(table.columns.length).toBe(1); + expect(table.columns[0].text).toEqual('count'); + expect(table.rows[0]).toEqual([37]); + }); + }); + describe('given annotation response', function() { describe('with empty tagsColumn', function() { var options = { From dfccabab75ae6d99ba5057f4dc2c8598b697760c Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Thu, 5 Apr 2018 20:59:11 +0200 Subject: [PATCH 2/2] singlestat: Fix optimization in setTableColumnToSensibleDefault tableColumnOptions is an associative array that has no property length. Use tableData.columns as in the default case which works just fine. --- public/app/plugins/panel/singlestat/module.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/panel/singlestat/module.ts b/public/app/plugins/panel/singlestat/module.ts index 776033536dd..c7f523a9591 100644 --- a/public/app/plugins/panel/singlestat/module.ts +++ b/public/app/plugins/panel/singlestat/module.ts @@ -159,8 +159,8 @@ class SingleStatCtrl extends MetricsPanelCtrl { } setTableColumnToSensibleDefault(tableData) { - if (this.tableColumnOptions.length === 1) { - this.panel.tableColumn = this.tableColumnOptions[0]; + if (tableData.columns.length === 1) { + this.panel.tableColumn = tableData.columns[0].text; } else { this.panel.tableColumn = _.find(tableData.columns, col => { return col.type !== 'time';