Browse Source

wip: error handling

pull/5/head
rozetko 1 year 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 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) {

1
src/types/export-status.ts

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

Loading…
Cancel
Save