From 8ba7131fcb6ea3c89a01e6c5269de777f1985155 Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Mon, 4 Nov 2024 12:08:12 +0100 Subject: [PATCH] Adjust group by feature --- .../src/lib/entities/kubernetes/index.ts | 10 +++++----- .../public/lib/entity_client.test.ts | 2 +- .../inventory_group_accordion.tsx | 3 ++- .../server/routes/entities/get_entity_groups.ts | 5 +---- .../server/routes/entities/get_entity_types.ts | 4 ++-- .../server/routes/has_data/get_has_data.ts | 15 +++++---------- 6 files changed, 16 insertions(+), 23 deletions(-) diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/index.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/index.ts index 6033533cf359b..6da1decaab9ab 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/index.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/index.ts @@ -55,11 +55,11 @@ export class K8sEntity extends Serializable { super({ ...fields, 'entity.type': entityTypeWithSchema, - 'entity.definitionId': `builtin_${entityTypeWithSchema}`, - 'entity.identityFields': identityFields, - 'entity.displayName': getDisplayName({ identityFields, fields }), - 'entity.definitionVersion': '1.0.0', - 'entity.schemaVersion': '1.0', + 'entity.definition_id': `builtin_${entityTypeWithSchema}`, + 'entity.identity_fields': identityFields, + 'entity.display_name': getDisplayName({ identityFields, fields }), + 'entity.definition_version': '1.0.0', + 'entity.schema_version': '1.0', }); } } diff --git a/x-pack/plugins/entity_manager/public/lib/entity_client.test.ts b/x-pack/plugins/entity_manager/public/lib/entity_client.test.ts index 0a13952a6d694..dbaf1205cdf98 100644 --- a/x-pack/plugins/entity_manager/public/lib/entity_client.test.ts +++ b/x-pack/plugins/entity_manager/public/lib/entity_client.test.ts @@ -14,7 +14,7 @@ const commonEntityFields: EnitityInstance = { id: '1', display_name: 'entity_name', definition_id: 'entity_definition_id', - }, + } as EnitityInstance['entity'], }; describe('EntityClient', () => { diff --git a/x-pack/plugins/observability_solution/inventory/public/components/grouped_inventory/inventory_group_accordion.tsx b/x-pack/plugins/observability_solution/inventory/public/components/grouped_inventory/inventory_group_accordion.tsx index 4c5d34e5a028f..f5b52603fb645 100644 --- a/x-pack/plugins/observability_solution/inventory/public/components/grouped_inventory/inventory_group_accordion.tsx +++ b/x-pack/plugins/observability_solution/inventory/public/components/grouped_inventory/inventory_group_accordion.tsx @@ -8,6 +8,7 @@ import React, { useCallback, useState } from 'react'; import { i18n } from '@kbn/i18n'; import { css } from '@emotion/react'; import { EuiAccordion, EuiPanel, EuiSpacer, EuiTitle, useEuiTheme } from '@elastic/eui'; +import { flattenObject } from '@kbn/observability-utils/object/flatten_object'; import { GroupedEntitiesGrid } from './grouped_entities_grid'; import type { EntityGroup } from '../../../common/entities'; import { InventoryPanelBadge } from './inventory_panel_badge'; @@ -29,7 +30,7 @@ export function InventoryGroupAccordion({ isLoading, }: InventoryGroupAccordionProps) { const { euiTheme } = useEuiTheme(); - const field = group[groupBy]; + const field = flattenObject(group)[groupBy]; const [open, setOpen] = useState(false); const onToggle = useCallback(() => { diff --git a/x-pack/plugins/observability_solution/inventory/server/routes/entities/get_entity_groups.ts b/x-pack/plugins/observability_solution/inventory/server/routes/entities/get_entity_groups.ts index b61f245f1aaf2..c978766fe46b0 100644 --- a/x-pack/plugins/observability_solution/inventory/server/routes/entities/get_entity_groups.ts +++ b/x-pack/plugins/observability_solution/inventory/server/routes/entities/get_entity_groups.ts @@ -7,7 +7,6 @@ import type { ObservabilityElasticsearchClient } from '@kbn/observability-utils/es/client/create_observability_es_client'; import { kqlQuery } from '@kbn/observability-utils/es/queries/kql_query'; -import { esqlResultToPlainObjects } from '@kbn/observability-utils/es/utils/esql_result_to_plain_objects'; import { ENTITY_TYPE } from '@kbn/observability-shared-plugin/common'; import { ScalarValue } from '@elastic/elasticsearch/lib/api/types'; import { @@ -44,7 +43,7 @@ export async function getEntityGroupsBy({ const limit = `LIMIT ${MAX_NUMBER_OF_ENTITIES}`; const query = [from, ...where, group, sort, limit].join(' | '); - const groups = await inventoryEsClient.esql('get_entities_groups', { + return inventoryEsClient.esql('get_entities_groups', { query, filter: { bool: { @@ -53,6 +52,4 @@ export async function getEntityGroupsBy({ }, params, }); - - return esqlResultToPlainObjects(groups); } diff --git a/x-pack/plugins/observability_solution/inventory/server/routes/entities/get_entity_types.ts b/x-pack/plugins/observability_solution/inventory/server/routes/entities/get_entity_types.ts index 32631e062d9b1..0815452702975 100644 --- a/x-pack/plugins/observability_solution/inventory/server/routes/entities/get_entity_types.ts +++ b/x-pack/plugins/observability_solution/inventory/server/routes/entities/get_entity_types.ts @@ -15,7 +15,7 @@ export async function getEntityTypes({ }: { inventoryEsClient: ObservabilityElasticsearchClient; }) { - const entityTypesEsqlResponse = await inventoryEsClient.esql<{ [ENTITY_TYPE]: string }>( + const entityTypesEsqlResponse = await inventoryEsClient.esql<{ entity: { type: string } }>( 'get_entity_types', { query: `FROM ${ENTITIES_LATEST_ALIAS} @@ -25,5 +25,5 @@ export async function getEntityTypes({ } ); - return entityTypesEsqlResponse.flatMap((types) => Object.values(types)); + return entityTypesEsqlResponse.map((response) => response.entity.type); } diff --git a/x-pack/plugins/observability_solution/inventory/server/routes/has_data/get_has_data.ts b/x-pack/plugins/observability_solution/inventory/server/routes/has_data/get_has_data.ts index d328a4f3b8d6f..ce9ca5611f0f2 100644 --- a/x-pack/plugins/observability_solution/inventory/server/routes/has_data/get_has_data.ts +++ b/x-pack/plugins/observability_solution/inventory/server/routes/has_data/get_has_data.ts @@ -16,19 +16,14 @@ export async function getHasData({ inventoryEsClient: ObservabilityElasticsearchClient; logger: Logger; }) { - try { - const esqlResults = await inventoryEsClient.esql<{ _count: number }>('get_has_data', { - query: `FROM ${ENTITIES_LATEST_ALIAS} + const esqlResults = await inventoryEsClient.esql<{ _count: number }>('get_has_data', { + query: `FROM ${ENTITIES_LATEST_ALIAS} | ${getBuiltinEntityDefinitionIdESQLWhereClause()} | STATS _count = COUNT(*) | LIMIT 1`, - }); + }); - const totalCount = esqlResults[0]._count; + const totalCount = esqlResults[0]._count; - return { hasData: totalCount > 0 }; - } catch (e) { - logger.error(e); - return { hasData: false }; - } + return { hasData: totalCount > 0 }; }