|
|
|
import { Target } from '../types/target';
|
|
|
|
import { exporterFactory } from '../services/exporter.factory';
|
|
|
|
import { EXPORTED_PATH } from '../config';
|
|
|
|
|
|
|
|
import { Task, TaskTableRowConfig } from '../types';
|
|
|
|
|
|
|
|
import * as express from 'express';
|
|
|
|
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
|
|
|
type TRequest = {
|
|
|
|
body: {
|
|
|
|
from: string,
|
|
|
|
to: string,
|
|
|
|
username: string,
|
|
|
|
tasks: Task[],
|
|
|
|
url: string,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getTasks(req, res) {
|
|
|
|
const resp: TaskTableRowConfig[] = [];
|
|
|
|
fs.readdir(EXPORTED_PATH, (err, items) => {
|
|
|
|
if(err) {
|
|
|
|
console.error(err);
|
|
|
|
res.status(500).send('Something went wrong');
|
|
|
|
} else {
|
|
|
|
for(let item of items) {
|
|
|
|
let file = path.parse(item);
|
|
|
|
if(file.ext !== '.json') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO: read async
|
|
|
|
let data = fs.readFileSync(path.join(EXPORTED_PATH, item), 'utf8');
|
|
|
|
let status = JSON.parse(data);
|
|
|
|
|
|
|
|
let requestedUrl = `http://${req.headers.host}`;
|
|
|
|
let downloadLink = '';
|
|
|
|
let deleteLink = '';
|
|
|
|
if(status.status === 'finished') {
|
|
|
|
downloadLink = `<a class="download-csv" href="${requestedUrl}/static/${file.name}.csv" target="_blank"><i class="fa fa-download"></i></a>`;
|
|
|
|
}
|
|
|
|
resp.push({
|
|
|
|
timestamp: status.time,
|
|
|
|
user: status.user,
|
|
|
|
datasourceRef: status.datasourceRef,
|
|
|
|
rowsCount: status.exportedRows,
|
|
|
|
progress: status.progress,
|
|
|
|
status: status.status,
|
|
|
|
downloadLink,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).send(resp);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addTask(req: TRequest, res) {
|
|
|
|
|
|
|
|
const body = req.body;
|
|
|
|
|
|
|
|
const clientUrl = body.url;
|
|
|
|
const from = parseInt(body.from);
|
|
|
|
const to = parseInt(body.to);
|
|
|
|
const username = body.username;
|
|
|
|
const tasks = body.tasks;
|
|
|
|
|
|
|
|
const datasourceUrl = `${new URL(clientUrl).origin}/api/ds/query`;
|
|
|
|
|
|
|
|
if(isNaN(from) || isNaN(to)) {
|
|
|
|
res.status(400).send('Range error: please fill both "from" and "to" fields');
|
|
|
|
} else if(from >= to) {
|
|
|
|
res.status(400).send('Range error: "from" should be less than "to"');
|
|
|
|
} else {
|
|
|
|
const names = tasks.map(item => item.datasource.name).join(', ');
|
|
|
|
res.status(200).send(`Exporting ${names} data from ${new Date(from).toLocaleString()} to ${new Date(to).toLocaleString()}`);
|
|
|
|
|
|
|
|
const targets = tasks.map((task: Task) => new Target(
|
|
|
|
task.panel,
|
|
|
|
task.datasource,
|
|
|
|
));
|
|
|
|
const exporter = exporterFactory.getExporter();
|
|
|
|
exporter.export(targets, datasourceUrl, username, from, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteTask(req, res) {
|
|
|
|
let filename = req.query.filename;
|
|
|
|
let csvFilePath = path.join(EXPORTED_PATH, `${filename}.csv`);
|
|
|
|
let jsonFilePath = path.join(EXPORTED_PATH, `${filename}.json`);
|
|
|
|
|
|
|
|
if(fs.existsSync(csvFilePath)) {
|
|
|
|
fs.unlink(csvFilePath, err => console.error(err));
|
|
|
|
}
|
|
|
|
if(fs.existsSync(jsonFilePath)) {
|
|
|
|
fs.unlink(jsonFilePath, err => console.error(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).send({ status: 'OK' });
|
|
|
|
}
|
|
|
|
|
|
|
|
export const router = express.Router();
|
|
|
|
|
|
|
|
router.get('/', getTasks);
|
|
|
|
router.post('/', addTask);
|
|
|
|
router.delete('/', deleteTask);
|