You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

6 years ago
import { Target } from '../target'
import * as express from 'express'
import { Datasource } from '@corpglory/tsdb-kit';
6 years ago
type TRequest = {
body: {
from: string,
to: string,
data: Array<{
panelUrl: string,
datasourceRequest: Datasource,
datasourceName: string
}>,
target: object,
user: string,
}
};
6 years ago
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 targets = [body.target];
const user = body.user;
6 years ago
if(isNaN(from) || isNaN(to)) {
res.status(400).send('Range error: please fill both "from" and "to" fields');
6 years ago
} else if(from >= to) {
res.status(400).send('Range error: "from" should be less than "to"');
6 years ago
} 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()}`);
data.forEach(request => {
const target = new Target(request.panelUrl, user, request.datasourceRequest, targets, from, to, request.datasourceName);
target.export();
});
6 years ago
}
}
export const router = express.Router();
router.post('/', addTask);