|
|
@ -29,7 +29,7 @@ const DEFAULT_PROGRESS = { |
|
|
|
export class Exporter { |
|
|
|
export class Exporter { |
|
|
|
private _task: ExportTask; |
|
|
|
private _task: ExportTask; |
|
|
|
|
|
|
|
|
|
|
|
public async export(task: ExportTask, datasourceUrl: string, timeZoneName: string) { |
|
|
|
public async export(task: ExportTask, datasourceUrl: string, timeZoneName: string, apiKey: string) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this._task = _.cloneDeep(task); |
|
|
|
this._task = _.cloneDeep(task); |
|
|
|
this._task.id = uuidv4(); |
|
|
|
this._task.id = uuidv4(); |
|
|
@ -72,9 +72,6 @@ export class Exporter { |
|
|
|
let values = []; |
|
|
|
let values = []; |
|
|
|
|
|
|
|
|
|
|
|
for(const queryConfig of queryConfigs) { |
|
|
|
for(const queryConfig of queryConfigs) { |
|
|
|
const host = new URL(datasourceUrl).origin; |
|
|
|
|
|
|
|
const apiKey = getApiKey(host); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const datasourceMetrics = await queryByConfig(queryConfig, datasourceUrl, from, to, apiKey); |
|
|
|
const datasourceMetrics = await queryByConfig(queryConfig, datasourceUrl, from, to, apiKey); |
|
|
|
|
|
|
|
|
|
|
|
if(_.isEmpty(columns)) { |
|
|
|
if(_.isEmpty(columns)) { |
|
|
@ -117,15 +114,15 @@ export class Exporter { |
|
|
|
const csvStream = csv.createWriteStream({ headers: true, delimiter: this._task.csvDelimiter }) |
|
|
|
const csvStream = csv.createWriteStream({ headers: true, delimiter: this._task.csvDelimiter }) |
|
|
|
.on('error', async e => await this._updateProgress({ status: ExportStatus.ERROR, errorMessage: e.message })); |
|
|
|
.on('error', async e => await this._updateProgress({ status: ExportStatus.ERROR, errorMessage: e.message })); |
|
|
|
|
|
|
|
|
|
|
|
const writableStream = fs.createWriteStream(this._getFilePath('csv')); |
|
|
|
const writableStream = fs.createWriteStream(this._getCsvFilePath()); |
|
|
|
|
|
|
|
|
|
|
|
csvStream.pipe(writableStream); |
|
|
|
csvStream.pipe(writableStream); |
|
|
|
writableStream.on('finish', async () => { |
|
|
|
writableStream.on('finish', async () => { |
|
|
|
if(this._task.progress.status !== ExportStatus.ERROR) { |
|
|
|
if(this._task.progress.status !== ExportStatus.ERROR) { |
|
|
|
console.log(`Everything is written to ${this._getFilename('csv')}`); |
|
|
|
console.log(`Everything is written to ${this._getCsvFilename()}`); |
|
|
|
await this._updateProgress({ status: ExportStatus.FINISHED, progress: 1 }); |
|
|
|
await this._updateProgress({ status: ExportStatus.FINISHED, progress: 1 }); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
console.log(`${this._getFilename('csv')} export is finished with error`); |
|
|
|
console.log(`${this._getCsvFilename()} export is finished with error`); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -141,7 +138,7 @@ export class Exporter { |
|
|
|
progress: _.assign(this._task.progress, progress, { time }), |
|
|
|
progress: _.assign(this._task.progress, progress, { time }), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
await promisify(fs.writeFile)(this._getFilePath('json'), JSON.stringify(data), 'utf8'); |
|
|
|
await promisify(fs.writeFile)(this._getJsonFilePath(), JSON.stringify(data), 'utf8'); |
|
|
|
} catch(err) { |
|
|
|
} catch(err) { |
|
|
|
console.error(err); |
|
|
|
console.error(err); |
|
|
|
throw new Error('Can`t write file'); |
|
|
|
throw new Error('Can`t write file'); |
|
|
@ -174,12 +171,16 @@ export class Exporter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private _getFilename(extension: string): string { |
|
|
|
private _getCsvFilename(): string { |
|
|
|
return `${this._task.filename}.${extension}`; |
|
|
|
return `${this._task.filename}.csv`; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private _getFilePath(extension: string): string { |
|
|
|
private _getCsvFilePath(): string { |
|
|
|
let filename = this._getFilename(extension); |
|
|
|
let filename = this._getCsvFilename(); |
|
|
|
return path.join(CSV_PATH, filename); |
|
|
|
return path.join(CSV_PATH, filename); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private _getJsonFilePath(): string { |
|
|
|
|
|
|
|
return path.join(CSV_PATH, `${this._task.id}.json`); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|