From cf64ac2acceed0f75f5ec2ba87063e3993d2d00b Mon Sep 17 00:00:00 2001 From: vargburz Date: Fri, 7 Apr 2023 17:22:47 +0300 Subject: [PATCH 1/2] proper way to delete csv and json --- src/routes/tasks.ts | 9 +++++---- src/services/exporter.ts | 20 ++++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/routes/tasks.ts b/src/routes/tasks.ts index 6a7303f..1d417ef 100644 --- a/src/routes/tasks.ts +++ b/src/routes/tasks.ts @@ -92,10 +92,11 @@ async function addTask(req: TRequest, res) { } async function deleteTask(req, res) { - let taskId = req.body.taskId; - let csvFilePath = path.join(CSV_PATH, `${taskId}.csv`); - let jsonFilePath = path.join(CSV_PATH, `${taskId}.json`); - + const taskId = req.body.taskId; + const jsonFilePath = path.join(CSV_PATH, `${taskId}.json`); + const data = fs.readFileSync(jsonFilePath, 'utf8'); + const csvName = JSON.parse(data)['filename']; + const csvFilePath = path.join(CSV_PATH, `${csvName}.csv`); if(fs.existsSync(csvFilePath)) { fs.unlink(csvFilePath, err => console.error(err)); } diff --git a/src/services/exporter.ts b/src/services/exporter.ts index 85605f6..0b18413 100644 --- a/src/services/exporter.ts +++ b/src/services/exporter.ts @@ -117,15 +117,15 @@ export class Exporter { const csvStream = csv.createWriteStream({ headers: true, delimiter: this._task.csvDelimiter }) .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); writableStream.on('finish', async () => { 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 }); } else { - console.log(`${this._getFilename('csv')} export is finished with error`); + console.log(`${this._getCsvFilename()} export is finished with error`); } }); @@ -141,7 +141,7 @@ export class Exporter { 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) { console.error(err); throw new Error('Can`t write file'); @@ -174,12 +174,16 @@ export class Exporter { } } - private _getFilename(extension: string): string { - return `${this._task.filename}.${extension}`; + private _getCsvFilename(): string { + return `${this._task.filename}.csv`; } - private _getFilePath(extension: string): string { - let filename = this._getFilename(extension); + private _getCsvFilePath(): string { + let filename = this._getCsvFilename(); return path.join(CSV_PATH, filename); } + + private _getJsonFilePath(): string { + return path.join(CSV_PATH, `${this._task.id}.json`); + } } -- 2.34.1 From a9aae8d1357f3e9701bc0f57984719f56eabecc3 Mon Sep 17 00:00:00 2001 From: vargburz Date: Mon, 10 Apr 2023 17:08:49 +0300 Subject: [PATCH 2/2] use apyKey as param on addTask --- src/routes/tasks.ts | 8 +++++++- src/services/exporter.ts | 5 +---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/routes/tasks.ts b/src/routes/tasks.ts index 1d417ef..de068b7 100644 --- a/src/routes/tasks.ts +++ b/src/routes/tasks.ts @@ -17,6 +17,7 @@ type TRequest = { task: ExportTask, url: string, timeZoneName: string, + apiKey: string, }, }; @@ -66,6 +67,11 @@ async function addTask(req: TRequest, res) { res.status(400).send('"task" field is required'); return; } + const apiKey = body.apiKey; + if (_.isEmpty(apiKey)) { + res.status(400).send('"apiKey" field is required'); + return; + } try { validateGrafanaUrl(clientUrl); @@ -87,7 +93,7 @@ async function addTask(req: TRequest, res) { } const exporter = exporterFactory.getExporter(); - exporter.export(task, datasourceUrl, body.timeZoneName); + exporter.export(task, datasourceUrl, body.timeZoneName, apiKey); res.status(200).send(`Export process started`); } diff --git a/src/services/exporter.ts b/src/services/exporter.ts index 0b18413..7c4c2e3 100644 --- a/src/services/exporter.ts +++ b/src/services/exporter.ts @@ -29,7 +29,7 @@ const DEFAULT_PROGRESS = { export class Exporter { private _task: ExportTask; - public async export(task: ExportTask, datasourceUrl: string, timeZoneName: string) { + public async export(task: ExportTask, datasourceUrl: string, timeZoneName: string, apiKey: string) { try { this._task = _.cloneDeep(task); this._task.id = uuidv4(); @@ -72,9 +72,6 @@ export class Exporter { let values = []; for(const queryConfig of queryConfigs) { - const host = new URL(datasourceUrl).origin; - const apiKey = getApiKey(host); - const datasourceMetrics = await queryByConfig(queryConfig, datasourceUrl, from, to, apiKey); if(_.isEmpty(columns)) { -- 2.34.1