diff --git a/src/services/exporter.ts b/src/services/exporter.ts index 7de9565..8505ce2 100644 --- a/src/services/exporter.ts +++ b/src/services/exporter.ts @@ -22,6 +22,8 @@ export class Exporter { private createdTimestamp: number; private username: string; private datasourceRef: DataSourceRef; + private from: number; + private to: number; private initCsvStream() { const csvStream = csv.createWriteStream({ headers: true }) @@ -38,16 +40,19 @@ export class Exporter { return csvStream; } - public async updateStatus(status: string, progress: number) { + public async updateStatus(status: string, progress: number, error?: string) { try { let time = moment().valueOf(); let data = { time, + progress, + status, + error, username: this.username, exportedRows: this.exportedRows, - progress: progress, - status, datasourceRef: this.datasourceRef, + from: this.from, + to: this.to, }; await promisify(fs.writeFile, this.getFilePath('json'), JSON.stringify(data), 'utf8') @@ -60,6 +65,8 @@ 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.from = from; + this.to = to; this.validateTargets(datasourceUrl, data); @@ -103,10 +110,15 @@ export class Exporter { const host = new URL(datasourceUrl).origin; const apiKey = apiKeys[host]; - const datasourceMetrics = await queryByConfig(target.metric, datasourceUrl, from, to, apiKey); + try { + const datasourceMetrics = await queryByConfig(target.metric, datasourceUrl, from, to, apiKey); - columns = datasourceMetrics.columns; - values = datasourceMetrics.values; + columns = datasourceMetrics.columns; + values = datasourceMetrics.values; + } catch (e) { + await this.updateStatus(ExportStatus.ERROR, (day + 1) / days, e.message); + break; + } } if(columns.length > 0) { diff --git a/src/types/export-status.ts b/src/types/export-status.ts index c209d54..a12b8d7 100644 --- a/src/types/export-status.ts +++ b/src/types/export-status.ts @@ -1,4 +1,5 @@ export enum ExportStatus { EXPORTING = 'exporting', FINISHED = 'finished', + ERROR = 'error', }