From d98ecc4d5f0d7324e3ad7adad61c038c87dc4914 Mon Sep 17 00:00:00 2001 From: rozetko Date: Wed, 11 Jan 2023 15:54:46 +0300 Subject: [PATCH] support multiple metrics in a query --- src/services/exporter.ts | 56 ++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/src/services/exporter.ts b/src/services/exporter.ts index 4f5ba90..7de9565 100644 --- a/src/services/exporter.ts +++ b/src/services/exporter.ts @@ -16,7 +16,6 @@ import * as path from 'path'; import * as _ from 'lodash'; const MS_IN_DAY = 24 * 60 * 60 * 1000; -const TIMESTAMP_COLUMN = 'timestamp'; export class Exporter { private exportedRows = 0; @@ -58,17 +57,17 @@ export class Exporter { } } + // TODO: rename `data` to `targets` or `queries` public async export(data: Target[], datasourceUrl: string, username: string, from: number, to: number) { this.username = username; this.validateTargets(datasourceUrl, data); - // console.log('ds', data[0].datasource) const targets = data.map(target => { - console.log({ + console.log('target', { ...target.datasource, url: datasourceUrl - }) + }); return { ...target, metric: new QueryConfig( @@ -83,7 +82,7 @@ export class Exporter { }); const datasource = data[0].datasource; - this.datasourceRef = data.length === 1 ? { uid: datasource.uid, type: datasource.type } : { uid: 'all', type: 'all' }; + this.datasourceRef = { uid: datasource.uid, type: datasource.type }; await this.updateStatus(ExportStatus.EXPORTING, 0); @@ -97,8 +96,8 @@ export class Exporter { console.log(`${day} day: ${from}ms -> ${to}ms`); - const columns = [TIMESTAMP_COLUMN]; - const values = {}; + let columns = []; + let values = []; for(const [index, target] of targets.entries()) { const host = new URL(datasourceUrl).origin; @@ -106,32 +105,15 @@ export class Exporter { const datasourceMetrics = await queryByConfig(target.metric, datasourceUrl, from, to, apiKey); - const column = `${target.panel.id}` + - `-${target.panel.title.replace(' ', '-')}-${datasourceMetrics.columns[1]}`; - - columns.push(column); - - for(const row of datasourceMetrics.values) { - const [timestamp, value] = row; - - if(values[timestamp] === undefined) { - values[timestamp] = new Array(targets.length); - } - values[timestamp][index] = value; - } + columns = datasourceMetrics.columns; + values = datasourceMetrics.values; } - const metricsValues = []; - - Object.keys(values).forEach(timestamp => { - metricsValues.push([timestamp, ...values[timestamp]]); - }); - - if(metricsValues.length > 0) { - console.log(metricsValues) + if(columns.length > 0) { + console.log('values', values); this.writeCsv(stream, { columns, - values: metricsValues, + values, }); } await this.updateStatus(ExportStatus.EXPORTING, (day + 1) / days); @@ -141,18 +123,20 @@ export class Exporter { stream.end(); } - private validateTargets(datasourceUrl, targets: Target[]) { + private validateTargets(datasourceUrl: string, targets: Target[]) { if(!targets || !Array.isArray(targets)) { throw new Error('Incorrect targets format'); } - for(const target of targets) { - const host = new URL(datasourceUrl).origin; - const apiKey = apiKeys[host]; + if(targets.length > 1) { + throw new Error(`Multiple queries are not supported yet`); + } - if(apiKey === undefined || apiKey === '') { - throw new Error(`Please configure API key for ${host}`); - } + const host = new URL(datasourceUrl).origin; + const apiKey = apiKeys[host]; + + if(apiKey === undefined || apiKey === '') { + throw new Error(`Please configure API key for ${host}`); } }