diff --git a/server/src/index.ts b/server/src/index.ts index c93f436..a5d6511 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -2,7 +2,7 @@ import { router as analyticUnitsRouter } from './routes/analytic_units_router'; import { router as segmentsRouter } from './routes/segments_router'; import { router as thresholdRouter } from './routes/threshold_router'; import { router as dataRouter } from './routes/data_router'; -import { router as detectionStatusRouter } from './routes/detection_status_router'; +import { router as detectionsRouter } from './routes/detections_router'; import * as AnalyticsController from './controllers/analytics_controller'; @@ -58,7 +58,7 @@ async function init() { rootRouter.use('/segments', segmentsRouter.routes(), segmentsRouter.allowedMethods()); rootRouter.use('/threshold', thresholdRouter.routes(), thresholdRouter.allowedMethods()); rootRouter.use('/query', dataRouter.routes(), dataRouter.allowedMethods()); - rootRouter.use('/detectionStatus', detectionStatusRouter.routes(), detectionStatusRouter.allowedMethods()); + rootRouter.use('/detections', detectionsRouter.routes(), detectionsRouter.allowedMethods()); rootRouter.get('/', async (ctx) => { const activeWebhooks = await AnalyticsController.getActiveWebhooks(); diff --git a/server/src/routes/detection_status_router.ts b/server/src/routes/detection_status_router.ts deleted file mode 100644 index 81782d5..0000000 --- a/server/src/routes/detection_status_router.ts +++ /dev/null @@ -1,74 +0,0 @@ -import * as AnalyticsController from '../controllers/analytics_controller'; -import { AnalyticUnitId } from '../models/analytic_unit_model'; - -import * as Router from 'koa-router'; -import * as _ from 'lodash'; - -export enum DetectionState { - READY = 'READY', - RUNNING = 'RUNNING', - FAILED = 'FAILED' -} - -declare type DetectionStatus = { - id: AnalyticUnitId, - from: number, - to: number, - state: DetectionState -} - -declare type DetectionStatusResponse = { - timeranges: DetectionStatus[] -} - -let runnnedDetections: DetectionStatus[] = []; - -export async function getDetectionStatus(ctx: Router.IRouterContext) { - let id: AnalyticUnitId = ctx.request.query.id; - if(id === undefined || id === '') { - throw new Error('analyticUnitId (id) is missing'); - } - - let from: number = +ctx.request.query.from; - if(isNaN(from) || ctx.request.query.from === '') { - throw new Error(`from is missing or corrupted (got ${ctx.request.query.from})`); - } - let to: number = +ctx.request.query.to; - if(isNaN(to) || ctx.request.query.to === '') { - throw new Error(`to is missing or corrupted (got ${ctx.request.query.to})`); - } - - const previousRun = _.find(runnnedDetections, {id, from, to}); - if(previousRun !== undefined) { - ctx.response.body = { - timeranges: [ - previousRun - ] - } - } - - const currentRun = { - id, - from, - to, - state: DetectionState.RUNNING - }; - runnnedDetections.push(currentRun); - - AnalyticsController.runDetect(id, from, to) - .then(() => _.find(runnnedDetections, {id, from, to}).state = DetectionState.READY) - .catch(err => { - console.error(err); - _.find(runnnedDetections, {id, from, to}).state = DetectionState.FAILED; - }); - - ctx.response.body = { - timeranges: [ - currentRun - ] - }; -} - -export const router = new Router(); - -router.get('/', getDetectionStatus); diff --git a/server/src/routes/detections_router.ts b/server/src/routes/detections_router.ts new file mode 100644 index 0000000..9680077 --- /dev/null +++ b/server/src/routes/detections_router.ts @@ -0,0 +1,75 @@ +import * as AnalyticsController from '../controllers/analytics_controller'; +import { AnalyticUnitId } from '../models/analytic_unit_model'; + +import * as Router from 'koa-router'; +import * as _ from 'lodash'; + + +// TODO: move this to analytics_controller +export enum DetectionStatus { + READY = 'READY', + RUNNING = 'RUNNING', + FAILED = 'FAILED' +} + +// TODO: move this to analytics_controller +declare type DetectionSpan = { + id: AnalyticUnitId, + from: number, + to: number, + status: DetectionStatus +} + +declare type DetectionSpansResponse = { + spans: DetectionSpan[] +} + +// TODO: move this to analytics_controller +let runningDetectionSpans: DetectionSpan[] = []; + +export async function getDetectionSpans(ctx: Router.IRouterContext) { + let id: AnalyticUnitId = ctx.request.query.id; + if(id === undefined || id === '') { + throw new Error('analyticUnitId (id) is missing'); + } + + let from: number = +ctx.request.query.from; + if(isNaN(from) || ctx.request.query.from === '') { + throw new Error(`from is missing or corrupted (got ${ctx.request.query.from})`); + } + let to: number = +ctx.request.query.to; + if(isNaN(to) || ctx.request.query.to === '') { + throw new Error(`to is missing or corrupted (got ${ctx.request.query.to})`); + } + + let response: DetectionSpansResponse = { spans: [] } + + // TODO: move this to analytics_controller + // TODO: what if we are inside a running span? + // TODO: this find will not find anything because of status field, use an id instead + const previousRun = _.find(runningDetectionSpans, { id, from, to }); + if(previousRun !== undefined) { + response.spans.push(previousRun); + } + + const currentRun: DetectionSpan = { id, from, to, status: DetectionStatus.RUNNING }; + runningDetectionSpans.push(currentRun); + + // TODO: move this to analytics_controller + AnalyticsController.runDetect(id, from, to) + .then(() => { + // TODO: this find will not find anything because of status field, use an id instead + _.find(runningDetectionSpans, { id, from, to }).status = DetectionStatus.READY + }) + .catch(err => { + console.error(err); + // TODO: this find will not find anything because of status field, use an id instead + _.find(runningDetectionSpans, { id, from, to }).status = DetectionStatus.FAILED; + }); + + ctx.response.body = response; +} + +export const router = new Router(); + +router.get('/spans', getDetectionSpans);