Skip to content

Commit

Permalink
Add enrichPolicyExecutionInterval to entity enablement and init APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoum committed Jan 21, 2025
1 parent aa7b0e9 commit 47697a4
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ export const InspectQuery = z.object({
response: z.array(z.string()),
dsl: z.array(z.string()),
});

/**
* Interval in which enrich policy runs. For example, `"1h"` means the rule runs every hour.
*/
export type Interval = z.infer<typeof Interval>;
export const Interval = z.string().regex(/^[1-9]\d*[smh]$/);
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,8 @@ components:
required:
- dsl
- response
Interval:
type: string
description: Interval in which enrich policy runs. For example, `"1h"` means the rule runs every hour.
pattern: '^[1-9]\d*[smh]$' # any number except zero followed by one of the suffixes 's', 'm', 'h'
example: '1h'
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { z } from '@kbn/zod';

import { IndexPattern, EntityType, EngineDescriptor } from './common.gen';
import { IndexPattern, EntityType, Interval, EngineDescriptor } from './common.gen';

export type InitEntityStoreRequestBody = z.infer<typeof InitEntityStoreRequestBody>;
export const InitEntityStoreRequestBody = z.object({
Expand All @@ -27,6 +27,7 @@ export const InitEntityStoreRequestBody = z.object({
indexPattern: IndexPattern.optional(),
filter: z.string().optional(),
entityTypes: z.array(EntityType).optional(),
enrichPolicyExecutionInterval: Interval.optional(),
});
export type InitEntityStoreRequestBodyInput = z.input<typeof InitEntityStoreRequestBody>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ paths:
type: array
items:
$ref: './common.schema.yaml#/components/schemas/EntityType'
enrichPolicyExecutionInterval:
$ref: './common.schema.yaml#/components/schemas/Interval'
responses:
'200':
description: Successful response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { z } from '@kbn/zod';

import { EntityType, IndexPattern, EngineDescriptor } from '../common.gen';
import { EntityType, IndexPattern, Interval, EngineDescriptor } from '../common.gen';

export type InitEntityEngineRequestParams = z.infer<typeof InitEntityEngineRequestParams>;
export const InitEntityEngineRequestParams = z.object({
Expand All @@ -35,6 +35,7 @@ export const InitEntityEngineRequestBody = z.object({
fieldHistoryLength: z.number().int().optional().default(10),
indexPattern: IndexPattern.optional(),
filter: z.string().optional(),
enrichPolicyExecutionInterval: Interval.optional(),
});
export type InitEntityEngineRequestBodyInput = z.input<typeof InitEntityEngineRequestBody>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ paths:
$ref: '../common.schema.yaml#/components/schemas/IndexPattern'
filter:
type: string
enrichPolicyExecutionInterval:
$ref: '../common.schema.yaml#/components/schemas/Interval'
responses:
'200':
description: Successful response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import {
import { CRITICALITY_VALUES } from '../asset_criticality/constants';
import { createEngineDescription } from './installation/engine_description';
import { convertToEntityManagerDefinition } from './entity_definitions/entity_manager_conversion';
import { DEFAULT_INTERVAL } from './task/constants';

// Workaround. TransformState type is wrong. The health type should be: TransformHealth from '@kbn/transform-plugin/common/types/transform_stats'
export interface TransformHealth extends estypes.TransformGetTransformStatsTransformStatsHealth {
Expand Down Expand Up @@ -204,6 +205,7 @@ export class EntityStoreDataClient {
filter = '',
fieldHistoryLength = 10,
entityTypes,
enrichPolicyExecutionInterval,
}: InitEntityStoreRequestBody,
{ pipelineDebugMode = false }: { pipelineDebugMode?: boolean } = {}
): Promise<InitEntityStoreResponse> {
Expand All @@ -225,7 +227,11 @@ export class EntityStoreDataClient {

const promises = enginesTypes.map((entity) =>
run(() =>
this.init(entity, { indexPattern, filter, fieldHistoryLength }, { pipelineDebugMode })
this.init(
entity,
{ indexPattern, filter, fieldHistoryLength, enrichPolicyExecutionInterval },
{ pipelineDebugMode }
)
)
);

Expand Down Expand Up @@ -283,7 +289,12 @@ export class EntityStoreDataClient {

public async init(
entityType: EntityType,
{ indexPattern = '', filter = '', fieldHistoryLength = 10 }: InitEntityEngineRequestBody,
{
indexPattern = '',
filter = '',
fieldHistoryLength = 10,
enrichPolicyExecutionInterval = DEFAULT_INTERVAL,
}: InitEntityEngineRequestBody,
{ pipelineDebugMode = false }: { pipelineDebugMode?: boolean } = {}
): Promise<InitEntityEngineResponse> {
const { experimentalFeatures } = this.options;
Expand Down Expand Up @@ -340,6 +351,7 @@ export class EntityStoreDataClient {
this.asyncSetup(
entityType,
fieldHistoryLength,
enrichPolicyExecutionInterval,
this.options.taskManager,
indexPattern,
filter,
Expand All @@ -355,6 +367,7 @@ export class EntityStoreDataClient {
private async asyncSetup(
entityType: EntityType,
fieldHistoryLength: number,
enrichPolicyExecutionInterval: string,
taskManager: TaskManagerStartContract,
indexPattern: string,
filter: string,
Expand Down Expand Up @@ -435,6 +448,7 @@ export class EntityStoreDataClient {
namespace,
logger,
taskManager,
interval: enrichPolicyExecutionInterval,
});

this.log(`debug`, entityType, `Started entity store field retention enrich task`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
export const SCOPE = ['securitySolution'];
export const TYPE = 'entity_store:field_retention:enrichment';
export const VERSION = '1.0.0';
export const INTERVAL = '1h';
export const DEFAULT_INTERVAL = '1h';
export const TIMEOUT = '10m';
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
stateSchemaByVersion,
type LatestTaskStateSchema as EntityStoreFieldRetentionTaskState,
} from './state';
import { INTERVAL, SCOPE, TIMEOUT, TYPE, VERSION } from './constants';
import { SCOPE, TIMEOUT, TYPE, VERSION } from './constants';
import type { EntityAnalyticsRoutesDeps } from '../../types';

import { executeFieldRetentionEnrichPolicy } from '../elasticsearch_assets';
Expand Down Expand Up @@ -120,10 +120,12 @@ export const startEntityStoreFieldRetentionEnrichTask = async ({
logger,
namespace,
taskManager,
interval,
}: {
logger: Logger;
namespace: string;
taskManager: TaskManagerStartContract;
interval: string;
}) => {
const taskId = getTaskId(namespace);
const log = logFactory(logger, taskId);
Expand All @@ -136,7 +138,7 @@ export const startEntityStoreFieldRetentionEnrichTask = async ({
taskType: getTaskName(),
scope: SCOPE,
schedule: {
interval: INTERVAL,
interval,
},
state: { ...defaultState, namespace },
params: { version: VERSION },
Expand Down Expand Up @@ -234,7 +236,7 @@ export const runTask = async ({

telemetry.reportEvent(FIELD_RETENTION_ENRICH_POLICY_EXECUTION_EVENT.eventType, {
duration: taskDurationInSeconds,
interval: INTERVAL,
interval: taskInstance.schedule?.interval,
});

// Track entity store usage
Expand Down

0 comments on commit 47697a4

Please sign in to comment.