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

Adding extract function to maps rule type #106733

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/plugins/data/server/saved_objects/index_patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { INDEX_PATTERN_SAVED_OBJECT_TYPE } from '../../common';
export const indexPatternSavedObjectType: SavedObjectsType = {
name: INDEX_PATTERN_SAVED_OBJECT_TYPE,
hidden: false,
namespaceType: 'single',
namespaceType: 'multiple-isolated',
convertToMultiNamespaceTypeVersion: '8.0.0',
management: {
icon: 'indexPatternApp',
defaultSearchField: 'title',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { i18n } from '@kbn/i18n';
import { schema } from '@kbn/config-schema';
import { Logger } from 'src/core/server';
import { Logger, SavedObjectReference } from 'src/core/server';
import { STACK_ALERTS_FEATURE_ID } from '../../../common';
import { getGeoContainmentExecutor } from './geo_containment';
import {
Expand All @@ -16,6 +16,7 @@ import {
AlertInstanceState,
AlertInstanceContext,
AlertTypeParams,
RuleParamsAndRefs,
} from '../../../../alerting/server';
import { Query } from '../../../../../../src/plugins/data/common/query';

Expand Down Expand Up @@ -117,6 +118,21 @@ export interface GeoContainmentParams extends AlertTypeParams {
indexQuery?: Query;
boundaryIndexQuery?: Query;
}
export interface GeoContainmentExtractedParams extends AlertTypeParams {
index: string;
indexRef: string;
geoField: string;
entity: string;
dateField: string;
boundaryType: string;
boundaryIndexTitle: string;
boundaryIndexRef: string;
boundaryGeoField: string;
boundaryNameField?: string;
indexQuery?: Query;
boundaryIndexQuery?: Query;
}

export interface GeoContainmentState extends AlertTypeState {
shapesFilters: Record<string, unknown>;
shapesIdsNamesMap: Record<string, unknown>;
Expand All @@ -140,7 +156,7 @@ export interface GeoContainmentInstanceContext extends AlertInstanceContext {

export type GeoContainmentAlertType = AlertType<
GeoContainmentParams,
never, // Only use if defining useSavedObjectReferences hook
GeoContainmentExtractedParams,
GeoContainmentState,
GeoContainmentInstanceState,
GeoContainmentInstanceContext,
Expand Down Expand Up @@ -179,5 +195,50 @@ export function getAlertType(logger: Logger): GeoContainmentAlertType {
actionVariables,
minimumLicenseRequired: 'gold',
isExportable: true,
useSavedObjectReferences: {
extractReferences: (
params: GeoContainmentParams
): RuleParamsAndRefs<GeoContainmentExtractedParams> => {
const { indexId, boundaryIndexId, ...otherParams } = params;

const references = [
{
name: `indexRef_0`,
id: indexId,
type: 'index-pattern',
},
{
name: `indexRef_1`,
id: boundaryIndexId,
type: 'index-pattern',
},
];
return {
params: { ...otherParams, indexRef: `indexRef_0`, boundaryIndexRef: `indexRef_1` },
references,
};
},
injectReferences: (
params: GeoContainmentExtractedParams,
references: SavedObjectReference[]
) => {
const { indexRef, boundaryIndexRef, ...otherParams } = params;
const indexReference = references.find((ref) => ref.name === indexRef);
const boundaryIndexReference = references.find((ref) => ref.name === boundaryIndexRef);
if (!indexReference) {
throw new Error(`Index reference "${indexRef}" not found in references array`);
}
if (!boundaryIndexReference) {
throw new Error(
`Boundary index reference "${boundaryIndexReference}" not found in references array`
);
}
return {
...otherParams,
indexId: indexReference.id,
boundaryIndexId: boundaryIndexReference.id,
} as GeoContainmentParams;
},
},
};
}