From f07480c7a6c52d4ccd7ac91a2a586d10a10ea3d7 Mon Sep 17 00:00:00 2001 From: Dmitry Nalobin Date: Fri, 2 Oct 2020 15:48:43 +0400 Subject: [PATCH] Support multiple panels export at once (#32) --- src/routes/tasks.ts | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/routes/tasks.ts b/src/routes/tasks.ts index 2ab8216..b4962cd 100644 --- a/src/routes/tasks.ts +++ b/src/routes/tasks.ts @@ -1,26 +1,42 @@ import { Target } from '../target' import * as express from 'express' +import { Datasource } from '@corpglory/tsdb-kit'; +type TRequest = { + body: { + from: string, + to: string, + data: Array<{ + panelUrl: string, + datasourceRequest: Datasource, + datasourceName: string + }>, + target: object, + user: string, + } +}; -async function addTask(req, res) { - let body = req.body; - let from = parseInt(body.from); - let to = parseInt(body.to); - let panelUrl = body.panelUrl; - let targets = [body.target]; - let datasource = body.datasourceRequest; - let datasourceName = body.datasourceName; - let user = body.user; +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; if(isNaN(from) || isNaN(to)) { - res.status(500).send('Range error: please fill both "from" and "to" fields'); + res.status(400).send('Range error: please fill both "from" and "to" fields'); } else if(from >= to) { - res.status(500).send('Range error: "from" should be less than "to"'); + res.status(400).send('Range error: "from" should be less than "to"'); } else { - res.status(200).send('Exporting ' + datasourceName + ' data from ' + new Date(from).toLocaleString() + ' to ' + new Date(to).toLocaleString()); - let target = new Target(panelUrl, user, datasource, targets, from, to, datasourceName); - target.export(); + 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(); + }); } }