Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Oct 25, 2024
1 parent 592039b commit 07a0050
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ export function esqlResultToPlainObjects<T extends Record<string, any>>(
return result.values.map((row) => {
return row.reduce<Record<string, unknown>>((acc, value, index) => {
const column = result.columns[index];
if (!/(text$|keyword$)/.test(column.name)) {
acc[column.name] = value;

if (!column) {
return acc;
}

// Removes the type suffix from the column name
const name = column.name.replace(/\.(text|keyword)$/, '');
if (!acc[name]) {
acc[name] = value;
}

return acc;
}, {});
}) as T[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@
import { z } from '@kbn/zod';
import { ENTITY_LATEST, entitiesAliasPattern, entityLatestSchema } from '@kbn/entities-schema';
import {
AGENT_NAME,
CLOUD_PROVIDER,
CONTAINER_ID,
ENTITY_DEFINITION_ID,
ENTITY_DISPLAY_NAME,
ENTITY_ID,
ENTITY_IDENTITY_FIELDS,
ENTITY_LAST_SEEN,
ENTITY_TYPE,
HOST_NAME,
SERVICE_ENVIRONMENT,
SERVICE_NAME,
} from '@kbn/observability-shared-plugin/common';
import { isRight } from 'fp-ts/lib/Either';
import * as t from 'io-ts';
Expand Down Expand Up @@ -64,7 +58,7 @@ export const entityTypesRt = new t.Type<string[], string, unknown>(
(arr) => arr.join()
);

export interface BaseEntity {
export interface Entity {
[ENTITY_LAST_SEEN]: string;
[ENTITY_ID]: string;
[ENTITY_TYPE]: string;
Expand All @@ -75,30 +69,6 @@ export interface BaseEntity {
[key: string]: any;
}

/**
* These types are based on service, host and container from the built in definition.
*/
export interface ServiceEntity extends BaseEntity {
[ENTITY_TYPE]: 'service';
[SERVICE_NAME]: string;
[SERVICE_ENVIRONMENT]?: string | string[] | null;
[AGENT_NAME]: string | string[] | null;
}

export interface HostEntity extends BaseEntity {
[ENTITY_TYPE]: 'host';
[HOST_NAME]: string;
[CLOUD_PROVIDER]: string | string[] | null;
}

export interface ContainerEntity extends BaseEntity {
[ENTITY_TYPE]: 'container';
[CONTAINER_ID]: string;
[CLOUD_PROVIDER]: string | string[] | null;
}

export type Entity = ServiceEntity | HostEntity | ContainerEntity;

export type InventoryEntityLatest = z.infer<typeof entityLatestSchema> & {
alertsCount?: number;
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('getIdentityFields', () => {
it('should return a Map with unique entity types and their respective identity fields', () => {
const serviceEntity: Entity = {
'agent.name': 'node',
'entity.identity_fields': ['service.name', 'service.environment'],
[ENTITY_IDENTITY_FIELDS]: ['service.name', 'service.environment'],
'service.name': 'my-service',
'entity.type': 'service',
...commonEntityFields,
Expand Down

0 comments on commit 07a0050

Please sign in to comment.