Skip to content

Commit

Permalink
[Discover] Centralize document flattening and introduce DataTableReco…
Browse files Browse the repository at this point in the history
…rd (#134174)
  • Loading branch information
kertal authored Jun 22, 2022
1 parent 206de80 commit 9c1202f
Show file tree
Hide file tree
Showing 79 changed files with 667 additions and 799 deletions.
25 changes: 0 additions & 25 deletions src/plugins/discover/public/__fixtures__/fake_row.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

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';
/*
Extensions:
gif: 5
Expand All @@ -23,10 +26,9 @@
All have the same index, ids are unique
*/

export default [
export const realHits: EsHitRecord[] = [
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '61',
_score: 1,
_source: {
Expand All @@ -36,7 +38,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '388',
_score: 1,
_source: {
Expand All @@ -46,7 +47,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '403',
_score: 1,
_source: {
Expand All @@ -56,7 +56,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '415',
_score: 1,
_source: {
Expand All @@ -66,7 +65,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '460',
_score: 1,
_source: {
Expand All @@ -77,7 +75,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '496',
_score: 1,
_source: {
Expand All @@ -87,7 +84,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '511',
_score: 1,
_source: {
Expand All @@ -97,7 +93,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '701',
_score: 1,
_source: {
Expand All @@ -107,7 +102,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '838',
_score: 1,
_source: {
Expand All @@ -118,7 +112,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '890',
_score: 1,
_source: {
Expand All @@ -129,7 +122,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'nginx',
_id: '927',
_score: 1,
_source: {
Expand All @@ -140,7 +132,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '1034',
_score: 1,
_source: {
Expand All @@ -150,7 +141,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '1142',
_score: 1,
_source: {
Expand All @@ -161,7 +151,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '1180',
_score: 1,
_source: {
Expand All @@ -171,7 +160,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'nginx',
_id: '1224',
_score: 1,
_source: {
Expand All @@ -181,7 +169,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '1243',
_score: 1,
_source: {
Expand All @@ -191,7 +178,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '1510',
_score: 1,
_source: {
Expand All @@ -201,7 +187,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '1628',
_score: 1,
_source: {
Expand All @@ -211,7 +196,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '1729',
_score: 1,
_source: {
Expand All @@ -221,7 +205,6 @@ export default [
},
{
_index: 'logstash-2014.09.09',
_type: 'apache',
_id: '1945',
_score: 1,
_source: {
Expand All @@ -230,3 +213,7 @@ export default [
},
},
];

export function getDataTableRecords(dataView: DataView) {
return cloneDeep(realHits).map((hit: EsHitRecord) => buildDataTableRecord(hit, dataView));
}
18 changes: 8 additions & 10 deletions src/plugins/discover/public/__mocks__/grid_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Side Public License, v 1.
*/

import { flattenHit } from '@kbn/data-plugin/common';
import type { DataView } from '@kbn/data-views-plugin/public';
import { indexPatternMock } from './index_pattern';
import { dataViewComplexMock } from './data_view_complex';
Expand All @@ -15,18 +14,18 @@ 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 type { ElasticSearchHit } from '../types';
import { buildDataTableRecord } from '../utils/build_data_record';
import { EsHitRecord } from '../types';

const buildGridContext = (dataView: DataView, rows: ElasticSearchHit[]): GridContext => {
const rowsFlattened = rows.map((hit) =>
flattenHit(hit, dataView, { includeIgnoredValues: true })
);
const buildGridContext = (dataView: DataView, rows: EsHitRecord[]): GridContext => {
const usedRows = rows.map((row) => {
return buildDataTableRecord(row, dataView);
});

return {
expanded: undefined,
setExpanded: jest.fn(),
rows,
rowsFlattened,
rows: usedRows,
onFilter: jest.fn(),
indexPattern: dataView,
isDarkMode: false,
Expand All @@ -37,8 +36,7 @@ const buildGridContext = (dataView: DataView, rows: ElasticSearchHit[]): GridCon
rowIndex,
columnId,
services: discoverServiceMock,
rows,
rowsFlattened,
rows: usedRows,
dataView,
options,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
* Side Public License, v 1.
*/

export const mockAnchorHit = {
_id: '123',
_index: 'the-index-pattern-id',
fields: { order_date: ['2021-06-07T18:52:17.000Z'] },
sort: [1623091937000, 2092],
isAnchor: true,
_version: 1,
};
import { indexPatternMock } from '../../../__mocks__/index_pattern';
import { buildDataTableRecord } from '../../../utils/build_data_record';

export const mockAnchorHit = buildDataTableRecord(
{
_id: '123',
_index: 'the-index-pattern-id',
fields: { order_date: ['2021-06-07T18:52:17.000Z'] },
sort: [1623091937000, 2092],
_version: 1,
},
indexPatternMock,
true
);

export const mockPredecessorHits = [
{
Expand All @@ -37,7 +43,7 @@ export const mockPredecessorHits = [
sort: ['2021-06-07T19:10:22.000Z', 2435],
_version: 1,
},
];
].map((entry) => buildDataTableRecord(entry, indexPatternMock));

export const mockSuccessorHits = [
{
Expand All @@ -61,4 +67,4 @@ export const mockSuccessorHits = [
sort: ['2021-06-07T18:47:16.000Z', 2437],
_version: 1,
},
];
].map((entry) => buildDataTableRecord(entry, indexPatternMock));
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const ContextApp = ({ indexPattern, anchorId }: ContextAppProps) => {
fetchContextRows,
fetchAllRows,
fetchSurroundingRows,
fetchedState.anchor._id,
fetchedState.anchor.id,
]);

const { columns, onAddColumn, onRemoveColumn, onSetColumns } = useColumns({
Expand All @@ -110,7 +110,7 @@ export const ContextApp = ({ indexPattern, anchorId }: ContextAppProps) => {
const rows = useMemo(
() => [
...(fetchedState.predecessors || []),
...(fetchedState.anchor._id ? [fetchedState.anchor] : []),
...(fetchedState.anchor.id ? [fetchedState.anchor] : []),
...(fetchedState.successors || []),
],
[fetchedState.predecessors, fetchedState.anchor, fetchedState.successors]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { indexPatternMock } from '../../__mocks__/index_pattern';
import { DiscoverGrid } from '../../components/discover_grid/discover_grid';
import { discoverServiceMock } from '../../__mocks__/services';
import { DocTableWrapper } from '../../components/doc_table/doc_table_wrapper';
import { EsHitRecordList } from '../types';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { buildDataTableRecord } from '../../utils/build_data_record';

describe('ContextAppContent test', () => {
const mountComponent = ({
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('ContextAppContent test', () => {
anchorStatus: anchorStatus || LoadingStatus.LOADED,
predecessorsStatus: LoadingStatus.LOADED,
successorsStatus: LoadingStatus.LOADED,
rows: [hit] as unknown as EsHitRecordList,
rows: [buildDataTableRecord(hit, indexPatternMock)],
predecessors: [],
successors: [],
defaultStepSize: 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import { AppState } from './services/context_state';
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 { EsHitRecordList } from '../types';
import { SortPairArr } from '../../components/doc_table/utils/get_sort';
import { ElasticSearchHit } from '../../types';
import type { SortPairArr } from '../../components/doc_table/utils/get_sort';
import { useDiscoverServices } from '../../hooks/use_discover_services';
import type { DataTableRecord } from '../../types';

export interface ContextAppContentProps {
columns: string[];
Expand All @@ -33,9 +32,9 @@ export interface ContextAppContentProps {
indexPattern: DataView;
predecessorCount: number;
successorCount: number;
rows: EsHitRecordList;
predecessors: EsHitRecordList;
successors: EsHitRecordList;
rows: DataTableRecord[];
predecessors: DataTableRecord[];
successors: DataTableRecord[];
anchorStatus: LoadingStatus;
predecessorsStatus: LoadingStatus;
successorsStatus: LoadingStatus;
Expand Down Expand Up @@ -76,7 +75,7 @@ export function ContextAppContent({
}: ContextAppContentProps) {
const { uiSettings: config } = useDiscoverServices();

const [expandedDoc, setExpandedDoc] = useState<ElasticSearchHit | undefined>();
const [expandedDoc, setExpandedDoc] = useState<DataTableRecord | undefined>();
const isAnchorLoading =
anchorStatus === LoadingStatus.LOADING || anchorStatus === LoadingStatus.UNINITIALIZED;
const arePredecessorsLoading =
Expand Down
Loading

0 comments on commit 9c1202f

Please sign in to comment.