Browse Source

Port to config #14 (#18)

pull/1/head
sanke1 6 years ago committed by rozetko
parent
commit
4aecf0ef48
  1. 2
      .gitignore
  2. 3
      api-keys-example.json
  3. 6
      config-example.json
  4. 1
      package.json
  5. 35
      src/config.ts
  6. 6
      src/index.ts
  7. 13
      src/target.ts

2
.gitignore vendored

@ -1,5 +1,5 @@
node_modules/
dist/
exported/
api-keys.json
config.json
package-lock.json

3
api-keys-example.json

@ -1,3 +0,0 @@
{
"http://localhost:3500": "eyJrIjoiTjlUcmtLSFRNcTdqeXBaQjB5REk2TFkyUDBDM0Z1bWciLCJuIjoiZXhwb3J0LW1hbmFnZXIiLCJpZCI6MX0="
}

6
config-example.json

@ -0,0 +1,6 @@
{
"apiKeys" : {
"http://localhost:3000": "eyJrIjoiTjlUcmtLSFRNcTdqeXBaQjB5REk2TFkyUDBDM0Z1bWciLCJuIjoiZXhwb3J0LW1hbmFnZXIiLCJpZCI6MX0="
},
"port": "8000"
}

1
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",

35
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;
}
if(val === undefined || val == '' || _.isEmpty(val)) {
throw new Error(`Please configure ${field} in ${configFile}`);
}
return JSON.parse(data)[host]
return val;
}
export const port = getConfigField('port', '8000');
export const apiKeys = getConfigField('apiKeys');

6
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}`);
})

13
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 {
@ -30,6 +29,7 @@ export class Target {
this.metric = new Metric(datasource, targets);
}
public updateStatus(status) {
let time = moment().valueOf();
let data = {
@ -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');
})
}

Loading…
Cancel
Save