Move more shared utils into @kbn/discover-utils (#162096)

## Summary

Requires https://github.com/elastic/kibana/pull/162004.

Moves some other shared utils (`formatFieldValue`, `formatHit`,
`getIgnoredReason`, `getShouldShowFieldHandler`) into the
`@kbn/discover-utils` package.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Lukas Olson 2023-07-20 11:07:38 -07:00 committed by GitHub
parent 08a72232c5
commit a2a14cf5d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 108 additions and 70 deletions

View file

@ -21,8 +21,7 @@
"dataViews": "src/plugins/data_views",
"defaultNavigation": "packages/default-nav",
"devTools": "src/plugins/dev_tools",
"discover": "src/plugins/discover",
"discover-utils": "packages/kbn-discover-utils",
"discover": ["src/plugins/discover", "packages/kbn-discover-utils"],
"savedSearch": "src/plugins/saved_search",
"embeddableApi": "src/plugins/embeddable",
"embeddableExamples": "examples/embeddable_examples",

View file

@ -6,4 +6,15 @@
* Side Public License, v 1.
*/
export { getDocId, buildDataTableRecord, buildDataTableRecordList } from './src';
export {
IgnoredReason,
buildDataTableRecord,
buildDataTableRecordList,
formatFieldValue,
formatHit,
getDocId,
getIgnoredReason,
getShouldShowFieldHandler,
isNestedFieldParent,
usePager,
} from './src';

View file

@ -0,0 +1,9 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/
export * from './use_pager';

View file

@ -6,4 +6,5 @@
* Side Public License, v 1.
*/
export * from './hooks';
export * from './utils';

View file

@ -8,6 +8,8 @@
import type { SearchHit } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
export type { IgnoredReason, ShouldShowFieldInTableHandler } from './utils';
export interface EsHitRecord extends Omit<SearchHit, '_source'> {
_source?: Record<string, unknown>;
}

View file

@ -6,11 +6,11 @@
* Side Public License, v 1.
*/
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { dataViewMock } from '../__mocks__';
import { formatHit } from './format_hit';
import { discoverServiceMock } from '../__mocks__/services';
import type { DataTableRecord, EsHitRecord } from '@kbn/discover-utils/types';
import { buildDataTableRecord } from '@kbn/discover-utils';
import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks';
import type { DataTableRecord, EsHitRecord } from '../types';
import { buildDataTableRecord } from './build_data_record';
describe('formatHit', () => {
let row: DataTableRecord;
@ -32,17 +32,13 @@ describe('formatHit', () => {
});
});
afterEach(() => {
(discoverServiceMock.uiSettings.get as jest.Mock).mockReset();
});
it('formats a document as expected', () => {
const formatted = formatHit(
row,
dataViewMock,
(fieldName) => ['message', 'extension', 'object.value'].includes(fieldName),
220,
discoverServiceMock.fieldFormats
fieldFormatsMock
);
expect(formatted).toEqual([
['extension', 'formatted:png'],
@ -67,7 +63,7 @@ describe('formatHit', () => {
dataViewMock,
(fieldName) => ['message', 'extension', 'object.value'].includes(fieldName),
220,
discoverServiceMock.fieldFormats
fieldFormatsMock
);
expect(formatted.map(([fieldName]) => fieldName)).toEqual([
'message',
@ -84,7 +80,7 @@ describe('formatHit', () => {
dataViewMock,
(fieldName) => ['message', 'extension', 'object.value'].includes(fieldName),
2,
discoverServiceMock.fieldFormats
fieldFormatsMock
);
expect(formatted).toEqual([
['extension', 'formatted:png'],
@ -99,7 +95,7 @@ describe('formatHit', () => {
dataViewMock,
(fieldName) => ['message', 'object.value'].includes(fieldName),
220,
discoverServiceMock.fieldFormats
fieldFormatsMock
);
expect(formatted).toEqual([
['message', 'formatted:foobar'],
@ -115,7 +111,7 @@ describe('formatHit', () => {
dataViewMock,
(fieldName) => ['bytes'].includes(fieldName),
220,
discoverServiceMock.fieldFormats
fieldFormatsMock
);
expect(formatted).toEqual([
['bytesDisplayName', 'formatted:123'],

View file

@ -6,15 +6,14 @@
* Side Public License, v 1.
*/
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { SearchHit } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { i18n } from '@kbn/i18n';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { DataView } from '@kbn/data-views-plugin/public';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { DataTableRecord, ShouldShowFieldInTableHandler } from '../types';
import { formatFieldValue } from './format_value';
import { type ShouldShowFieldInTableHandler } from './get_should_show_field_handler';
const formattedHitCache = new WeakMap<estypes.SearchHit, FormattedHit>();
const formattedHitCache = new WeakMap<SearchHit, FormattedHit>();
type FormattedHit = Array<readonly [fieldName: string, formattedValue: string]>;
@ -79,7 +78,7 @@ export function formatHit(
: [
...pairs.slice(0, maxEntries),
[
i18n.translate('discover.utils.formatHit.moreFields', {
i18n.translate('discover.formatHit.moreFields', {
defaultMessage: 'and {count} more {count, plural, one {field} other {fields}}',
values: { count: pairs.length - maxEntries },
}),

View file

@ -8,7 +8,7 @@
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { FieldFormat } from '@kbn/field-formats-plugin/common';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { dataViewMock } from '../__mocks__';
import { formatFieldValue } from './format_value';
const services = {

View file

@ -6,10 +6,10 @@
* Side Public License, v 1.
*/
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { KBN_FIELD_TYPES } from '@kbn/data-plugin/public';
import { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import type { SearchHit } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { KBN_FIELD_TYPES } from '@kbn/field-types';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import type {
FieldFormatsContentType,
HtmlContextTypeOptions,
@ -31,7 +31,7 @@ import type {
*/
export function formatFieldValue(
value: unknown,
hit: estypes.SearchHit,
hit: SearchHit,
fieldFormats: FieldFormatsStart,
dataView?: DataView,
field?: DataViewField,

View file

@ -8,7 +8,7 @@
import { getIgnoredReason, IgnoredReason } from './get_ignored_reason';
import type { DataViewField } from '@kbn/data-views-plugin/public';
import { KBN_FIELD_TYPES } from '@kbn/data-plugin/public';
import { KBN_FIELD_TYPES } from '@kbn/field-types';
function field(params: Partial<DataViewField>): DataViewField {
return {

View file

@ -6,9 +6,9 @@
* Side Public License, v 1.
*/
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { KBN_FIELD_TYPES } from '@kbn/data-plugin/public';
import { DataViewField } from '@kbn/data-views-plugin/public';
import type { SearchHit } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { KBN_FIELD_TYPES } from '@kbn/field-types';
import type { DataViewField } from '@kbn/data-views-plugin/public';
export enum IgnoredReason {
IGNORE_ABOVE = 'ignore_above',
@ -27,7 +27,7 @@ export enum IgnoredReason {
*/
export function getIgnoredReason(
field: DataViewField | string,
ignoredFields: estypes.SearchHit['_ignored']
ignoredFields: SearchHit['_ignored']
): IgnoredReason | undefined {
const fieldName = typeof field === 'string' ? field : field.name;
if (!ignoredFields?.includes(fieldName)) {

View file

@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { getFieldSubtypeMulti } from '@kbn/data-views-plugin/public';
import { getDataViewFieldSubtypeMulti } from '@kbn/es-query';
import type { DataView } from '@kbn/data-views-plugin/public';
export type ShouldShowFieldInTableHandler = (fieldName: string) => boolean;
@ -53,7 +53,7 @@ const canShowFieldInTable = (
return { show: true };
}
const subTypeMulti = getFieldSubtypeMulti(mapped.spec);
const subTypeMulti = getDataViewFieldSubtypeMulti(mapped.spec);
const isMultiField = Boolean(subTypeMulti?.multi);
return { show: !isMultiField, parentName: subTypeMulti?.multi?.parent };

View file

@ -7,4 +7,9 @@
*/
export * from './build_data_record';
export * from './format_hit';
export * from './format_value';
export * from './get_doc_id';
export * from './get_ignored_reason';
export * from './get_should_show_field_handler';
export * from './nested_fields';

View file

@ -7,7 +7,7 @@
*/
import { escapeRegExp } from 'lodash/fp';
import { getFieldSubtypeNested } from '@kbn/data-views-plugin/public';
import { getDataViewFieldSubtypeNested } from '@kbn/es-query';
import type { DataView } from '@kbn/data-views-plugin/public';
/**
@ -52,7 +52,7 @@ export function isNestedFieldParent(fieldName: string, dataView: DataView): bool
!!dataView.fields.getAll().find((patternField) => {
// We only want to match a full path segment
const nestedRootRegex = new RegExp(escapeRegExp(fieldName) + '(\\.|$)');
const subTypeNested = getFieldSubtypeNested(patternField);
const subTypeNested = getDataViewFieldSubtypeNested(patternField);
return nestedRootRegex.test(subTypeNested?.nested.path ?? '');
})
);

View file

@ -16,7 +16,11 @@
"target/**/*"
],
"kbn_references": [
"@kbn/data-views-plugin",
"@kbn/data-service",
"@kbn/data-views-plugin",
"@kbn/es-query",
"@kbn/field-formats-plugin",
"@kbn/field-types",
"@kbn/i18n"
]
}

View file

@ -6,4 +6,9 @@
* Side Public License, v 1.
*/
export type { DataTableRecord, EsHitRecord } from './src/types';
export type {
DataTableRecord,
EsHitRecord,
IgnoredReason,
ShouldShowFieldInTableHandler,
} from './src/types';

View file

@ -10,7 +10,7 @@ import { difference } from 'lodash';
import { type DataView, DataViewField } from '@kbn/data-views-plugin/public';
import type { DatatableColumn } from '@kbn/expressions-plugin/common';
import { fieldWildcardFilter } from '@kbn/kibana-utils-plugin/public';
import { isNestedFieldParent } from '../../../utils/nested_fields';
import { isNestedFieldParent } from '@kbn/discover-utils';
export function getDataViewFieldList(
dataView: DataView | undefined | null,

View file

@ -36,6 +36,7 @@ import type { ToastsStart, IUiSettingsClient, HttpStart, CoreStart } from '@kbn/
import { DataViewFieldEditorStart } from '@kbn/data-view-field-editor-plugin/public';
import { Serializable } from '@kbn/utility-types';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { getShouldShowFieldHandler } from '@kbn/discover-utils';
import { DocViewFilterFn } from '../../services/doc_views/doc_views_types';
import { getSchemaDetectors } from './discover_grid_schema';
import { DiscoverGridFlyout } from './discover_grid_flyout';
@ -55,7 +56,6 @@ import {
SHOW_MULTIFIELDS,
} from '../../../common';
import { DiscoverGridDocumentToolbarBtn } from './discover_grid_document_selection';
import { getShouldShowFieldHandler } from '../../utils/get_should_show_field_handler';
import type { ValueToStringConverter } from '../../types';
import { useRowHeightsOptions } from '../../hooks/use_row_heights_options';
import { convertValueToString } from '../../utils/convert_value_to_string';

View file

@ -21,15 +21,17 @@ import {
EuiFlexItem,
} from '@elastic/eui';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { DataTableRecord, EsHitRecord } from '@kbn/discover-utils/types';
import type {
DataTableRecord,
EsHitRecord,
ShouldShowFieldInTableHandler,
} from '@kbn/discover-utils/types';
import { formatFieldValue, formatHit } from '@kbn/discover-utils';
import { DiscoverGridContext } from './discover_grid_context';
import { JsonCodeEditor } from '../json_code_editor/json_code_editor';
import { defaultMonacoEditorWidth } from './constants';
import { formatFieldValue } from '../../utils/format_value';
import { formatHit } from '../../utils/format_hit';
import { useDiscoverServices } from '../../hooks/use_discover_services';
import { MAX_DOC_FIELDS_DISPLAYED } from '../../../common';
import { type ShouldShowFieldInTableHandler } from '../../utils/get_should_show_field_handler';
const CELL_CLASS = 'dscDiscoverGrid__cellValue';

View file

@ -12,8 +12,12 @@ import { i18n } from '@kbn/i18n';
import { EuiButtonEmpty, EuiIcon } from '@elastic/eui';
import { DataView } from '@kbn/data-views-plugin/public';
import { Filter } from '@kbn/es-query';
import type { DataTableRecord, EsHitRecord } from '@kbn/discover-utils/types';
import { formatFieldValue } from '../../../utils/format_value';
import type {
DataTableRecord,
EsHitRecord,
ShouldShowFieldInTableHandler,
} from '@kbn/discover-utils/types';
import { formatFieldValue } from '@kbn/discover-utils';
import { DocViewRenderProps } from '../../../services/doc_views/doc_views_types';
import { TableCell } from './table_row/table_cell';
import { formatRow, formatTopLevelObject } from '../utils/row_formatter';
@ -21,7 +25,6 @@ import { DocViewFilterFn } from '../../../services/doc_views/doc_views_types';
import { TableRowDetails } from './table_row_details';
import { useDiscoverServices } from '../../../hooks/use_discover_services';
import { DOC_HIDE_TIME_COLUMN_SETTING, MAX_DOC_FIELDS_DISPLAYED } from '../../../../common';
import { type ShouldShowFieldInTableHandler } from '../../../utils/get_should_show_field_handler';
export type DocTableRow = EsHitRecord & {
isAnchor?: boolean;

View file

@ -10,8 +10,8 @@ import React, { memo, useCallback, useMemo, useRef } from 'react';
import './index.scss';
import { FormattedMessage } from '@kbn/i18n-react';
import { EuiText } from '@elastic/eui';
import { usePager } from '@kbn/discover-utils';
import { SAMPLE_SIZE_SETTING } from '../../../common';
import { usePager } from '../../hooks/use_pager';
import {
ToolBarPagination,
MAX_ROWS_PER_PAGE_OPTION,

View file

@ -13,11 +13,11 @@ import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { FormattedMessage } from '@kbn/i18n-react';
import { Filter } from '@kbn/es-query';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { getShouldShowFieldHandler } from '@kbn/discover-utils';
import { TableHeader } from './components/table_header/table_header';
import { SHOW_MULTIFIELDS } from '../../../common';
import { TableRow } from './components/table_row';
import { DocViewFilterFn, DocViewRenderProps } from '../../services/doc_views/doc_views_types';
import { getShouldShowFieldHandler } from '../../utils/get_should_show_field_handler';
import { useDiscoverServices } from '../../hooks/use_discover_services';
export interface DocTableProps {

View file

@ -9,11 +9,10 @@
import React, { Fragment } from 'react';
import type { DataView } from '@kbn/data-views-plugin/public';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { formatHit } from '../../../utils/format_hit';
import type { DataTableRecord, ShouldShowFieldInTableHandler } from '@kbn/discover-utils/types';
import { formatHit } from '@kbn/discover-utils';
import './row_formatter.scss';
import { type ShouldShowFieldInTableHandler } from '../../../utils/get_should_show_field_handler';
interface Props {
defPairs: Array<readonly [string, string]>;

View file

@ -10,14 +10,16 @@ import '../table.scss';
import React, { useCallback, useMemo } from 'react';
import { EuiInMemoryTable } from '@elastic/eui';
import { getFieldIconType } from '@kbn/unified-field-list/src/utils/field_types/get_field_icon_type';
import {
formatFieldValue,
getIgnoredReason,
getShouldShowFieldHandler,
isNestedFieldParent,
} from '@kbn/discover-utils';
import { useDiscoverServices } from '../../../../../hooks/use_discover_services';
import { SHOW_MULTIFIELDS } from '../../../../../../common';
import { DocViewRenderProps, FieldRecordLegacy } from '../../../doc_views_types';
import { ACTIONS_COLUMN, MAIN_COLUMNS } from './table_columns';
import { getShouldShowFieldHandler } from '../../../../../utils/get_should_show_field_handler';
import { getIgnoredReason } from '../../../../../utils/get_ignored_reason';
import { formatFieldValue } from '../../../../../utils/format_value';
import { isNestedFieldParent } from '../../../../../application/main/utils/nested_fields';
export const DocViewerLegacyTable = ({
columns,

View file

@ -30,15 +30,17 @@ import { FormattedMessage } from '@kbn/i18n-react';
import { debounce } from 'lodash';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { getFieldIconType } from '@kbn/unified-field-list/src/utils/field_types/get_field_icon_type';
import {
formatFieldValue,
getIgnoredReason,
getShouldShowFieldHandler,
isNestedFieldParent,
usePager,
} from '@kbn/discover-utils';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { usePager } from '../../../../hooks/use_pager';
import { FieldName } from '../../../../components/field_name/field_name';
import { SHOW_MULTIFIELDS } from '../../../../../common';
import { DocViewRenderProps, FieldRecordLegacy } from '../../doc_views_types';
import { getShouldShowFieldHandler } from '../../../../utils/get_should_show_field_handler';
import { getIgnoredReason } from '../../../../utils/get_ignored_reason';
import { formatFieldValue } from '../../../../utils/format_value';
import { isNestedFieldParent } from '../../../../application/main/utils/nested_fields';
import { TableFieldValue } from './table_cell_value';
import { TableActions } from './table_cell_actions';

View file

@ -11,7 +11,7 @@ import { EuiFlexGroup, EuiFlexItem, EuiIcon, EuiTextColor, EuiToolTip } from '@e
import classNames from 'classnames';
import React, { Fragment, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { IgnoredReason } from '../../../../utils/get_ignored_reason';
import { IgnoredReason } from '@kbn/discover-utils';
import { FieldRecord } from './table';
import { DocViewTableRowBtnCollapse } from './legacy/table_row_btn_collapse';

View file

@ -8,8 +8,7 @@
import { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import type { AggregateQuery, Query } from '@kbn/es-query';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { IgnoredReason } from '../../utils/get_ignored_reason';
import type { DataTableRecord, IgnoredReason } from '@kbn/discover-utils/types';
export interface FieldMapping {
filterable?: boolean;

View file

@ -10,7 +10,7 @@ import { DataView } from '@kbn/data-views-plugin/public';
import { cellHasFormulas, createEscapeValue } from '@kbn/data-plugin/common';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { formatFieldValue } from './format_value';
import { formatFieldValue } from '@kbn/discover-utils';
interface ConvertedResult {
formattedString: string;

View file

@ -2213,7 +2213,7 @@
"discover.showingDefaultDataViewWarningDescription": "Affichage de la vue de données par défaut : \"{loadedDataViewTitle}\" ({loadedDataViewId})",
"discover.showingSavedDataViewWarningDescription": "Affichage de la vue de données enregistrée : \"{ownDataViewTitle}\" ({ownDataViewId})",
"discover.singleDocRoute.errorMessage": "Aucune vue de données correspondante pour l'ID {dataViewId}",
"discover.utils.formatHit.moreFields": "et {count} autre(s) {count, plural, one {champ} many {champs} other {champs}}",
"discover.formatHit.moreFields": "et {count} autre(s) {count, plural, one {champ} many {champs} other {champs}}",
"discover.valueIsNotConfiguredDataViewIDWarningTitle": "{stateVal} n'est pas un ID de vue de données configuré",
"discover.advancedSettings.context.defaultSizeText": "Le nombre d'entrées connexes à afficher dans la vue contextuelle",
"discover.advancedSettings.context.defaultSizeTitle": "Taille de contexte",

View file

@ -2227,7 +2227,7 @@
"discover.showingDefaultDataViewWarningDescription": "デフォルトのデータビューを表示しています:\"{loadedDataViewTitle}\"{loadedDataViewId}",
"discover.showingSavedDataViewWarningDescription": "保存されたデータビューを表示しています:\"{ownDataViewTitle}\"{ownDataViewId}",
"discover.singleDocRoute.errorMessage": "ID {dataViewId}の一致するデータビューが見つかりません",
"discover.utils.formatHit.moreFields": "およびその他{count}個の{count, plural, other {フィールド}}",
"discover.formatHit.moreFields": "およびその他{count}個の{count, plural, other {フィールド}}",
"discover.valueIsNotConfiguredDataViewIDWarningTitle": "{stateVal}は設定されたデータビューIDではありません",
"discover.advancedSettings.context.defaultSizeText": "コンテキストビューに表示される周りのエントリーの数",
"discover.advancedSettings.context.defaultSizeTitle": "コンテキストサイズ",

View file

@ -2227,7 +2227,7 @@
"discover.showingDefaultDataViewWarningDescription": "正在显示默认数据视图:“{loadedDataViewTitle}”({loadedDataViewId}",
"discover.showingSavedDataViewWarningDescription": "正在显示已保存数据视图:“{ownDataViewTitle}”({ownDataViewId}",
"discover.singleDocRoute.errorMessage": "没有与 ID {dataViewId} 相匹配的数据视图",
"discover.utils.formatHit.moreFields": "及另外 {count} 个{count, plural, other {字段}}",
"discover.formatHit.moreFields": "及另外 {count} 个{count, plural, other {字段}}",
"discover.valueIsNotConfiguredDataViewIDWarningTitle": "{stateVal} 不是配置的数据视图 ID",
"discover.advancedSettings.context.defaultSizeText": "要在上下文视图中显示的周围条目数目",
"discover.advancedSettings.context.defaultSizeTitle": "上下文大小",