Skip to content

Commit

Permalink
[One Discover] Add summary column for logs contextual experience (ela…
Browse files Browse the repository at this point in the history
…stic#192567)

## 📓 Summary

Closes elastic/logs-dev#165

This work introduces a Summary column as a replacement for the Document
one for the Discover logs contextual experience.
> We also decided to port this change as a replacement for the
**resource** and **content** virtual columns in Logs Explorer to have a
better alignment between the 2 apps.

## 🎥 Demo


https://github.com/user-attachments/assets/3dfca483-768e-471c-8735-d5fc8bbd5d00

## 💡 Reviewer hints

I left notes through the changes to help answer some questions.

The notable changes on this PR are:
- Rename `Document` column to `Summary`.
- Refactor `resource` and `content` virtual columns into a single
`Summary` column, which replaces the default Summary content for the
logs' contextual experience.
- Provide support for the plugin services to the context awareness
profiles.
- Disable virtual columns (clean up will go in a follow-up work) in Logs
Explorer and rely on the Summary column as the default selection.

---------

Co-authored-by: Marco Antonio Ghiani <marcoantonio.ghiani@elastic.co>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Davis McPhee <davis.mcphee@elastic.co>
  • Loading branch information
4 people authored Sep 30, 2024
1 parent 94120d9 commit 36a73ce
Show file tree
Hide file tree
Showing 92 changed files with 1,482 additions and 1,327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const degradedDocButtonLabelWhenPresent = i18n.translate(
'discover.customControl.degradedDocPresent',
{
defaultMessage:
"This document couldn't be parsed correctly. Not all fields are properly populated",
"This document couldn't be parsed correctly. Not all fields are properly populated.",
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface LogsContextService {
}

export interface LogsContextServiceDeps {
logsDataAccessPlugin?: LogsDataAccessPluginStart;
logsDataAccess?: LogsDataAccessPluginStart;
}

export const DEFAULT_ALLOWED_LOGS_BASE_PATTERNS = [
Expand All @@ -31,13 +31,11 @@ export const DEFAULT_ALLOWED_LOGS_BASE_PATTERNS_REGEXP = createRegExpPatternFrom
DEFAULT_ALLOWED_LOGS_BASE_PATTERNS
);

export const createLogsContextService = async ({
logsDataAccessPlugin,
}: LogsContextServiceDeps) => {
export const createLogsContextService = async ({ logsDataAccess }: LogsContextServiceDeps) => {
let logSources: string[] | undefined;

if (logsDataAccessPlugin) {
const logSourcesService = logsDataAccessPlugin.services.logSourcesService;
if (logsDataAccess) {
const logSourcesService = logsDataAccess.services.logSourcesService;
logSources = (await logSourcesService.getLogSources())
.map((logSource) => logSource.indexPattern)
.join(',') // TODO: Will be replaced by helper in: https://github.com/elastic/kibana/pull/192003
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export * from './get_field_from_doc';
export * from './get_log_level_color';
export * from './get_log_level_coalesed_value';
5 changes: 4 additions & 1 deletion packages/kbn-discover-utils/src/utils/get_field_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

import { DataTableRecord } from '../types';

export const getFieldValue = (record: DataTableRecord, field: string) => {
export const getFieldValue = <TRecord extends DataTableRecord, TField extends string>(
record: TRecord,
field: TField & keyof TRecord['flattened']
): TRecord['flattened'][TField] => {
const value = record.flattened[field];
return Array.isArray(value) ? value[0] : value;
};
18 changes: 4 additions & 14 deletions packages/kbn-discover-utils/src/utils/get_log_document_overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getLogDocumentOverview(
): LogDocumentOverview {
const formatField = <T extends keyof LogDocumentOverview>(field: T) => {
return (
field in doc.flattened
doc.flattened[field] !== undefined && doc.flattened[field] !== null
? formatFieldValue(
doc.flattened[field],
doc.raw,
Expand All @@ -32,19 +32,9 @@ export function getLogDocumentOverview(
const levelArray = doc.flattened[fieldConstants.LOG_LEVEL_FIELD];
const level =
Array.isArray(levelArray) && levelArray.length ? levelArray[0].toLowerCase() : levelArray;
const messageArray = doc.flattened[fieldConstants.MESSAGE_FIELD];
const message =
Array.isArray(messageArray) && messageArray.length ? messageArray[0] : messageArray;
const errorMessageArray = doc.flattened[fieldConstants.ERROR_MESSAGE_FIELD];
const errorMessage =
Array.isArray(errorMessageArray) && errorMessageArray.length
? errorMessageArray[0]
: errorMessageArray;
const eventOriginalArray = doc.flattened[fieldConstants.EVENT_ORIGINAL_FIELD];
const eventOriginal =
Array.isArray(eventOriginalArray) && eventOriginalArray.length
? eventOriginalArray[0]
: eventOriginalArray;
const message = formatField(fieldConstants.MESSAGE_FIELD);
const errorMessage = formatField(fieldConstants.ERROR_MESSAGE_FIELD);
const eventOriginal = formatField(fieldConstants.EVENT_ORIGINAL_FIELD);
const timestamp = formatField(fieldConstants.TIMESTAMP_FIELD);

// Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getFieldFromDoc, LogDocument, StackTraceFields } from '..';
import { getFieldValue, LogDocument, StackTraceFields } from '..';
import {
ERROR_EXCEPTION_STACKTRACE,
ERROR_LOG_STACKTRACE,
ERROR_STACK_TRACE,
} from '../field_constants';

export const getStacktraceFields = (doc: LogDocument): StackTraceFields => {
const errorStackTrace = getFieldFromDoc(doc, ERROR_STACK_TRACE);
const errorExceptionStackTrace = getFieldFromDoc(doc, ERROR_EXCEPTION_STACKTRACE);
const errorLogStackTrace = getFieldFromDoc(doc, ERROR_LOG_STACKTRACE);
const errorStackTrace = getFieldValue(doc, ERROR_STACK_TRACE);
const errorExceptionStackTrace = getFieldValue(doc, ERROR_EXCEPTION_STACKTRACE);
const errorLogStackTrace = getFieldValue(doc, ERROR_LOG_STACKTRACE);

return {
[ERROR_STACK_TRACE]: errorStackTrace,
Expand Down
6 changes: 5 additions & 1 deletion packages/kbn-unified-data-table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export {
RowHeightSettings,
type RowHeightSettingsProps,
} from './src/components/row_height_settings';
export { getDisplayedColumns } from './src/utils/columns';
export { getDisplayedColumns, SOURCE_COLUMN } from './src/utils/columns';
export { getTextBasedColumnsMeta } from './src/utils/get_columns_meta';
export { ROWS_HEIGHT_OPTIONS, DataGridDensity } from './src/constants';

export { JSONCodeEditorCommonMemoized } from './src/components/json_code_editor/json_code_editor_common';
export { SourceDocument } from './src/components/source_document';

export * from './src/types';
export * as columnActions from './src/components/actions/columns';
Expand All @@ -37,3 +38,6 @@ export {
getRenderCustomToolbarWithElements,
renderCustomToolbar,
} from './src/components/custom_toolbar/render_custom_toolbar';

export { getDataGridDensity } from './src/hooks/use_data_grid_density';
export { getRowHeight } from './src/hooks/use_row_height';
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const getColumnDisplayName = (

if (columnName === '_source') {
return i18n.translate('unifiedDataTable.grid.documentHeader', {
defaultMessage: 'Document',
defaultMessage: 'Summary',
});
}

Expand Down
18 changes: 11 additions & 7 deletions packages/kbn-unified-data-table/src/hooks/use_data_grid_density.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ interface UseDataGridDensityProps {
onUpdateDataGridDensity?: (density: DataGridDensity) => void;
}

export const DATA_GRID_DENSITY_STORAGE_KEY = 'dataGridDensity';

export function getDensityFromStyle(style: EuiDataGridStyle) {
return style.cellPadding === DATA_GRID_STYLE_COMPACT.cellPadding &&
style.fontSize === DATA_GRID_STYLE_COMPACT.fontSize
Expand All @@ -42,24 +40,30 @@ export function getDensityFromStyle(style: EuiDataGridStyle) {
: DataGridDensity.EXPANDED;
}

const DATA_GRID_DENSITY_STORAGE_KEY = 'dataGridDensity';
const getStorageKey = (consumer: string) => `${consumer}:${DATA_GRID_DENSITY_STORAGE_KEY}`;

export const getDataGridDensity = (storage: Storage, consumer: string): DataGridDensity => {
return storage.get(getStorageKey(consumer)) ?? DataGridDensity.COMPACT;
};

export const useDataGridDensity = ({
storage,
consumer,
dataGridDensityState,
onUpdateDataGridDensity,
}: UseDataGridDensityProps) => {
const storageKey = `${consumer}:${DATA_GRID_DENSITY_STORAGE_KEY}`;
const dataGridDensity = useMemo<DataGridDensity>(() => {
return dataGridDensityState ?? storage.get(storageKey) ?? DataGridDensity.COMPACT;
}, [dataGridDensityState, storage, storageKey]);
return dataGridDensityState ?? getDataGridDensity(storage, consumer);
}, [consumer, dataGridDensityState, storage]);

const onChangeDataGridDensity = useCallback(
(gridStyle: EuiDataGridStyle) => {
const newDensity = getDensityFromStyle(gridStyle);
storage.set(storageKey, newDensity);
storage.set(getStorageKey(consumer), newDensity);
onUpdateDataGridDensity?.(newDensity);
},
[storageKey, storage, onUpdateDataGridDensity]
[storage, consumer, onUpdateDataGridDensity]
);

return {
Expand Down
77 changes: 60 additions & 17 deletions packages/kbn-unified-data-table/src/hooks/use_row_height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,59 @@ interface UseRowHeightProps {
onUpdateRowHeight?: (rowHeight: number) => void;
}

interface ResolveRowHeightParams {
storage: Storage;
consumer: string;
key: string;
configRowHeight: number;
rowHeightState?: number;
}

const resolveRowHeight = ({
storage,
consumer,
key,
configRowHeight,
rowHeightState,
}: ResolveRowHeightParams): number => {
const rowHeightFromLS = getStoredRowHeight(storage, consumer, key);

const configHasNotChanged = (
localStorageRecord: DataGridOptionsRecord | null
): localStorageRecord is DataGridOptionsRecord =>
localStorageRecord !== null && configRowHeight === localStorageRecord.previousConfigRowHeight;

let currentRowLines: number;
if (isValidRowHeight(rowHeightState)) {
currentRowLines = rowHeightState;
} else if (configHasNotChanged(rowHeightFromLS)) {
currentRowLines = rowHeightFromLS.previousRowHeight;
} else {
currentRowLines = configRowHeight;
}

return currentRowLines;
};

export const ROW_HEIGHT_STORAGE_KEY = 'dataGridRowHeight';

export const getRowHeight = ({
storage,
consumer,
rowHeightState,
configRowHeight,
}: Pick<ResolveRowHeightParams, 'storage' | 'consumer' | 'rowHeightState'> & {
configRowHeight?: number;
}) => {
return resolveRowHeight({
storage,
consumer,
key: ROW_HEIGHT_STORAGE_KEY,
configRowHeight: configRowHeight ?? ROWS_HEIGHT_OPTIONS.default,
rowHeightState,
});
};

export const useRowHeight = ({
storage,
consumer,
Expand All @@ -36,23 +89,13 @@ export const useRowHeight = ({
onUpdateRowHeight,
}: UseRowHeightProps) => {
const rowHeightLines = useMemo(() => {
const rowHeightFromLS = getStoredRowHeight(storage, consumer, key);

const configHasNotChanged = (
localStorageRecord: DataGridOptionsRecord | null
): localStorageRecord is DataGridOptionsRecord =>
localStorageRecord !== null && configRowHeight === localStorageRecord.previousConfigRowHeight;

let currentRowLines: number;
if (isValidRowHeight(rowHeightState)) {
currentRowLines = rowHeightState;
} else if (configHasNotChanged(rowHeightFromLS)) {
currentRowLines = rowHeightFromLS.previousRowHeight;
} else {
currentRowLines = configRowHeight;
}

return currentRowLines;
return resolveRowHeight({
storage,
consumer,
key,
configRowHeight,
rowHeightState,
});
}, [configRowHeight, consumer, key, rowHeightState, storage]);

const rowHeight = useMemo<RowHeightSettingsProps['rowHeight']>(() => {
Expand Down
4 changes: 3 additions & 1 deletion packages/kbn-unified-data-table/src/utils/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

import type { DataView } from '@kbn/data-views-plugin/public';

export const SOURCE_COLUMN = '_source';

// We store this outside the function as a constant, so we're not creating a new array every time
// the function is returning this. A changing array might cause the data grid to think it got
// new columns, and thus performing worse than using the same array over multiple renders.
const SOURCE_ONLY = ['_source'];
const SOURCE_ONLY = [SOURCE_COLUMN];

/**
* Function to provide fallback when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const defaultProps: EuiDataGridCellPopoverElementProps = {
cellContentsElement: (<div>{'cellContentsElement'}</div>) as unknown as HTMLDivElement,
};

const renderTestComponent = () => {
const renderTestComponent = (overrideProps = {}) => {
const Renderer = getCustomCellPopoverRenderer();

render(<Renderer {...defaultProps} />);
render(<Renderer {...defaultProps} {...overrideProps} />);
};

describe('getCustomCellPopoverRenderer', () => {
Expand All @@ -45,4 +45,17 @@ describe('getCustomCellPopoverRenderer', () => {
panelClassName: 'unifiedDataTable__cellPopover',
});
});

it('should render a DefaultCellPopover with a wider panel for allowed columns', () => {
renderTestComponent({ columnId: '_source' });

expect(setCellPopoverPropsMocks).toHaveBeenCalledWith({
panelClassName: 'unifiedDataTable__cellPopover',
panelProps: {
css: {
maxInlineSize: 'min(75vw, 600px) !important',
},
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

import { EuiDataGridCellPopoverElementProps } from '@elastic/eui';
import React, { memo, useEffect } from 'react';
import { SOURCE_COLUMN } from './columns';

const FIELDS_WITH_WIDE_POPOVER = [SOURCE_COLUMN];

/*
*
Expand All @@ -23,13 +26,20 @@ export const getCustomCellPopoverRenderer = () => {
const RenderCustomCellPopoverMemoized = memo(function RenderCustomCellPopoverMemoized(
props: EuiDataGridCellPopoverElementProps
) {
const { setCellPopoverProps, DefaultCellPopover } = props;
const { columnId, setCellPopoverProps, DefaultCellPopover } = props;

useEffect(() => {
setCellPopoverProps({
const popoverProps: Parameters<typeof setCellPopoverProps>[0] = {
panelClassName: 'unifiedDataTable__cellPopover',
});
}, [setCellPopoverProps]);
};

const shouldRenderWidePopover = FIELDS_WITH_WIDE_POPOVER.includes(columnId);
if (shouldRenderWidePopover) {
popoverProps.panelProps = { css: { maxInlineSize: 'min(75vw, 600px) !important' } };
}

setCellPopoverProps(popoverProps);
}, [columnId, setCellPopoverProps]);

return <DefaultCellPopover {...props} />;
});
Expand Down
Loading

0 comments on commit 36a73ce

Please sign in to comment.