Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EEM][PoC] Add entity driven Dashboard action to view entity details #204970

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions x-pack/platform/plugins/shared/entity_manager/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
"security",
"encryptedSavedObjects",
"licensing",
"features"
"features",
"uiActions",
],
"requiredBundles": []
"requiredBundles": [
"data",
"dashboard",
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*
* 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 React, { useState } from 'react';
import { toMountPoint } from '@kbn/react-kibana-mount';
import { CoreStart } from '@kbn/core/public';
import {
EuiButtonIcon,
EuiCard,
EuiFlexGroup,
EuiFlexItem,
EuiIcon,
EuiModalBody,
EuiModalHeader,
EuiModalHeaderTitle,
} from '@elastic/eui';
import { isPhraseFilter, type Filter } from '@kbn/es-query';
import { DashboardCreationOptions, DashboardRenderer } from '@kbn/dashboard-plugin/public';
import { compact, isEqual } from 'lodash';
import { DashboardState } from '@kbn/dashboard-plugin/public/dashboard_api/types';
import { EntityClient } from './entity_client';

interface FilterTriggerContext {
filters: Filter[];
}

export function createEntityNavigationAction(coreStart: CoreStart, entityClient: EntityClient) {
return {
id: 'eem_navigation_action',
getDisplayName: () => 'Go to entity view',
getIconType: () => 'bullseye',
isCompatible: async (context: FilterTriggerContext) => {
try {
const filterFields = compact(
context.filters.filter(isPhraseFilter).map((filter) => filter.meta.key)
);

if (filterFields.length === 0) {
return false;
}

const types = await entityClient.repositoryClient(
'GET /internal/entities/v2/definitions/types'
);

if (types.types.length === 0) {
return false;
}

const sources = await entityClient.repositoryClient(
'GET /internal/entities/v2/definitions/sources'
);

if (sources.sources.length === 0) {
return false;
}

const typesWithSources = types.types.map((type) => ({
...type,
sources: sources.sources.filter((source) => source.type_id === type.id),
}));

return typesWithSources
.filter((type) => type.dashboard_id)
.some((typeWithSource) =>
typeWithSource.sources.some((source) => isEqual(source.identity_fields, filterFields))
);
} catch (error) {
coreStart.notifications.toasts.addError(error, { title: 'Unexpected error' });
return false;
}
},
execute: async (context: FilterTriggerContext) => {
try {
const filters = compact(
context.filters.filter(isPhraseFilter).map((filter) => ({
field: filter.meta.key,
value: filter.meta.params?.query,
}))
);

if (filters.length === 0) {
return;
}

const types = await entityClient.repositoryClient(
'GET /internal/entities/v2/definitions/types'
);

if (types.types.length === 0) {
return;
}

const sources = await entityClient.repositoryClient(
'GET /internal/entities/v2/definitions/sources'
);

if (sources.sources.length === 0) {
return;
}

const typesWithSources = types.types.map((type) => ({
...type,
sources: sources.sources.filter((source) => source.type_id === type.id),
}));

const matchingTypes = typesWithSources
.filter((type) => type.dashboard_id)
.filter((typeWithSource) =>
typeWithSource.sources.some((source) =>
isEqual(
source.identity_fields,
filters.map((filter) => filter.field)
)
)
);

if (matchingTypes.length === 0) {
return;
}

coreStart.overlays.openFlyout(
toMountPoint(
<EntityNavigationModal matchingTypes={matchingTypes} filters={context.filters} />,
coreStart
)
);
} catch (error) {
coreStart.notifications.toasts.addError(error, { title: 'Unexpected error' });
}
},
};
}

interface Type {
id: string;
display_name: string;
dashboard_id?: string;
}

interface EntityNavigationModalProps {
matchingTypes: Type[];
filters: Filter[];
}

function EntityNavigationModal({ matchingTypes, filters }: EntityNavigationModalProps) {
const [selectedType, setSelectedType] = useState(
matchingTypes.length === 1 ? matchingTypes[0] : undefined
);

async function getCreationOptions(): Promise<DashboardCreationOptions> {
function getInitialInput(): Partial<DashboardState> {
return { filters: filters.filter(isPhraseFilter) };
}

return {
getInitialInput,
};
}

if (selectedType) {
return (
<React.Fragment>
<EuiModalHeader>
<EuiModalHeaderTitle>
{matchingTypes.length > 1 ? (
<EuiButtonIcon
css={{ marginRight: '10px' }}
onClick={() => {
setSelectedType(undefined);
}}
iconType="arrowLeft"
aria-label="Back"
/>
) : null}
{selectedType.display_name}
</EuiModalHeaderTitle>
</EuiModalHeader>

<EuiModalBody>
<DashboardRenderer
savedObjectId={selectedType.dashboard_id}
getCreationOptions={getCreationOptions}
/>
</EuiModalBody>
</React.Fragment>
);
} else {
return (
<React.Fragment>
<EuiModalHeader>
<EuiModalHeaderTitle>Matching entity types</EuiModalHeaderTitle>
</EuiModalHeader>

<EuiModalBody>
<EuiFlexGroup gutterSize="l">
{matchingTypes.map((type) => (
<EuiFlexItem key={type.id}>
<EuiCard
icon={<EuiIcon size="xxl" type="sparkles" />}
title={type.display_name}
onClick={() => {
setSelectedType(type);
}}
/>
</EuiFlexItem>
))}
</EuiFlexGroup>
</EuiModalBody>
</React.Fragment>
);
}
}
14 changes: 11 additions & 3 deletions x-pack/platform/plugins/shared/entity_manager/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import { CoreSetup, CoreStart, PluginInitializerContext } from '@kbn/core/public';
import { Logger } from '@kbn/logging';
import { APPLY_FILTER_TRIGGER } from '@kbn/data-plugin/public';

import { EntityManagerPluginClass } from './types';
import { EntityManagerPluginClass, EntityManagerPublicPluginStartDependencies } from './types';
import type { EntityManagerPublicConfig } from '../common/config';
import { EntityClient } from './lib/entity_client';
import { createEntityNavigationAction } from './lib/entity_navigation_action';

export class Plugin implements EntityManagerPluginClass {
public config: EntityManagerPublicConfig;
Expand All @@ -27,9 +29,15 @@ export class Plugin implements EntityManagerPluginClass {
};
}

start(core: CoreStart) {
start(core: CoreStart, plugins: EntityManagerPublicPluginStartDependencies) {
const entityClient = new EntityClient(core);
plugins.uiActions.addTriggerAction(
APPLY_FILTER_TRIGGER,
createEntityNavigationAction(core, entityClient)
);

return {
entityClient: new EntityClient(core),
entityClient,
};
}

Expand Down
13 changes: 12 additions & 1 deletion x-pack/platform/plugins/shared/entity_manager/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/
import type { Plugin as PluginClass } from '@kbn/core/public';
import { UiActionsSetup, UiActionsStart } from '@kbn/ui-actions-plugin/public';
import type { EntityClient } from './lib/entity_client';

export interface EntityManagerPublicPluginSetup {
Expand All @@ -14,7 +15,17 @@ export interface EntityManagerPublicPluginStart {
entityClient: EntityClient;
}

export interface EntityManagerPublicPluginSetupDependencies {
uiActions: UiActionsSetup;
}

export interface EntityManagerPublicPluginStartDependencies {
uiActions: UiActionsStart;
}

export type EntityManagerPluginClass = PluginClass<
EntityManagerPublicPluginSetup,
EntityManagerPublicPluginStart
EntityManagerPublicPluginStart,
EntityManagerPublicPluginSetupDependencies,
EntityManagerPublicPluginStartDependencies
>;
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const builtInHostsFromEcsEntityDefinition: BuiltInDefinition = {
type: {
id: `${BUILT_IN_ID_PREFIX}hosts_from_ecs_data`,
display_name: 'Hosts',
dashboard_id: '2e0c5f85-3599-40a9-9280-b60c6f2145dc',
},
sources: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type InternalClusterClient = Pick<IClusterClient, 'asInternalUser'>;
export const entityTypeDefinitionRt = z.object({
id: z.string(),
display_name: z.string(),
dashboard_id: z.string().optional(),
});

export type EntityTypeDefinition = z.TypeOf<typeof entityTypeDefinitionRt>;
Expand Down