From c37761b253a1ab751e6fe4c5193b513b1375a125 Mon Sep 17 00:00:00 2001 From: rozetko Date: Tue, 27 Dec 2022 19:19:30 +0300 Subject: [PATCH] WIP: new plugin connectivity: - mock tasks list (`GET /api/tasks`) - add `/api` prefix to all endpoints except `/` - remove `/datasource` endpoint as we don't need it anymore - remove cors as we don't need it anymore --- src/index.ts | 16 ++---- src/routes/datasource.ts | 118 --------------------------------------- src/routes/tasks.ts | 12 ++++ 3 files changed, 16 insertions(+), 130 deletions(-) delete mode 100644 src/routes/datasource.ts diff --git a/src/index.ts b/src/index.ts index 1966375..73841bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ import { EXPORTED_PATH } from './config'; import { router as tasksRouter } from './routes/tasks'; import { router as statusRouter } from './routes/status'; -import { router as datasourceRouter } from './routes/datasource'; import { router as deleteRouter } from './routes/delete'; import { port } from './config'; @@ -13,20 +12,13 @@ const app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); -app.use(function(req, res, next) { - res.header('Access-Control-Allow-Origin', '*'); - res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); - res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); - next(); -}) - +// TODO: move everything with /api prefix to an apiRouter app.use('/api/status', statusRouter); -app.use('/tasks', tasksRouter); -app.use('/datasource', datasourceRouter); -app.use('/delete', deleteRouter); +app.use('/api/tasks', tasksRouter); +app.use('/api/delete', deleteRouter); -app.use('/static', express.static(EXPORTED_PATH)); +app.use('/api/static', express.static(EXPORTED_PATH)); app.use('/', (req, res) => { res.send('Grafana-data-exporter server works') }); app.listen(port, () => { diff --git a/src/routes/datasource.ts b/src/routes/datasource.ts deleted file mode 100644 index 3f03aa8..0000000 --- a/src/routes/datasource.ts +++ /dev/null @@ -1,118 +0,0 @@ -import { EXPORTED_PATH } from '../config'; - -import * as express from 'express'; -import * as path from 'path'; -import * as fs from 'fs'; - - -function sendOk(req, res) { - res.status(200).send('Datasource works'); -} - -function search(req, res) { - fs.readdir(EXPORTED_PATH, err => { - if(err) { - console.error(err); - res.status(500).send('Something went wrong'); - } else { - res.status(200).send(['All tasks']); - } - }) -} - -function query(req, res) { - let body = req.body; - let targets = body.targets; - - let resp = { - target: targets[0].target, - type: 'table', - columns: [ - { - text: 'Progress updated', - type: 'time' - }, - { - text: 'User', - type: 'string' - }, - { - text: 'Datasource', - type: 'string' - }, - { - text: 'Exported Rows', - type: 'number' - }, - { - text: 'Progress', - type: 'string' - }, - { - text: 'Download CSV', - type: 'string' - }, - { - text: 'Status', - type: 'string' - }, - { - text: 'Delete task', - type: 'string' - } - ], - rows: [] - }; - - for(let target of targets) { - 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 = ``; - deleteLink = ``; - } - resp.rows.push([ - status.time, - status.user, - status.datasourceName, - status.exportedRows, - status.progress, - downloadLink, - status.status, - deleteLink - ]); - } - - res.status(200).send([resp]); - } - }) - } - -} - -function sendAnnotations(req, res) { - res.status(200).send([]); -} - -export const router = express.Router(); - -router.get('/', sendOk); -router.post('/search', search); -router.post('/query', query); -router.post('/annotations', sendAnnotations); - diff --git a/src/routes/tasks.ts b/src/routes/tasks.ts index 1884076..27fbed5 100644 --- a/src/routes/tasks.ts +++ b/src/routes/tasks.ts @@ -20,6 +20,17 @@ type TRequest = { } }; +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); @@ -51,4 +62,5 @@ async function addTask(req: TRequest, res) { export const router = express.Router(); +router.get('/', getTasks); router.post('/', addTask);