forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Maps] Use SO-references for geo-containment alerts (elastic#114559)
- Loading branch information
1 parent
b25dd4e
commit c41007e
Showing
9 changed files
with
428 additions
and
27 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { extractEntityAndBoundaryReferences } from './migrations'; | ||
|
||
describe('geo_containment migration utilities', () => { | ||
test('extractEntityAndBoundaryReferences', () => { | ||
expect( | ||
extractEntityAndBoundaryReferences({ | ||
index: 'foo*', | ||
indexId: 'foobar', | ||
geoField: 'geometry', | ||
entity: 'vehicle_id', | ||
dateField: '@timestamp', | ||
boundaryType: 'entireIndex', | ||
boundaryIndexTitle: 'boundary*', | ||
boundaryIndexId: 'boundaryid', | ||
boundaryGeoField: 'geometry', | ||
}) | ||
).toEqual({ | ||
params: { | ||
boundaryGeoField: 'geometry', | ||
boundaryIndexRefName: 'boundary_index_boundaryid', | ||
boundaryIndexTitle: 'boundary*', | ||
boundaryType: 'entireIndex', | ||
dateField: '@timestamp', | ||
entity: 'vehicle_id', | ||
geoField: 'geometry', | ||
index: 'foo*', | ||
indexRefName: 'tracked_index_foobar', | ||
}, | ||
references: [ | ||
{ | ||
id: 'foobar', | ||
name: 'param:tracked_index_foobar', | ||
type: 'index-pattern', | ||
}, | ||
{ | ||
id: 'boundaryid', | ||
name: 'param:boundary_index_boundaryid', | ||
type: 'index-pattern', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
93 changes: 93 additions & 0 deletions
93
x-pack/plugins/alerting/server/saved_objects/geo_containment/migrations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
SavedObjectAttributes, | ||
SavedObjectReference, | ||
SavedObjectUnsanitizedDoc, | ||
} from 'kibana/server'; | ||
import { AlertTypeParams } from '../../index'; | ||
import { Query } from '../../../../../../src/plugins/data/common/query'; | ||
import { RawAlert } from '../../types'; | ||
|
||
// These definitions are dupes of the SO-types in stack_alerts/geo_containment | ||
// There are not exported to avoid deep imports from stack_alerts plugins into here | ||
const GEO_CONTAINMENT_ID = '.geo-containment'; | ||
interface GeoContainmentParams extends AlertTypeParams { | ||
index: string; | ||
indexId: string; | ||
geoField: string; | ||
entity: string; | ||
dateField: string; | ||
boundaryType: string; | ||
boundaryIndexTitle: string; | ||
boundaryIndexId: string; | ||
boundaryGeoField: string; | ||
boundaryNameField?: string; | ||
indexQuery?: Query; | ||
boundaryIndexQuery?: Query; | ||
} | ||
type GeoContainmentExtractedParams = Omit<GeoContainmentParams, 'indexId' | 'boundaryIndexId'> & { | ||
indexRefName: string; | ||
boundaryIndexRefName: string; | ||
}; | ||
|
||
export function extractEntityAndBoundaryReferences(params: GeoContainmentParams): { | ||
params: GeoContainmentExtractedParams; | ||
references: SavedObjectReference[]; | ||
} { | ||
const { indexId, boundaryIndexId, ...otherParams } = params; | ||
|
||
const indexRefNamePrefix = 'tracked_index_'; | ||
const boundaryRefNamePrefix = 'boundary_index_'; | ||
|
||
// Since these are stack-alerts, we need to prefix with the `param:`-namespace | ||
const references = [ | ||
{ | ||
name: `param:${indexRefNamePrefix}${indexId}`, | ||
type: `index-pattern`, | ||
id: indexId as string, | ||
}, | ||
{ | ||
name: `param:${boundaryRefNamePrefix}${boundaryIndexId}`, | ||
type: 'index-pattern', | ||
id: boundaryIndexId as string, | ||
}, | ||
]; | ||
return { | ||
params: { | ||
...otherParams, | ||
indexRefName: `${indexRefNamePrefix}${indexId}`, | ||
boundaryIndexRefName: `${boundaryRefNamePrefix}${boundaryIndexId}`, | ||
}, | ||
references, | ||
}; | ||
} | ||
|
||
export function extractRefsFromGeoContainmentAlert( | ||
doc: SavedObjectUnsanitizedDoc<RawAlert> | ||
): SavedObjectUnsanitizedDoc<RawAlert> { | ||
if (doc.attributes.alertTypeId !== GEO_CONTAINMENT_ID) { | ||
return doc; | ||
} | ||
|
||
const { | ||
attributes: { params }, | ||
} = doc; | ||
|
||
const { params: newParams, references } = extractEntityAndBoundaryReferences( | ||
params as GeoContainmentParams | ||
); | ||
return { | ||
...doc, | ||
attributes: { | ||
...doc.attributes, | ||
params: newParams as SavedObjectAttributes, | ||
}, | ||
references: [...(doc.references || []), ...references], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.