From e8028a7ac8eb8ded9fbbf83c7a9e720f7768a4af Mon Sep 17 00:00:00 2001 From: rozetko Date: Thu, 12 Jan 2023 16:30:21 +0300 Subject: [PATCH] wip: error handling --- src/services/exporter.ts | 24 ++++++++++++++++++------ src/types/export-status.ts | 1 + 2 files changed, 19 insertions(+), 6 deletions(-) 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', }