From c0957d1ee846d969cf4ad3f20542140f3fb5157a Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Tue, 16 Aug 2022 19:43:50 +0200 Subject: [PATCH] fix(ui): handle flux tables with no time column --- .../shared/parsing/flux/parseTablesByTime.ts | 98 ++++++++++--------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/ui/src/shared/parsing/flux/parseTablesByTime.ts b/ui/src/shared/parsing/flux/parseTablesByTime.ts index 7724b0b1a3..c25d154916 100644 --- a/ui/src/shared/parsing/flux/parseTablesByTime.ts +++ b/ui/src/shared/parsing/flux/parseTablesByTime.ts @@ -42,64 +42,68 @@ export const parseTablesByTime = ( const allColumnNames = [] const nonNumericColumns = [] - const tablesByTime = tables.map(table => { - const header = table.data[0] as string[] - const columnNames: {[k: number]: string} = {} + const tablesByTime = tables + .map(table => { + const header = table.data[0] as string[] + const columnNames: {[k: number]: string} = {} - for (let i = 0; i < header.length; i++) { - const columnName = header[i] - const dataType = table.dataTypes[columnName] + for (let i = 0; i < header.length; i++) { + const columnName = header[i] + const dataType = table.dataTypes[columnName] - if (COLUMN_BLACKLIST.has(columnName)) { - continue - } + if (COLUMN_BLACKLIST.has(columnName)) { + continue + } - if (table.groupKey[columnName]) { - continue - } + if (table.groupKey[columnName]) { + continue + } - if (!NUMERIC_DATATYPES.includes(dataType)) { - nonNumericColumns.push(columnName) - continue - } + if (!NUMERIC_DATATYPES.includes(dataType)) { + nonNumericColumns.push(columnName) + continue + } - const uniqueColumnName = fluxTableKey(table, columnName) + const uniqueColumnName = fluxTableKey(table, columnName) - columnNames[i] = uniqueColumnName - allColumnNames.push(uniqueColumnName) - } + columnNames[i] = uniqueColumnName + allColumnNames.push(uniqueColumnName) + } - let timeIndex = header.indexOf('_time') + let timeIndex = header.indexOf('_time') - let isTimeFound = true - if (timeIndex < 0) { - timeIndex = header.indexOf('_stop') - isTimeFound = false - } + let isTimeFound = true + if (timeIndex < 0) { + timeIndex = header.indexOf('_stop') + if (timeIndex < 0) { + return undefined + } + isTimeFound = false + } - const result = {} - for (let i = 1; i < table.data.length; i++) { - const row = table.data[i] - const timeValue = row[timeIndex] - let time = '' - - if (isTimeFound) { - time = timeValue.toString() - } else { - // _stop and _start have values in date string format instead of number - time = Date.parse(timeValue as string).toString() + const result = {} + for (let i = 1; i < table.data.length; i++) { + const row = table.data[i] + const timeValue = row[timeIndex] + let time: string + if (isTimeFound) { + time = timeValue.toString() + } else { + // _stop and _start have values in date string format instead of number + time = Date.parse(timeValue as string).toString() + } + result[time] = Object.entries(columnNames).reduce( + (acc, [valueIndex, columnName]) => ({ + ...acc, + [columnName]: row[valueIndex], + }), + {} + ) } - result[time] = Object.entries(columnNames).reduce( - (acc, [valueIndex, columnName]) => ({ - ...acc, - [columnName]: row[valueIndex], - }), - {} - ) - } - return result - }) + return result + }) + .filter(x => x !== undefined) return {nonNumericColumns, tablesByTime, allColumnNames} }