|
|
|
import { Target } from '../types/target';
|
|
|
|
import { exporterFactory } from '../services/exporter.factory';
|
|
|
|
import { EXPORTED_PATH } from '../config';
|
|
|
|
|
|
|
|
import { Datasource } from '@corpglory/tsdb-kit';
|
|
|
|
|
|
|
|
import * as express from 'express';
|
|
|
|
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
|
|
|
type TRequest = {
|
|
|
|
body: {
|
|
|
|
from: string,
|
|
|
|
to: string,
|
|
|
|
data: Array<{
|
|
|
|
panelUrl: string,
|
|
|
|
panelTitle: string,
|
|
|
|
panelId: number,
|
|
|
|
datasourceRequest: Datasource,
|
|
|
|
datasourceName: string,
|
|
|
|
target: object,
|
|
|
|
}>,
|
|
|
|
user: string,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getTasks(req, res) {
|
|
|
|
res.status(200).send([{
|
|
|
|
timestamp: 12343567,
|
|
|
|
user: 'admin',
|
|
|
|
datasource: 'postgres',
|
|
|
|
rowsCount: 2345,
|
|
|
|
progress: 100,
|
|
|
|
status: 'Success',
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addTask(req: TRequest, res) {
|
|
|
|
const body = req.body;
|
|
|
|
const from = parseInt(body.from);
|
|
|
|
const to = parseInt(body.to);
|
|
|
|
const data = body.data;
|
|
|
|
const user = body.user;
|
|
|
|
|
|
|
|
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 = data.map(item => item.datasourceName).join(', ');
|
|
|
|
res.status(200).send(`Exporting ${names} data from ${new Date(from).toLocaleString()} to ${new Date(to).toLocaleString()}`);
|
|
|
|
|
|
|
|
const targets = data.map(item => new Target(
|
|
|
|
item.panelUrl,
|
|
|
|
item.panelTitle,
|
|
|
|
item.panelId,
|
|
|
|
item.datasourceRequest,
|
|
|
|
[item.target],
|
|
|
|
item.datasourceName,
|
|
|
|
));
|
|
|
|
|
|
|
|
const exporter = exporterFactory.getExporter();
|
|
|
|
exporter.export(targets, user, 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);
|