Skip to content

Commit

Permalink
Add default columns extension
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed May 17, 2024
1 parent 2b5faf1 commit 3798d6f
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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$;
Expand Down Expand Up @@ -147,6 +150,7 @@ export function getDataStateContainer({
getSavedSearch,
setDataView,
setDataSourceProfile,
uppdateAppState,
}: {
services: DiscoverServices;
searchSessionManager: DiscoverSearchSessionManager;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -298,6 +298,7 @@ export function getDiscoverStateContainer({
getSavedSearch: savedSearchContainer.getState,
setDataView,
setDataSourceProfile,
uppdateAppState: appStateContainer.update,
});

const loadDataViewList = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Profile>;

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/discover/public/context_awareness/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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<TContext> =
| { isMatch: true; context: TContext }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -80,7 +88,7 @@ export const logsDataSourceProfileProvider: DataSourceProfileProvider = {
</EuiBadge>
);
},
['log.level']: (props) => {
'log.level': (props) => {
const level = getFieldValue(props.row, 'log.level');

if (!level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComposableProfile[]>([]);

Expand Down
13 changes: 13 additions & 0 deletions src/plugins/discover/public/context_awareness/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentProfile>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <TKey extends keyof Profile>(
key: TKey,
Expand Down

0 comments on commit 3798d6f

Please sign in to comment.