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

[Entity Store] Do not require full entity definition to execute enrich policy (remove magic number) #195961

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import type { EnrichPutPolicyRequest } from '@elastic/elasticsearch/lib/api/type
import { getEntitiesIndexName } from '../utils';
import type { UnitedEntityDefinition } from '../united_entity_definitions';

type DefinitionMetadata = Pick<UnitedEntityDefinition, 'namespace' | 'entityType' | 'version'>;

export const getFieldRetentionEnrichPolicyName = ({
namespace,
entityType,
version,
}: Pick<UnitedEntityDefinition, 'namespace' | 'entityType' | 'version'>): string => {
}: DefinitionMetadata): string => {
return `entity_store_field_retention_${entityType}_${namespace}_v${version}`;
};

Expand Down Expand Up @@ -48,7 +50,7 @@ export const executeFieldRetentionEnrichPolicy = async ({
unitedDefinition,
logger,
}: {
unitedDefinition: UnitedEntityDefinition;
unitedDefinition: DefinitionMetadata;
esClient: ElasticsearchClient;
logger: Logger;
}): Promise<{ executed: boolean }> => {
Expand All @@ -72,7 +74,7 @@ export const deleteFieldRetentionEnrichPolicy = async ({
esClient,
}: {
esClient: ElasticsearchClient;
unitedDefinition: UnitedEntityDefinition;
unitedDefinition: DefinitionMetadata;
}) => {
const name = getFieldRetentionEnrichPolicyName(unitedDefinition);
return esClient.enrich.deletePolicy({ name }, { ignore: [404] });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
} from './state';
import { INTERVAL, SCOPE, TIMEOUT, TYPE, VERSION } from './constants';
import type { EntityAnalyticsRoutesDeps } from '../../types';
import { getAvailableEntityTypes, getUnitedEntityDefinition } from '../united_entity_definitions';
import {
getAvailableEntityTypes,
getUnitedEntityDefinitionVersion,
} from '../united_entity_definitions';
import { executeFieldRetentionEnrichPolicy } from '../elasticsearch_assets';

const logFactory =
Expand Down Expand Up @@ -63,13 +66,10 @@ export const registerEntityStoreFieldRetentionEnrichTask = ({
const [coreStart, _] = await getStartServices();
const esClient = coreStart.elasticsearch.client.asInternalUser;

const unitedDefinition = getUnitedEntityDefinition({
namespace,
entityType,
fieldHistoryLength: 10, // we are not using this value so it can be anything
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting rid of this magic number is the motivation of the changes

});
const unitedDefinitionVersion = getUnitedEntityDefinitionVersion(entityType);

return executeFieldRetentionEnrichPolicy({
unitedDefinition,
unitedDefinition: { namespace, entityType, version: unitedDefinitionVersion },
esClient,
logger,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import { collectValuesWithLength } from '../definition_utils';
import type { UnitedDefinitionBuilder } from '../types';

export const HOST_DEFINITION_VERSION = '1.0.0';
export const getHostUnitedDefinition: UnitedDefinitionBuilder = (fieldHistoryLength: number) => {
const collect = collectValuesWithLength(fieldHistoryLength);
return {
entityType: 'host',
version: '1.0.0',
version: HOST_DEFINITION_VERSION,
fields: [
collect({ field: 'host.domain' }),
collect({ field: 'host.hostname' }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* 2.0.
*/

export { getHostUnitedDefinition } from './host';
export { getUserUnitedDefinition } from './user';
export * from './host';
export * from './user';
export { getCommonUnitedFieldDefinitions } from './common';
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import { collectValuesWithLength } from '../definition_utils';
import type { UnitedDefinitionBuilder } from '../types';

export const USER_DEFINITION_VERSION = '1.0.0';
export const getUserUnitedDefinition: UnitedDefinitionBuilder = (fieldHistoryLength: number) => {
const collect = collectValuesWithLength(fieldHistoryLength);
return {
entityType: 'user',
version: '1.0.0',
version: USER_DEFINITION_VERSION,
fields: [
collect({ field: 'user.domain' }),
collect({ field: 'user.email' }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
getHostUnitedDefinition,
getUserUnitedDefinition,
getCommonUnitedFieldDefinitions,
USER_DEFINITION_VERSION,
HOST_DEFINITION_VERSION,
} from './entity_types';
import type { UnitedDefinitionBuilder } from './types';
import { UnitedEntityDefinition } from './united_entity_definition';
Expand Down Expand Up @@ -44,5 +46,8 @@ export const getUnitedEntityDefinition = memoize(
`${entityType}-${namespace}-${fieldHistoryLength}`
);

export const getUnitedEntityDefinitionVersion = (entityType: EntityType): string =>
entityType === 'host' ? HOST_DEFINITION_VERSION : USER_DEFINITION_VERSION;

export const getAvailableEntityTypes = (): EntityType[] =>
Object.keys(unitedDefinitionBuilders) as EntityType[];