Skip to content

Commit

Permalink
update type considering the latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Nov 4, 2024
1 parent 5cdef5e commit ba6ffe0
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 189 deletions.
10 changes: 5 additions & 5 deletions x-pack/packages/kbn-entities-schema/src/schema/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { arrayOfStringsSchema } from './common';
export const entityBaseSchema = z.object({
id: z.string(),
type: z.string(),
identityFields: z.union([arrayOfStringsSchema, z.string()]),
displayName: z.string(),
identity_fields: z.union([arrayOfStringsSchema, z.string()]),
display_name: z.string(),
metrics: z.optional(z.record(z.string(), z.number())),
definitionVersion: z.string(),
schemaVersion: z.string(),
definitionId: z.string(),
definition_version: z.string(),
schema_version: z.string(),
definition_id: z.string(),
});

export interface MetadataRecord {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const commonEntityFields: EnitityInstance = {
id: '1',
display_name: 'entity_name',
definition_id: 'entity_definition_id',
} as EnitityInstance['entity'],
},
};

describe('EntityClient', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ export function getMockInventoryContext(): InventoryKibanaContext {

return {
...coreStart,
entityManager: {} as unknown as EntityManagerPublicPluginStart,
entityManager: {
entityClient: {
asKqlFilter: jest.fn(),
getIdentityFieldsValue() {
return 'entity_id';
},
},
} as unknown as EntityManagerPublicPluginStart,
observabilityShared: {} as unknown as ObservabilitySharedPluginStart,
inference: {} as unknown as InferencePublicStart,
share: {
Expand Down
36 changes: 0 additions & 36 deletions x-pack/plugins/observability_solution/inventory/common/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
import { z } from '@kbn/zod';
import { ENTITY_LATEST, entitiesAliasPattern, entityLatestSchema } from '@kbn/entities-schema';
import {
ENTITY_DEFINITION_ID,
ENTITY_DISPLAY_NAME,
ENTITY_ID,
ENTITY_IDENTITY_FIELDS,
ENTITY_LAST_SEEN,
ENTITY_TYPE,
} from '@kbn/observability-shared-plugin/common';
import { decode, encode } from '@kbn/rison';
import { isRight } from 'fp-ts/lib/Either';
import * as t from 'io-ts';
import { AgentName } from '@kbn/elastic-agent-utils';

export const entityColumnIdsRt = t.union([
t.literal(ENTITY_DISPLAY_NAME),
Expand Down Expand Up @@ -103,17 +99,6 @@ export const entityTypesRt = new t.Type<string[], string, unknown>(
(arr) => arr.join()
);

export interface Entity {
[ENTITY_LAST_SEEN]: string;
[ENTITY_ID]: string;
[ENTITY_TYPE]: string;
[ENTITY_DISPLAY_NAME]: string;
[ENTITY_DEFINITION_ID]: string;
[ENTITY_IDENTITY_FIELDS]: string | string[];
alertsCount?: number;
[key: string]: any;
}

export type EntityGroup = {
count: number;
} & {
Expand All @@ -123,24 +108,3 @@ export type EntityGroup = {
export type InventoryEntityLatest = z.infer<typeof entityLatestSchema> & {
alertsCount?: number;
};

export const isHostEntity = (
entity: InventoryEntityLatest
): entity is InventoryEntityLatest & { cloud?: { provider: string } } => {
return entity.entity.type === 'host';
};

export const isContainerEntity = (
entity: InventoryEntityLatest
): entity is InventoryEntityLatest & { cloud?: { provider: string } } => {
return entity.entity.type === 'container';
};

export const isServiceEntity = (
entity: InventoryEntityLatest
): entity is InventoryEntityLatest & {
agent?: { name: AgentName };
service: { name: string; environment: string };
} => {
return entity.entity.type === 'service';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { AgentName } from '@kbn/elastic-agent-utils';
import type { InventoryEntityLatest } from '../entities';

interface EntityMap {
host: InventoryEntityLatest & { cloud?: { provider?: string } };
container: InventoryEntityLatest & { cloud?: { provider?: string } };
service: InventoryEntityLatest & {
agent?: { name: AgentName };
service?: { name: string; environment?: string };
};
}

export const isEntityOfType = <T extends keyof EntityMap>(
type: T,
entity: InventoryEntityLatest
): entity is EntityMap[T] => {
return entity.entity.type === type;
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import { AlertsBadge } from './alerts_badge';
import { useKibana } from '../../hooks/use_kibana';
import type { Entity } from '../../../common/entities';
import type { InventoryEntityLatest } from '../../../common/entities';

jest.mock('../../hooks/use_kibana');
const useKibanaMock = useKibana as jest.Mock;

const commonEntityFields: Partial<InventoryEntityLatest['entity']> = {
last_seen_timestamp: 'foo',
id: '1',
definition_version: '1',
schema_version: '1',
};

describe('AlertsBadge', () => {
const mockAsKqlFilter = jest.fn();

Expand Down Expand Up @@ -40,16 +47,21 @@ describe('AlertsBadge', () => {
});

it('render alerts badge for a host entity', () => {
const entity: Entity = {
'entity.last_seen_timestamp': 'foo',
'entity.id': '1',
'entity.type': 'host',
'entity.display_name': 'foo',
'entity.identity_fields': 'host.name',
'host.name': 'foo',
'entity.definition_id': 'host',
'cloud.provider': null,
const entity: InventoryEntityLatest = {
entity: {
...(commonEntityFields as InventoryEntityLatest['entity']),
type: 'host',
display_name: 'foo',
identity_fields: 'host.name',
definition_id: 'host',
},
alertsCount: 1,
host: {
name: 'foo',
},
cloud: {
provider: null,
},
};
mockAsKqlFilter.mockReturnValue('host.name: foo');

Expand All @@ -60,16 +72,24 @@ describe('AlertsBadge', () => {
expect(screen.queryByTestId('inventoryAlertsBadgeLink')?.textContent).toEqual('1');
});
it('render alerts badge for a service entity', () => {
const entity: Entity = {
'entity.last_seen_timestamp': 'foo',
'agent.name': 'node',
'entity.id': '1',
'entity.type': 'service',
'entity.display_name': 'foo',
'entity.identity_fields': 'service.name',
'service.name': 'bar',
'entity.definition_id': 'host',
'cloud.provider': null,
const entity: InventoryEntityLatest = {
entity: {
...(commonEntityFields as InventoryEntityLatest['entity']),
type: 'service',
display_name: 'foo',
identity_fields: 'service.name',
definition_id: 'service',
},
service: {
name: 'bar',
},
agent: {
name: 'node',
},
cloud: {
provider: null,
},

alertsCount: 5,
};
mockAsKqlFilter.mockReturnValue('service.name: bar');
Expand All @@ -81,17 +101,24 @@ describe('AlertsBadge', () => {
expect(screen.queryByTestId('inventoryAlertsBadgeLink')?.textContent).toEqual('5');
});
it('render alerts badge for a service entity with multiple identity fields', () => {
const entity: Entity = {
'entity.last_seen_timestamp': 'foo',
'agent.name': 'node',
'entity.id': '1',
'entity.type': 'service',
'entity.display_name': 'foo',
'entity.identity_fields': ['service.name', 'service.environment'],
'service.name': 'bar',
'service.environment': 'prod',
'entity.definition_id': 'host',
'cloud.provider': null,
const entity: InventoryEntityLatest = {
entity: {
...(commonEntityFields as InventoryEntityLatest['entity']),
type: 'service',
display_name: 'foo',
identity_fields: ['service.name', 'service.environment'],
definition_id: 'service',
},
service: {
name: 'bar',
environment: 'prod',
},
agent: {
name: 'node',
},
cloud: {
provider: null,
},
alertsCount: 2,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EuiButton, EuiDataGridSorting, EuiFlexGroup, EuiFlexItem } from '@elast
import { Meta, Story } from '@storybook/react';
import { orderBy } from 'lodash';
import React, { useMemo, useState } from 'react';
import { ENTITY_LAST_SEEN, ENTITY_TYPE } from '@kbn/observability-shared-plugin/common';
import { ENTITY_LAST_SEEN } from '@kbn/observability-shared-plugin/common';
import { useArgs } from '@storybook/addons';
import { EntitiesGrid } from '.';
import { entitiesMock } from './mock/entities_mock';
Expand Down Expand Up @@ -45,7 +45,7 @@ export const Grid: Story<EntityGridStoriesArgs> = (args) => {
const filteredAndSortedItems = useMemo(
() =>
orderBy(
entityType ? entitiesMock.filter((mock) => mock[ENTITY_TYPE] === entityType) : entitiesMock,
entityType ? entitiesMock.filter((mock) => mock.entity.type === entityType) : entitiesMock,
sort.id,
sort.direction
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,24 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import { EntityName } from '.';
import { useDetailViewRedirect } from '../../../hooks/use_detail_view_redirect';
import { Entity } from '../../../../common/entities';
import {
ENTITY_DEFINITION_ID,
ENTITY_DISPLAY_NAME,
ENTITY_ID,
ENTITY_IDENTITY_FIELDS,
ENTITY_LAST_SEEN,
ENTITY_TYPE,
} from '@kbn/observability-shared-plugin/common';
import { InventoryEntityLatest } from '../../../../common/entities';

jest.mock('../../../hooks/use_detail_view_redirect');

const useDetailViewRedirectMock = useDetailViewRedirect as jest.Mock;

describe('EntityName', () => {
const mockEntity: Entity = {
[ENTITY_LAST_SEEN]: '2023-10-09T00:00:00Z',
[ENTITY_ID]: '1',
[ENTITY_DISPLAY_NAME]: 'entity_name',
[ENTITY_DEFINITION_ID]: 'entity_definition_id',
[ENTITY_IDENTITY_FIELDS]: ['service.name', 'service.environment'],
[ENTITY_TYPE]: 'service',
const mockEntity: InventoryEntityLatest = {
entity: {
last_seen_timestamp: '2023-10-09T00:00:00Z',
id: '1',
type: 'service',
display_name: 'entity_name',
identity_fields: ['service.name', 'service.environment'],
definition_id: 'entity_definition_id',
definition_version: '1.0.0',
schema_version: '1.0.0',
},
};

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { EuiFlexGroup, EuiFlexItem, EuiLink } from '@elastic/eui';
import React from 'react';
import React, { useCallback } from 'react';
import { useKibana } from '../../../hooks/use_kibana';
import { InventoryEntityLatest } from '../../../../common/entities';
import { EntityIcon } from '../../entity_icon';
import { useDetailViewRedirect } from '../../../hooks/use_detail_view_redirect';
Expand All @@ -27,7 +28,7 @@ export function EntityName({ entity }: EntityNameProps) {
const handleLinkClick = useCallback(() => {
telemetry.reportEntityViewClicked({
view_type: 'detail',
entity_type: entity['entity.type'],
entity_type: entity.entity.type,
});
}, [entity, telemetry]);

Expand All @@ -38,7 +39,7 @@ export function EntityName({ entity }: EntityNameProps) {
</EuiFlexItem>
<EuiFlexItem className="eui-textTruncate">
<span className="eui-textTruncate" data-test-subj="entityNameDisplayName">
{entity.entity.displayName}
{entity.entity.display_name}
</span>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ export function EntitiesGrid({
values={{
date: (
<FormattedDate
value={entity.entity.lastSeenTimestamp}
value={entity.entity.last_seen_timestamp}
month="short"
day="numeric"
year="numeric"
/>
),
time: (
<FormattedTime
value={entity.entity.lastSeenTimestamp}
value={entity.entity.last_seen_timestamp}
hour12={false}
hour="2-digit"
minute="2-digit"
Expand Down
Loading

0 comments on commit ba6ffe0

Please sign in to comment.