From 3798d6f4d48a3193b22133cc5353211777e663ae Mon Sep 17 00:00:00 2001 From: Davis McPhee Date: Thu, 16 May 2024 23:17:50 -0300 Subject: [PATCH] Add default columns extension --- .../discover_data_state_container.ts | 30 +++++++++++++++++-- .../main/state_management/discover_state.ts | 3 +- .../context_awareness/composable_profile.ts | 10 +------ .../public/context_awareness/index.ts | 3 +- .../context_awareness/profile_service.ts | 3 +- .../profiles/data_source_profile.tsx | 8 ++--- .../profiles/document_profile.tsx | 5 ++-- .../profiles/example_profiles.tsx | 12 ++++++-- .../profiles/root_profile.ts | 2 +- .../context_awareness/profiles_provider.tsx | 2 +- .../public/context_awareness/types.ts | 13 ++++++++ .../context_awareness/use_profile_accessor.ts | 5 ++-- 12 files changed, 68 insertions(+), 28 deletions(-) diff --git a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts index a387715c73177..9215fce5ed019 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts @@ -19,7 +19,7 @@ import type { SearchResponseWarning } from '@kbn/search-response-warnings'; import type { DataTableRecord } from '@kbn/discover-utils/types'; import { SEARCH_FIELDS_FROM_SOURCE, SEARCH_ON_PAGE_LOAD_SETTING } from '@kbn/discover-utils'; import { getEsqlDataView } from './utils/get_esql_data_view'; -import { DiscoverAppState } from './discover_app_state_container'; +import { DiscoverAppState, DiscoverAppStateContainer } from './discover_app_state_container'; import { DiscoverServices } from '../../../build_services'; import { DiscoverSearchSessionManager } from './discover_search_session'; import { FetchStatus } from '../../types'; @@ -28,8 +28,11 @@ import { fetchAll, fetchMoreDocuments } from '../data_fetching/fetch_all'; import { sendResetMsg } from '../hooks/use_saved_search_messages'; import { getFetch$ } from '../data_fetching/get_fetch_observable'; import { InternalState } from './discover_internal_state_container'; -import { ComposableProfile } from '../../../context_awareness/composable_profile'; -import { dataSourceProfileService } from '../../../context_awareness'; +import { + ComposableProfile, + dataSourceProfileService, + getMergedAccessor, +} from '../../../context_awareness'; export interface SavedSearchData { main$: DataMain$; @@ -147,6 +150,7 @@ export function getDataStateContainer({ getSavedSearch, setDataView, setDataSourceProfile, + uppdateAppState, }: { services: DiscoverServices; searchSessionManager: DiscoverSearchSessionManager; @@ -155,6 +159,7 @@ export function getDataStateContainer({ getSavedSearch: () => SavedSearch; setDataView: (dataView: DataView) => void; setDataSourceProfile: (profile: ComposableProfile) => void; + uppdateAppState: DiscoverAppStateContainer['update']; }): DiscoverDataStateContainer { const { data, uiSettings, toastNotifications } = services; const { timefilter } = data.query.timefilter; @@ -306,6 +311,25 @@ export function getDataStateContainer({ setDataSourceProfile(dataSourceProfile); + const defaultColumns = getMergedAccessor( + [dataSourceProfile], + 'getDefaultColumns', + () => undefined + )(); + + if (defaultColumns) { + uppdateAppState( + { + columns: defaultColumns.columns, + grid: { + ...getAppState().grid, + columns: defaultColumns.settings, + }, + }, + true + ); + } + if (resetQuery) { refetch$.next('reset'); } else { diff --git a/src/plugins/discover/public/application/main/state_management/discover_state.ts b/src/plugins/discover/public/application/main/state_management/discover_state.ts index 93f1d94b621a9..ab9d7a27d4dbd 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_state.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_state.ts @@ -59,7 +59,7 @@ import { DataSourceType, isDataSourceType, } from '../../../../common/data_sources'; -import { ComposableProfile } from '../../../context_awareness/composable_profile'; +import { ComposableProfile } from '../../../context_awareness'; export interface DiscoverStateContainerParams { /** @@ -298,6 +298,7 @@ export function getDiscoverStateContainer({ getSavedSearch: savedSearchContainer.getState, setDataView, setDataSourceProfile, + uppdateAppState: appStateContainer.update, }); const loadDataViewList = async () => { diff --git a/src/plugins/discover/public/context_awareness/composable_profile.ts b/src/plugins/discover/public/context_awareness/composable_profile.ts index 731aa64f55a4b..c2211dee3f370 100644 --- a/src/plugins/discover/public/context_awareness/composable_profile.ts +++ b/src/plugins/discover/public/context_awareness/composable_profile.ts @@ -6,15 +6,7 @@ * Side Public License, v 1. */ -import { TopNavMenuData } from '@kbn/navigation-plugin/public'; -import { CustomCellRenderer } from '@kbn/unified-data-table'; -import { DocViewsRegistry } from '@kbn/unified-doc-viewer'; - -export interface Profile { - getTopNavItems: () => TopNavMenuData[]; - getCellRenderers: () => CustomCellRenderer; - getDocViewsRegistry: (prevRegistry: DocViewsRegistry) => DocViewsRegistry; -} +import type { Profile } from './types'; export type PartialProfile = Partial; diff --git a/src/plugins/discover/public/context_awareness/index.ts b/src/plugins/discover/public/context_awareness/index.ts index 7ee5bc91c7cbb..628341e3e1423 100644 --- a/src/plugins/discover/public/context_awareness/index.ts +++ b/src/plugins/discover/public/context_awareness/index.ts @@ -8,5 +8,6 @@ export * from './types'; export * from './profiles'; -export { useProfileAccessor } from './use_profile_accessor'; +export { type ComposableProfile, getMergedAccessor } from './composable_profile'; export { ProfilesProvider, useProfiles } from './profiles_provider'; +export { useProfileAccessor } from './use_profile_accessor'; diff --git a/src/plugins/discover/public/context_awareness/profile_service.ts b/src/plugins/discover/public/context_awareness/profile_service.ts index 8e245f905c5f9..1c0d811447887 100644 --- a/src/plugins/discover/public/context_awareness/profile_service.ts +++ b/src/plugins/discover/public/context_awareness/profile_service.ts @@ -8,7 +8,8 @@ /* eslint-disable max-classes-per-file */ -import { ComposableProfile, PartialProfile, Profile } from './composable_profile'; +import type { ComposableProfile, PartialProfile } from './composable_profile'; +import type { Profile } from './types'; export type ResolveProfileResult = | { isMatch: true; context: TContext } diff --git a/src/plugins/discover/public/context_awareness/profiles/data_source_profile.tsx b/src/plugins/discover/public/context_awareness/profiles/data_source_profile.tsx index c9d65a70acc33..8115c4ea7b136 100644 --- a/src/plugins/discover/public/context_awareness/profiles/data_source_profile.tsx +++ b/src/plugins/discover/public/context_awareness/profiles/data_source_profile.tsx @@ -6,11 +6,11 @@ * Side Public License, v 1. */ -import { DataView } from '@kbn/data-views-plugin/common'; -import { AggregateQuery, Query } from '@kbn/es-query'; -import { DiscoverDataSource } from '../../../common/data_sources'; -import { Profile } from '../composable_profile'; +import type { DataView } from '@kbn/data-views-plugin/common'; +import type { AggregateQuery, Query } from '@kbn/es-query'; +import type { DiscoverDataSource } from '../../../common/data_sources'; import { AsyncProfileService } from '../profile_service'; +import { Profile } from '../types'; export enum DataSourceCategory { Logs = 'logs', diff --git a/src/plugins/discover/public/context_awareness/profiles/document_profile.tsx b/src/plugins/discover/public/context_awareness/profiles/document_profile.tsx index 18a78e95671db..a433d7565a3b6 100644 --- a/src/plugins/discover/public/context_awareness/profiles/document_profile.tsx +++ b/src/plugins/discover/public/context_awareness/profiles/document_profile.tsx @@ -6,10 +6,9 @@ * Side Public License, v 1. */ -import { DataTableRecord } from '@kbn/discover-utils'; -import { Profile } from '../composable_profile'; +import type { DataTableRecord } from '@kbn/discover-utils'; +import type { DataTableRecordWithProfile, Profile } from '../types'; import { ProfileService } from '../profile_service'; -import { DataTableRecordWithProfile } from '../types'; export enum DocumentType { Log = 'log', diff --git a/src/plugins/discover/public/context_awareness/profiles/example_profiles.tsx b/src/plugins/discover/public/context_awareness/profiles/example_profiles.tsx index 7da6f23c418be..8e94eb644ca91 100644 --- a/src/plugins/discover/public/context_awareness/profiles/example_profiles.tsx +++ b/src/plugins/discover/public/context_awareness/profiles/example_profiles.tsx @@ -69,9 +69,17 @@ export const logsDataSourceProfileProvider: DataSourceProfileProvider = { }, ...prev(), ], + getDefaultColumns: () => () => ({ + columns: ['@timestamp', 'log.level', 'message'], + settings: { + 'log.level': { + width: 120, + }, + }, + }), getCellRenderers: (prev) => () => ({ ...prev(), - ['@timestamp']: (props) => { + '@timestamp': (props) => { const timestamp = getFieldValue(props.row, '@timestamp'); return ( @@ -80,7 +88,7 @@ export const logsDataSourceProfileProvider: DataSourceProfileProvider = { ); }, - ['log.level']: (props) => { + 'log.level': (props) => { const level = getFieldValue(props.row, 'log.level'); if (!level) { diff --git a/src/plugins/discover/public/context_awareness/profiles/root_profile.ts b/src/plugins/discover/public/context_awareness/profiles/root_profile.ts index 762449f5028dd..c8f250d8fd3cd 100644 --- a/src/plugins/discover/public/context_awareness/profiles/root_profile.ts +++ b/src/plugins/discover/public/context_awareness/profiles/root_profile.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { Profile } from '../composable_profile'; +import type { Profile } from '../types'; import { AsyncProfileService } from '../profile_service'; export enum SolutionType { diff --git a/src/plugins/discover/public/context_awareness/profiles_provider.tsx b/src/plugins/discover/public/context_awareness/profiles_provider.tsx index b1b4f155fdb4f..5cda69d88fa48 100644 --- a/src/plugins/discover/public/context_awareness/profiles_provider.tsx +++ b/src/plugins/discover/public/context_awareness/profiles_provider.tsx @@ -7,7 +7,7 @@ */ import React, { createContext, FC, useContext, useMemo } from 'react'; -import { ComposableProfile } from './composable_profile'; +import type { ComposableProfile } from './composable_profile'; const profilesContext = createContext([]); diff --git a/src/plugins/discover/public/context_awareness/types.ts b/src/plugins/discover/public/context_awareness/types.ts index 9e80861dd0741..7ff323c94f205 100644 --- a/src/plugins/discover/public/context_awareness/types.ts +++ b/src/plugins/discover/public/context_awareness/types.ts @@ -7,9 +7,22 @@ */ import type { DataTableRecord } from '@kbn/discover-utils'; +import type { TopNavMenuData } from '@kbn/navigation-plugin/public'; +import type { DiscoverGridSettings } from '@kbn/saved-search-plugin/common'; +import type { CustomCellRenderer } from '@kbn/unified-data-table'; +import type { DocViewsRegistry } from '@kbn/unified-doc-viewer'; import type { ComposableProfile } from './composable_profile'; import type { DocumentProfile } from './profiles'; +export interface Profile { + getTopNavItems: () => TopNavMenuData[]; + getDefaultColumns: () => + | { columns: string[]; settings?: DiscoverGridSettings['columns'] } + | undefined; + getCellRenderers: () => CustomCellRenderer; + getDocViewsRegistry: (prevRegistry: DocViewsRegistry) => DocViewsRegistry; +} + export interface DataTableRecordWithProfile extends DataTableRecord { profile: ComposableProfile; } diff --git a/src/plugins/discover/public/context_awareness/use_profile_accessor.ts b/src/plugins/discover/public/context_awareness/use_profile_accessor.ts index 11b98a2145af0..52f66c35e5e06 100644 --- a/src/plugins/discover/public/context_awareness/use_profile_accessor.ts +++ b/src/plugins/discover/public/context_awareness/use_profile_accessor.ts @@ -6,11 +6,12 @@ * Side Public License, v 1. */ -import { DataTableRecord } from '@kbn/discover-utils'; +import type { DataTableRecord } from '@kbn/discover-utils'; import { useMemo } from 'react'; -import { getMergedAccessor, Profile } from './composable_profile'; +import { getMergedAccessor } from './composable_profile'; import { recordHasProfile } from './profiles'; import { useProfiles } from './profiles_provider'; +import type { Profile } from './types'; export const useProfileAccessor = ( key: TKey,