diff --git a/public/app/plugins/datasource/influxdb/influx_series.ts b/public/app/plugins/datasource/influxdb/influx_series.ts index 1c7fc7fcfb8..2d253af38c8 100644 --- a/public/app/plugins/datasource/influxdb/influx_series.ts +++ b/public/app/plugins/datasource/influxdb/influx_series.ts @@ -171,19 +171,24 @@ export default class InfluxSeries { return table; } + // the order is: + // - first the first item from the value-array (this is often (always?) the timestamp) + // - then all the tag-values + // - then the rest of the value-array + // + // we have to keep this order both in table.columns and table.rows + each(this.series, (series: any, seriesIndex: number) => { if (seriesIndex === 0) { - 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: FieldType.time }); - j++; - } + const firstCol = series.columns[0]; + // Check the first column's name, if it is `time`, we + // mark it as having the type time + const firstTableCol = firstCol === 'time' ? { text: 'Time', type: FieldType.time } : { text: firstCol }; + table.columns.push(firstTableCol); each(keys(series.tags), (key) => { table.columns.push({ text: key }); }); - for (; j < series.columns.length; j++) { + for (j = 1; j < series.columns.length; j++) { table.columns.push({ text: series.columns[j] }); } } diff --git a/public/app/plugins/datasource/influxdb/specs/influx_series.test.ts b/public/app/plugins/datasource/influxdb/specs/influx_series.test.ts index 7bc8db097ff..130385d7d31 100644 --- a/public/app/plugins/datasource/influxdb/specs/influx_series.test.ts +++ b/public/app/plugins/datasource/influxdb/specs/influx_series.test.ts @@ -1,3 +1,4 @@ +import produce from 'immer'; import InfluxSeries from '../influx_series'; describe('when generating timeseries from influxdb response', () => { @@ -300,5 +301,62 @@ describe('when generating timeseries from influxdb response', () => { expect(annotations[0].tags[1]).toBe('backend'); }); }); + + describe('given a time-column in the json-response', () => { + const options = { + alias: '', + series: [ + { + name: 'cpu', + tags: { cpu: 'cpu1' }, + columns: ['time', 'usage_idle'], + values: [[1481549440372, 42]], + }, + ], + }; + + it('the column-names should be correct if the time-column is not renamed', () => { + const series = new InfluxSeries(options); + const table = series.getTable(); + + expect(table.columns).toStrictEqual([ + { + text: 'Time', + type: 'time', + }, + { + text: 'cpu', + }, + { + text: 'usage_idle', + }, + ]); + + expect(table.rows).toStrictEqual([[1481549440372, 'cpu1', 42]]); + }); + + it('the column-names should be correct if the time-column is renamed', () => { + const renamedOptions = produce(options, (draft) => { + // we rename the time-column to `zeit` + draft.series[0].columns[0] = 'zeit'; + }); + const series = new InfluxSeries(renamedOptions); + const table = series.getTable(); + + expect(table.columns).toStrictEqual([ + { + text: 'zeit', + }, + { + text: 'cpu', + }, + { + text: 'usage_idle', + }, + ]); + + expect(table.rows).toStrictEqual([[1481549440372, 'cpu1', 42]]); + }); + }); }); });