From 711322192fb4b1756509e85f935217208397fc0d Mon Sep 17 00:00:00 2001 From: rozetko Date: Tue, 19 Jan 2021 14:37:01 +0300 Subject: [PATCH] Fix anomaly detector webhooks (#940) --- server/src/controllers/analytics_controller.ts | 8 ++++++-- server/src/models/segment_model.ts | 10 +++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index a34ab3c..2235da2 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -72,8 +72,11 @@ export async function onDetect(detectionResult: DetectionResult): Promise 0 means that there was at least 1 merge - if(insertionResult.removedIds.length > 0) { + + if(!insertionResult.anyNewSegments) { + if(insertionResult.removedIds.length > 0) { + console.log('All found segments are merged with the existing ones'); + } return []; } @@ -86,6 +89,7 @@ export async function onDetect(detectionResult: DetectionResult): Promise { const analyticUnit = await AnalyticUnit.findById(detectionResult.analyticUnitId); + console.log('Webhook detection result:'); const segments = await onDetect(detectionResult); if(!_.isEmpty(segments) && analyticUnit.alert) { try { diff --git a/server/src/models/segment_model.ts b/server/src/models/segment_model.ts index 53ecfd1..bc219d2 100644 --- a/server/src/models/segment_model.ts +++ b/server/src/models/segment_model.ts @@ -156,10 +156,11 @@ export async function findIntersectedSegments( */ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ addedIds: SegmentId[], - removedIds: SegmentId[] + removedIds: SegmentId[], + anyNewSegments: boolean }> { if(_.isEmpty(segments)) { - return { addedIds: [], removedIds: [] }; + return { addedIds: [], removedIds: [], anyNewSegments: false }; } const analyticUnitId: AnalyticUnitId = segments[0].analyticUnitId; const unit = await AnalyticUnit.findById(analyticUnitId); @@ -173,6 +174,7 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ let segmentIdsToRemove: SegmentId[] = []; let segmentsToInsert: Segment[] = []; + let anyNewSegments = false; for(let segment of segments) { if(await isIntersectedWithExistingLabeled(segment)) { continue; @@ -229,6 +231,7 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ segmentIdsToRemove = segmentIdsToRemove.concat(_.compact(intersectedIds)); segmentsToInsert.push(newSegment); } else { + anyNewSegments = true; segmentsToInsert.push(segment); } } @@ -237,7 +240,8 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ const addedIds = await db.insertMany(segmentsToInsert.map(s => s.toObject())); return { addedIds, - removedIds: segmentIdsToRemove + removedIds: segmentIdsToRemove, + anyNewSegments }; }