|
|
@ -16,7 +16,6 @@ import * as path from 'path'; |
|
|
|
import * as _ from 'lodash'; |
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
|
|
const MS_IN_DAY = 24 * 60 * 60 * 1000; |
|
|
|
const MS_IN_DAY = 24 * 60 * 60 * 1000; |
|
|
|
const TIMESTAMP_COLUMN = 'timestamp'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class Exporter { |
|
|
|
export class Exporter { |
|
|
|
private exportedRows = 0; |
|
|
|
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) { |
|
|
|
public async export(data: Target[], datasourceUrl: string, username: string, from: number, to: number) { |
|
|
|
this.username = username; |
|
|
|
this.username = username; |
|
|
|
|
|
|
|
|
|
|
|
this.validateTargets(datasourceUrl, data); |
|
|
|
this.validateTargets(datasourceUrl, data); |
|
|
|
|
|
|
|
|
|
|
|
// console.log('ds', data[0].datasource)
|
|
|
|
|
|
|
|
const targets = data.map(target => { |
|
|
|
const targets = data.map(target => { |
|
|
|
console.log({ |
|
|
|
console.log('target', { |
|
|
|
...target.datasource, |
|
|
|
...target.datasource, |
|
|
|
url: datasourceUrl |
|
|
|
url: datasourceUrl |
|
|
|
}) |
|
|
|
}); |
|
|
|
return { |
|
|
|
return { |
|
|
|
...target, |
|
|
|
...target, |
|
|
|
metric: new QueryConfig( |
|
|
|
metric: new QueryConfig( |
|
|
@ -83,7 +82,7 @@ export class Exporter { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const datasource = data[0].datasource; |
|
|
|
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); |
|
|
|
await this.updateStatus(ExportStatus.EXPORTING, 0); |
|
|
|
|
|
|
|
|
|
|
@ -97,8 +96,8 @@ export class Exporter { |
|
|
|
|
|
|
|
|
|
|
|
console.log(`${day} day: ${from}ms -> ${to}ms`); |
|
|
|
console.log(`${day} day: ${from}ms -> ${to}ms`); |
|
|
|
|
|
|
|
|
|
|
|
const columns = [TIMESTAMP_COLUMN]; |
|
|
|
let columns = []; |
|
|
|
const values = {}; |
|
|
|
let values = []; |
|
|
|
|
|
|
|
|
|
|
|
for(const [index, target] of targets.entries()) { |
|
|
|
for(const [index, target] of targets.entries()) { |
|
|
|
const host = new URL(datasourceUrl).origin; |
|
|
|
const host = new URL(datasourceUrl).origin; |
|
|
@ -106,32 +105,15 @@ export class Exporter { |
|
|
|
|
|
|
|
|
|
|
|
const datasourceMetrics = await queryByConfig(target.metric, datasourceUrl, from, to, apiKey); |
|
|
|
const datasourceMetrics = await queryByConfig(target.metric, datasourceUrl, from, to, apiKey); |
|
|
|
|
|
|
|
|
|
|
|
const column = `${target.panel.id}` + |
|
|
|
columns = datasourceMetrics.columns; |
|
|
|
`-${target.panel.title.replace(' ', '-')}-${datasourceMetrics.columns[1]}`; |
|
|
|
values = datasourceMetrics.values; |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const metricsValues = []; |
|
|
|
if(columns.length > 0) { |
|
|
|
|
|
|
|
console.log('values', values); |
|
|
|
Object.keys(values).forEach(timestamp => { |
|
|
|
|
|
|
|
metricsValues.push([timestamp, ...values[timestamp]]); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(metricsValues.length > 0) { |
|
|
|
|
|
|
|
console.log(metricsValues) |
|
|
|
|
|
|
|
this.writeCsv(stream, { |
|
|
|
this.writeCsv(stream, { |
|
|
|
columns, |
|
|
|
columns, |
|
|
|
values: metricsValues, |
|
|
|
values, |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
await this.updateStatus(ExportStatus.EXPORTING, (day + 1) / days); |
|
|
|
await this.updateStatus(ExportStatus.EXPORTING, (day + 1) / days); |
|
|
@ -141,12 +123,15 @@ export class Exporter { |
|
|
|
stream.end(); |
|
|
|
stream.end(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private validateTargets(datasourceUrl, targets: Target[]) { |
|
|
|
private validateTargets(datasourceUrl: string, targets: Target[]) { |
|
|
|
if(!targets || !Array.isArray(targets)) { |
|
|
|
if(!targets || !Array.isArray(targets)) { |
|
|
|
throw new Error('Incorrect targets format'); |
|
|
|
throw new Error('Incorrect targets format'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for(const target of targets) { |
|
|
|
if(targets.length > 1) { |
|
|
|
|
|
|
|
throw new Error(`Multiple queries are not supported yet`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const host = new URL(datasourceUrl).origin; |
|
|
|
const host = new URL(datasourceUrl).origin; |
|
|
|
const apiKey = apiKeys[host]; |
|
|
|
const apiKey = apiKeys[host]; |
|
|
|
|
|
|
|
|
|
|
@ -154,7 +139,6 @@ export class Exporter { |
|
|
|
throw new Error(`Please configure API key for ${host}`); |
|
|
|
throw new Error(`Please configure API key for ${host}`); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private writeCsv(stream, series) { |
|
|
|
private writeCsv(stream, series) { |
|
|
|
for(let row of series.values) { |
|
|
|
for(let row of series.values) { |
|
|
|