import { SavedObjectAttributes , SavedObjectReference , SavedObjectUnsanitizedDoc , } from 'kibana/server' ; import { AlertTypeParams } from '../../index' ; import { Query } from '../../../../../../src/plugins/data/common/query' ; import { RawRule } from '../../types' ; 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 ) { const { indexId , boundaryIndexId , ... otherParams } = params ; const indexRefNamePrefix = 'tracked_index_' ; const boundaryRefNamePrefix = 'boundary_index_' ; 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 ) { 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 ] , } ; }