From 4aecf0ef48a1f0ba754d6c4caf279a44ebfc6f3f Mon Sep 17 00:00:00 2001 From: sanke1 <22073083+sankerust@users.noreply.github.com> Date: Fri, 28 Sep 2018 17:24:33 +0300 Subject: [PATCH] Port to config #14 (#18) --- .gitignore | 2 +- api-keys-example.json | 3 --- config-example.json | 6 ++++++ package.json | 1 + src/config.ts | 35 +++++++++++++++++++++++++++++++---- src/index.ts | 6 +++--- src/target.ts | 13 +++++++++---- 7 files changed, 51 insertions(+), 15 deletions(-) delete mode 100644 api-keys-example.json create mode 100644 config-example.json diff --git a/.gitignore b/.gitignore index 1beaf89..e89fcc3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ node_modules/ dist/ exported/ -api-keys.json +config.json package-lock.json diff --git a/api-keys-example.json b/api-keys-example.json deleted file mode 100644 index 5035b7a..0000000 --- a/api-keys-example.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "http://localhost:3500": "eyJrIjoiTjlUcmtLSFRNcTdqeXBaQjB5REk2TFkyUDBDM0Z1bWciLCJuIjoiZXhwb3J0LW1hbmFnZXIiLCJpZCI6MX0=" -} diff --git a/config-example.json b/config-example.json new file mode 100644 index 0000000..4707b2d --- /dev/null +++ b/config-example.json @@ -0,0 +1,6 @@ +{ + "apiKeys" : { + "http://localhost:3000": "eyJrIjoiTjlUcmtLSFRNcTdqeXBaQjB5REk2TFkyUDBDM0Z1bWciLCJuIjoiZXhwb3J0LW1hbmFnZXIiLCJpZCI6MX0=" + }, + "port": "8000" +} diff --git a/package.json b/package.json index 1ed5d15..ef537f3 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": {}, "devDependencies": { "@types/express": "^4.11.1", + "@types/lodash": "^4.14.116", "axios": "^0.18.0", "express": "^4.16.3", "fast-csv": "^2.4.1", diff --git a/src/config.ts b/src/config.ts index b847c17..f27ade2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,14 +1,41 @@ import * as path from 'path'; import * as fs from 'fs'; +import * as _ from 'lodash'; + +const DEFAULT_CONFIG = { + 'apiKeys': { + 'http://localhost:3000': '' + }, + 'port': '8000' +}; export const EXPORTED_PATH = path.join(__dirname, '../exported'); if(!fs.existsSync(EXPORTED_PATH)) { - console.log(`${EXPORTED_PATH} don't exist, creating`); + console.log(`${EXPORTED_PATH} doesn't exist, creating`); fs.mkdirSync(EXPORTED_PATH); } -export function getApiKey(host) { - let data = fs.readFileSync(path.join(__dirname, '../api-keys.json'), 'utf8'); +function getConfigField(field: string, defaultVal?: any) { + let val = defaultVal; + let configFile = path.join(__dirname, '../config.json'); + + if(!fs.existsSync(configFile)) { + console.log(`${configFile} doesn't exist, creating`); + fs.writeFileSync(configFile, JSON.stringify(DEFAULT_CONFIG), 'utf8'); + } + + let data = fs.readFileSync(configFile, 'utf8'); + let configField = JSON.parse(data)[field]; + if(configField !== undefined) { + val = configField; + } - return JSON.parse(data)[host] + if(val === undefined || val == '' || _.isEmpty(val)) { + throw new Error(`Please configure ${field} in ${configFile}`); + } + + return val; } + +export const port = getConfigField('port', '8000'); +export const apiKeys = getConfigField('apiKeys'); diff --git a/src/index.ts b/src/index.ts index 202ca0b..efc2aa3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,12 +2,12 @@ import { EXPORTED_PATH } from './config'; import { router as tasksRouter } from './routes/tasks'; import { router as datasourceRouter } from './routes/datasource'; import { router as deleteRouter } from './routes/delete'; +import { port } from './config'; import * as express from 'express'; import * as bodyParser from 'body-parser'; const app = express(); -const PORT = 8000; app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); @@ -26,6 +26,6 @@ app.use('/delete', deleteRouter); app.use('/static', express.static(EXPORTED_PATH)); app.use('/', (req, res) => { res.send('Grafana-data-exporter server works') }); -app.listen(PORT, () => { - console.log(`Server is running on :${PORT}`); +app.listen(port, () => { + console.log(`Server is running on :${port}`); }) diff --git a/src/target.ts b/src/target.ts index 004b6c3..3d173af 100644 --- a/src/target.ts +++ b/src/target.ts @@ -1,5 +1,5 @@ import { queryByMetric, Datasource, Metric } from 'grafana-datasource-kit'; -import { getApiKey } from './config'; +import { apiKeys } from './config'; import * as csv from 'fast-csv'; import * as path from 'path'; @@ -7,7 +7,6 @@ import * as fs from 'fs'; import * as moment from 'moment'; import { URL } from 'url'; - const MS_IN_DAY = 24 * 60 * 60 * 1000; export class Target { @@ -29,6 +28,7 @@ export class Target { ) { this.metric = new Metric(datasource, targets); } + public updateStatus(status) { let time = moment().valueOf(); @@ -68,7 +68,12 @@ export class Target { console.log(`${this.day} day: ${from}ms -> ${to}ms`); - let apiKey = getApiKey(new URL(this.panelUrl).origin); + let host = new URL(this.panelUrl).origin; + let apiKey = apiKeys[host]; + + if(apiKey === undefined || apiKey === '') { + throw new Error(`Please configure API key for ${host}`); + } let metrics = await queryByMetric(this.metric, this.panelUrl, from, to, apiKey); if(metrics.values.length > 0) { @@ -89,7 +94,7 @@ export class Target { this.csvStream.pipe(writableStream); writableStream.on('finish', async () => { - console.log('Everything is written'); + console.log(`Everything is written to ${this.getFilename('csv')}`); await this.updateStatus('finished'); }) }