Browse Source

Fix anomaly detector webhooks (#940)

pull/1/head
rozetko 3 years ago committed by GitHub
parent
commit
711322192f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      server/src/controllers/analytics_controller.ts
  2. 10
      server/src/models/segment_model.ts

8
server/src/controllers/analytics_controller.ts

@ -72,8 +72,11 @@ export async function onDetect(detectionResult: DetectionResult): Promise<Segmen
AnalyticUnitCache.setData(id, payload.cache), AnalyticUnitCache.setData(id, payload.cache),
AnalyticUnit.setDetectionTime(id, payload.lastDetectionTime), AnalyticUnit.setDetectionTime(id, payload.lastDetectionTime),
]); ]);
// removedIds.length > 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 []; return [];
} }
@ -86,6 +89,7 @@ export async function onDetect(detectionResult: DetectionResult): Promise<Segmen
*/ */
async function onPushDetect(detectionResult: DetectionResult): Promise<void> { async function onPushDetect(detectionResult: DetectionResult): Promise<void> {
const analyticUnit = await AnalyticUnit.findById(detectionResult.analyticUnitId); const analyticUnit = await AnalyticUnit.findById(detectionResult.analyticUnitId);
console.log('Webhook detection result:');
const segments = await onDetect(detectionResult); const segments = await onDetect(detectionResult);
if(!_.isEmpty(segments) && analyticUnit.alert) { if(!_.isEmpty(segments) && analyticUnit.alert) {
try { try {

10
server/src/models/segment_model.ts

@ -156,10 +156,11 @@ export async function findIntersectedSegments(
*/ */
export async function mergeAndInsertSegments(segments: Segment[]): Promise<{ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{
addedIds: SegmentId[], addedIds: SegmentId[],
removedIds: SegmentId[] removedIds: SegmentId[],
anyNewSegments: boolean
}> { }> {
if(_.isEmpty(segments)) { if(_.isEmpty(segments)) {
return { addedIds: [], removedIds: [] }; return { addedIds: [], removedIds: [], anyNewSegments: false };
} }
const analyticUnitId: AnalyticUnitId = segments[0].analyticUnitId; const analyticUnitId: AnalyticUnitId = segments[0].analyticUnitId;
const unit = await AnalyticUnit.findById(analyticUnitId); const unit = await AnalyticUnit.findById(analyticUnitId);
@ -173,6 +174,7 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{
let segmentIdsToRemove: SegmentId[] = []; let segmentIdsToRemove: SegmentId[] = [];
let segmentsToInsert: Segment[] = []; let segmentsToInsert: Segment[] = [];
let anyNewSegments = false;
for(let segment of segments) { for(let segment of segments) {
if(await isIntersectedWithExistingLabeled(segment)) { if(await isIntersectedWithExistingLabeled(segment)) {
continue; continue;
@ -229,6 +231,7 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{
segmentIdsToRemove = segmentIdsToRemove.concat(_.compact(intersectedIds)); segmentIdsToRemove = segmentIdsToRemove.concat(_.compact(intersectedIds));
segmentsToInsert.push(newSegment); segmentsToInsert.push(newSegment);
} else { } else {
anyNewSegments = true;
segmentsToInsert.push(segment); segmentsToInsert.push(segment);
} }
} }
@ -237,7 +240,8 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{
const addedIds = await db.insertMany(segmentsToInsert.map(s => s.toObject())); const addedIds = await db.insertMany(segmentsToInsert.map(s => s.toObject()));
return { return {
addedIds, addedIds,
removedIds: segmentIdsToRemove removedIds: segmentIdsToRemove,
anyNewSegments
}; };
} }

Loading…
Cancel
Save