Browse Source

Merge pull request 'New plugin connectivity pt.1' (#2) from mock-tasks-list into master

Reviewed-on: #2
pull/4/head
rozetko 2 years ago
parent
commit
8a92f6fecf
  1. 16
      src/index.ts
  2. 118
      src/routes/datasource.ts
  3. 12
      src/routes/tasks.ts

16
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, () => {

118
src/routes/datasource.ts

@ -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 = `<a class="download-csv" href="${requestedUrl}/static/${file.name}.csv" target="_blank"><i class="fa fa-download"></i></a>`;
deleteLink = `<a class="delete-task" href="${requestedUrl}/delete?filename=${file.name}"><i class="fa fa-times"></i></a>`;
}
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);

12
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);

Loading…
Cancel
Save