Browse Source

Fix anomaly detector webhooks (#940)

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

6
server/src/controllers/analytics_controller.ts

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

10
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
};
}

Loading…
Cancel
Save