From 322620952d1e1ca77817c99b72d246fba2103bf6 Mon Sep 17 00:00:00 2001 From: vargburz Date: Wed, 3 May 2023 12:28:49 +0300 Subject: [PATCH] fix missing column --- src/services/exporter.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/services/exporter.ts b/src/services/exporter.ts index 1908852..3333280 100644 --- a/src/services/exporter.ts +++ b/src/services/exporter.ts @@ -63,25 +63,29 @@ export class Exporter { console.log(`Total days: ${days}`); const stream = this._initCsvStream(); - console.log("queryConfigs", queryConfigs); + for(let day = 0; day < days; day++) { to = from + MS_IN_DAY; console.log(`${day} day: ${from}ms -> ${to}ms`); - let columns = []; + let metricColumns = []; + let keyColumn; let rowsDict = {}; - for(const queryConfig of queryConfigs) { const datasourceMetrics: DataTable = await queryByConfig(queryConfig, datasourceUrl, from, to, apiKey); - columns = this._updateColumns(columns, datasourceMetrics.columns); + if(keyColumn == undefined) { + keyColumn = datasourceMetrics.columns[0]; + } + metricColumns = this._updateColumns(metricColumns, datasourceMetrics.columns); rowsDict = this._updateRows(rowsDict, datasourceMetrics.values, datasourceMetrics.columns); } - const rows = this._getRowsFromDict(rowsDict, columns); - // const formattedRows = rows.map((row: number[]) => [toIsoString(row[0], timeZoneName), ...row.slice(1)]); + const rows = this._getRowsFromDict(rowsDict, metricColumns); + const columns = _.concat(keyColumn, metricColumns); + const formattedRows = rows.map((row: number[]) => [toIsoString(row[0], timeZoneName), ...row.slice(1)]); - if(columns.length > 0) { - this._writeCsv(stream, { columns, rows }); + if(metricColumns.length > 0) { + this._writeCsv(stream, { columns, rows: formattedRows }); } await this._updateProgress({ status: ExportStatus.EXPORTING, progress: (day + 1) / days }); @@ -98,7 +102,7 @@ export class Exporter { return _.concat(columnsToUpdate, queriedColumns.slice(1)); } - private _updateRows(dict, queriedRows: (number | null)[][], queriedColumns: string[]): any { + private _updateRows(dict, queriedRows: (number | null)[][], queriedColumns: string[]): { [key: number]: { [column: string]: number} } { const columns = queriedColumns.slice(1); for(const rowIdx in queriedRows) { const key = queriedRows[rowIdx][0]; @@ -115,7 +119,7 @@ export class Exporter { let keyList = _.orderBy(_.keys(dict)); let rows = []; for(const keyIdx in keyList) { - const key = keyList[keyIdx]; + const key = _.toNumber(keyList[keyIdx]); rows[keyIdx] = [key]; for(const column of columns) { const value = dict[key][column] || null;