From d9f6adc6e4f0b20cdc03ecc87f6e954f66e2a2ec Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 21 Sep 2018 16:35:14 +0300 Subject: [PATCH 1/6] Change exported filenames #8 --- src/target.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/target.ts b/src/target.ts index 46d4c58..b8628c8 100644 --- a/src/target.ts +++ b/src/target.ts @@ -23,7 +23,8 @@ export class Target { datasource: Datasource, targets: Array, private from: number, - private to: number + private to: number, + private datasourceName: string, ) { this.metric = new Metric(datasource, targets); } @@ -35,7 +36,8 @@ export class Target { user: this.user, exportedRows: this.exportedRows, progress: (this.day / this.days).toLocaleString('en', { style: 'percent' }), - status + status, + datasourceName: this.datasourceName }; return new Promise((resolve, reject) => { fs.writeFile(this.getFilePath('json'), JSON.stringify(data), 'utf8', err => { @@ -105,9 +107,7 @@ export class Target { } private getFilename(extension) { - // TODO: use something unique instead of measurement in filename - // as measurement field exists only in influxDB metric - return `${this.metric.targets[0].measurement}.${this.from}-${this.to}.${extension}`; + return `${this.datasourceName}.${this.from}-${this.to}.${extension}`; } private getFilePath(extension) { From 2c782d245bfd54c63619fe1db7e7ba489dcff65e Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 21 Sep 2018 16:35:32 +0300 Subject: [PATCH 2/6] export-manager-backend -> grafana-data-exporter --- package.json | 2 +- src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 62ca9de..551b93e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "export-manager-server", + "name": "grafana-data-exporter", "version": "0.3.1", "description": "Server for fetching data from Grafana datasources", "scripts": { diff --git a/src/index.ts b/src/index.ts index c8f95e7..202ca0b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,7 @@ app.use('/datasource', datasourceRouter); app.use('/delete', deleteRouter); app.use('/static', express.static(EXPORTED_PATH)); -app.use('/', (req, res) => { res.send('Export-manager backend server works') }); +app.use('/', (req, res) => { res.send('Grafana-data-exporter server works') }); app.listen(PORT, () => { console.log(`Server is running on :${PORT}`); From 67b23f2855156fd650d3f2fe51b4bd06de151822 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 21 Sep 2018 16:36:11 +0300 Subject: [PATCH 3/6] Use datasource name instead of measurement --- src/routes/datasource.ts | 7 +------ src/routes/tasks.ts | 3 ++- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/routes/datasource.ts b/src/routes/datasource.ts index 7cb94f1..bf7d766 100644 --- a/src/routes/datasource.ts +++ b/src/routes/datasource.ts @@ -40,10 +40,6 @@ function query(req, res) { text: 'Datasource', type: 'string' }, - { - text: 'Measurement', - type: 'string' - }, { text: 'Exported Rows', type: 'number' @@ -93,8 +89,7 @@ function query(req, res) { resp.rows.push([ status.time, status.user, - status.datasource, - status.measurement, + status.datasourceName, status.exportedRows, status.progress, downloadLink, diff --git a/src/routes/tasks.ts b/src/routes/tasks.ts index c609b90..56ff35e 100644 --- a/src/routes/tasks.ts +++ b/src/routes/tasks.ts @@ -10,6 +10,7 @@ async function addTask(req, res) { let panelUrl = body.panelUrl; let targets = [body.target]; let datasource = body.datasourceRequest; + let datasourceName = body.datasourceName; let user = body.user; if(isNaN(from) || isNaN(to)) { @@ -18,7 +19,7 @@ async function addTask(req, res) { res.status(500).send('Range error: "from" should be less than "to"'); } else { res.status(200).send('Task added'); - let target = new Target(panelUrl, user, datasource, targets, from, to); + let target = new Target(panelUrl, user, datasource, targets, from, to, datasourceName); target.export(); } } From aa93809b12c46cdfc27231593d092e9bef1b5868 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 21 Sep 2018 16:45:14 +0300 Subject: [PATCH 4/6] Check for exported directory #6 --- src/config.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index 6d5bf3a..b847c17 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,7 +2,10 @@ import * as path from 'path'; import * as fs from 'fs'; export const EXPORTED_PATH = path.join(__dirname, '../exported'); - +if(!fs.existsSync(EXPORTED_PATH)) { + console.log(`${EXPORTED_PATH} don't exist, creating`); + fs.mkdirSync(EXPORTED_PATH); +} export function getApiKey(host) { let data = fs.readFileSync(path.join(__dirname, '../api-keys.json'), 'utf8'); From 0120271b2738b4faa5a88ae10e66465a25245693 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 21 Sep 2018 19:18:56 +0300 Subject: [PATCH 5/6] Minor fix --- src/routes/datasource.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/datasource.ts b/src/routes/datasource.ts index bf7d766..336d88b 100644 --- a/src/routes/datasource.ts +++ b/src/routes/datasource.ts @@ -10,7 +10,7 @@ function sendOk(req, res) { } function search(req, res) { - fs.readdir(EXPORTED_PATH, (err, items) => { + fs.readdir(EXPORTED_PATH, err => { if(err) { console.error(err); res.status(500).send('Something went wrong'); From 1302800fc07846c512299191a8e3d33b9dd2b3a2 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 21 Sep 2018 19:19:25 +0300 Subject: [PATCH 6/6] Add current timestamp to filename --- src/target.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/target.ts b/src/target.ts index b8628c8..e797c33 100644 --- a/src/target.ts +++ b/src/target.ts @@ -16,6 +16,7 @@ export class Target { private day: number; private csvStream: any; private metric: Metric; + private createdTimestamp: number; constructor( private panelUrl: string, @@ -81,7 +82,7 @@ export class Target { } this.csvStream.end(); } - + // TODO: move csv-related stuff to service private initCsvStream() { this.csvStream = csv.createWriteStream({ headers: true }); let writableStream = fs.createWriteStream(this.getFilePath('csv')); @@ -107,7 +108,10 @@ export class Target { } private getFilename(extension) { - return `${this.datasourceName}.${this.from}-${this.to}.${extension}`; + if(this.createdTimestamp === undefined) { + this.createdTimestamp = moment().valueOf(); + } + return `${this.createdTimestamp}.${this.datasourceName}.${extension}`; } private getFilePath(extension) {