Skip to content

Commit

Permalink
Create @kbn/discover-utils package (#162004)
Browse files Browse the repository at this point in the history
## Summary

Creates a shared package for Discover utils/services/hooks,
`@kbn/discover-utils`. Moves a couple of utils there
(`buildDataTableRecord`/`getDocId`). This will be the future home for
many services/utils that are shared between the Discover app and its
packaged components (like unified field list, unified histogram, etc.)

### Checklist

- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
lukasolson and kibanamachine authored Jul 18, 2023
1 parent 384e2ed commit 2a689fa
Show file tree
Hide file tree
Showing 116 changed files with 752 additions and 586 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ examples/discover_customization_examples @elastic/kibana-data-discovery
x-pack/plugins/discover_enhanced @elastic/kibana-data-discovery
x-pack/plugins/discover_log_explorer @elastic/infra-monitoring-ui
src/plugins/discover @elastic/kibana-data-discovery
packages/kbn-discover-utils @elastic/kibana-data-discovery
packages/kbn-doc-links @elastic/docs
packages/kbn-docs-utils @elastic/kibana-operations
packages/kbn-dom-drag-drop @elastic/kibana-visualizations @elastic/kibana-data-discovery
Expand Down
1 change: 1 addition & 0 deletions .i18nrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"defaultNavigation": "packages/default-nav",
"devTools": "src/plugins/dev_tools",
"discover": "src/plugins/discover",
"discover-utils": "packages/kbn-discover-utils",
"savedSearch": "src/plugins/saved_search",
"embeddableApi": "src/plugins/embeddable",
"embeddableExamples": "examples/embeddable_examples",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@
"@kbn/discover-enhanced-plugin": "link:x-pack/plugins/discover_enhanced",
"@kbn/discover-log-explorer-plugin": "link:x-pack/plugins/discover_log_explorer",
"@kbn/discover-plugin": "link:src/plugins/discover",
"@kbn/discover-utils": "link:packages/kbn-discover-utils",
"@kbn/doc-links": "link:packages/kbn-doc-links",
"@kbn/dom-drag-drop": "link:packages/kbn-dom-drag-drop",
"@kbn/ebt-tools": "link:packages/kbn-ebt-tools",
Expand Down
4 changes: 4 additions & 0 deletions packages/kbn-discover-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @kbn/discover-utils

This is the package contains extracting parts of Discover for the usage in other
unified components. This are mainly shared types, utils and mocks.
9 changes: 9 additions & 0 deletions packages/kbn-discover-utils/index.ts
Original file line number Diff line number Diff line change
@@ -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 { getDocId, buildDataTableRecord, buildDataTableRecordList } from './src';
13 changes: 13 additions & 0 deletions packages/kbn-discover-utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-discover-utils'],
};
5 changes: 5 additions & 0 deletions packages/kbn-discover-utils/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/discover-utils",
"owner": "@elastic/kibana-data-discovery"
}
6 changes: 6 additions & 0 deletions packages/kbn-discover-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/discover-utils",
"private": true,
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

export const esHits = [
export const esHitsMock = [
{
_index: 'i',
_id: '1',
Expand Down
10 changes: 10 additions & 0 deletions packages/kbn-discover-utils/src/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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 './data_view';
export * from './es_hits';
9 changes: 9 additions & 0 deletions packages/kbn-discover-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 './utils';
35 changes: 35 additions & 0 deletions packages/kbn-discover-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.
*/

import type { SearchHit } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

export interface EsHitRecord extends Omit<SearchHit, '_source'> {
_source?: Record<string, unknown>;
}

/**
* This is the record/row of data provided to our Data Table
*/
export interface DataTableRecord {
/**
* A unique id generated by index, id and routing of a record
*/
id: string;
/**
* The document returned by Elasticsearch for search queries
*/
raw: EsHitRecord;
/**
* A flattened version of the ES doc or data provided by SQL, aggregations ...
*/
flattened: Record<string, unknown>;
/**
* Determines that the given doc is the anchor doc when rendering view surrounding docs
*/
isAnchor?: boolean;
}
34 changes: 34 additions & 0 deletions packages/kbn-discover-utils/src/utils/build_data_record.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.
*/

import { buildDataTableRecord, buildDataTableRecordList } from './build_data_record';
import { dataViewMock, esHitsMock } from '../__mocks__';

describe('Data table record utils', () => {
describe('buildDataTableRecord', () => {
test('should return a DataTableRecord', () => {
const result = buildDataTableRecord(esHitsMock[0], dataViewMock, false);
expect(result).toHaveProperty('id', 'i::1::');
expect(result).toHaveProperty('raw', esHitsMock[0]);
expect(result).toHaveProperty('flattened');
expect(result).toHaveProperty('isAnchor', false);
});
});

describe('buildDataTableRecordList', () => {
test('should return a list of DataTableRecord', () => {
const result = buildDataTableRecordList(esHitsMock, dataViewMock);
result.forEach((doc) => {
expect(doc).toHaveProperty('id');
expect(doc).toHaveProperty('raw');
expect(doc).toHaveProperty('flattened');
expect(doc).toHaveProperty('isAnchor');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Side Public License, v 1.
*/

import { DataView } from '@kbn/data-views-plugin/common';
import { flattenHit } from '@kbn/data-plugin/common';
import { getDocId } from './get_doc_id';
import type { DataView } from '@kbn/data-views-plugin/common';
import { flattenHit } from '@kbn/data-service';
import type { DataTableRecord, EsHitRecord } from '../types';
import { getDocId } from './get_doc_id';

/**
* Build a record for data table, explorer + classic one
Expand Down
17 changes: 17 additions & 0 deletions packages/kbn-discover-utils/src/utils/get_doc_id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.
*/

import { esHitsMock } from '../__mocks__';
import { getDocId } from './get_doc_id';

describe('getDocId', () => {
test('should return unique ID', () => {
const result = getDocId(esHitsMock[0]);
expect(result).toMatchInlineSnapshot(`"i::1::"`);
});
});
10 changes: 10 additions & 0 deletions packages/kbn-discover-utils/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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 './build_data_record';
export * from './get_doc_id';
22 changes: 22 additions & 0 deletions packages/kbn-discover-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node",
"react"
]
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"exclude": [
"target/**/*"
],
"kbn_references": [
"@kbn/data-views-plugin",
"@kbn/data-service",
]
}
9 changes: 9 additions & 0 deletions packages/kbn-discover-utils/types.ts
Original file line number Diff line number Diff line change
@@ -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 type { DataTableRecord, EsHitRecord } from './src/types';
4 changes: 2 additions & 2 deletions src/plugins/discover/public/__fixtures__/real_hits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/
import type { DataView } from '@kbn/data-views-plugin/common';
import { cloneDeep } from 'lodash';
import { buildDataTableRecord } from '../utils/build_data_record';
import type { EsHitRecord } from '../types';
import { buildDataTableRecord } from '@kbn/discover-utils';
import type { EsHitRecord } from '@kbn/discover-utils/types';
/*
Extensions:
gif: 5
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover/public/__mocks__/data_view_complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { DataView } from '@kbn/data-views-plugin/public';
import { buildDataViewMock } from './data_view';
import { buildDataViewMock } from '@kbn/discover-utils/src/__mocks__';

const fields = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { DataView } from '@kbn/data-views-plugin/public';
import { buildDataViewMock } from './data_view';
import { buildDataViewMock } from '@kbn/discover-utils/src/__mocks__';

const fields = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover/public/__mocks__/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { DataViewsContract } from '@kbn/data-views-plugin/public';
import { dataViewMock } from './data_view';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { dataViewComplexMock } from './data_view_complex';
import { dataViewWithTimefieldMock } from './data_view_with_timefield';

Expand Down
9 changes: 4 additions & 5 deletions src/plugins/discover/public/__mocks__/grid_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
*/

import type { DataView } from '@kbn/data-views-plugin/public';
import { dataViewMock } from './data_view';
import { dataViewMock, esHitsMock } from '@kbn/discover-utils/src/__mocks__';
import { dataViewComplexMock } from './data_view_complex';
import { esHits } from './es_hits';
import { esHitsComplex } from './es_hits_complex';
import { discoverServiceMock } from './services';
import { GridContext } from '../components/discover_grid/discover_grid_context';
import { convertValueToString } from '../utils/convert_value_to_string';
import { buildDataTableRecord } from '../utils/build_data_record';
import { EsHitRecord } from '../types';
import { buildDataTableRecord } from '@kbn/discover-utils';
import type { EsHitRecord } from '@kbn/discover-utils/types';

const buildGridContext = (dataView: DataView, rows: EsHitRecord[]): GridContext => {
const usedRows = rows.map((row) => {
Expand Down Expand Up @@ -43,6 +42,6 @@ const buildGridContext = (dataView: DataView, rows: EsHitRecord[]): GridContext
};
};

export const discoverGridContextMock = buildGridContext(dataViewMock, esHits);
export const discoverGridContextMock = buildGridContext(dataViewMock, esHitsMock);

export const discoverGridContextComplexMock = buildGridContext(dataViewComplexMock, esHitsComplex);
2 changes: 1 addition & 1 deletion src/plugins/discover/public/__mocks__/saved_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { createSearchSourceMock } from '@kbn/data-plugin/public/mocks';
import { dataViewMock } from './data_view';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { dataViewWithTimefieldMock } from './data_view_with_timefield';
import { dataViewAdHoc } from './data_view_complex';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Side Public License, v 1.
*/

import { dataViewMock } from '../../../__mocks__/data_view';
import { buildDataTableRecord } from '../../../utils/build_data_record';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { buildDataTableRecord } from '@kbn/discover-utils';

export const mockAnchorHit = buildDataTableRecord(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { mountWithIntl } from '@kbn/test-jest-helpers';
import { createFilterManagerMock } from '@kbn/data-plugin/public/query/filter_manager/filter_manager.mock';
import { mockTopNavMenu } from './__mocks__/top_nav_menu';
import { ContextAppContent } from './context_app_content';
import { dataViewMock } from '../../__mocks__/data_view';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { ContextApp } from './context_app';
import { DiscoverServices } from '../../build_services';
import { dataViewsMock } from '../../__mocks__/data_views';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { GetStateReturn } from './services/context_state';
import { SortDirection } from '@kbn/data-plugin/public';
import { ContextAppContent, ContextAppContentProps } from './context_app_content';
import { LoadingStatus } from './services/context_query_state';
import { dataViewMock } from '../../__mocks__/data_view';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { discoverServiceMock } from '../../__mocks__/services';
import { DiscoverGrid } from '../../components/discover_grid/discover_grid';
import { DocTableWrapper } from '../../components/doc_table/doc_table_wrapper';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { buildDataTableRecord } from '../../utils/build_data_record';
import { buildDataTableRecord } from '@kbn/discover-utils';
import { act } from 'react-dom/test-utils';

describe('ContextAppContent test', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { DataView } from '@kbn/data-views-plugin/public';
import { SortDirection } from '@kbn/data-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { CellActionsProvider } from '@kbn/cell-actions';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { CONTEXT_STEP_SETTING, DOC_HIDE_TIME_COLUMN_SETTING } from '../../../common';
import { LoadingStatus } from './services/context_query_state';
import { ActionBar } from './components/action_bar/action_bar';
Expand All @@ -23,7 +24,6 @@ import { SurrDocType } from './services/context';
import { MAX_CONTEXT_SIZE, MIN_CONTEXT_SIZE } from './services/constants';
import { DocTableContext } from '../../components/doc_table/doc_table_context';
import { useDiscoverServices } from '../../hooks/use_discover_services';
import type { DataTableRecord } from '../../types';
import { DiscoverGridFlyout } from '../../components/discover_grid/discover_grid_flyout';
import { DocViewer } from '../../services/doc_views/components/doc_viewer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n';
import { MarkdownSimple, toMountPoint, wrapWithTheme } from '@kbn/kibana-react-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import { SortDirection } from '@kbn/data-plugin/public';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { CONTEXT_TIE_BREAKER_FIELDS_SETTING } from '../../../../common';
import { fetchAnchor } from '../services/anchor';
import { fetchSurroundingDocs, SurrDocType } from '../services/context';
Expand All @@ -22,7 +23,6 @@ import {
import { AppState } from '../services/context_state';
import { getFirstSortableField } from '../utils/sorting';
import { useDiscoverServices } from '../../../hooks/use_discover_services';
import type { DataTableRecord } from '../../../types';

const createError = (statusKey: string, reason: FailureReason, error?: Error) => ({
[statusKey]: { value: LoadingStatus.FAILED, error, reason },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import moment from 'moment';
import { of } from 'rxjs';
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { IKibanaSearchResponse } from '@kbn/data-plugin/common';
import { EsHitRecord } from '../../../types';
import type { EsHitRecord } from '@kbn/discover-utils/types';

type SortHit = {
[key in string]: number; // timeField name
Expand Down
Loading

0 comments on commit 2a689fa

Please sign in to comment.