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",
"description": "Server for fetching data from Grafana datasources",
"scripts": {

5
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');

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

9
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');
@ -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,

3
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();
}
}

16
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,
@ -23,7 +24,8 @@ export class Target {
datasource: Datasource,
targets: Array<Object>,
private from: number,
private to: number
private to: number,
private datasourceName: string,
) {
this.metric = new Metric(datasource, targets);
}
@ -35,7 +37,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 => {
@ -79,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'));
@ -105,9 +108,10 @@ 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}`;
if(this.createdTimestamp === undefined) {
this.createdTimestamp = moment().valueOf();
}
return `${this.createdTimestamp}.${this.datasourceName}.${extension}`;
}
private getFilePath(extension) {

Loading…
Cancel
Save