Fix merge issue on multi-query table transforms

* after a match has been found the merger should keep looking for more
This commit is contained in:
David Kaltschmidt 2017-12-06 13:12:16 +01:00
parent eb31833d52
commit 011b2cd1b2
2 changed files with 39 additions and 18 deletions

View File

@ -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', () => {

View File

@ -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);
}