Skip to content

Commit

Permalink
Refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Oct 25, 2024
1 parent 9b19596 commit 584a3b2
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export class K8sEntity extends Serializable<EntityFields> {
super({
...fields,
'entity.type': entityTypeWithSchema,
'entity.definitionId': `builtin_${entityTypeWithSchema}`,
'entity.identityFields': identityFields,
'entity.displayName': getDisplayName({ identityFields, fields }),
'entity.definition_id': `builtin_${entityTypeWithSchema}`,
'entity.identity_fields': identityFields,
'entity.display_name': getDisplayName({ identityFields, fields }),
});
}
}
Expand Down
74 changes: 56 additions & 18 deletions x-pack/plugins/entity_manager/public/lib/entity_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { coreMock } from '@kbn/core/public/mocks';

const commonEntityFields: EntityLatest = {
entity: {
lastSeenTimestamp: '2023-10-09T00:00:00Z',
last_seen_timestamp: '2023-10-09T00:00:00Z',
id: '1',
displayName: 'entity_name',
definitionId: 'entity_definition_id',
display_name: 'entity_name',
definition_id: 'entity_definition_id',
} as EntityLatest['entity'],
};

Expand All @@ -25,11 +25,11 @@ describe('EntityClient', () => {
});

describe('asKqlFilter', () => {
it('should return the value when identityFields is a single string', () => {
it('should return the value when indentity_fields is a single string', () => {
const entityLatest: EntityLatest = {
entity: {
...commonEntityFields.entity,
identityFields: ['service.name', 'service.environment'],
identity_fields: ['service.name', 'service.environment'],
type: 'service',
},
service: {
Expand All @@ -41,11 +41,11 @@ describe('EntityClient', () => {
expect(result).toEqual('service.name: my-service');
});

it('should return values when identityFields is an array of strings', () => {
it('should return values when indentity_fields is composed by multiple fields', () => {
const entityLatest: EntityLatest = {
entity: {
...commonEntityFields.entity,
identityFields: ['service.name', 'service.environment'],
identity_fields: ['service.name', 'service.environment'],
type: 'service',
},
service: {
Expand All @@ -58,6 +58,25 @@ describe('EntityClient', () => {
expect(result).toEqual('(service.name: my-service AND service.environment: staging)');
});

it('should return identity fields values when an indentity field value is an array', () => {
const entityLatest: EntityLatest = {
entity: {
...commonEntityFields.entity,
identity_fields: ['service.name', 'service.environment'],
type: 'service',
},
service: {
name: 'my-service',
environment: ['prod', 'staging', 'dev'],
},
};

const result = entityClient.asKqlFilter(entityLatest);
expect(result).toEqual(
'(service.name: my-service AND (service.environment: prod OR service.environment: staging OR service.environment: dev))'
);
});

it('should throw an error when identity fields are missing', () => {
const entityLatest: EntityLatest = {
...commonEntityFields,
Expand All @@ -70,7 +89,7 @@ describe('EntityClient', () => {
const entityLatest: EntityLatest = {
entity: {
...commonEntityFields.entity,
identityFields: ['host.name', 'foo.bar'],
identity_fields: ['host.name', 'foo.bar'],
},
host: {
name: 'my-host',
Expand All @@ -84,27 +103,27 @@ describe('EntityClient', () => {

describe('getIdentityFieldsValue', () => {
it('should return identity fields values', () => {
const serviceEntity: EntityLatest = {
const entityLatest: EntityLatest = {
entity: {
...commonEntityFields.entity,
identityFields: ['service.name', 'service.environment'],
identity_fields: ['service.name', 'service.environment'],
type: 'service',
},
service: {
name: 'my-service',
},
};

expect(entityClient.getIdentityFieldsValue(serviceEntity)).toEqual({
expect(entityClient.getIdentityFieldsValue(entityLatest)).toEqual({
'service.name': 'my-service',
});
});

it('should return identity fields values when indentity field is an array of string', () => {
const serviceEntity: EntityLatest = {
it('should return identity fields values when indentity_fields is composed by multiple fields', () => {
const entityLatest: EntityLatest = {
entity: {
...commonEntityFields.entity,
identityFields: ['service.name', 'service.environment'],
identity_fields: ['service.name', 'service.environment'],
type: 'service',
},
service: {
Expand All @@ -113,23 +132,42 @@ describe('EntityClient', () => {
},
};

expect(entityClient.getIdentityFieldsValue(serviceEntity)).toEqual({
expect(entityClient.getIdentityFieldsValue(entityLatest)).toEqual({
'service.name': 'my-service',
'service.environment': 'staging',
});
});

it('should return identity fields values when an indentity field value is an array', () => {
const entityLatest: EntityLatest = {
entity: {
...commonEntityFields.entity,
identity_fields: ['service.name', 'service.environment'],
type: 'service',
},
service: {
name: 'my-service',
environment: ['prod', 'staging', 'dev'],
},
};

expect(entityClient.getIdentityFieldsValue(entityLatest)).toEqual({
'service.name': 'my-service',
'service.environment': ['prod', 'staging', 'dev'],
});
});

it('should return identity fields when field is in the root', () => {
const serviceEntity: EntityLatest = {
const entityLatest: EntityLatest = {
entity: {
...commonEntityFields.entity,
identityFields: ['name'],
identity_fields: ['name'],
type: 'service',
},
name: 'foo',
};

expect(entityClient.getIdentityFieldsValue(serviceEntity)).toEqual({
expect(entityClient.getIdentityFieldsValue(entityLatest)).toEqual({
name: 'foo',
});
});
Expand Down
14 changes: 10 additions & 4 deletions x-pack/plugins/entity_manager/public/lib/entity_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ export class EntityClient {
asKqlFilter(entityLatest: EntityLatest) {
const identityFieldsValue = this.getIdentityFieldsValue(entityLatest);

const nodes: KueryNode[] = Object.entries(identityFieldsValue).map(([identityField, value]) =>
nodeTypes.function.buildNode('is', identityField, value)
);
const nodes: KueryNode[] = Object.entries(identityFieldsValue).map(([identityField, value]) => {
if (Array.isArray(value)) {
return nodeTypes.function.buildNode(
'or',
value.map((v) => nodeTypes.function.buildNode('is', identityField, v))
);
}
return nodeTypes.function.buildNode('is', identityField, value);
});

if (nodes.length === 0) return '';

Expand All @@ -105,7 +111,7 @@ export class EntityClient {
}

getIdentityFieldsValue(entityLatest: EntityLatest) {
const { identityFields } = entityLatest.entity;
const { identity_fields: identityFields } = entityLatest.entity;

if (!identityFields) {
throw new Error('Identity fields are missing');
Expand Down
12 changes: 0 additions & 12 deletions x-pack/plugins/observability_solution/apm/common/entities/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
*/

import * as z from '@kbn/zod';
import { EntityDataStreamType, EntityType } from '@kbn/observability-shared-plugin/common';
import { EntityDataStreamType, MANAGED_ENTITY_TYPE } from '@kbn/observability-shared-plugin/common';
import { useFetcher } from '../../../hooks/use_fetcher';

const EntityTypeSchema = z.union([z.literal(EntityType.HOST), z.literal(EntityType.CONTAINER)]);
const EntityTypeSchema = z.union([
z.literal(MANAGED_ENTITY_TYPE.HOST),
z.literal(MANAGED_ENTITY_TYPE.CONTAINER),
]);
const EntityDataStreamSchema = z.union([
z.literal(EntityDataStreamType.METRICS),
z.literal(EntityDataStreamType.LOGS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { schema } from '@kbn/config-schema';
import { METRICS_APP_ID } from '@kbn/deeplinks-observability/constants';
import { entityCentricExperience } from '@kbn/observability-plugin/common';
import { createObservabilityEsClient } from '@kbn/observability-utils/es/client/create_observability_es_client';
import { MANAGED_ENTITY_TYPE } from '@kbn/observability-shared-plugin/common';
import { getInfraMetricsClient } from '../../lib/helpers/get_infra_metrics_client';
import { InfraBackendLibs } from '../../lib/infra_types';
import { getDataStreamTypes } from './get_data_stream_types';
Expand All @@ -22,7 +23,10 @@ export const initEntitiesConfigurationRoutes = (libs: InfraBackendLibs) => {
path: '/api/infra/entities/{entityType}/{entityId}/summary',
validate: {
params: schema.object({
entityType: schema.oneOf([schema.literal('host'), schema.literal('container')]),
entityType: schema.oneOf([
schema.literal(MANAGED_ENTITY_TYPE.HOST),
schema.literal(MANAGED_ENTITY_TYPE.CONTAINER),
]),
entityId: schema.string(),
}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import { z } from '@kbn/zod';
import { ENTITY_LATEST, entitiesAliasPattern, entityLatestSchema } from '@kbn/entities-schema';
import { entityLatestSchema } from '@kbn/entities-schema';
import {
ENTITY_DEFINITION_ID,
ENTITY_DISPLAY_NAME,
Expand All @@ -30,10 +30,11 @@ export const defaultEntitySortField: EntityColumnIds = 'alertsCount';

export const MAX_NUMBER_OF_ENTITIES = 500;

export const ENTITIES_LATEST_ALIAS = entitiesAliasPattern({
type: '*',
dataset: ENTITY_LATEST,
});
export const ENTITIES_LATEST_ALIAS = '.entities.v1.latest*';
// entitiesAliasPattern({
// type: '*',
// dataset: ENTITY_LATEST,
// });

const entityArrayRt = t.array(t.string);
export const entityTypesRt = new t.Type<string[], string, unknown>(
Expand Down
Loading

0 comments on commit 584a3b2

Please sign in to comment.