Skip to content

Commit

Permalink
[Discover] Move state management and data fetching code to new folders (
Browse files Browse the repository at this point in the history
#182063)

## Summary

These commits are split out from my working branch for #181963 to make
reviewing easier.

Our state management code is currently split between a folder called
`services` and the `utils` folder. This makes working with the code more
difficult, especially where One Discover will soon introduce additional
complexity. To make the state management code easier to work with, I
moved all of the related files into a single folder called
`state_management` with a `utils` subfolder for its utility functions.

Similarly, all of our data fetching code was spread between files in the
`utils` folder. For the same reason, I moved these files into a new
folder called `data_fetching`, since they will also undergo a lot of
changes for One Discover.

Part of #181963.

### Checklist

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [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
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
  • Loading branch information
davismcphee authored May 3, 2024
1 parent f841763 commit 828b5c9
Show file tree
Hide file tree
Showing 102 changed files with 183 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { createBrowserHistory } from 'history';
import { getDiscoverStateContainer } from '../application/main/services/discover_state';
import { getDiscoverStateContainer } from '../application/main/state_management/discover_state';
import { savedSearchMockWithTimeField, savedSearchMock } from './saved_search';
import { discoverServiceMock } from './services';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover/public/__mocks__/search_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { createMemoryHistory } from 'history';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import { DataPublicPluginStart } from '@kbn/data-plugin/public';
import { DiscoverSearchSessionManager } from '../application/main/services/discover_search_session';
import { DiscoverSearchSessionManager } from '../application/main/state_management/discover_search_session';

export function createSearchSessionMock(
session = dataPluginMock.createStartContract().search.session as jest.Mocked<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React from 'react';
import { useQuerySubscriber } from '@kbn/unified-field-list/src/hooks/use_query_subscriber';
import { useSavedSearch } from '../../services/discover_state_provider';
import { useSavedSearch } from '../../state_management/discover_state_provider';
import { FieldStatisticsTable, type FieldStatisticsTableProps } from './field_stats_table';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import useObservable from 'react-use/lib/useObservable';
import { of } from 'rxjs';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { FIELD_STATISTICS_LOADED } from './constants';
import type { DiscoverStateContainer } from '../../services/discover_state';
import type { DiscoverStateContainer } from '../../state_management/discover_state';
export interface RandomSamplingOption {
mode: 'random_sampling';
seed: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import { act } from 'react-dom/test-utils';
import { BehaviorSubject } from 'rxjs';
import { findTestSubject } from '@elastic/eui/lib/test';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { DataDocuments$ } from '../../services/discover_data_state_container';
import { DataDocuments$ } from '../../state_management/discover_data_state_container';
import { discoverServiceMock } from '../../../../__mocks__/services';
import { FetchStatus } from '../../../types';
import { DiscoverDocuments, onResize } from './discover_documents';
import { dataViewMock, esHitsMock } from '@kbn/discover-utils/src/__mocks__';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { buildDataTableRecord } from '@kbn/discover-utils';
import type { EsHitRecord } from '@kbn/discover-utils/types';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { DiscoverMainProvider } from '../../state_management/discover_state_provider';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { DiscoverAppState } from '../../services/discover_app_state_container';
import { DiscoverAppState } from '../../state_management/discover_app_state_container';
import { DiscoverCustomization, DiscoverCustomizationProvider } from '../../../../customizations';
import { createCustomizationService } from '../../../../customizations/customization_service';
import { DiscoverGrid } from '../../../../components/discover_grid';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ import useObservable from 'react-use/lib/useObservable';
import type { DocViewFilterFn } from '@kbn/unified-doc-viewer/types';
import { DiscoverGrid } from '../../../../components/discover_grid';
import { getDefaultRowsPerPage } from '../../../../../common/constants';
import { useInternalStateSelector } from '../../services/discover_internal_state_container';
import { useAppStateSelector } from '../../services/discover_app_state_container';
import { useInternalStateSelector } from '../../state_management/discover_internal_state_container';
import { useAppStateSelector } from '../../state_management/discover_app_state_container';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { FetchStatus } from '../../../types';
import { RecordRawType } from '../../services/discover_data_state_container';
import { DiscoverStateContainer } from '../../services/discover_state';
import { RecordRawType } from '../../state_management/discover_data_state_container';
import { DiscoverStateContainer } from '../../state_management/discover_state';
import { useDataState } from '../../hooks/use_data_state';
import { DocTableInfinite } from '../../../../components/doc_table/doc_table_infinite';
import { DocumentExplorerCallout } from '../document_explorer_callout';
Expand All @@ -62,7 +62,7 @@ import {
getAllowedSampleSize,
} from '../../../../utils/get_allowed_sample_size';
import { DiscoverGridFlyout } from '../../../../components/discover_grid_flyout';
import { useSavedSearchInitial } from '../../services/discover_state_provider';
import { useSavedSearchInitial } from '../../state_management/discover_state_provider';
import { useFetchMoreRecords } from './use_fetch_more_records';
import { SelectedVSAvailableCallout } from './selected_vs_available_callout';
import { useDiscoverCustomization } from '../../../../customizations';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
DataMain$,
DataTotalHits$,
RecordRawType,
} from '../../services/discover_data_state_container';
} from '../../state_management/discover_data_state_container';
import { discoverServiceMock } from '../../../../__mocks__/services';
import { FetchStatus, SidebarToggleState } from '../../../types';
import { KibanaRenderContextProvider } from '@kbn/react-kibana-context-render';
Expand All @@ -31,7 +31,7 @@ import { createSearchSessionMock } from '../../../../__mocks__/search_session';
import { searchSourceInstanceMock } from '@kbn/data-plugin/common/search/search_source/mocks';
import { getSessionServiceMock } from '@kbn/data-plugin/public/search/session/mocks';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { DiscoverMainProvider } from '../../state_management/discover_state_provider';
import { act } from 'react-dom/test-utils';
import { PanelsToggle } from '../../../../components/panels_toggle';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import useObservable from 'react-use/lib/useObservable';
import type { Datatable } from '@kbn/expressions-plugin/common';
import { useDiscoverHistogram } from './use_discover_histogram';
import { type DiscoverMainContentProps, DiscoverMainContent } from './discover_main_content';
import { useAppStateSelector } from '../../services/discover_app_state_container';
import { useAppStateSelector } from '../../state_management/discover_app_state_container';
import { FetchStatus } from '../../../types';

export interface DiscoverHistogramLayoutProps extends DiscoverMainContentProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
DataMain$,
DataTotalHits$,
RecordRawType,
} from '../../services/discover_data_state_container';
} from '../../state_management/discover_data_state_container';
import { createDiscoverServicesMock } from '../../../../__mocks__/services';
import { FetchStatus } from '../../../types';
import { RequestAdapter } from '@kbn/inspector-plugin/common';
Expand All @@ -35,7 +35,7 @@ import { buildDataTableRecord } from '@kbn/discover-utils';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { createSearchSessionMock } from '../../../../__mocks__/search_session';
import { getSessionServiceMock } from '@kbn/data-plugin/public/search/session/mocks';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { DiscoverMainProvider } from '../../state_management/discover_state_provider';
import { act } from 'react-dom/test-utils';
import { ErrorCallout } from '../../../../components/common/error_callout';
import { PanelsToggle } from '../../../../components/panels_toggle';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ import {
import { popularizeField, useColumns } from '@kbn/unified-data-table';
import { DocViewFilterFn } from '@kbn/unified-doc-viewer/types';
import { BehaviorSubject } from 'rxjs';
import { useSavedSearchInitial } from '../../services/discover_state_provider';
import { DiscoverStateContainer } from '../../services/discover_state';
import { useSavedSearchInitial } from '../../state_management/discover_state_provider';
import { DiscoverStateContainer } from '../../state_management/discover_state';
import { VIEW_MODE } from '../../../../../common/constants';
import { useInternalStateSelector } from '../../services/discover_internal_state_container';
import { useAppStateSelector } from '../../services/discover_app_state_container';
import { useInternalStateSelector } from '../../state_management/discover_internal_state_container';
import { useAppStateSelector } from '../../state_management/discover_app_state_container';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { DiscoverNoResults } from '../no_results';
import { LoadingSpinner } from '../loading_spinner/loading_spinner';
import { DiscoverSidebarResponsive } from '../sidebar';
import { DiscoverTopNav } from '../top_nav/discover_topnav';
import { getResultState } from '../../utils/get_result_state';
import { DiscoverUninitialized } from '../uninitialized/uninitialized';
import { DataMainMsg, RecordRawType } from '../../services/discover_data_state_container';
import { DataMainMsg, RecordRawType } from '../../state_management/discover_data_state_container';
import { FetchStatus, SidebarToggleState } from '../../../types';
import { useDataState } from '../../hooks/use_data_state';
import { getRawRecordType } from '../../utils/get_raw_record_type';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
DataMain$,
DataTotalHits$,
RecordRawType,
} from '../../services/discover_data_state_container';
} from '../../state_management/discover_data_state_container';
import { createDiscoverServicesMock } from '../../../../__mocks__/services';
import { FetchStatus, SidebarToggleState } from '../../../types';
import { KibanaRenderContextProvider } from '@kbn/react-kibana-context-render';
Expand All @@ -31,7 +31,7 @@ import { DocumentViewModeToggle } from '../../../../components/view_mode_toggle'
import { searchSourceInstanceMock } from '@kbn/data-plugin/common/search/search_source/mocks';
import { DiscoverDocuments } from './discover_documents';
import { FieldStatisticsTab } from '../field_stats_table';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { DiscoverMainProvider } from '../../state_management/discover_state_provider';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { PanelsToggle } from '../../../../components/panels_toggle';
import type { Storage } from '@kbn/kibana-utils-plugin/public';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import type { DocViewFilterFn } from '@kbn/unified-doc-viewer/types';
import { VIEW_MODE } from '../../../../../common/constants';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { DocumentViewModeToggle } from '../../../../components/view_mode_toggle';
import { DiscoverStateContainer } from '../../services/discover_state';
import { DiscoverStateContainer } from '../../state_management/discover_state';
import { FieldStatisticsTab } from '../field_stats_table';
import { DiscoverDocuments } from './discover_documents';
import { DOCUMENTS_VIEW_CLICK, FIELD_STATISTICS_VIEW_CLICK } from '../field_stats_table/constants';
import { useAppStateSelector } from '../../services/discover_app_state_container';
import { useAppStateSelector } from '../../state_management/discover_app_state_container';
import type { PanelsToggleProps } from '../../../../components/panels_toggle';

const DROP_PROPS = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { AggregateQuery, Query } from '@kbn/es-query';
import { act, renderHook, WrapperComponent } from '@testing-library/react-hooks';
import { BehaviorSubject, Subject } from 'rxjs';
import { FetchStatus } from '../../../types';
import type { DiscoverStateContainer } from '../../services/discover_state';
import type { DiscoverStateContainer } from '../../state_management/discover_state';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import { useDiscoverHistogram, UseDiscoverHistogramProps } from './use_discover_histogram';
import { setTimeout } from 'timers/promises';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { DiscoverMainProvider } from '../../state_management/discover_state_provider';
import { RequestAdapter } from '@kbn/inspector-plugin/public';
import {
UnifiedHistogramFetchStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { FetchStatus } from '../../../types';
import type { InspectorAdapters } from '../../hooks/use_inspector';
import { checkHitCount, sendErrorTo } from '../../hooks/use_saved_search_messages';
import type { DiscoverStateContainer } from '../../services/discover_state';
import type { DiscoverStateContainer } from '../../state_management/discover_state';
import { addLog } from '../../../../utils/add_log';
import { useInternalStateSelector } from '../../services/discover_internal_state_container';
import type { DiscoverAppState } from '../../services/discover_app_state_container';
import { DataDocumentsMsg, RecordRawType } from '../../services/discover_data_state_container';
import { useSavedSearch } from '../../services/discover_state_provider';
import { useInternalStateSelector } from '../../state_management/discover_internal_state_container';
import type { DiscoverAppState } from '../../state_management/discover_app_state_container';
import {
DataDocumentsMsg,
RecordRawType,
} from '../../state_management/discover_data_state_container';
import { useSavedSearch } from '../../state_management/discover_state_provider';

const EMPTY_TEXT_BASED_COLUMNS: DatatableColumn[] = [];
const EMPTY_FILTERS: Filter[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { buildDataTableRecord } from '@kbn/discover-utils';
import { dataViewMock, esHitsMockWithSort } from '@kbn/discover-utils/src/__mocks__';
import { useFetchMoreRecords } from './use_fetch_more_records';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { DataDocuments$, DataTotalHits$ } from '../../services/discover_data_state_container';
import {
DataDocuments$,
DataTotalHits$,
} from '../../state_management/discover_data_state_container';
import { FetchStatus } from '../../../types';

describe('useFetchMoreRecords', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { useMemo } from 'react';
import { FetchStatus } from '../../../types';
import { useDataState } from '../../hooks/use_data_state';
import type { DiscoverStateContainer } from '../../services/discover_state';
import type { DiscoverStateContainer } from '../../state_management/discover_state';

/**
* Params for the hook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { DataView } from '@kbn/data-views-plugin/common';
import type { AggregateQuery, Filter, Query } from '@kbn/es-query';
import { SearchResponseWarningsEmptyPrompt } from '@kbn/search-response-warnings';
import { NoResultsSuggestions } from './no_results_suggestions';
import type { DiscoverStateContainer } from '../../services/discover_state';
import type { DiscoverStateContainer } from '../../state_management/discover_state';
import { useDataState } from '../../hooks/use_data_state';
import './_no_results.scss';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import {
AvailableFields$,
DataDocuments$,
RecordRawType,
} from '../../services/discover_data_state_container';
} from '../../state_management/discover_data_state_container';
import { stubLogstashDataView } from '@kbn/data-plugin/common/stubs';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { DiscoverAppStateProvider } from '../../services/discover_app_state_container';
import { DiscoverAppStateProvider } from '../../state_management/discover_app_state_container';
import * as ExistingFieldsServiceApi from '@kbn/unified-field-list/src/services/field_existing/load_field_existing';
import { resetExistingFieldsCache } from '@kbn/unified-field-list/src/hooks/use_existing_fields';
import { createDiscoverServicesMock } from '../../../../__mocks__/services';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
AvailableFields$,
DataDocuments$,
RecordRawType,
} from '../../services/discover_data_state_container';
} from '../../state_management/discover_data_state_container';
import { calcFieldCounts } from '../../utils/calc_field_counts';
import { FetchStatus, SidebarToggleState } from '../../../types';
import { DISCOVER_TOUR_STEP_ANCHOR_IDS } from '../../../../components/discover_tour';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DiscoverTopNav, DiscoverTopNavProps } from './discover_topnav';
import { TopNavMenu, TopNavMenuData } from '@kbn/navigation-plugin/public';
import { discoverServiceMock as mockDiscoverService } from '../../../../__mocks__/services';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { DiscoverMainProvider } from '../../state_management/discover_state_provider';
import type { SearchBarCustomization, TopNavCustomization } from '../../../../customizations';
import type { DiscoverCustomizationId } from '../../../../customizations/customization_service';
import { useDiscoverCustomization } from '../../../../customizations';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import {
useSavedSearch,
useSavedSearchHasChanged,
useSavedSearchInitial,
} from '../../services/discover_state_provider';
import { useInternalStateSelector } from '../../services/discover_internal_state_container';
} from '../../state_management/discover_state_provider';
import { useInternalStateSelector } from '../../state_management/discover_internal_state_container';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import type { DiscoverStateContainer } from '../../services/discover_state';
import type { DiscoverStateContainer } from '../../state_management/discover_state';
import { onSaveSearch } from './on_save_search';
import { useDiscoverCustomization } from '../../../../customizations';
import { addLog } from '../../../../utils/add_log';
import { useAppStateSelector } from '../../services/discover_app_state_container';
import { useAppStateSelector } from '../../state_management/discover_app_state_container';
import { isTextBasedQuery } from '../../utils/is_text_based_query';
import { useDiscoverTopNav } from './use_discover_topnav';

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

import React from 'react';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { DiscoverMainProvider } from '../../state_management/discover_state_provider';
import { DiscoverTopNavInline } from './discover_topnav_inline';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { euiThemeVars } from '@kbn/ui-theme';
import { LogsExplorerTabs } from '../../../../components/logs_explorer_tabs';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { useDiscoverTopNav } from './use_discover_topnav';
import type { DiscoverStateContainer } from '../../services/discover_state';
import type { DiscoverStateContainer } from '../../state_management/discover_state';

export const DiscoverTopNavInline = ({
stateContainer,
Expand Down
Loading

0 comments on commit 828b5c9

Please sign in to comment.