Skip to content

Commit

Permalink
[ECO][Inventory v2] Ad hoc data view: Add get entities definition end…
Browse files Browse the repository at this point in the history
…point using sources (elastic#203424)

Closes elastic#202298 
⚠️ Depends on elastic#203246

## Summary

This PR changes the way we get the entity index patterns to v2. It
creates an endpoint part of the inventory API which returns the index
patterns by entity type.

## Testing

⚠️ Currently in order to test we need to create the definition using the
dev tools manually before this
[PR](elastic#203246) is merged

- Open Dev tools and add a definition for an existing entity type
(example with host)
 ```
POST kbn:/internal/entities/v2/definitions/sources
{
  "source": {
    "id": "host_id",
    "type_id": "host",
    "index_patterns": ["metrics-*", "metricbeat-*"],
    "identity_fields": ["host.name"],
    "metadata_fields": [],
    "filters": [],
    "timestamp_field": "@timestamp"
  }
 ```

### Test the endpoint: 
- Open Dev tools and add
` GET kbn:/internal/inventory/entity/definitions/sources?type=host `
- Response: 
<img width="408" alt="image"
src="https://github.com/user-attachments/assets/1c7416b0-50a4-4e63-8081-928ec7856ff8">

### Test in the UI
- After the previous steps add some host data (oblt cluster /
metricbeat) or use synthtrace (for example use `node scripts/synthtrace
infra_hosts_with_apm_hosts --scenarioOpts.numInstances=10`)
- Go to Inventory and expand the host group
- Click on the actions button for any host and click on the Discover
link
- The correct dataview should be selected based on the created
definition
The same can be done for other entity types


https://github.com/user-attachments/assets/c9c3a7ae-daff-4d4b-b1b7-898c3f1603c6

---------

Co-authored-by: Carlos Crespo <[email protected]>
  • Loading branch information
jennypavlova and crespocarlos authored Dec 11, 2024
1 parent c3f96bb commit 5bacf1f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ export const EntityActions = ({ entity, setShowActions }: Props) => {
? `inventoryEntityActionsButton-${entity.entityDisplayName}`
: 'inventoryEntityActionsButton';

const { getDiscoverEntitiesRedirectUrl, isEntityDefinitionLoading } = useDiscoverRedirect(entity);
const { getDiscoverEntitiesRedirectUrl, isEntityDefinitionIndexPatternsLoading } =
useDiscoverRedirect(entity);
const discoverUrl = getDiscoverEntitiesRedirectUrl();

const actions: React.ReactElement[] = [];

if (!discoverUrl && !isEntityDefinitionLoading) {
if (!discoverUrl && !isEntityDefinitionIndexPatternsLoading) {
setShowActions(false);
return null;
}

if (!isEntityDefinitionLoading) {
if (!isEntityDefinitionIndexPatternsLoading) {
actions.push(
<EuiContextMenuItem
data-test-subj="inventoryEntityActionExploreInDiscover"
Expand Down Expand Up @@ -65,7 +66,7 @@ export const EntityActions = ({ entity, setShowActions }: Props) => {
iconType="boxesHorizontal"
color="text"
onClick={togglePopover}
isLoading={isEntityDefinitionLoading}
isLoading={isEntityDefinitionIndexPatternsLoading}
/>
}
closePopover={closePopover}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
import { useCallback, useMemo } from 'react';
import type { InventoryEntity } from '../../common/entities';
import { useAdHocDataView } from './use_adhoc_data_view';
import { useFetchEntityDefinition } from './use_fetch_entity_definition';
import { useFetchEntityDefinitionIndexPattern } from './use_fetch_entity_definition_index_patterns';
import { useKibana } from './use_kibana';

export const useDiscoverRedirect = (entity: InventoryEntity) => {
const {
services: { share, application, entityManager },
} = useKibana();
const { entityDefinitions, isEntityDefinitionLoading } = useFetchEntityDefinition(
entity.entityDefinitionId
);
const { entityDefinitionIndexPatterns, isEntityDefinitionIndexPatternsLoading } =
useFetchEntityDefinitionIndexPattern(entity.entityType);

const title = useMemo(
() =>
!isEntityDefinitionLoading && entityDefinitions && entityDefinitions?.length > 0
? entityDefinitions[0]?.indexPatterns?.join(',')
!isEntityDefinitionIndexPatternsLoading && (entityDefinitionIndexPatterns ?? []).length > 0
? entityDefinitionIndexPatterns[0].join()
: '',
[entityDefinitions, isEntityDefinitionLoading]
[entityDefinitionIndexPatterns, isEntityDefinitionIndexPatternsLoading]
);

const { dataView } = useAdHocDataView(title);
Expand Down Expand Up @@ -54,5 +53,5 @@ export const useDiscoverRedirect = (entity: InventoryEntity) => {
entityManager.entityClient,
]);

return { getDiscoverEntitiesRedirectUrl, isEntityDefinitionLoading };
return { getDiscoverEntitiesRedirectUrl, isEntityDefinitionIndexPatternsLoading };
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { useInventoryAbortableAsync } from './use_inventory_abortable_async';
import { useKibana } from './use_kibana';

export const useFetchEntityDefinitionIndexPattern = (type: string) => {
const {
services: { inventoryAPIClient },
} = useKibana();

const { value = { definitionIndexPatterns: [] }, loading } = useInventoryAbortableAsync(
({ signal }) => {
return inventoryAPIClient.fetch('GET /internal/inventory/entity/definitions/sources', {
params: {
query: {
type,
},
},
signal,
});
},
[inventoryAPIClient]
);

return {
entityDefinitionIndexPatterns: value?.definitionIndexPatterns,
isEntityDefinitionIndexPatternsLoading: loading,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 * as t from 'io-ts';
import { createInventoryServerRoute } from '../create_inventory_server_route';

export const getEntityDefinitionSourceIndexPatternsByType = createInventoryServerRoute({
endpoint: 'GET /internal/inventory/entity/definitions/sources',
params: t.type({
query: t.type({
type: t.string,
}),
}),
options: {
tags: ['access:inventory'],
},
async handler({ context, params, request, plugins }) {
const [_coreContext, entityManagerStart] = await Promise.all([
context.core,
plugins.entityManager.start(),
]);
const { type } = params.query;
const entityManagerClient = await entityManagerStart.getScopedClient({ request });

const entityDefinitionsSource = await entityManagerClient.v2.readSourceDefinitions({ type });

return {
definitionIndexPatterns: entityDefinitionsSource.map(
(definition) => definition.index_patterns,
[]
),
};
},
});

export const entityDefinitionsRoutes = {
...getEntityDefinitionSourceIndexPatternsByType,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/

import { entitiesRoutes } from './entities/route';
import { entityDefinitionsRoutes } from './entity_definition/get_entity_definitions';
import { hasDataRoutes } from './has_data/route';

export function getGlobalInventoryServerRouteRepository() {
return {
...entitiesRoutes,
...entityDefinitionsRoutes,
...hasDataRoutes,
};
}
Expand Down

0 comments on commit 5bacf1f

Please sign in to comment.