Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Nov 8, 2024
1 parent a186a60 commit b046148
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 59 deletions.
20 changes: 14 additions & 6 deletions x-pack/plugins/entity_manager/public/lib/entity_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
isHttpFetchError,
} from '@kbn/server-route-repository-client';
import { type KueryNode, nodeTypes, toKqlExpression } from '@kbn/es-query';
import type { EntityInstance } from '@kbn/entities-schema';
import type { EntityInstance, EntityMetadata } from '@kbn/entities-schema';
import { castArray } from 'lodash';
import {
DisableManagedEntityResponse,
Expand Down Expand Up @@ -87,8 +87,12 @@ export class EntityClient {
}
}

asKqlFilter(entityLatest: EntityInstance) {
const identityFieldsValue = this.getIdentityFieldsValue(entityLatest);
asKqlFilter(
entityInstance: {
entity: Pick<EntityInstance['entity'], 'identity_fields'>;
} & Required<EntityMetadata>
) {
const identityFieldsValue = this.getIdentityFieldsValue(entityInstance);

const nodes: KueryNode[] = Object.entries(identityFieldsValue).map(([identityField, value]) => {
return nodeTypes.function.buildNode('is', identityField, value);
Expand All @@ -101,8 +105,12 @@ export class EntityClient {
return toKqlExpression(kqlExpression);
}

getIdentityFieldsValue(entityLatest: EntityInstance) {
const { identity_fields: identityFields } = entityLatest.entity;
getIdentityFieldsValue(
entityInstance: {
entity: Pick<EntityInstance['entity'], 'identity_fields'>;
} & Required<EntityMetadata>
) {
const { identity_fields: identityFields } = entityInstance.entity;

if (!identityFields) {
throw new Error('Identity fields are missing');
Expand All @@ -111,7 +119,7 @@ export class EntityClient {
return castArray(identityFields).reduce((acc, field) => {
const value = field.split('.').reduce((obj: any, part: string) => {
return obj && typeof obj === 'object' ? (obj as Record<string, any>)[part] : undefined;
}, entityLatest);
}, entityInstance);

if (value) {
acc[field] = value;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import rison from '@kbn/rison';
import { EuiBadge, EuiToolTip } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import type { InventoryEntity } from '../../../common/entities';
import { toEntityLatest } from '../../../common/utils/mappers';
import { useKibana } from '../../hooks/use_kibana';

export function AlertsBadge({ entity }: { entity: InventoryEntity }) {
Expand All @@ -22,7 +21,12 @@ export function AlertsBadge({ entity }: { entity: InventoryEntity }) {

const activeAlertsHref = basePath.prepend(
`/app/observability/alerts?_a=${rison.encode({
kuery: entityManager.entityClient.asKqlFilter(toEntityLatest(entity)),
kuery: entityManager.entityClient.asKqlFilter({
entity: {
identity_fields: entity.entityIdentityFields,
},
...entity,
}),
status: 'active',
})}`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { DASHBOARD_APP_LOCATOR } from '@kbn/deeplinks-analytics';
import { castArray } from 'lodash';
import { isEntityOfType } from '../../common/utils/entity_type_guards';
import type { InventoryEntity } from '../../common/entities';
import { toEntityLatest } from '../../common/utils/mappers';
import { useKibana } from './use_kibana';

const KUBERNETES_DASHBOARDS_IDS: Record<string, string> = {
Expand All @@ -43,23 +42,25 @@ export const useDetailViewRedirect = () => {
const serviceOverviewLocator = locators.get<ServiceOverviewParams>(SERVICE_OVERVIEW_LOCATOR_ID);

const getDetailViewRedirectUrl = useCallback(
(latestEntity: InventoryEntity) => {
const entityFields = entityManager.entityClient.getIdentityFieldsValue(
toEntityLatest(latestEntity)
);
const identityFields = castArray(latestEntity.entityIdentityFields);
(entity: InventoryEntity) => {
const entityFields = entityManager.entityClient.getIdentityFieldsValue({
entity: {
identity_fields: entity.entityIdentityFields,
},
});
const identityFields = castArray(entity.entityIdentityFields);

if (isEntityOfType('host', latestEntity) || isEntityOfType('container', latestEntity)) {
if (isEntityOfType('host', entity) || isEntityOfType('container', entity)) {
return assetDetailsLocator?.getRedirectUrl({
assetId: entityFields[identityFields[0]],
assetType: latestEntity.entityType,
assetType: entity.entityType,
});
}

if (isEntityOfType('service', latestEntity)) {
if (isEntityOfType('service', entity)) {
return serviceOverviewLocator?.getRedirectUrl({
serviceName: entityFields[identityFields[0]],
environment: latestEntity.service?.environment,
environment: entity.service?.environment,
});
}

Expand All @@ -69,16 +70,21 @@ export const useDetailViewRedirect = () => {
);

const getDashboardRedirectUrl = useCallback(
(latestEntity: InventoryEntity) => {
const type = latestEntity.entityType;
(entity: InventoryEntity) => {
const type = entity.entityType;
const dashboardId = KUBERNETES_DASHBOARDS_IDS[type];

return dashboardId
? dashboardLocator?.getRedirectUrl({
dashboardId,
query: {
language: 'kuery',
query: entityManager.entityClient.asKqlFilter(toEntityLatest(latestEntity)),
query: entityManager.entityClient.asKqlFilter({
entity: {
identity_fields: entity.entityIdentityFields,
},
...entity,
}),
},
})
: undefined;
Expand Down

0 comments on commit b046148

Please sign in to comment.