diff --git a/server/src/index.ts b/server/src/index.ts index 78db2bd..4ca1d05 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,5 +1,6 @@ import * as Koa from 'koa'; import * as Router from 'koa-router'; +import * as bodyParser from 'koa-bodyparser'; import { router as anomaliesRouter } from './routes/anomalies'; @@ -13,10 +14,13 @@ checkDataFolders(); var app = new Koa(); const PORT = process.env.HASTIC_PORT || 8000; -app.use(async function(ctx) { +app.use(bodyParser()) + +app.use(async function(ctx, next) { ctx.set('Access-Control-Allow-Origin', '*'); ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); + next(); }); var rootRouter = new Router(); @@ -24,7 +28,7 @@ rootRouter.use('/anomalies', anomaliesRouter.routes(), anomaliesRouter.allowedMe rootRouter.use('/segments', segmentsRouter.routes(), segmentsRouter.allowedMethods()); rootRouter.use('/alerts', alertsRouter.routes(), alertsRouter.allowedMethods()); rootRouter.get('/', async (ctx) => { - ctx.body = { status: 'OK' }; + ctx.response.body = { status: 'OK' }; }); app diff --git a/server/src/routes/alerts.ts b/server/src/routes/alerts.ts index 56905dc..0eb48b8 100644 --- a/server/src/routes/alerts.ts +++ b/server/src/routes/alerts.ts @@ -6,9 +6,9 @@ import * as Router from 'koa-router'; function getAlert(ctx: Router.IRouterContext) { - let anomalyId: AnomalyId = ctx.request.body.query.anomaly_id; + let anomalyId: AnomalyId = ctx.request.query.anomaly_id; let anomaly = loadAnomalyById(anomalyId) - if (anomaly == null) { + if(anomaly == null) { anomalyId = getAnomalyIdByName(anomalyId.toLowerCase()); } @@ -21,25 +21,26 @@ function getAlert(ctx: Router.IRouterContext) { } function changeAlert(ctx: Router.IRouterContext) { - - let anomalyId: AnomalyId = ctx.request.body.anomaly_id; - let enable: boolean = ctx.body.enable; - - let anomaly = loadAnomalyById(anomalyId) - if (anomaly == null) { - anomalyId = getAnomalyIdByName(anomalyId.toLowerCase()); - } - - let alertsAnomalies = getAlertsAnomalies(); - let pos: number = alertsAnomalies.indexOf(anomalyId); - if(enable && pos == -1) { - alertsAnomalies.push(anomalyId); - saveAlertsAnomalies(alertsAnomalies); - } else if(!enable && pos > -1) { - alertsAnomalies.splice(pos, 1); - saveAlertsAnomalies(alertsAnomalies); - } - ctx.response.body = { status: 'Ok' }; + + let anomalyId: AnomalyId = ctx.request.body.anomaly_id; + let enable: boolean = ctx.request.body.enable; + + let anomaly = loadAnomalyById(anomalyId) + if(anomaly == null) { + anomalyId = getAnomalyIdByName(anomalyId.toLowerCase()); + } + + let alertsAnomalies = getAlertsAnomalies(); + let pos: number = alertsAnomalies.indexOf(anomalyId); + if(enable && pos == -1) { + alertsAnomalies.push(anomalyId); + saveAlertsAnomalies(alertsAnomalies); + } else if(!enable && pos > -1) { + alertsAnomalies.splice(pos, 1); + saveAlertsAnomalies(alertsAnomalies); + } + ctx.response.body = { status: 'OK' }; + } export const router = new Router(); diff --git a/server/src/routes/anomalies.ts b/server/src/routes/anomalies.ts index a27465d..76f2e23 100644 --- a/server/src/routes/anomalies.ts +++ b/server/src/routes/anomalies.ts @@ -10,9 +10,9 @@ import { import { runLearning } from '../services/analytics' import { saveTargets } from '../services/metrics'; -async function sendAnomalyTypeStatus(req, res) { - let id = req.query.id; - let name = req.query.name; +async function sendAnomalyTypeStatus(ctx: Router.IRouterContext) { + let id = ctx.request.query.id; + let name = ctx.request.query.name; try { let anomaly: Anomaly; if(id !== undefined) { @@ -21,28 +21,26 @@ async function sendAnomalyTypeStatus(req, res) { anomaly = loadAnomalyByName(name); } if(anomaly === null) { - res.status(404).send({ - code: 404, - message: 'Not found' - }); + ctx.response.status = 404; return; } if(anomaly.status === undefined) { throw new Error('No status for ' + name); } - res.status(200).send({ status: anomaly.status, errorMessage: anomaly.error }); + ctx.response.body = { status: anomaly.status, errorMessage: anomaly.error }; } catch(e) { console.error(e); // TODO: better send 404 when we know than isn`t found - res.status(500).send({ error: 'Can`t return anything' }); + ctx.response.status = 500; + ctx.response.body = { error: 'Can`t return anything' }; } } -async function getAnomaly(req, res) { +async function getAnomaly(ctx: Router.IRouterContext) { try { - let id = req.query.id; - let name = req.query.name; + let id = ctx.request.query.id; + let name = ctx.request.query.name; let anomaly:Anomaly; if(id !== undefined) { @@ -51,66 +49,65 @@ async function getAnomaly(req, res) { anomaly = loadAnomalyByName(name.toLowerCase()); } if(anomaly === null) { - res.status(404).send({ - code: 404, - message: 'Not found' - }); + ctx.response.status = 404; return; } - let payload = JSON.stringify({ + ctx.response.body = { name: anomaly.name, metric: anomaly.metric, status: anomaly.status - }); - res.status(200).send(payload) + }; } catch(e) { console.error(e); // TODO: better send 404 when we know than isn`t found - res.status(500).send('Can`t get anything'); + ctx.response.status = 500; + ctx.response.body = 'Can`t get anything'; } } -async function createAnomaly(req, res) { +async function createAnomaly(ctx: Router.IRouterContext) { try { + let body = ctx.request.body; const metric:Metric = { - datasource: req.body.metric.datasource, - targets: saveTargets(req.body.metric.targets) + datasource: body.metric.datasource, + targets: saveTargets(body.metric.targets) }; const anomaly:Anomaly = { - name: req.body.name, - panelUrl: req.body.panelUrl, + name: body.name, + panelUrl: body.panelUrl, metric: metric, - datasource: req.body.datasource, + datasource: body.datasource, status: 'learning', last_prediction_time: 0, next_id: 0 }; let anomalyId = insertAnomaly(anomaly); if(anomalyId === null) { - res.status(403).send({ + ctx.response.status = 403; + ctx.response.body = { code: 403, message: 'Already exists' - }); + }; } - let payload = JSON.stringify({ anomaly_id: anomalyId }) - res.status(200).send(payload); + ctx.response.body = { anomaly_id: anomalyId }; runLearning(anomalyId); } catch(e) { - res.status(500).send({ + ctx.response.status = 500; + ctx.response.body = { code: 500, message: 'Internal error' - }); + }; } } -function deleteAnomaly(req, res) { +function deleteAnomaly(ctx: Router.IRouterContext) { try { - let id = req.query.id; - let name = req.query.name; + let id = ctx.request.query.id; + let name = ctx.request.query.name; if(id !== undefined) { removeAnomaly(id); @@ -118,15 +115,16 @@ function deleteAnomaly(req, res) { removeAnomaly(name.toLowerCase()); } - res.status(200).send({ + ctx.response.body = { code: 200, message: 'Success' - }); + }; } catch(e) { - res.status(500).send({ + ctx.response.status = 500; + ctx.response.body = { code: 500, message: 'Internal error' - }); + }; } } diff --git a/server/src/routes/segments.ts b/server/src/routes/segments.ts index 3b180bb..e84701a 100644 --- a/server/src/routes/segments.ts +++ b/server/src/routes/segments.ts @@ -15,15 +15,15 @@ import { runLearning } from '../services/analytics'; async function sendSegments(ctx: Router.IRouterContext) { - let anomalyId: AnomalyId = ctx.query.anomaly_id; + let anomalyId: AnomalyId = ctx.request.query.anomaly_id; let anomaly:Anomaly = loadAnomalyById(anomalyId); if(anomaly === null) { anomalyId = getAnomalyIdByName(anomalyId); } - let lastSegmentId = ctx.query.last_segment; - let timeFrom = ctx.query.from; - let timeTo = ctx.query.to; + let lastSegmentId = ctx.request.query.last_segment; + let timeFrom = ctx.request.query.from; + let timeTo = ctx.request.query.to; let segments = getLabeledSegments(anomalyId); @@ -45,9 +45,9 @@ async function sendSegments(ctx: Router.IRouterContext) { } -async function updateSegments(req, res) { +async function updateSegments(ctx: Router.IRouterContext) { try { - let segmentsUpdate = req.body; + let segmentsUpdate = ctx.request.body; let anomalyId = segmentsUpdate.anomaly_id; let anomalyName = segmentsUpdate.name; @@ -59,15 +59,15 @@ async function updateSegments(req, res) { let addedIds = insertSegments(anomalyId, segmentsUpdate.added_segments, true); removeSegments(anomalyId, segmentsUpdate.removed_segments); - let payload = JSON.stringify({ added_ids: addedIds }); - res.status(200).send(payload); + ctx.response.body = { added_ids: addedIds }; runLearning(anomalyId); } catch(e) { - res.status(500).send({ + ctx.response.status = 500; + ctx.response.body = { code: 500, message: 'Internal error' - }); + }; } }