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] Add container for data state (migrated from useSavedSearch hook) #148614

Merged
merged 21 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
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
4 changes: 4 additions & 0 deletions src/plugins/discover/public/__mocks__/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export function createDiscoverServicesMock(): DiscoverServices {
dataPlugin.query.timefilter.timefilter.getTime = jest.fn(() => {
return { from: 'now-15m', to: 'now' };
});
dataPlugin.query.timefilter.timefilter.getRefreshInterval = jest.fn(() => {
return { pause: true, value: 1000 };
});

dataPlugin.query.timefilter.timefilter.calculateBounds = jest.fn(calculateBounds);
dataPlugin.query.getState = jest.fn(() => ({
query: { query: '', language: 'lucene' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { css } from '@emotion/react';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { FIELD_STATISTICS_LOADED } from './constants';
import type { DiscoverStateContainer } from '../../services/discover_state';
import { AvailableFields$, DataRefetch$, DataTotalHits$ } from '../../hooks/use_saved_search';
export interface RandomSamplingOption {
mode: 'random_sampling';
seed: string;
Expand Down Expand Up @@ -106,15 +105,11 @@ export interface FieldStatisticsTableProps {
* @param eventName
*/
trackUiMetric?: (metricType: UiCounterMetricType, eventName: string | string[]) => void;
savedSearchRefetch$?: DataRefetch$;
availableFields$?: AvailableFields$;
searchSessionId?: string;
savedSearchDataTotalHits$?: DataTotalHits$;
}

export const FieldStatisticsTable = (props: FieldStatisticsTableProps) => {
const {
availableFields$,
dataView,
savedSearch,
query,
Expand All @@ -123,10 +118,9 @@ export const FieldStatisticsTable = (props: FieldStatisticsTableProps) => {
stateContainer,
onAddFilter,
trackUiMetric,
savedSearchRefetch$,
searchSessionId,
savedSearchDataTotalHits$,
} = props;
const totalHits$ = stateContainer?.dataState.data$.totalHits$;
const services = useDiscoverServices();
const [embeddable, setEmbeddable] = useState<
| ErrorEmbeddable
Expand All @@ -141,13 +135,14 @@ export const FieldStatisticsTable = (props: FieldStatisticsTableProps) => {
);

useEffect(() => {
const availableFields$ = stateContainer?.dataState.data$.availableFields$;
kertal marked this conversation as resolved.
Show resolved Hide resolved
const sub = embeddable?.getOutput$().subscribe((output: DataVisualizerGridEmbeddableOutput) => {
if (output.showDistributions !== undefined && stateContainer) {
stateContainer.setAppState({ hideAggregatedPreview: !output.showDistributions });
}
});

const refetch = savedSearchRefetch$?.subscribe(() => {
const refetch = stateContainer?.dataState.refetch$.subscribe(() => {
if (embeddable && !isErrorEmbeddable(embeddable)) {
embeddable.updateInput({ lastReloadRequestTime: Date.now() });
}
Expand All @@ -164,7 +159,7 @@ export const FieldStatisticsTable = (props: FieldStatisticsTableProps) => {
refetch?.unsubscribe();
fields?.unsubscribe();
};
}, [embeddable, stateContainer, savedSearchRefetch$, availableFields$]);
}, [embeddable, stateContainer]);

useEffect(() => {
if (embeddable && !isErrorEmbeddable(embeddable)) {
Expand All @@ -177,10 +172,8 @@ export const FieldStatisticsTable = (props: FieldStatisticsTableProps) => {
visibleFieldNames: columns,
onAddFilter,
sessionId: searchSessionId,
fieldsToFetch: availableFields$?.getValue().fields,
totalDocuments: savedSearchDataTotalHits$
? savedSearchDataTotalHits$.getValue()?.result
: undefined,
fieldsToFetch: stateContainer?.dataState.data$.availableFields$?.getValue().fields,
totalDocuments: totalHits$ ? totalHits$.getValue()?.result : undefined,
samplingOption: {
mode: 'normal_sampling',
shardSize: 5000,
Expand All @@ -198,8 +191,8 @@ export const FieldStatisticsTable = (props: FieldStatisticsTableProps) => {
filters,
onAddFilter,
searchSessionId,
availableFields$,
savedSearchDataTotalHits$,
totalHits$,
stateContainer,
]);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
DataMain$,
DataTotalHits$,
RecordRawType,
} from '../../../hooks/use_saved_search';
} from '../../../services/discover_data_state_container';
import { buildDataTableRecordList } from '../../../../../utils/build_data_record';
import { esHits } from '../../../../../__mocks__/es_hits';
import { SavedSearch } from '../../../../..';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { setHeaderActionMenuMounter } from '../../../../kibana_services';
import { esHits } from '../../../../__mocks__/es_hits';
import { savedSearchMock } from '../../../../__mocks__/saved_search';
import { DiscoverStateContainer } from '../../services/discover_state';
import { DataDocuments$ } from '../../hooks/use_saved_search';
import { DataDocuments$ } from '../../services/discover_data_state_container';
import { discoverServiceMock } from '../../../../__mocks__/services';
import { FetchStatus } from '../../../types';
import { DiscoverDocuments, onResize } from './discover_documents';
Expand All @@ -39,14 +39,14 @@ function mountComponent(fetchStatus: FetchStatus, hits: EsHitRecord[]) {
}) as DataDocuments$;
const stateContainer = getDiscoverStateMock({});
stateContainer.setAppState({ index: dataViewMock.id });
stateContainer.dataState.data$.documents$ = documents$;

const props = {
expandedDoc: undefined,
dataView: dataViewMock,
onAddFilter: jest.fn(),
savedSearch: savedSearchMock,
documents$,
searchSource: documents$,
searchSource: savedSearchMock.searchSource,
setExpandedDoc: jest.fn(),
state: { columns: [] },
stateContainer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
HIDE_ANNOUNCEMENTS,
} from '../../../../../common';
import { useColumns } from '../../../../hooks/use_data_grid_columns';
import { DataDocuments$, RecordRawType } from '../../hooks/use_saved_search';
import { RecordRawType } from '../../services/discover_data_state_container';
import { DiscoverStateContainer } from '../../services/discover_state';
import { useDataState } from '../../hooks/use_data_state';
import { DocTableInfinite } from '../../../../components/doc_table/doc_table_infinite';
Expand Down Expand Up @@ -59,7 +59,6 @@ export const onResize = (
};

function DiscoverDocumentsComponent({
documents$,
expandedDoc,
dataView,
onAddFilter,
Expand All @@ -68,7 +67,6 @@ function DiscoverDocumentsComponent({
stateContainer,
onFieldEdited,
}: {
documents$: DataDocuments$;
expandedDoc?: DataTableRecord;
dataView: DataView;
navigateTo: (url: string) => void;
Expand All @@ -79,6 +77,7 @@ function DiscoverDocumentsComponent({
onFieldEdited?: () => void;
}) {
const services = useDiscoverServices();
const documents$ = stateContainer.dataState.data$.documents$;
const { dataViews, capabilities, uiSettings } = services;
const [query, sort, rowHeight, rowsPerPage, grid, columns, index] = useAppStateSelector(
(state) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { Subject, BehaviorSubject, of } from 'rxjs';
import { BehaviorSubject, of } from 'rxjs';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { esHits } from '../../../../__mocks__/es_hits';
import { dataViewMock } from '../../../../__mocks__/data_view';
Expand All @@ -18,7 +18,7 @@ import {
DataMain$,
DataTotalHits$,
RecordRawType,
} from '../../hooks/use_saved_search';
} from '../../services/discover_data_state_container';
import { discoverServiceMock } from '../../../../__mocks__/services';
import { FetchStatus } from '../../../types';
import { KibanaContextProvider, KibanaThemeProvider } from '@kbn/kibana-react-plugin/public';
Expand Down Expand Up @@ -117,16 +117,14 @@ const mountComponent = ({
session.getSession$.mockReturnValue(new BehaviorSubject('123'));

const stateContainer = getStateContainer();
stateContainer.dataState.data$ = savedSearchData$;

const props: DiscoverHistogramLayoutProps = {
isPlainRecord,
dataView: dataViewMock,
navigateTo: jest.fn(),
setExpandedDoc: jest.fn(),
savedSearch,
savedSearchData$,
savedSearchFetch$: new Subject(),
savedSearchRefetch$: new Subject(),
stateContainer,
onFieldEdited: jest.fn(),
columns: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@ import type { DiscoverSearchSessionManager } from '../../services/discover_searc
import type { InspectorAdapters } from '../../hooks/use_inspector';
import { type DiscoverMainContentProps, DiscoverMainContent } from './discover_main_content';
import { ResetSearchButton } from './reset_search_button';
import type { DataFetch$ } from '../../hooks/use_saved_search';

export interface DiscoverHistogramLayoutProps extends DiscoverMainContentProps {
resetSavedSearch: () => void;
isTimeBased: boolean;
resizeRef: RefObject<HTMLDivElement>;
inspectorAdapters: InspectorAdapters;
searchSessionManager: DiscoverSearchSessionManager;
savedSearchFetch$: DataFetch$;
}

export const DiscoverHistogramLayout = ({
isPlainRecord,
dataView,
resetSavedSearch,
savedSearch,
savedSearchData$,
savedSearchFetch$,
stateContainer,
isTimeBased,
resizeRef,
Expand All @@ -47,14 +43,14 @@ export const DiscoverHistogramLayout = ({
isPlainRecord,
stateContainer,
savedSearch,
savedSearchData$,
savedSearchData$: stateContainer.dataState.data$,
};

const histogramProps = useDiscoverHistogram({
isTimeBased,
inspectorAdapters,
searchSessionManager,
savedSearchFetch$,
savedSearchFetch$: stateContainer.dataState.fetch$,
...commonProps,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { Subject, BehaviorSubject, of } from 'rxjs';
import { BehaviorSubject, of } from 'rxjs';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import type { Query, AggregateQuery } from '@kbn/es-query';
import { setHeaderActionMenuMounter } from '../../../../kibana_services';
Expand All @@ -24,12 +24,10 @@ import { dataViewWithTimefieldMock } from '../../../../__mocks__/data_view_with_
import {
AvailableFields$,
DataDocuments$,
DataFetch$,
DataMain$,
DataRefetch$,
DataTotalHits$,
RecordRawType,
} from '../../hooks/use_saved_search';
} from '../../services/discover_data_state_container';
import { createDiscoverServicesMock } from '../../../../__mocks__/services';
import { FetchStatus } from '../../../types';
import { RequestAdapter } from '@kbn/inspector-plugin/common';
Expand Down Expand Up @@ -93,7 +91,7 @@ function mountComponent(
result: Number(esHits.length),
}) as DataTotalHits$;

const savedSearchData$ = {
stateContainer.dataState.data$ = {
main$,
documents$,
totalHits$,
Expand All @@ -115,9 +113,6 @@ function mountComponent(
onUpdateQuery: jest.fn(),
resetSavedSearch: jest.fn(),
savedSearch: savedSearchMock,
savedSearchData$,
savedSearchFetch$: new Subject() as DataFetch$,
savedSearchRefetch$: new Subject() as DataRefetch$,
searchSource: searchSourceMock,
state: { columns: [], query, hideChart: false, interval: 'auto' },
stateContainer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { DiscoverTopNav } from '../top_nav/discover_topnav';
import { DocViewFilterFn } from '../../../../services/doc_views/doc_views_types';
import { getResultState } from '../../utils/get_result_state';
import { DiscoverUninitialized } from '../uninitialized/uninitialized';
import { DataMainMsg, RecordRawType } from '../../hooks/use_saved_search';
import { DataMainMsg, RecordRawType } from '../../services/discover_data_state_container';
import { useColumns } from '../../../../hooks/use_data_grid_columns';
import { FetchStatus } from '../../../types';
import { useDataState } from '../../hooks/use_data_state';
Expand All @@ -62,10 +62,7 @@ export function DiscoverLayout({
onChangeDataView,
onUpdateQuery,
setExpandedDoc,
savedSearchFetch$,
savedSearchRefetch$,
resetSavedSearch,
savedSearchData$,
savedSearch,
searchSource,
stateContainer,
Expand All @@ -86,7 +83,7 @@ export function DiscoverLayout({
spaces,
inspector,
} = useDiscoverServices();
const { main$ } = savedSearchData$;
const { main$ } = stateContainer.dataState.data$;
const [query, savedQuery, filters, columns, sort] = useAppStateSelector((state) => [
state.query,
state.savedQuery,
Expand Down Expand Up @@ -166,8 +163,8 @@ export function DiscoverLayout({
if (!dataView.isPersisted()) {
await updateAdHocDataViewId(dataView);
}
savedSearchRefetch$.next('reset');
}, [dataView, savedSearchRefetch$, updateAdHocDataViewId]);
stateContainer.dataState.refetch$.next('reset');
}, [dataView, stateContainer, updateAdHocDataViewId]);

const onDisableFilters = useCallback(() => {
const disabledFilters = filterManager
Expand Down Expand Up @@ -224,7 +221,11 @@ export function DiscoverLayout({
}

if (resultState === 'uninitialized') {
return <DiscoverUninitialized onRefresh={() => savedSearchRefetch$.next(undefined)} />;
return (
<DiscoverUninitialized
onRefresh={() => stateContainer.dataState.refetch$.next(undefined)}
/>
);
}

return (
Expand All @@ -237,9 +238,6 @@ export function DiscoverLayout({
expandedDoc={expandedDoc}
setExpandedDoc={setExpandedDoc}
savedSearch={savedSearch}
savedSearchData$={savedSearchData$}
savedSearchFetch$={savedSearchFetch$}
savedSearchRefetch$={savedSearchRefetch$}
stateContainer={stateContainer}
isTimeBased={isTimeBased}
columns={currentColumns}
Expand Down Expand Up @@ -271,9 +269,6 @@ export function DiscoverLayout({
resetSavedSearch,
resultState,
savedSearch,
savedSearchData$,
savedSearchFetch$,
savedSearchRefetch$,
searchSessionManager,
setExpandedDoc,
stateContainer,
Expand Down Expand Up @@ -328,7 +323,7 @@ export function DiscoverLayout({
<EuiFlexGroup className="dscPageBody__contents" gutterSize="s">
<EuiFlexItem grow={false} className="dscPageBody__sidebar">
<SidebarMemoized
documents$={savedSearchData$.documents$}
documents$={stateContainer.dataState.data$.documents$}
onAddField={onAddColumn}
columns={currentColumns}
onAddFilter={!isPlainRecord ? onAddFilter : undefined}
Expand All @@ -341,7 +336,7 @@ export function DiscoverLayout({
onFieldEdited={onFieldEdited}
viewMode={viewMode}
onDataViewCreated={onDataViewCreated}
availableFields$={savedSearchData$.availableFields$}
availableFields$={stateContainer.dataState.data$.availableFields$}
/>
</EuiFlexItem>
<EuiHideFor sizes={['xs', 's']}>
Expand Down
Loading