From 65813cd00689d14991da4abbc7cc8fb5db183bfe Mon Sep 17 00:00:00 2001 From: vargburz Date: Fri, 28 Apr 2023 20:11:35 +0300 Subject: [PATCH] concat rows in correct way --- src/services/exporter.ts | 43 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/services/exporter.ts b/src/services/exporter.ts index 3a80f15..1908852 100644 --- a/src/services/exporter.ts +++ b/src/services/exporter.ts @@ -70,17 +70,15 @@ export class Exporter { console.log(`${day} day: ${from}ms -> ${to}ms`); let columns = []; - let rows = []; + let rowsDict = {}; for(const queryConfig of queryConfigs) { const datasourceMetrics: DataTable = await queryByConfig(queryConfig, datasourceUrl, from, to, apiKey); columns = this._updateColumns(columns, datasourceMetrics.columns); - console.log("columns", columns) - rows = this._updateRows(rows, datasourceMetrics.values); - console.log("rows", rows.slice(0, 10)); + rowsDict = this._updateRows(rowsDict, datasourceMetrics.values, datasourceMetrics.columns); } - - rows = rows.map((row: number[]) => [toIsoString(row[0], timeZoneName), ...row.slice(1)]); + const rows = this._getRowsFromDict(rowsDict, columns); + // const formattedRows = rows.map((row: number[]) => [toIsoString(row[0], timeZoneName), ...row.slice(1)]); if(columns.length > 0) { this._writeCsv(stream, { columns, rows }); @@ -100,20 +98,31 @@ export class Exporter { return _.concat(columnsToUpdate, queriedColumns.slice(1)); } - private _updateRows(rowsToUpdate: (number | null)[][], queriedRows: (number | null)[][]): (number | null)[][] { - if(_.isEmpty(rowsToUpdate)) { - return queriedRows; - } - if(rowsToUpdate.length !== queriedRows.length) { - throw new Error(`All queries should return rows of the same lengths`); + private _updateRows(dict, queriedRows: (number | null)[][], queriedColumns: string[]): any { + const columns = queriedColumns.slice(1); + for(const rowIdx in queriedRows) { + const key = queriedRows[rowIdx][0]; + const values = queriedRows[rowIdx].slice(1); + dict[key] = dict[key] || {}; + for(const valueIdx in values) { + dict[key][columns[valueIdx]] = values[valueIdx]; + } } - for(const rowIdx in rowsToUpdate) { - if(queriedRows[rowIdx][0] !== rowsToUpdate[rowIdx][0]) { - throw new Error('Queries should return the same timestamps'); + return dict; + } + + private _getRowsFromDict(dict: any, columns: string[]): (number | null)[][] { + let keyList = _.orderBy(_.keys(dict)); + let rows = []; + for(const keyIdx in keyList) { + const key = keyList[keyIdx]; + rows[keyIdx] = [key]; + for(const column of columns) { + const value = dict[key][column] || null; + rows[keyIdx].push(value) } - rowsToUpdate[rowIdx] = _.concat(rowsToUpdate[rowIdx], queriedRows[rowIdx].slice(1)); } - return rowsToUpdate; + return rows; } private _initCsvStream() {