|
|
@ -1,5 +1,6 @@ |
|
|
|
import { exporterFactory } from '../services/exporter.factory'; |
|
|
|
import { exporterFactory } from '../services/exporter.factory'; |
|
|
|
import { EXPORTED_PATH } from '../config'; |
|
|
|
import { CSV_PATH } from '../config'; |
|
|
|
|
|
|
|
import { validateGrafanaUrl } from '../services/api_keys'; |
|
|
|
|
|
|
|
|
|
|
|
import { ExportTask } from '../types'; |
|
|
|
import { ExportTask } from '../types'; |
|
|
|
|
|
|
|
|
|
|
@ -10,6 +11,7 @@ import * as _ from 'lodash'; |
|
|
|
import * as path from 'path'; |
|
|
|
import * as path from 'path'; |
|
|
|
import * as fs from 'fs'; |
|
|
|
import * as fs from 'fs'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type TRequest = { |
|
|
|
type TRequest = { |
|
|
|
body: { |
|
|
|
body: { |
|
|
|
task: ExportTask, |
|
|
|
task: ExportTask, |
|
|
@ -20,7 +22,7 @@ type TRequest = { |
|
|
|
|
|
|
|
|
|
|
|
async function getTasks(req, res) { |
|
|
|
async function getTasks(req, res) { |
|
|
|
const resp: ExportTask[] = []; |
|
|
|
const resp: ExportTask[] = []; |
|
|
|
fs.readdir(EXPORTED_PATH, (err, items) => { |
|
|
|
fs.readdir(CSV_PATH, (err, items) => { |
|
|
|
if(err) { |
|
|
|
if(err) { |
|
|
|
console.error(err); |
|
|
|
console.error(err); |
|
|
|
res.status(500).send(err.message); |
|
|
|
res.status(500).send(err.message); |
|
|
@ -31,7 +33,7 @@ async function getTasks(req, res) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
// TODO: read async
|
|
|
|
// TODO: read async
|
|
|
|
let data = fs.readFileSync(path.join(EXPORTED_PATH, item), 'utf8'); |
|
|
|
let data = fs.readFileSync(path.join(CSV_PATH, item), 'utf8'); |
|
|
|
try { |
|
|
|
try { |
|
|
|
let status = JSON.parse(data); |
|
|
|
let status = JSON.parse(data); |
|
|
|
resp.push(status); |
|
|
|
resp.push(status); |
|
|
@ -51,10 +53,19 @@ async function addTask(req: TRequest, res) { |
|
|
|
const clientUrl = body.url; |
|
|
|
const clientUrl = body.url; |
|
|
|
if (_.isEmpty(clientUrl)) { |
|
|
|
if (_.isEmpty(clientUrl)) { |
|
|
|
res.status(400).send('"url" field is required'); |
|
|
|
res.status(400).send('"url" field is required'); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
const task = body.task; |
|
|
|
const task = body.task; |
|
|
|
if (_.isEmpty(task)) { |
|
|
|
if (_.isEmpty(task)) { |
|
|
|
res.status(400).send('"task" field is required'); |
|
|
|
res.status(400).send('"task" field is required'); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
validateGrafanaUrl(clientUrl); |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
res.status(500).send(e.message); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const datasourceUrl = `${new URL(clientUrl).origin}/api/ds/query`; |
|
|
|
const datasourceUrl = `${new URL(clientUrl).origin}/api/ds/query`; |
|
|
@ -63,8 +74,10 @@ async function addTask(req: TRequest, res) { |
|
|
|
const to = +task.timeRange.to; |
|
|
|
const to = +task.timeRange.to; |
|
|
|
if (isNaN(from) || isNaN(to)) { |
|
|
|
if (isNaN(from) || isNaN(to)) { |
|
|
|
res.status(400).send('Range error: please fill both "from" and "to" fields'); |
|
|
|
res.status(400).send('Range error: please fill both "from" and "to" fields'); |
|
|
|
|
|
|
|
return; |
|
|
|
} else if (from >= to) { |
|
|
|
} else if (from >= to) { |
|
|
|
res.status(400).send('Range error: "from" should be less than "to"'); |
|
|
|
res.status(400).send('Range error: "from" should be less than "to"'); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const exporter = exporterFactory.getExporter(); |
|
|
|
const exporter = exporterFactory.getExporter(); |
|
|
@ -74,8 +87,8 @@ async function addTask(req: TRequest, res) { |
|
|
|
|
|
|
|
|
|
|
|
async function deleteTask(req, res) { |
|
|
|
async function deleteTask(req, res) { |
|
|
|
let taskId = req.body.taskId; |
|
|
|
let taskId = req.body.taskId; |
|
|
|
let csvFilePath = path.join(EXPORTED_PATH, `${taskId}.csv`); |
|
|
|
let csvFilePath = path.join(CSV_PATH, `${taskId}.csv`); |
|
|
|
let jsonFilePath = path.join(EXPORTED_PATH, `${taskId}.json`); |
|
|
|
let jsonFilePath = path.join(CSV_PATH, `${taskId}.json`); |
|
|
|
|
|
|
|
|
|
|
|
if(fs.existsSync(csvFilePath)) { |
|
|
|
if(fs.existsSync(csvFilePath)) { |
|
|
|
fs.unlink(csvFilePath, err => console.error(err)); |
|
|
|
fs.unlink(csvFilePath, err => console.error(err)); |
|
|
|