Browse Source

Merge Bugfixes pull request #9

pull/1/head
rozetko 6 years ago committed by GitHub
parent
commit
acbd07d34c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      package.json
  2. 5
      src/config.ts
  3. 2
      src/index.ts
  4. 9
      src/routes/datasource.ts
  5. 3
      src/routes/tasks.ts
  6. 16
      src/target.ts

2
package.json

@ -1,5 +1,5 @@
{ {
"name": "export-manager-server", "name": "grafana-data-exporter",
"version": "0.3.1", "version": "0.3.1",
"description": "Server for fetching data from Grafana datasources", "description": "Server for fetching data from Grafana datasources",
"scripts": { "scripts": {

5
src/config.ts

@ -2,7 +2,10 @@ import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
export const EXPORTED_PATH = path.join(__dirname, '../exported'); 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) { export function getApiKey(host) {
let data = fs.readFileSync(path.join(__dirname, '../api-keys.json'), 'utf8'); let data = fs.readFileSync(path.join(__dirname, '../api-keys.json'), 'utf8');

2
src/index.ts

@ -24,7 +24,7 @@ app.use('/datasource', datasourceRouter);
app.use('/delete', deleteRouter); app.use('/delete', deleteRouter);
app.use('/static', express.static(EXPORTED_PATH)); 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, () => { app.listen(PORT, () => {
console.log(`Server is running on :${PORT}`); console.log(`Server is running on :${PORT}`);

9
src/routes/datasource.ts

@ -10,7 +10,7 @@ function sendOk(req, res) {
} }
function search(req, res) { function search(req, res) {
fs.readdir(EXPORTED_PATH, (err, items) => { fs.readdir(EXPORTED_PATH, err => {
if(err) { if(err) {
console.error(err); console.error(err);
res.status(500).send('Something went wrong'); res.status(500).send('Something went wrong');
@ -40,10 +40,6 @@ function query(req, res) {
text: 'Datasource', text: 'Datasource',
type: 'string' type: 'string'
}, },
{
text: 'Measurement',
type: 'string'
},
{ {
text: 'Exported Rows', text: 'Exported Rows',
type: 'number' type: 'number'
@ -93,8 +89,7 @@ function query(req, res) {
resp.rows.push([ resp.rows.push([
status.time, status.time,
status.user, status.user,
status.datasource, status.datasourceName,
status.measurement,
status.exportedRows, status.exportedRows,
status.progress, status.progress,
downloadLink, downloadLink,

3
src/routes/tasks.ts

@ -10,6 +10,7 @@ async function addTask(req, res) {
let panelUrl = body.panelUrl; let panelUrl = body.panelUrl;
let targets = [body.target]; let targets = [body.target];
let datasource = body.datasourceRequest; let datasource = body.datasourceRequest;
let datasourceName = body.datasourceName;
let user = body.user; let user = body.user;
if(isNaN(from) || isNaN(to)) { 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"'); res.status(500).send('Range error: "from" should be less than "to"');
} else { } else {
res.status(200).send('Task added'); 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(); target.export();
} }
} }

16
src/target.ts

@ -16,6 +16,7 @@ export class Target {
private day: number; private day: number;
private csvStream: any; private csvStream: any;
private metric: Metric; private metric: Metric;
private createdTimestamp: number;
constructor( constructor(
private panelUrl: string, private panelUrl: string,
@ -23,7 +24,8 @@ export class Target {
datasource: Datasource, datasource: Datasource,
targets: Array<Object>, targets: Array<Object>,
private from: number, private from: number,
private to: number private to: number,
private datasourceName: string,
) { ) {
this.metric = new Metric(datasource, targets); this.metric = new Metric(datasource, targets);
} }
@ -35,7 +37,8 @@ export class Target {
user: this.user, user: this.user,
exportedRows: this.exportedRows, exportedRows: this.exportedRows,
progress: (this.day / this.days).toLocaleString('en', { style: 'percent' }), progress: (this.day / this.days).toLocaleString('en', { style: 'percent' }),
status status,
datasourceName: this.datasourceName
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.writeFile(this.getFilePath('json'), JSON.stringify(data), 'utf8', err => { fs.writeFile(this.getFilePath('json'), JSON.stringify(data), 'utf8', err => {
@ -79,7 +82,7 @@ export class Target {
} }
this.csvStream.end(); this.csvStream.end();
} }
// TODO: move csv-related stuff to service
private initCsvStream() { private initCsvStream() {
this.csvStream = csv.createWriteStream({ headers: true }); this.csvStream = csv.createWriteStream({ headers: true });
let writableStream = fs.createWriteStream(this.getFilePath('csv')); let writableStream = fs.createWriteStream(this.getFilePath('csv'));
@ -105,9 +108,10 @@ export class Target {
} }
private getFilename(extension) { private getFilename(extension) {
// TODO: use something unique instead of measurement in filename if(this.createdTimestamp === undefined) {
// as measurement field exists only in influxDB metric this.createdTimestamp = moment().valueOf();
return `${this.metric.targets[0].measurement}.${this.from}-${this.to}.${extension}`; }
return `${this.createdTimestamp}.${this.datasourceName}.${extension}`;
} }
private getFilePath(extension) { private getFilePath(extension) {

Loading…
Cancel
Save