Browse Source

wip: error handling

pull/5/head
rozetko 2 years ago
parent
commit
e8028a7ac8
  1. 24
      src/services/exporter.ts
  2. 1
      src/types/export-status.ts

24
src/services/exporter.ts

@ -22,6 +22,8 @@ export class Exporter {
private createdTimestamp: number; private createdTimestamp: number;
private username: string; private username: string;
private datasourceRef: DataSourceRef; private datasourceRef: DataSourceRef;
private from: number;
private to: number;
private initCsvStream() { private initCsvStream() {
const csvStream = csv.createWriteStream({ headers: true }) const csvStream = csv.createWriteStream({ headers: true })
@ -38,16 +40,19 @@ export class Exporter {
return csvStream; return csvStream;
} }
public async updateStatus(status: string, progress: number) { public async updateStatus(status: string, progress: number, error?: string) {
try { try {
let time = moment().valueOf(); let time = moment().valueOf();
let data = { let data = {
time, time,
progress,
status,
error,
username: this.username, username: this.username,
exportedRows: this.exportedRows, exportedRows: this.exportedRows,
progress: progress,
status,
datasourceRef: this.datasourceRef, datasourceRef: this.datasourceRef,
from: this.from,
to: this.to,
}; };
await promisify(fs.writeFile, this.getFilePath('json'), JSON.stringify(data), 'utf8') 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` // 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.from = from;
this.to = to;
this.validateTargets(datasourceUrl, data); this.validateTargets(datasourceUrl, data);
@ -103,10 +110,15 @@ export class Exporter {
const host = new URL(datasourceUrl).origin; const host = new URL(datasourceUrl).origin;
const apiKey = apiKeys[host]; 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; columns = datasourceMetrics.columns;
values = datasourceMetrics.values; values = datasourceMetrics.values;
} catch (e) {
await this.updateStatus(ExportStatus.ERROR, (day + 1) / days, e.message);
break;
}
} }
if(columns.length > 0) { if(columns.length > 0) {

1
src/types/export-status.ts

@ -1,4 +1,5 @@
export enum ExportStatus { export enum ExportStatus {
EXPORTING = 'exporting', EXPORTING = 'exporting',
FINISHED = 'finished', FINISHED = 'finished',
ERROR = 'error',
} }

Loading…
Cancel
Save