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] Breakdown support for fieldstats #199028

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3c51e10
[Discover] Breakdown support for fieldstats
mohamedhamed-ahmed Nov 5, 2024
3fad548
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Nov 5, 2024
3662347
Merge branch 'main' into 192700-fieldstats-popover-breakdown-field
mohamedhamed-ahmed Nov 5, 2024
2c66f0d
code refactor
mohamedhamed-ahmed Nov 6, 2024
064dd18
Merge branch '192700-fieldstats-popover-breakdown-field' of https://g…
mohamedhamed-ahmed Nov 6, 2024
93ffa66
bug fix
mohamedhamed-ahmed Nov 6, 2024
96848c1
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine Nov 6, 2024
93b9894
revert code
mohamedhamed-ahmed Nov 6, 2024
39dc06a
Merge branch '192700-fieldstats-popover-breakdown-field' of https://g…
mohamedhamed-ahmed Nov 6, 2024
6f06144
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine Nov 6, 2024
7297442
add unit tests
mohamedhamed-ahmed Nov 6, 2024
dfd7fb6
Merge branch '192700-fieldstats-popover-breakdown-field' of https://g…
mohamedhamed-ahmed Nov 6, 2024
3dd24d4
Merge branch 'main' into 192700-fieldstats-popover-breakdown-field
mohamedhamed-ahmed Nov 6, 2024
26c21e9
added component tests
mohamedhamed-ahmed Nov 6, 2024
a56102e
Merge branch 'main' into 192700-fieldstats-popover-breakdown-field
mohamedhamed-ahmed Nov 7, 2024
2e13155
code refactor
mohamedhamed-ahmed Nov 7, 2024
e491c54
code refactor
mohamedhamed-ahmed Nov 8, 2024
3b19865
Merge branch 'main' into 192700-fieldstats-popover-breakdown-field
mohamedhamed-ahmed Nov 8, 2024
263eaab
export fix
mohamedhamed-ahmed Nov 8, 2024
98ed647
icon change
mohamedhamed-ahmed Nov 8, 2024
f3d78dd
Merge branch 'main' into 192700-fieldstats-popover-breakdown-field
mohamedhamed-ahmed Nov 11, 2024
923e305
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine Nov 11, 2024
62c0e56
code refactor and tests
mohamedhamed-ahmed Nov 11, 2024
31978dd
Tests
mohamedhamed-ahmed Nov 11, 2024
e0a313d
swap icon positions
mohamedhamed-ahmed Nov 12, 2024
f0e94eb
Merge branch 'main' into 192700-fieldstats-popover-breakdown-field
mohamedhamed-ahmed Nov 12, 2024
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
1 change: 1 addition & 0 deletions packages/kbn-data-view-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

export * from './src/constants';
export { convertDatatableColumnToDataViewFieldSpec } from './src/utils/convert_to_data_view_field_spec';
export { convertDataViewFieldToDatatableColumn } from './src/utils/convert_to_data_table_column';
export { createRegExpPatternFrom } from './src/utils/create_regexp_pattern_from';
export { testPatternAgainstAllowedList } from './src/utils/test_pattern_against_allowed_list';
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { FieldSpec } from '@kbn/data-views-plugin/common';
import { convertDataViewFieldToDatatableColumn } from './convert_to_data_table_column';

describe('convertDataViewFieldToDatatableColumn', () => {
it('should return a correct DatatableColumn object for a counter timeseries field', () => {
const field: FieldSpec = {
name: 'bytes_counter',
timeSeriesMetric: 'counter',
type: 'number',
esTypes: ['long'],
searchable: true,
aggregatable: false,
isNull: false,
};
const result = convertDataViewFieldToDatatableColumn(field);
expect(result).toEqual(
expect.objectContaining({
id: 'bytes_counter',
name: 'bytes_counter',
meta: {
type: 'number',
esType: 'counter_long',
},
isNull: false,
})
);
});

it('should return a correct DatatableColumn object for a non-counter timeseries field', () => {
const field: FieldSpec = {
name: 'bytes',
timeSeriesMetric: 'summary',
type: 'number',
esTypes: ['long'],
searchable: true,
aggregatable: false,
isNull: false,
};
const result = convertDataViewFieldToDatatableColumn(field);
expect(result).toEqual(
expect.objectContaining({
id: 'bytes',
name: 'bytes',
meta: {
type: 'number',
esType: 'long',
},
isNull: false,
})
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { DatatableColumn } from '@kbn/expressions-plugin/common';
import type { FieldSpec } from '@kbn/data-views-plugin/common';

/**
* Convert a DataViewField to a DatatableColumn
*/
export function convertDataViewFieldToDatatableColumn(field: FieldSpec): DatatableColumn {
const isCounterTimeSeries = field.timeSeriesMetric === 'counter';
const esType =
isCounterTimeSeries && field.esTypes?.[0] ? `counter_${field.esTypes[0]}` : field.esTypes?.[0];

return {
id: field.name,
name: field.name,
meta: {
type: field.type,
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
esType,
},
isNull: Boolean(field?.isNull),
} as DatatableColumn;
}
1 change: 1 addition & 0 deletions packages/kbn-field-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
comboBoxFieldOptionMatcher,
getFieldSearchMatchingHighlight,
} from './src/utils/field_name_wildcard_matcher';
export { fieldSupportsBreakdown } from './src/utils/field_supports_breakdown';

export { FieldIcon, type FieldIconProps, getFieldIconProps } from './src/components/field_icon';
export { FieldDescription, type FieldDescriptionProps } from './src/components/field_description';
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { DataViewField } from '@kbn/data-views-plugin/public';
import { type DataViewField } from '@kbn/data-views-plugin/common';
import { KNOWN_FIELD_TYPES } from './field_types';

const supportedTypes = new Set(['string', 'boolean', 'number', 'ip']);
const supportedTypes = new Set([
KNOWN_FIELD_TYPES.STRING,
KNOWN_FIELD_TYPES.BOOLEAN,
KNOWN_FIELD_TYPES.NUMBER,
KNOWN_FIELD_TYPES.IP,
]);

export const fieldSupportsBreakdown = (field: DataViewField) =>
supportedTypes.has(field.type) &&
supportedTypes.has(field.type as KNOWN_FIELD_TYPES) &&
field.aggregatable &&
!field.scripted &&
field.timeSeriesMetric !== 'counter';
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import type { AddFieldFilterHandler } from '../../types';
export interface FieldPopoverHeaderProps {
field: DataViewField;
closePopover: EuiPopoverProps['closePopover'];
buttonAddBreakdownFieldProps?: Partial<EuiButtonIconProps>;
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
buttonAddFieldToWorkspaceProps?: Partial<EuiButtonIconProps>;
buttonAddFilterProps?: Partial<EuiButtonIconProps>;
buttonEditFieldProps?: Partial<EuiButtonIconProps>;
buttonDeleteFieldProps?: Partial<EuiButtonIconProps>;
onAddBreakdownField?: (field: DataViewField | undefined) => void;
onAddFieldToWorkspace?: (field: DataViewField) => unknown;
onAddFilter?: AddFieldFilterHandler;
onEditField?: (fieldName: string) => unknown;
Expand All @@ -43,10 +45,12 @@ export interface FieldPopoverHeaderProps {
export const FieldPopoverHeader: React.FC<FieldPopoverHeaderProps> = ({
field,
closePopover,
buttonAddBreakdownFieldProps,
buttonAddFieldToWorkspaceProps,
buttonAddFilterProps,
buttonEditFieldProps,
buttonDeleteFieldProps,
onAddBreakdownField,
onAddFieldToWorkspace,
onAddFilter,
onEditField,
Expand Down Expand Up @@ -82,6 +86,13 @@ export const FieldPopoverHeader: React.FC<FieldPopoverHeaderProps> = ({
defaultMessage: 'Delete data view field',
});

const addBreakdownFieldTooltip = i18n.translate(
'unifiedFieldList.fieldPopover.addBreakdownFieldLabel',
{
defaultMessage: 'Add breakdown',
}
);

return (
<>
<EuiFlexGroup alignItems="center" gutterSize="s" responsive={false}>
Expand All @@ -90,6 +101,24 @@ export const FieldPopoverHeader: React.FC<FieldPopoverHeaderProps> = ({
<h5 className="eui-textBreakWord">{field.displayName}</h5>
</EuiTitle>
</EuiFlexItem>
{onAddBreakdownField && (
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
<EuiFlexItem grow={false} data-test-subj="fieldPopoverHeader_addBreakdownField">
<EuiToolTip
content={buttonAddBreakdownFieldProps?.['aria-label'] ?? addBreakdownFieldTooltip}
>
<EuiButtonIcon
data-test-subj={`fieldPopoverHeader_addBreakdownField-${field.name}`}
aria-label={addBreakdownFieldTooltip}
{...(buttonAddBreakdownFieldProps || {})}
iconType="visBarVertical"
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
onClick={() => {
closePopover();
onAddBreakdownField(field);
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
}}
/>
</EuiToolTip>
</EuiFlexItem>
)}
{onAddFieldToWorkspace && (
<EuiFlexItem grow={false} data-test-subj="fieldPopoverHeader_addField">
<EuiToolTip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/publ
import { Draggable } from '@kbn/dom-drag-drop';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import { Filter } from '@kbn/es-query';
import { fieldSupportsBreakdown } from '@kbn/field-utils';
import { isESQLColumnGroupable } from '@kbn/esql-utils';
import { convertDataViewFieldToDatatableColumn } from '@kbn/data-view-utils';
import type { SearchMode } from '../../types';
import { FieldItemButton, type FieldItemButtonProps } from '../../components/field_item_button';
import {
Expand Down Expand Up @@ -140,6 +143,10 @@ export interface UnifiedFieldListItemProps {
* The currently selected data view
*/
dataView: DataView;
/**
* Callback to update breakdown field
*/
onAddBreakdownField?: (breakdownField: DataViewField | undefined) => void;
/**
* Callback to add/select the field
*/
Expand Down Expand Up @@ -215,6 +222,7 @@ function UnifiedFieldListItemComponent({
field,
highlight,
dataView,
onAddBreakdownField,
onAddFieldToWorkspace,
onRemoveFieldFromWorkspace,
onAddFilter,
Expand All @@ -232,6 +240,11 @@ function UnifiedFieldListItemComponent({
}: UnifiedFieldListItemProps) {
const [infoIsOpen, setOpen] = useState(false);

const isBreakdownSupported =
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
searchMode === 'documents'
? fieldSupportsBreakdown(field)
: isESQLColumnGroupable(convertDataViewFieldToDatatableColumn(field.spec));

const addFilterAndClosePopover: typeof onAddFilter | undefined = useMemo(
() =>
onAddFilter
Expand Down Expand Up @@ -394,13 +407,14 @@ function UnifiedFieldListItemComponent({
data-test-subj={stateService.creationOptions.dataTestSubj?.fieldListItemPopoverDataTestSubj}
renderHeader={() => (
<FieldPopoverHeader
services={services}
field={field}
closePopover={closePopover}
field={field}
onAddBreakdownField={isBreakdownSupported ? onAddBreakdownField : undefined}
onAddFieldToWorkspace={!isSelected ? toggleDisplay : undefined}
onAddFilter={onAddFilter}
onEditField={onEditField}
onDeleteField={onDeleteField}
onEditField={onEditField}
services={services}
{...customPopoverHeaderProps}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type UnifiedFieldListSidebarCustomizableProps = Pick<
| 'dataView'
| 'trackUiMetric'
| 'onAddFilter'
| 'onAddBreakdownField'
| 'onAddFieldToWorkspace'
| 'onRemoveFieldFromWorkspace'
| 'additionalFilters'
Expand Down Expand Up @@ -161,6 +162,7 @@ export const UnifiedFieldListSidebarComponent: React.FC<UnifiedFieldListSidebarP
fullWidth,
isAffectedByGlobalFilter,
prepend,
onAddBreakdownField,
onAddFieldToWorkspace,
onRemoveFieldFromWorkspace,
onAddFilter,
Expand Down Expand Up @@ -264,30 +266,31 @@ export const UnifiedFieldListSidebarComponent: React.FC<UnifiedFieldListSidebarP
({ field, groupName, groupIndex, itemIndex, fieldSearchHighlight }) => (
<li key={`field${field.name}`} data-attr-field={field.name}>
<UnifiedFieldListItem
stateService={stateService}
searchMode={searchMode}
services={services}
additionalFilters={additionalFilters}
alwaysShowActionButton={alwaysShowActionButton}
field={field}
size={compressed ? 'xs' : 's'}
highlight={fieldSearchHighlight}
dataView={dataView!}
onAddFieldToWorkspace={onAddFieldToWorkspace}
onRemoveFieldFromWorkspace={onRemoveFieldFromWorkspace}
onAddFilter={onAddFilter}
trackUiMetric={trackUiMetric}
multiFields={multiFieldsMap?.get(field.name)} // ideally we better calculate multifields when they are requested first from the popover
onEditField={onEditField}
onDeleteField={onDeleteField}
workspaceSelectedFieldNames={workspaceSelectedFieldNames}
field={field}
groupIndex={groupIndex}
itemIndex={itemIndex}
highlight={fieldSearchHighlight}
isEmpty={groupName === FieldsGroupNames.EmptyFields}
isSelected={
groupName === FieldsGroupNames.SelectedFields ||
Boolean(selectedFieldsState.selectedFieldsMap[field.name])
}
additionalFilters={additionalFilters}
itemIndex={itemIndex}
multiFields={multiFieldsMap?.get(field.name)} // ideally we better calculate multifields when they are requested first from the popover
onAddBreakdownField={onAddBreakdownField}
onAddFieldToWorkspace={onAddFieldToWorkspace}
onAddFilter={onAddFilter}
onDeleteField={onDeleteField}
onEditField={onEditField}
onRemoveFieldFromWorkspace={onRemoveFieldFromWorkspace}
searchMode={searchMode}
services={services}
size={compressed ? 'xs' : 's'}
stateService={stateService}
trackUiMetric={trackUiMetric}
workspaceSelectedFieldNames={workspaceSelectedFieldNames}
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
/>
</li>
),
Expand All @@ -298,6 +301,7 @@ export const UnifiedFieldListSidebarComponent: React.FC<UnifiedFieldListSidebarP
alwaysShowActionButton,
compressed,
dataView,
onAddBreakdownField,
onAddFieldToWorkspace,
onRemoveFieldFromWorkspace,
onAddFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import {
import { css } from '@emotion/react';
import { i18n } from '@kbn/i18n';
import { isOfAggregateQueryType } from '@kbn/es-query';
import { appendWhereClauseToESQLQuery } from '@kbn/esql-utils';
import { appendWhereClauseToESQLQuery, hasTransformationalCommand } from '@kbn/esql-utils';
import { METRIC_TYPE } from '@kbn/analytics';
import classNames from 'classnames';
import { generateFilters } from '@kbn/data-plugin/public';
import { useDragDropContext } from '@kbn/dom-drag-drop';
import { DataViewType } from '@kbn/data-views-plugin/public';
import { type DataViewField, DataViewType } from '@kbn/data-views-plugin/public';
import {
SEARCH_FIELDS_FROM_SOURCE,
SHOW_FIELD_STATISTICS,
Expand Down Expand Up @@ -256,6 +256,18 @@ export function DiscoverLayout({ stateContainer }: DiscoverLayoutProps) {

const onFilter = isEsqlMode ? onPopulateWhereClause : onAddFilter;

const canSetBreakdownField = useMemo(
() => (isOfAggregateQueryType(query) ? !hasTransformationalCommand(query.esql) : true),
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
[query]
);

const onSetBreakdownField = useCallback(
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
(field: DataViewField | undefined) => {
stateContainer.appState.update({ breakdownField: field?.name });
},
[stateContainer]
);

const onFieldEdited = useCallback(
async ({ removedFieldName }: { removedFieldName?: string } = {}) => {
if (removedFieldName && currentColumns.includes(removedFieldName)) {
Expand Down Expand Up @@ -423,18 +435,19 @@ export function DiscoverLayout({ stateContainer }: DiscoverLayoutProps) {
sidebarToggleState$={sidebarToggleState$}
sidebarPanel={
<SidebarMemoized
additionalFilters={customFilters}
columns={currentColumns}
documents$={stateContainer.dataState.data$.documents$}
onAddBreakdownField={canSetBreakdownField ? onSetBreakdownField : undefined}
onAddField={onAddColumnWithTracking}
onRemoveField={onRemoveColumnWithTracking}
columns={currentColumns}
onAddFilter={onFilter}
onChangeDataView={stateContainer.actions.onChangeDataView}
selectedDataView={dataView}
trackUiMetric={trackUiMetric}
onFieldEdited={onFieldEdited}
onDataViewCreated={stateContainer.actions.onDataViewCreated}
onFieldEdited={onFieldEdited}
onRemoveField={onRemoveColumnWithTracking}
selectedDataView={dataView}
sidebarToggleState$={sidebarToggleState$}
additionalFilters={customFilters}
trackUiMetric={trackUiMetric}
/>
}
mainPanel={
Expand Down
Loading