diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 2a7141e..61c65f7 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -89,3 +89,7 @@ export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { AnalyticUnit.setPredictionTime(id, result.lastPredictionTime); return result.segments; } + +export function isAnalyticReady(): boolean { + return analyticsService.ready; +} \ No newline at end of file diff --git a/server/src/index.ts b/server/src/index.ts index 4599d4f..ad26ad3 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -2,6 +2,8 @@ import { router as anomaliesRouter } from './routes/analytic_units_router'; import { router as segmentsRouter } from './routes/segments_router'; import { router as alertsRouter } from './routes/alerts_router'; +import * as AnalyticsController from './controllers/analytics_controller'; + import * as Data from './services/data_service'; import { HASTIC_PORT } from './config'; @@ -29,7 +31,11 @@ rootRouter.use('/analyticUnits', anomaliesRouter.routes(), anomaliesRouter.allow rootRouter.use('/segments', segmentsRouter.routes(), segmentsRouter.allowedMethods()); rootRouter.use('/alerts', alertsRouter.routes(), alertsRouter.allowedMethods()); rootRouter.get('/', async (ctx) => { - ctx.response.body = { status: 'Ok' }; + + ctx.response.body = { + server: 'Ok', + analyticsReady: AnalyticsController.isAnalyticReady() + }; }); app diff --git a/server/src/services/analytics_service.ts b/server/src/services/analytics_service.ts index 9b2bab4..38acbfb 100644 --- a/server/src/services/analytics_service.ts +++ b/server/src/services/analytics_service.ts @@ -10,13 +10,17 @@ import * as path from 'path'; export class AnalyticsService { private _requester: any; - + private _ready: boolean = false; + constructor(private _onResponse: (response: any) => void) { this._initConnection(); } public async sendTask(msgObj: any): Promise { + if(!this._ready) { + return Promise.reject("Analytics is not ready"); + } let message = JSON.stringify(msgObj); return this.sendMessage(message); } @@ -38,6 +42,8 @@ export class AnalyticsService { this._requester.close(); } + public get ready() { return this._ready; } + private async _initConnection() { this._requester = zmq.socket('pair'); @@ -55,6 +61,8 @@ export class AnalyticsService { this._requester.on("message", this._onAnalyticsMessage.bind(this)); + this._ready = true; + } private async _connectToAnalytics() {