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

[Discover] [Embeddable] Fix Discover session embeddable drilldown #211678

Merged
merged 10 commits into from
Feb 21, 2025
3 changes: 2 additions & 1 deletion src/platform/plugins/shared/discover/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"observabilityAIAssistant",
"aiops",
"fieldsMetadata",
"logsDataAccess"
"logsDataAccess",
"embeddableEnhanced"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ dependencies OSS => x-pack; acceptable as long as they are optional.

],
"requiredBundles": [
"kibanaUtils",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import type { DataVisualizerPluginStart } from '@kbn/data-visualizer-plugin/publ
import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/public';
import { LogsDataAccessPluginStart } from '@kbn/logs-data-access-plugin/public';
import { DiscoverSharedPublicStart } from '@kbn/discover-shared-plugin/public';
import type { EmbeddableEnhancedPluginStart } from '@kbn/embeddable-enhanced-plugin/public';
import type { DiscoverStartPlugins } from './types';
import type { DiscoverContextAppLocator } from './application/context/services/locator';
import type { DiscoverSingleDocLocator } from './application/doc/locator';
Expand Down Expand Up @@ -140,6 +141,7 @@ export interface DiscoverServices {
ebtManager: DiscoverEBTManager;
fieldsMetadata?: FieldsMetadataPublicStart;
logsDataAccess?: LogsDataAccessPluginStart;
embeddableEnhanced?: EmbeddableEnhancedPluginStart;
}

export const buildServices = memoize(
Expand Down Expand Up @@ -233,6 +235,7 @@ export const buildServices = memoize(
ebtManager,
fieldsMetadata: plugins.fieldsMetadata,
logsDataAccess: plugins.logsDataAccess,
embeddableEnhanced: plugins.embeddableEnhanced,
};
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { SavedSearchAttributes } from '@kbn/saved-search-plugin/common';
import type { SavedSearchAttributes } from '@kbn/saved-search-plugin/common';
import type { Trigger } from '@kbn/ui-actions-plugin/public';
import type { SearchEmbeddableSerializedState } from './types';

export { SEARCH_EMBEDDABLE_TYPE } from '@kbn/discover-utils';

Expand Down Expand Up @@ -37,9 +38,10 @@ export const EDITABLE_SAVED_SEARCH_KEYS: Readonly<Array<keyof SavedSearchAttribu
] as const;

/** This constant refers to the dashboard panel specific state */
export const EDITABLE_PANEL_KEYS = [
export const EDITABLE_PANEL_KEYS: Readonly<Array<keyof SearchEmbeddableSerializedState>> = [
'title', // panel title
'description', // panel description
'timeRange', // panel custom time range
'hidePanelTitles', // panel hidden title
'enhancements', // panel enhancements (e.g. drilldowns)
] as const;
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export const getSearchEmbeddableFactory = ({
/** Build API */
const titleManager = initializeTitleManager(initialState);
const timeRange = initializeTimeRange(initialState);
const dynamicActionsApi =
discoverServices.embeddableEnhanced?.initializeReactEmbeddableDynamicActions(
uuid,
() => titleManager.api.title$.getValue(),
initialState
);
const maybeStopDynamicActions = dynamicActionsApi?.startDynamicActions();
const searchEmbeddable = await initializeSearchEmbeddableApi(initialState, {
discoverServices,
});
Expand Down Expand Up @@ -126,6 +133,7 @@ export const getSearchEmbeddableFactory = ({
savedSearch: searchEmbeddable.api.savedSearch$.getValue(),
serializeTitles: titleManager.serialize,
serializeTimeRange: timeRange.serialize,
serializeDynamicActions: dynamicActionsApi?.serializeDynamicActions,
savedObjectId,
});

Expand All @@ -134,6 +142,7 @@ export const getSearchEmbeddableFactory = ({
...titleManager.api,
...searchEmbeddable.api,
...timeRange.api,
...dynamicActionsApi?.dynamicActionsApi,
...initializeEditApi({
uuid,
parentApi,
Expand Down Expand Up @@ -178,10 +187,18 @@ export const getSearchEmbeddableFactory = ({
getSerializedStateByReference: (newId: string) => serialize(newId),
serializeState: () => serialize(savedObjectId$.getValue()),
getInspectorAdapters: () => searchEmbeddable.stateManager.inspectorAdapters.getValue(),
supportedTriggers: () => {
// No triggers are supported, but this is still required to pass the drilldown
// compatibilty check and ensure top-level drilldowns (e.g. URL) work as expected
return [];
},
Comment on lines +190 to +194
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another candidate that would benefit from #202054 🙈

},
{
...titleManager.comparators,
...timeRange.comparators,
...(dynamicActionsApi?.dynamicActionsComparator ?? {
enhancements: getUnchangingComparator(),
}),
...searchEmbeddable.comparators,
rawSavedObjectAttributes: getUnchangingComparator(),
savedObjectId: [savedObjectId$, (value) => savedObjectId$.next(value)],
Expand All @@ -206,6 +223,7 @@ export const getSearchEmbeddableFactory = ({
return () => {
searchEmbeddable.cleanup();
unsubscribeFromFetch();
maybeStopDynamicActions?.stopDynamicActions();
};
}, []);

Expand Down
27 changes: 17 additions & 10 deletions src/platform/plugins/shared/discover/public/embeddable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { DataTableRecord } from '@kbn/discover-utils/types';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import type { DefaultEmbeddableApi } from '@kbn/embeddable-plugin/public';
import { HasInspectorAdapters } from '@kbn/inspector-plugin/public';
import {
import type { HasInspectorAdapters } from '@kbn/inspector-plugin/public';
import type {
EmbeddableApiContext,
HasEditCapabilities,
HasLibraryTransforms,
HasSupportedTriggers,
PublishesBlockingError,
PublishesDataLoading,
PublishesSavedObjectId,
Expand All @@ -23,15 +24,17 @@ import {
SerializedTimeRange,
SerializedTitles,
} from '@kbn/presentation-publishing';
import {
import type {
SavedSearch,
SavedSearchAttributes,
SerializableSavedSearch,
} from '@kbn/saved-search-plugin/common/types';
import { DataTableColumnsMeta } from '@kbn/unified-data-table';
import { BehaviorSubject } from 'rxjs';
import { PublishesWritableDataViews } from '@kbn/presentation-publishing/interfaces/publishes_data_views';
import { EDITABLE_SAVED_SEARCH_KEYS } from './constants';
import type { DataTableColumnsMeta } from '@kbn/unified-data-table';
import type { BehaviorSubject } from 'rxjs';
import type { PublishesWritableDataViews } from '@kbn/presentation-publishing/interfaces/publishes_data_views';
import type { DynamicActionsSerializedState } from '@kbn/embeddable-enhanced-plugin/public/plugin';
import type { HasDynamicActions } from '@kbn/embeddable-enhanced-plugin/public';
import type { EDITABLE_SAVED_SEARCH_KEYS } from './constants';

export type SearchEmbeddableState = Pick<
SerializableSavedSearch,
Expand Down Expand Up @@ -75,6 +78,7 @@ export type EditableSavedSearchAttributes = Partial<

export type SearchEmbeddableSerializedState = SerializedTitles &
SerializedTimeRange &
Partial<DynamicActionsSerializedState> &
EditableSavedSearchAttributes & {
// by value
attributes?: SavedSearchAttributes & { references: SavedSearch['references'] };
Expand All @@ -85,7 +89,8 @@ export type SearchEmbeddableSerializedState = SerializedTitles &

export type SearchEmbeddableRuntimeState = SearchEmbeddableSerializedAttributes &
SerializedTitles &
SerializedTimeRange & {
SerializedTimeRange &
Partial<DynamicActionsSerializedState> & {
rawSavedObjectAttributes?: EditableSavedSearchAttributes;
savedObjectTitle?: string;
savedObjectId?: string;
Expand All @@ -107,7 +112,9 @@ export type SearchEmbeddableApi = DefaultEmbeddableApi<
HasLibraryTransforms &
HasTimeRange &
HasInspectorAdapters &
Partial<HasEditCapabilities & PublishesSavedObjectId>;
Partial<HasEditCapabilities & PublishesSavedObjectId> &
HasDynamicActions &
HasSupportedTriggers;

export interface PublishesSavedSearch {
savedSearch$: PublishingSubject<SavedSearch>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe('Serialization utils', () => {
savedSearch,
serializeTitles: jest.fn(),
serializeTimeRange: jest.fn(),
serializeDynamicActions: jest.fn(),
});

expect(serializedState).toEqual({
Expand Down Expand Up @@ -155,6 +156,7 @@ describe('Serialization utils', () => {
savedSearch,
serializeTitles: jest.fn(),
serializeTimeRange: jest.fn(),
serializeDynamicActions: jest.fn(),
savedObjectId: 'test-id',
});

Expand All @@ -175,6 +177,7 @@ describe('Serialization utils', () => {
savedSearch: { ...savedSearch, sampleSize: 500, sort: [['order_date', 'asc']] },
serializeTitles: jest.fn(),
serializeTimeRange: jest.fn(),
serializeDynamicActions: jest.fn(),
savedObjectId: 'test-id',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '@kbn/saved-search-plugin/common';
import { SavedSearchUnwrapResult } from '@kbn/saved-search-plugin/public';

import type { DynamicActionsSerializedState } from '@kbn/embeddable-enhanced-plugin/public/plugin';
import { extract, inject } from '../../../common/embeddable/search_inject_extract';
import { DiscoverServices } from '../../build_services';
import {
Expand Down Expand Up @@ -85,13 +86,15 @@ export const serializeState = ({
savedSearch,
serializeTitles,
serializeTimeRange,
serializeDynamicActions,
savedObjectId,
}: {
uuid: string;
initialState: SearchEmbeddableRuntimeState;
savedSearch: SavedSearch;
serializeTitles: () => SerializedTitles;
serializeTimeRange: () => SerializedTimeRange;
serializeDynamicActions: (() => DynamicActionsSerializedState) | undefined;
savedObjectId?: string;
}): SerializedPanelState<SearchEmbeddableSerializedState> => {
const searchSource = savedSearch.searchSource;
Expand All @@ -115,6 +118,7 @@ export const serializeState = ({
// Serialize the current dashboard state into the panel state **without** updating the saved object
...serializeTitles(),
...serializeTimeRange(),
...serializeDynamicActions?.(),
...overwriteState,
},
// No references to extract for by-reference embeddable since all references are stored with by-reference saved object
Expand Down
2 changes: 2 additions & 0 deletions src/platform/plugins/shared/discover/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type { DataVisualizerPluginStart } from '@kbn/data-visualizer-plugin/publ
import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/public';
import type { LogsDataAccessPluginStart } from '@kbn/logs-data-access-plugin/public';
import type { DiscoverSharedPublicStart } from '@kbn/discover-shared-plugin/public';
import type { EmbeddableEnhancedPluginStart } from '@kbn/embeddable-enhanced-plugin/public';
import type { DiscoverAppLocator } from '../common';
import { type DiscoverContainerProps } from './components/discover_container';

Expand Down Expand Up @@ -168,4 +169,5 @@ export interface DiscoverStartPlugins {
unifiedSearch: UnifiedSearchPublicPluginStart;
urlForwarding: UrlForwardingStart;
usageCollection?: UsageCollectionSetup;
embeddableEnhanced?: EmbeddableEnhancedPluginStart;
}
3 changes: 2 additions & 1 deletion src/platform/plugins/shared/discover/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"@kbn/logs-data-access-plugin",
"@kbn/core-lifecycle-browser",
"@kbn/esql-ast",
"@kbn/discover-shared-plugin"
"@kbn/discover-shared-plugin",
"@kbn/embeddable-enhanced-plugin"
],
"exclude": ["target/**/*"]
}
12 changes: 10 additions & 2 deletions test/functional/services/dashboard/drilldowns_manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ export function DashboardDrilldownsManageProvider({ getService }: FtrProviderCon
}: {
drilldownName: string;
destinationURLTemplate: string;
trigger: 'VALUE_CLICK_TRIGGER' | 'SELECT_RANGE_TRIGGER' | 'IMAGE_CLICK_TRIGGER';
trigger:
| 'VALUE_CLICK_TRIGGER'
| 'SELECT_RANGE_TRIGGER'
| 'IMAGE_CLICK_TRIGGER'
| 'CONTEXT_MENU_TRIGGER';
}) {
await this.fillInDrilldownName(drilldownName);
await this.selectTriggerIfNeeded(trigger);
Expand All @@ -94,7 +98,11 @@ export function DashboardDrilldownsManageProvider({ getService }: FtrProviderCon
}

async selectTriggerIfNeeded(
trigger: 'VALUE_CLICK_TRIGGER' | 'SELECT_RANGE_TRIGGER' | 'IMAGE_CLICK_TRIGGER'
trigger:
| 'VALUE_CLICK_TRIGGER'
| 'SELECT_RANGE_TRIGGER'
| 'IMAGE_CLICK_TRIGGER'
| 'CONTEXT_MENU_TRIGGER'
) {
if (await testSubjects.exists(`triggerPicker`)) {
const container = await testSubjects.find(`triggerPicker-${trigger}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const dataGrid = getService('dataGrid');
const dashboardAddPanel = getService('dashboardAddPanel');
const dashboardPanelActions = getService('dashboardPanelActions');
const dashboardDrilldownPanelActions = getService('dashboardDrilldownPanelActions');
const dashboardDrilldownsManage = getService('dashboardDrilldownsManage');
const filterBar = getService('filterBar');
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
const testSubjects = getService('testSubjects');
const { common, dashboard, header } = getPageObjects(['common', 'dashboard', 'header']);
const find = getService('find');
const queryBar = getService('queryBar');
const { common, dashboard, header, discover } = getPageObjects([
'common',
'dashboard',
'header',
'discover',
]);

describe('discover saved search embeddable', () => {
before(async () => {
Expand Down Expand Up @@ -106,5 +115,39 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
await header.waitUntilLoadingHasFinished();
await testSubjects.missingOrFail('embeddableError');
});

it('should support URL drilldown', async () => {
await addSearchEmbeddableToDashboard();
await dashboardDrilldownPanelActions.clickCreateDrilldown();
const drilldownName = 'URL drilldown';
const urlTemplate =
"{{kibanaUrl}}/app/discover#/?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:'{{context.panel.timeRange.from}}',to:'{{context.panel.timeRange.to}}'))" +
"&_a=(columns:!(_source),filters:{{rison context.panel.filters}},index:'{{context.panel.indexPatternId}}',interval:auto," +
"query:(language:{{context.panel.query.language}},query:'clientip:239.190.189.77'),sort:!())";
await testSubjects.click('actionFactoryItem-URL_DRILLDOWN');
await dashboardDrilldownsManage.fillInDashboardToURLDrilldownWizard({
drilldownName,
destinationURLTemplate: urlTemplate,
trigger: 'CONTEXT_MENU_TRIGGER',
});
await testSubjects.click('urlDrilldownAdditionalOptions');
await testSubjects.click('urlDrilldownOpenInNewTab');
await dashboardDrilldownsManage.saveChanges();
await dashboard.saveDashboard('Dashboard with URL drilldown', {
saveAsNew: true,
waitDialogIsClosed: true,
exitFromEditMode: true,
});
await browser.refresh();
await header.waitUntilLoadingHasFinished();
await dashboard.waitForRenderComplete();
await dashboardPanelActions.openContextMenu();
await find.clickByLinkText(drilldownName);
await discover.waitForDiscoverAppOnScreen();
await header.waitUntilLoadingHasFinished();
await discover.waitForDocTableLoadingComplete();
expect(await queryBar.getQueryString()).to.be('clientip:239.190.189.77');
expect(await discover.getHitCount()).to.be('6');
});
});
}