Skip to content

Commit

Permalink
[Maps] Add 'crossed' & 'exited' events to tracking alert (elastic#82463)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Caldwell authored and chrisronline committed Nov 19, 2020
1 parent cd07732 commit 0dfb2c4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export const GeoThresholdAlertTypeExpression: React.FunctionComponent<AlertTypeP
}
fullWidth
onChange={(e) => setAlertParams('trackingEvent', e.target.value)}
options={[conditionOptions[0]]} // TODO: Make all options avab. before merge
options={conditionOptions}
/>
</div>
</EuiFormRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Query } from '../../../../../../src/plugins/data/common';
export enum TrackingEvent {
entered = 'entered',
exited = 'exited',
crossed = 'crossed',
}

export interface GeoThresholdAlertParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ export function getMovedEntities(
[]
)
// Do not track entries to or exits from 'other'
.filter((entityMovementDescriptor: EntityMovementDescriptor) =>
trackingEvent === 'entered'
? entityMovementDescriptor.currLocation.shapeId !== OTHER_CATEGORY
: entityMovementDescriptor.prevLocation.shapeId !== OTHER_CATEGORY
)
.filter((entityMovementDescriptor: EntityMovementDescriptor) => {
if (trackingEvent !== 'crossed') {
return trackingEvent === 'entered'
? entityMovementDescriptor.currLocation.shapeId !== OTHER_CATEGORY
: entityMovementDescriptor.prevLocation.shapeId !== OTHER_CATEGORY;
}
return true;
})
);
}

Expand Down Expand Up @@ -254,27 +257,36 @@ export const getGeoThresholdExecutor = (log: Logger) =>
movedEntities.forEach(({ entityName, currLocation, prevLocation }) => {
const toBoundaryName = shapesIdsNamesMap[currLocation.shapeId] || currLocation.shapeId;
const fromBoundaryName = shapesIdsNamesMap[prevLocation.shapeId] || prevLocation.shapeId;
services
.alertInstanceFactory(`${entityName}-${toBoundaryName || currLocation.shapeId}`)
.scheduleActions(ActionGroupId, {
entityId: entityName,
timeOfDetection: new Date(currIntervalEndTime).getTime(),
crossingLine: `LINESTRING (${prevLocation.location[0]} ${prevLocation.location[1]}, ${currLocation.location[0]} ${currLocation.location[1]})`,
let alertInstance;
if (params.trackingEvent === 'entered') {
alertInstance = `${entityName}-${toBoundaryName || currLocation.shapeId}`;
} else if (params.trackingEvent === 'exited') {
alertInstance = `${entityName}-${fromBoundaryName || prevLocation.shapeId}`;
} else {
// == 'crossed'
alertInstance = `${entityName}-${fromBoundaryName || prevLocation.shapeId}-${
toBoundaryName || currLocation.shapeId
}`;
}
services.alertInstanceFactory(alertInstance).scheduleActions(ActionGroupId, {
entityId: entityName,
timeOfDetection: new Date(currIntervalEndTime).getTime(),
crossingLine: `LINESTRING (${prevLocation.location[0]} ${prevLocation.location[1]}, ${currLocation.location[0]} ${currLocation.location[1]})`,

toEntityLocation: `POINT (${currLocation.location[0]} ${currLocation.location[1]})`,
toEntityDateTime: currLocation.date,
toEntityDocumentId: currLocation.docId,
toEntityLocation: `POINT (${currLocation.location[0]} ${currLocation.location[1]})`,
toEntityDateTime: currLocation.date,
toEntityDocumentId: currLocation.docId,

toBoundaryId: currLocation.shapeId,
toBoundaryName,
toBoundaryId: currLocation.shapeId,
toBoundaryName,

fromEntityLocation: `POINT (${prevLocation.location[0]} ${prevLocation.location[1]})`,
fromEntityDateTime: prevLocation.date,
fromEntityDocumentId: prevLocation.docId,
fromEntityLocation: `POINT (${prevLocation.location[0]} ${prevLocation.location[1]})`,
fromEntityDateTime: prevLocation.date,
fromEntityDocumentId: prevLocation.docId,

fromBoundaryId: prevLocation.shapeId,
fromBoundaryName,
});
fromBoundaryId: prevLocation.shapeId,
fromBoundaryName,
});
});

// Combine previous results w/ current results for state of next run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ describe('geo_threshold', () => {
});

describe('getMovedEntities', () => {
const trackingEvent = 'entered';
it('should return empty array if only movements were within same shapes', async () => {
const currLocationArr = [
{
Expand Down Expand Up @@ -133,7 +132,7 @@ describe('geo_threshold', () => {
shapeLocationId: 'sameShape2',
},
];
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, trackingEvent);
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, 'entered');
expect(movedEntities).toEqual([]);
});

Expand Down Expand Up @@ -170,7 +169,7 @@ describe('geo_threshold', () => {
shapeLocationId: 'thisOneDidntMove',
},
];
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, trackingEvent);
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, 'entered');
expect(movedEntities.length).toEqual(1);
});

Expand All @@ -193,7 +192,7 @@ describe('geo_threshold', () => {
shapeLocationId: 'oldShapeLocation',
},
];
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, trackingEvent);
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, 'entered');
expect(movedEntities).toEqual([]);
});

Expand All @@ -219,5 +218,51 @@ describe('geo_threshold', () => {
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, 'exited');
expect(movedEntities).toEqual([]);
});

it('should not ignore "crossed" results from "other"', async () => {
const currLocationArr = [
{
dateInShape: '2020-09-28T18:01:41.190Z',
docId: 'N-ng1XQB6yyY-xQxnGSM',
entityName: '936',
location: [-82.8814151789993, 41.62806099653244],
shapeLocationId: 'newShapeLocation',
},
];
const prevLocationArr = [
{
dateInShape: '2020-08-28T18:01:41.190Z',
docId: 'N-ng1XQB6yyY-xQxnGSM',
entityName: '936',
location: [-82.8814151789993, 40.62806099653244],
shapeLocationId: OTHER_CATEGORY,
},
];
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, 'crossed');
expect(movedEntities.length).toEqual(1);
});

it('should not ignore "crossed" results to "other"', async () => {
const currLocationArr = [
{
dateInShape: '2020-08-28T18:01:41.190Z',
docId: 'N-ng1XQB6yyY-xQxnGSM',
entityName: '936',
location: [-82.8814151789993, 40.62806099653244],
shapeLocationId: OTHER_CATEGORY,
},
];
const prevLocationArr = [
{
dateInShape: '2020-09-28T18:01:41.190Z',
docId: 'N-ng1XQB6yyY-xQxnGSM',
entityName: '936',
location: [-82.8814151789993, 41.62806099653244],
shapeLocationId: 'newShapeLocation',
},
];
const movedEntities = getMovedEntities(currLocationArr, prevLocationArr, 'crossed');
expect(movedEntities.length).toEqual(1);
});
});
});

0 comments on commit 0dfb2c4

Please sign in to comment.