|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { i18n } from '@kbn/i18n'; |
| 8 | +import { schema } from '@kbn/config-schema'; |
| 9 | +import { Service } from '../../types'; |
| 10 | +import { BUILT_IN_ALERTS_FEATURE_ID } from '../../../common'; |
| 11 | +import { getGeoThresholdExecutor } from './geo_threshold'; |
| 12 | +import { |
| 13 | + ActionGroup, |
| 14 | + AlertServices, |
| 15 | + ActionVariable, |
| 16 | + AlertTypeState, |
| 17 | +} from '../../../../alerts/server'; |
| 18 | + |
| 19 | +export const GEO_THRESHOLD_ID = '.geo-threshold'; |
| 20 | +export type TrackingEvent = 'entered' | 'exited'; |
| 21 | +export const ActionGroupId = 'tracking threshold met'; |
| 22 | + |
| 23 | +const actionVariableContextToEntityDateTimeLabel = i18n.translate( |
| 24 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextToEntityDateTimeLabel', |
| 25 | + { |
| 26 | + defaultMessage: `The time the entity was detected in the current boundary`, |
| 27 | + } |
| 28 | +); |
| 29 | + |
| 30 | +const actionVariableContextFromEntityDateTimeLabel = i18n.translate( |
| 31 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextFromEntityDateTimeLabel', |
| 32 | + { |
| 33 | + defaultMessage: `The last time the entity was recorded in the previous boundary`, |
| 34 | + } |
| 35 | +); |
| 36 | + |
| 37 | +const actionVariableContextToEntityLocationLabel = i18n.translate( |
| 38 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextToEntityLocationLabel', |
| 39 | + { |
| 40 | + defaultMessage: 'The most recently captured location of the entity', |
| 41 | + } |
| 42 | +); |
| 43 | + |
| 44 | +const actionVariableContextCrossingLineLabel = i18n.translate( |
| 45 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextCrossingLineLabel', |
| 46 | + { |
| 47 | + defaultMessage: |
| 48 | + 'GeoJSON line connecting the two locations that were used to determine the crossing event', |
| 49 | + } |
| 50 | +); |
| 51 | + |
| 52 | +const actionVariableContextFromEntityLocationLabel = i18n.translate( |
| 53 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextFromEntityLocationLabel', |
| 54 | + { |
| 55 | + defaultMessage: 'The previously captured location of the entity', |
| 56 | + } |
| 57 | +); |
| 58 | + |
| 59 | +const actionVariableContextToBoundaryIdLabel = i18n.translate( |
| 60 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextCurrentBoundaryIdLabel', |
| 61 | + { |
| 62 | + defaultMessage: 'The current boundary id containing the entity (if any)', |
| 63 | + } |
| 64 | +); |
| 65 | + |
| 66 | +const actionVariableContextToBoundaryNameLabel = i18n.translate( |
| 67 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextToBoundaryNameLabel', |
| 68 | + { |
| 69 | + defaultMessage: 'The boundary (if any) the entity has crossed into and is currently located', |
| 70 | + } |
| 71 | +); |
| 72 | + |
| 73 | +const actionVariableContextFromBoundaryNameLabel = i18n.translate( |
| 74 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextFromBoundaryNameLabel', |
| 75 | + { |
| 76 | + defaultMessage: 'The boundary (if any) the entity has crossed from and was previously located', |
| 77 | + } |
| 78 | +); |
| 79 | + |
| 80 | +const actionVariableContextFromBoundaryIdLabel = i18n.translate( |
| 81 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextFromBoundaryIdLabel', |
| 82 | + { |
| 83 | + defaultMessage: 'The previous boundary id containing the entity (if any)', |
| 84 | + } |
| 85 | +); |
| 86 | + |
| 87 | +const actionVariableContextToEntityDocumentIdLabel = i18n.translate( |
| 88 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextCrossingDocumentIdLabel', |
| 89 | + { |
| 90 | + defaultMessage: 'The id of the crossing entity document', |
| 91 | + } |
| 92 | +); |
| 93 | + |
| 94 | +const actionVariableContextFromEntityDocumentIdLabel = i18n.translate( |
| 95 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextFromEntityDocumentIdLabel', |
| 96 | + { |
| 97 | + defaultMessage: 'The id of the crossing entity document', |
| 98 | + } |
| 99 | +); |
| 100 | + |
| 101 | +const actionVariableContextTimeOfDetectionLabel = i18n.translate( |
| 102 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextTimeOfDetectionLabel', |
| 103 | + { |
| 104 | + defaultMessage: 'The alert interval end time this change was recorded', |
| 105 | + } |
| 106 | +); |
| 107 | + |
| 108 | +const actionVariableContextEntityIdLabel = i18n.translate( |
| 109 | + 'xpack.alertingBuiltins.geoThreshold.actionVariableContextEntityIdLabel', |
| 110 | + { |
| 111 | + defaultMessage: 'The entity ID of the document that triggered the alert', |
| 112 | + } |
| 113 | +); |
| 114 | + |
| 115 | +const actionVariables = { |
| 116 | + context: [ |
| 117 | + // Alert-specific data |
| 118 | + { name: 'entityId', description: actionVariableContextEntityIdLabel }, |
| 119 | + { name: 'timeOfDetection', description: actionVariableContextTimeOfDetectionLabel }, |
| 120 | + { name: 'crossingLine', description: actionVariableContextCrossingLineLabel }, |
| 121 | + |
| 122 | + // Corresponds to a specific document in the entity-index |
| 123 | + { name: 'toEntityLocation', description: actionVariableContextToEntityLocationLabel }, |
| 124 | + { |
| 125 | + name: 'toEntityDateTime', |
| 126 | + description: actionVariableContextToEntityDateTimeLabel, |
| 127 | + }, |
| 128 | + { name: 'toEntityDocumentId', description: actionVariableContextToEntityDocumentIdLabel }, |
| 129 | + |
| 130 | + // Corresponds to a specific document in the boundary-index |
| 131 | + { name: 'toBoundaryId', description: actionVariableContextToBoundaryIdLabel }, |
| 132 | + { name: 'toBoundaryName', description: actionVariableContextToBoundaryNameLabel }, |
| 133 | + |
| 134 | + // Corresponds to a specific document in the entity-index (from) |
| 135 | + { name: 'fromEntityLocation', description: actionVariableContextFromEntityLocationLabel }, |
| 136 | + { name: 'fromEntityDateTime', description: actionVariableContextFromEntityDateTimeLabel }, |
| 137 | + { name: 'fromEntityDocumentId', description: actionVariableContextFromEntityDocumentIdLabel }, |
| 138 | + |
| 139 | + // Corresponds to a specific document in the boundary-index (from) |
| 140 | + { name: 'fromBoundaryId', description: actionVariableContextFromBoundaryIdLabel }, |
| 141 | + { name: 'fromBoundaryName', description: actionVariableContextFromBoundaryNameLabel }, |
| 142 | + ], |
| 143 | +}; |
| 144 | + |
| 145 | +export const ParamsSchema = schema.object({ |
| 146 | + index: schema.string({ minLength: 1 }), |
| 147 | + indexId: schema.string({ minLength: 1 }), |
| 148 | + geoField: schema.string({ minLength: 1 }), |
| 149 | + entity: schema.string({ minLength: 1 }), |
| 150 | + dateField: schema.string({ minLength: 1 }), |
| 151 | + trackingEvent: schema.string({ minLength: 1 }), |
| 152 | + boundaryType: schema.string({ minLength: 1 }), |
| 153 | + boundaryIndexTitle: schema.string({ minLength: 1 }), |
| 154 | + boundaryIndexId: schema.string({ minLength: 1 }), |
| 155 | + boundaryGeoField: schema.string({ minLength: 1 }), |
| 156 | + boundaryNameField: schema.maybe(schema.string({ minLength: 1 })), |
| 157 | + delayOffsetWithUnits: schema.maybe(schema.string({ minLength: 1 })), |
| 158 | +}); |
| 159 | + |
| 160 | +export interface GeoThresholdParams { |
| 161 | + index: string; |
| 162 | + indexId: string; |
| 163 | + geoField: string; |
| 164 | + entity: string; |
| 165 | + dateField: string; |
| 166 | + trackingEvent: string; |
| 167 | + boundaryType: string; |
| 168 | + boundaryIndexTitle: string; |
| 169 | + boundaryIndexId: string; |
| 170 | + boundaryGeoField: string; |
| 171 | + boundaryNameField?: string; |
| 172 | + delayOffsetWithUnits?: string; |
| 173 | +} |
| 174 | + |
| 175 | +export function getAlertType( |
| 176 | + service: Omit<Service, 'indexThreshold'> |
| 177 | +): { |
| 178 | + defaultActionGroupId: string; |
| 179 | + actionGroups: ActionGroup[]; |
| 180 | + executor: ({ |
| 181 | + previousStartedAt: currIntervalStartTime, |
| 182 | + startedAt: currIntervalEndTime, |
| 183 | + services, |
| 184 | + params, |
| 185 | + alertId, |
| 186 | + state, |
| 187 | + }: { |
| 188 | + previousStartedAt: Date | null; |
| 189 | + startedAt: Date; |
| 190 | + services: AlertServices; |
| 191 | + params: GeoThresholdParams; |
| 192 | + alertId: string; |
| 193 | + state: AlertTypeState; |
| 194 | + }) => Promise<AlertTypeState>; |
| 195 | + validate?: { |
| 196 | + params?: { |
| 197 | + validate: (object: unknown) => GeoThresholdParams; |
| 198 | + }; |
| 199 | + }; |
| 200 | + name: string; |
| 201 | + producer: string; |
| 202 | + id: string; |
| 203 | + actionVariables?: { |
| 204 | + context?: ActionVariable[]; |
| 205 | + state?: ActionVariable[]; |
| 206 | + params?: ActionVariable[]; |
| 207 | + }; |
| 208 | +} { |
| 209 | + const alertTypeName = i18n.translate('xpack.alertingBuiltins.geoThreshold.alertTypeTitle', { |
| 210 | + defaultMessage: 'Geo tracking threshold', |
| 211 | + }); |
| 212 | + |
| 213 | + const actionGroupName = i18n.translate( |
| 214 | + 'xpack.alertingBuiltins.geoThreshold.actionGroupThresholdMetTitle', |
| 215 | + { |
| 216 | + defaultMessage: 'Tracking threshold met', |
| 217 | + } |
| 218 | + ); |
| 219 | + |
| 220 | + return { |
| 221 | + id: GEO_THRESHOLD_ID, |
| 222 | + name: alertTypeName, |
| 223 | + actionGroups: [{ id: ActionGroupId, name: actionGroupName }], |
| 224 | + defaultActionGroupId: ActionGroupId, |
| 225 | + executor: getGeoThresholdExecutor(service), |
| 226 | + producer: BUILT_IN_ALERTS_FEATURE_ID, |
| 227 | + validate: { |
| 228 | + params: ParamsSchema, |
| 229 | + }, |
| 230 | + actionVariables, |
| 231 | + }; |
| 232 | +} |
0 commit comments