From ead459ccb037933b7d1e19ba73536609e9dcde74 Mon Sep 17 00:00:00 2001 From: rozetko Date: Tue, 16 Apr 2019 21:25:25 +0300 Subject: [PATCH] Don't run detection if learning failed #549 (#578) --- server/src/controllers/analytics_controller.ts | 8 +++++--- server/src/routes/analytic_units_router.ts | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 4298059..b10887c 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -229,8 +229,9 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { } await AnalyticUnitCache.setData(id, result.payload.cache); } catch (err) { - let message = err.message || JSON.stringify(err); + const message = err.message || JSON.stringify(err); await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, message); + throw new Error(message) } } @@ -429,9 +430,10 @@ export async function updateThreshold( await Threshold.updateThreshold(id, value, condition); } -export async function runFirstLearning(id: AnalyticUnit.AnalyticUnitId) { +export async function runLearningWithDetection(id: AnalyticUnit.AnalyticUnitId) { // TODO: move setting status somehow "inside" learning await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.PENDING); runLearning(id) - .then(() => runDetect(id)); + .then(() => runDetect(id)) + .catch(err => console.error(err)); } diff --git a/server/src/routes/analytic_units_router.ts b/server/src/routes/analytic_units_router.ts index 406b288..c282c8a 100644 --- a/server/src/routes/analytic_units_router.ts +++ b/server/src/routes/analytic_units_router.ts @@ -125,7 +125,7 @@ async function deleteUnit(ctx: Router.IRouterContext) { async function runDetect(ctx: Router.IRouterContext) { const { ids } = ctx.request.body as { ids: AnalyticUnit.AnalyticUnitId[] }; - await Promise.all(ids.map(AnalyticsController.runFirstLearning)); + await Promise.all(ids.map(AnalyticsController.runLearningWithDetection)); ctx.response.body = { code: 200,