From 1708cefaa7b3d5952311b3b86a0bf416614ccecc Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 25 Jan 2021 16:56:43 -0700 Subject: [PATCH] Cover case where 'other' and earlier entries are removed --- .../alert_types/geo_containment/geo_containment.ts | 10 +++++++++- .../geo_containment/tests/geo_containment.test.ts | 13 +++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts index c3afb92509457..22c5bd4ca563d 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts @@ -112,9 +112,17 @@ export function getActiveEntriesAndGenerateAlerts( alertInstanceFactory(alertInstanceId).scheduleActions(ActionGroupId, context); } }); - // If the latest location is "other", don't carry it through to the next interval + let otherIndex; if (locationsArr[0].shapeLocationId === OTHER_CATEGORY) { allActiveEntriesMap.delete(entityName); + } else if ( + locationsArr.some(({ shapeLocationId }, index) => { + otherIndex = index; + return shapeLocationId === OTHER_CATEGORY; + }) + ) { + const afterOtherLocationsArr = locationsArr.slice(0, otherIndex); + allActiveEntriesMap.set(entityName, afterOtherLocationsArr); } else { allActiveEntriesMap.set(entityName, locationsArr); } diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts index 4ba9412fd3c2d..c67acaf2cef1f 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/tests/geo_containment.test.ts @@ -403,7 +403,7 @@ describe('geo_containment', () => { expect(allActiveEntriesMap).toEqual(currLocationMap); }); - it('should return entity as active entry if "other" not the latest location', () => { + it('should return entity as active entry if "other" not the latest location but remove "other" and earlier entries', () => { const emptyPrevLocationMap = new Map(); const currLocationMapWithOther = new Map([...currLocationMap]).set('d', [ { @@ -432,7 +432,16 @@ describe('geo_containment', () => { emptyShapesIdsNamesMap, currentDateTime ); - expect(allActiveEntriesMap).toEqual(currLocationMapWithOther); + expect(allActiveEntriesMap).toEqual( + new Map([...currLocationMap]).set('d', [ + { + location: [0, 0], + shapeLocationId: '123', + dateInShape: 'Wed Dec 10 2020 14:31:31 GMT-0700 (Mountain Standard Time)', + docId: 'docId1', + }, + ]) + ); }); }); });