diff --git a/public/app/plugins/panel/table/specs/transformers.jest.ts b/public/app/plugins/panel/table/specs/transformers.jest.ts index f3b58270bfc..1c7fc3e7852 100644 --- a/public/app/plugins/panel/table/specs/transformers.jest.ts +++ b/public/app/plugins/panel/table/specs/transformers.jest.ts @@ -156,6 +156,18 @@ describe('when transforming time series table', () => { rows: [ [time, 'Label Value 1', 'Label Value 2', 13], ], + }, + { + type: 'table', + columns: [ + { text: 'Time' }, + { text: 'Label Key 1' }, + { text: 'Label Key 2' }, + { text: 'Value' }, + ], + rows: [ + [time, 'Label Value 1', 'Label Value 2', 4], + ], } ]; @@ -226,12 +238,13 @@ describe('when transforming time series table', () => { it ('should return the union of columns for multiple queries', () => { table = transformDataToTable(multipleQueriesDataSameLabels, panel); - expect(table.columns.length).toBe(5); + expect(table.columns.length).toBe(6); expect(table.columns[0].text).toBe('Time'); expect(table.columns[1].text).toBe('Label Key 1'); expect(table.columns[2].text).toBe('Label Key 2'); expect(table.columns[3].text).toBe('Value #A'); expect(table.columns[4].text).toBe('Value #B'); + expect(table.columns[5].text).toBe('Value #C'); }); it ('should return 1 row for a single query', () => { @@ -250,6 +263,7 @@ describe('when transforming time series table', () => { expect(table.rows[0][2]).toBe('Label Value 2'); expect(table.rows[0][3]).toBe(42); expect(table.rows[0][4]).toBe(13); + expect(table.rows[0][5]).toBe(4); }); it ('should return 2 rows for a mulitple queries with different label values', () => { diff --git a/public/app/plugins/panel/table/transformers.ts b/public/app/plugins/panel/table/transformers.ts index 5caf8f09f8c..5081ab785b6 100644 --- a/public/app/plugins/panel/table/transformers.ts +++ b/public/app/plugins/panel/table/transformers.ts @@ -235,26 +235,33 @@ transformers['table'] = { const mergedRows = {}; rows = rows.reduce((acc, row, i) => { if (!mergedRows[i]) { - const match = _.findIndex(rows, (other, j) => { - let same = true; - for (let index = 0; index < nonValueColumnCount; index++) { - if (row[index] !== other[index]) { - same = false; - break; + let offset = i + 1; + while (offset < rows.length) { + const match = _.findIndex(rows, (other, j) => { + let same = true; + for (let index = 0; index < nonValueColumnCount; index++) { + if (row[index] !== other[index]) { + same = false; + break; + } } - } - return same; - }, i + 1); - if (match > -1) { - const matchedRow = rows[match]; - // Merge values into current row - for (let index = nonValueColumnCount; index < columns.length; index++) { - if (row[index] === undefined && matchedRow[index] !== undefined) { - row[index] = matchedRow[index]; - break; + return same; + }, offset); + if (match > -1) { + const matchedRow = rows[match]; + // Merge values into current row + for (let index = nonValueColumnCount; index < columns.length; index++) { + if (row[index] === undefined && matchedRow[index] !== undefined) { + row[index] = matchedRow[index]; + break; + } } + mergedRows[match] = matchedRow; + // Keep looking for more rows to merge + offset = match + 1; + } else { + break; } - mergedRows[match] = matchedRow; } acc.push(row); }