Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] Add 'crossed' & 'exited' events to tracking alert #82463

Merged
merged 9 commits into from
Nov 18, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,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 @@ -7,6 +7,7 @@
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 @@ -253,27 +256,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);
});
});
});