From a472a2a5249d671c065c8826be6da031f3288d6b Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 6 Dec 2024 10:04:20 +0100 Subject: [PATCH 1/9] Initial histogram debugging --- .../components/layout/discover_documents.tsx | 9 ++---- .../layout/use_discover_histogram.ts | 15 +++------ .../main/data_fetching/fetch_all.ts | 2 ++ .../main/data_fetching/fetch_esql.ts | 2 +- .../data_fetching/update_search_source.ts | 6 ++-- .../main/hooks/use_saved_search_messages.ts | 31 ++++++++++++++++--- .../discover_data_state_container.ts | 8 +++-- 7 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/plugins/discover/public/application/main/components/layout/discover_documents.tsx b/src/plugins/discover/public/application/main/components/layout/discover_documents.tsx index 6092a59333290..376bfe103adcf 100644 --- a/src/plugins/discover/public/application/main/components/layout/discover_documents.tsx +++ b/src/plugins/discover/public/application/main/components/layout/discover_documents.tsx @@ -49,7 +49,6 @@ import useObservable from 'react-use/lib/useObservable'; import type { DocViewFilterFn } from '@kbn/unified-doc-viewer/types'; import { DiscoverGridSettings } from '@kbn/saved-search-plugin/common'; import { useQuerySubscriber } from '@kbn/unified-field-list'; -import { map } from 'rxjs'; import { DiscoverGrid } from '../../../../components/discover_grid'; import { getDefaultRowsPerPage } from '../../../../../common/constants'; import { useInternalStateSelector } from '../../state_management/discover_internal_state_container'; @@ -116,6 +115,7 @@ function DiscoverDocumentsComponent({ }) { const services = useDiscoverServices(); const documents$ = stateContainer.dataState.data$.documents$; + const main$ = stateContainer.dataState.data$.main$; const savedSearch = useSavedSearchInitial(); const { dataViews, capabilities, uiSettings, uiActions, ebtManager, fieldsMetadata } = services; const [ @@ -286,17 +286,12 @@ function DiscoverDocumentsComponent({ const { filters } = useQuerySubscriber({ data: services.data }); - const timeRange = useObservable( - services.timefilter.getTimeUpdate$().pipe(map(() => services.timefilter.getTime())), - services.timefilter.getTime() - ); - const cellActionsMetadata = useAdditionalCellActions({ dataSource, dataView, query, filters, - timeRange, + timeRange: main$.getValue().timeRange, }); const renderDocumentView = useCallback( diff --git a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts index 3f2acf0ce933b..6d2f620c36b47 100644 --- a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts +++ b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts @@ -216,14 +216,10 @@ export const useDiscoverHistogram = ({ /** * Request params */ - const { query, filters } = useQuerySubscriber({ data: services.data }); + const { filters } = useQuerySubscriber({ data: services.data }); const customFilters = useInternalStateSelector((state) => state.customFilters); - const timefilter = services.data.query.timefilter.timefilter; - const timeRange = timefilter.getAbsoluteTime(); - const relativeTimeRange = useObservable( - timefilter.getTimeUpdate$().pipe(map(() => timefilter.getTime())), - timefilter.getTime() - ); + const relativeTimeRange = main$.getValue().timeRangeRelative; + const query = main$.getValue().query; // When in ES|QL mode, update the data view, query, and // columns only when documents are done fetching so the Lens suggestions @@ -337,9 +333,6 @@ export const useDiscoverHistogram = ({ return allFilters.length ? allFilters : EMPTY_FILTERS; }, [filters, customFilters]); - // eslint-disable-next-line react-hooks/exhaustive-deps - const timeRangeMemoized = useMemo(() => timeRange, [timeRange?.from, timeRange?.to]); - const onVisContextChanged = useCallback( ( nextVisContext: UnifiedHistogramVisContext | undefined, @@ -399,7 +392,7 @@ export const useDiscoverHistogram = ({ dataView: isEsqlMode ? esqlDataView : dataView, query: isEsqlMode ? esqlQuery : query, filters: filtersMemoized, - timeRange: timeRangeMemoized, + timeRange: main$.getValue().timeRange, relativeTimeRange, columns: isEsqlMode ? esqlColumns : undefined, onFilter: histogramCustomization?.onFilter, diff --git a/src/plugins/discover/public/application/main/data_fetching/fetch_all.ts b/src/plugins/discover/public/application/main/data_fetching/fetch_all.ts index 6a493b94d2fe4..8eb0c83efb4b1 100644 --- a/src/plugins/discover/public/application/main/data_fetching/fetch_all.ts +++ b/src/plugins/discover/public/application/main/data_fetching/fetch_all.ts @@ -98,6 +98,7 @@ export function fetchAll( services, sort: getAppState().sort as SortOrder[], customFilters: getInternalState().customFilters, + inputTimeRange: dataSubjects.main$.getValue().timeRange, }); } @@ -118,6 +119,7 @@ export function fetchAll( data, expressions, profilesManager, + inputTimeRange: dataSubjects.main$.getValue().timeRange, }) : fetchDocuments(searchSource, fetchDeps); const fetchType = isEsqlQuery ? 'fetchTextBased' : 'fetchDocuments'; diff --git a/src/plugins/discover/public/application/main/data_fetching/fetch_esql.ts b/src/plugins/discover/public/application/main/data_fetching/fetch_esql.ts index dca01247296a4..ad681bd969fe4 100644 --- a/src/plugins/discover/public/application/main/data_fetching/fetch_esql.ts +++ b/src/plugins/discover/public/application/main/data_fetching/fetch_esql.ts @@ -51,7 +51,7 @@ export function fetchEsql({ expressions: ExpressionsStart; profilesManager: ProfilesManager; }): Promise { - const timeRange = inputTimeRange ?? data.query.timefilter.timefilter.getTime(); + const timeRange = inputTimeRange ?? data.query.timefilter.timefilter.getAbsoluteTime(); return textBasedQueryStateToAstWithValidation({ filters, query, diff --git a/src/plugins/discover/public/application/main/data_fetching/update_search_source.ts b/src/plugins/discover/public/application/main/data_fetching/update_search_source.ts index 72c60fa584f3f..a6f6a35344ed8 100644 --- a/src/plugins/discover/public/application/main/data_fetching/update_search_source.ts +++ b/src/plugins/discover/public/application/main/data_fetching/update_search_source.ts @@ -9,7 +9,7 @@ import { ISearchSource } from '@kbn/data-plugin/public'; import { DataViewType, DataView } from '@kbn/data-views-plugin/public'; -import { Filter } from '@kbn/es-query'; +import { Filter, TimeRange} from '@kbn/es-query'; import type { SortOrder } from '@kbn/saved-search-plugin/public'; import { SEARCH_FIELDS_FROM_SOURCE, SORT_DEFAULT_ORDER_SETTING } from '@kbn/discover-utils'; import { DiscoverServices } from '../../../build_services'; @@ -25,11 +25,13 @@ export function updateVolatileSearchSource( services, sort, customFilters, + inputTimeRange, }: { dataView: DataView; services: DiscoverServices; sort?: SortOrder[]; customFilters: Filter[]; + inputTimeRange?: TimeRange; } ) { const { uiSettings, data } = services; @@ -49,7 +51,7 @@ export function updateVolatileSearchSource( if (dataView.type !== DataViewType.ROLLUP) { // Set the date range filter fields from timeFilter using the absolute format. Search sessions requires that it be converted from a relative range - const timeFilter = data.query.timefilter.timefilter.createFilter(dataView); + const timeFilter = data.query.timefilter.timefilter.createFilter(dataView, inputTimeRange); filters = timeFilter ? [...filters, timeFilter] : filters; } diff --git a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts index dbe7da63f5e76..0b41bbccecd82 100644 --- a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts +++ b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts @@ -10,6 +10,7 @@ import type { BehaviorSubject } from 'rxjs'; import type { DataTableRecord } from '@kbn/discover-utils/src/types'; import type { SearchResponseWarning } from '@kbn/search-response-warnings'; +import { AggregateQuery, Query, TimeRange } from '@kbn/es-query'; import { FetchStatus } from '../../types'; import type { DataDocuments$, @@ -37,7 +38,25 @@ export function sendCompleteMsg(main$: DataMain$, foundDocuments = true) { if (main$.getValue().fetchStatus === FetchStatus.COMPLETE) { return; } - main$.next({ fetchStatus: FetchStatus.COMPLETE, foundDocuments, error: undefined }); + main$.next({ + ...main$.getValue(), + fetchStatus: FetchStatus.COMPLETE, + foundDocuments, + error: undefined, + }); +} + +/** + * Send message containing timeRange via main observable + */ +export function sendTimeRangeMsg( + main$: DataMain$, + timeRange: TimeRange, + timeRangeRelative?: TimeRange, + query?: AggregateQuery | Query | undefined +) { + console.log('sendTimeRangeMsg', { timeRange }); + main$.next({ ...main$.getValue(), timeRange, timeRangeRelative, query }); } /** @@ -45,7 +64,7 @@ export function sendCompleteMsg(main$: DataMain$, foundDocuments = true) { */ export function sendPartialMsg(main$: DataMain$) { if (main$.getValue().fetchStatus === FetchStatus.LOADING) { - main$.next({ fetchStatus: FetchStatus.PARTIAL }); + main$.next({ ...main$.getValue(), fetchStatus: FetchStatus.PARTIAL }); } } @@ -57,7 +76,7 @@ export function sendLoadingMsg( props?: Omit ) { if (data$.getValue().fetchStatus !== FetchStatus.LOADING) { - data$.next({ ...props, fetchStatus: FetchStatus.LOADING } as T); + data$.next({ ...data$.getValue(), ...props, fetchStatus: FetchStatus.LOADING } as T); } } @@ -108,7 +127,11 @@ export function sendErrorMsg(data$: DataMain$ | DataDocuments$ | DataTotalHits$, * Needed when data view is switched or a new runtime field is added */ export function sendResetMsg(data: SavedSearchData, initialFetchStatus: FetchStatus) { - data.main$.next({ fetchStatus: initialFetchStatus, foundDocuments: undefined }); + data.main$.next({ + ...data.main$.getValue(), + fetchStatus: initialFetchStatus, + foundDocuments: undefined, + }); data.documents$.next({ fetchStatus: initialFetchStatus, result: [] }); data.totalHits$.next({ fetchStatus: initialFetchStatus, result: undefined }); } diff --git a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts index 5bc27fdb45a60..57a1a012b0482 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts @@ -12,7 +12,7 @@ import type { AutoRefreshDoneFn } from '@kbn/data-plugin/public'; import type { DatatableColumn } from '@kbn/expressions-plugin/common'; import { RequestAdapter } from '@kbn/inspector-plugin/common'; import type { SavedSearch } from '@kbn/saved-search-plugin/public'; -import { AggregateQuery, isOfAggregateQueryType, Query } from '@kbn/es-query'; +import { AggregateQuery, Filter, isOfAggregateQueryType, Query, TimeRange } from '@kbn/es-query'; import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types'; import type { DataView } from '@kbn/data-views-plugin/common'; import { reportPerformanceMetricEvent } from '@kbn/ebt-tools'; @@ -30,7 +30,7 @@ import type { DiscoverSearchSessionManager } from './discover_search_session'; import { FetchStatus } from '../../types'; import { validateTimeRange } from './utils/validate_time_range'; import { fetchAll, fetchMoreDocuments } from '../data_fetching/fetch_all'; -import { sendResetMsg } from '../hooks/use_saved_search_messages'; +import { sendResetMsg, sendTimeRangeMsg } from '../hooks/use_saved_search_messages'; import { getFetch$ } from '../data_fetching/get_fetch_observable'; import type { DiscoverInternalStateContainer } from './discover_internal_state_container'; import { getDefaultProfileState } from './utils/get_default_profile_state'; @@ -57,6 +57,9 @@ export interface DataMsg { export interface DataMainMsg extends DataMsg { foundDocuments?: boolean; + timeRange?: TimeRange; + timeRangeRelative?: TimeRange; + filter?: Filter[]; } export interface DataDocumentsMsg extends DataMsg { @@ -342,6 +345,7 @@ export function getDataStateContainer({ const fetchQuery = async (resetQuery?: boolean) => { const query = appStateContainer.getState().query; const currentDataView = getSavedSearch().searchSource.getField('index'); + sendTimeRangeMsg(dataSubjects.main$, timefilter.getAbsoluteTime(), timefilter.getTime(), query); if (isOfAggregateQueryType(query)) { const nextDataView = await getEsqlDataView(query, currentDataView, services); From 3e0ffc753c04934cebe3c5af33caccc38cad1ba0 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 6 Dec 2024 11:49:23 +0100 Subject: [PATCH 2/9] Fixing tests and lining --- .../components/layout/discover_histogram_layout.test.tsx | 8 ++------ .../main/components/layout/discover_layout.test.tsx | 4 +++- .../application/main/hooks/use_saved_search_messages.ts | 1 - 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/plugins/discover/public/application/main/components/layout/discover_histogram_layout.test.tsx b/src/plugins/discover/public/application/main/components/layout/discover_histogram_layout.test.tsx index 9aec3589c753e..7107046d73ef1 100644 --- a/src/plugins/discover/public/application/main/components/layout/discover_histogram_layout.test.tsx +++ b/src/plugins/discover/public/application/main/components/layout/discover_histogram_layout.test.tsx @@ -65,12 +65,6 @@ const mountComponent = async ({ const dataView = savedSearch?.searchSource?.getField('index') as DataView; let services = discoverServiceMock; - services.data.query.timefilter.timefilter.getAbsoluteTime = () => { - return { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }; - }; - services.data.query.timefilter.timefilter.getTime = () => { - return { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }; - }; (services.data.query.queryString.getDefaultQuery as jest.Mock).mockReturnValue({ language: 'kuery', query: '', @@ -86,6 +80,8 @@ const mountComponent = async ({ const main$ = new BehaviorSubject({ fetchStatus: FetchStatus.COMPLETE, foundDocuments: true, + timeRange: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, + timeRangeRelative: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, }) as DataMain$; const documents$ = new BehaviorSubject({ diff --git a/src/plugins/discover/public/application/main/components/layout/discover_layout.test.tsx b/src/plugins/discover/public/application/main/components/layout/discover_layout.test.tsx index 13a8e2c9c402a..d23a28aa7752d 100644 --- a/src/plugins/discover/public/application/main/components/layout/discover_layout.test.tsx +++ b/src/plugins/discover/public/application/main/components/layout/discover_layout.test.tsx @@ -53,12 +53,14 @@ async function mountComponent( main$: DataMain$ = new BehaviorSubject({ fetchStatus: FetchStatus.COMPLETE, foundDocuments: true, + timeRange: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, + timeRangeRelative: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, }) as DataMain$ ) { const searchSourceMock = createSearchSourceMock({ index: dataView }); const services = createDiscoverServicesMock(); const time = { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }; - services.data.query.timefilter.timefilter.getTime = () => time; + (services.data.query.queryString.getDefaultQuery as jest.Mock).mockReturnValue({ language: 'kuery', query: '', diff --git a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts index 0b41bbccecd82..9f4b834040d2b 100644 --- a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts +++ b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts @@ -55,7 +55,6 @@ export function sendTimeRangeMsg( timeRangeRelative?: TimeRange, query?: AggregateQuery | Query | undefined ) { - console.log('sendTimeRangeMsg', { timeRange }); main$.next({ ...main$.getValue(), timeRange, timeRangeRelative, query }); } From e5b1b4c7bea0c81557275181f210cc16d751719f Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:10:56 +0000 Subject: [PATCH 3/9] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- .../application/main/data_fetching/update_search_source.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/discover/public/application/main/data_fetching/update_search_source.ts b/src/plugins/discover/public/application/main/data_fetching/update_search_source.ts index a6f6a35344ed8..6c89bb52f10bd 100644 --- a/src/plugins/discover/public/application/main/data_fetching/update_search_source.ts +++ b/src/plugins/discover/public/application/main/data_fetching/update_search_source.ts @@ -9,7 +9,7 @@ import { ISearchSource } from '@kbn/data-plugin/public'; import { DataViewType, DataView } from '@kbn/data-views-plugin/public'; -import { Filter, TimeRange} from '@kbn/es-query'; +import { Filter, TimeRange } from '@kbn/es-query'; import type { SortOrder } from '@kbn/saved-search-plugin/public'; import { SEARCH_FIELDS_FROM_SOURCE, SORT_DEFAULT_ORDER_SETTING } from '@kbn/discover-utils'; import { DiscoverServices } from '../../../build_services'; From 0204ac5575e7932bf240da19111b8e1285350d25 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 6 Dec 2024 14:01:18 +0100 Subject: [PATCH 4/9] Fix test --- .../main/state_management/discover_data_state_container.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts index 57a1a012b0482..2f6237a53ecad 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts @@ -243,6 +243,12 @@ export function getDataStateContainer({ abortController?.abort(); abortControllerFetchMore?.abort(); + sendTimeRangeMsg( + dataSubjects.main$, + timefilter.getAbsoluteTime(), + timefilter.getTime(), + appStateContainer.getState().query + ); if (options.fetchMore) { abortControllerFetchMore = new AbortController(); From 51fbf2eea02a43da86b62e4aa018d04ceb88c0b2 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 6 Dec 2024 16:39:13 +0100 Subject: [PATCH 5/9] Fix functional test --- .../main/components/layout/use_discover_histogram.ts | 6 +++--- .../main/state_management/discover_data_state_container.ts | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts index 6d2f620c36b47..90a8c71838c89 100644 --- a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts +++ b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts @@ -218,8 +218,8 @@ export const useDiscoverHistogram = ({ */ const { filters } = useQuerySubscriber({ data: services.data }); const customFilters = useInternalStateSelector((state) => state.customFilters); - const relativeTimeRange = main$.getValue().timeRangeRelative; const query = main$.getValue().query; + const { timeRangeRelative, timeRange } = useObservable(main$, main$.getValue()); // When in ES|QL mode, update the data view, query, and // columns only when documents are done fetching so the Lens suggestions @@ -392,8 +392,8 @@ export const useDiscoverHistogram = ({ dataView: isEsqlMode ? esqlDataView : dataView, query: isEsqlMode ? esqlQuery : query, filters: filtersMemoized, - timeRange: main$.getValue().timeRange, - relativeTimeRange, + timeRange, + relativeTimeRange: timeRangeRelative, columns: isEsqlMode ? esqlColumns : undefined, onFilter: histogramCustomization?.onFilter, onBrushEnd: histogramCustomization?.onBrushEnd, diff --git a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts index 2f6237a53ecad..69f7284982975 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts @@ -351,7 +351,6 @@ export function getDataStateContainer({ const fetchQuery = async (resetQuery?: boolean) => { const query = appStateContainer.getState().query; const currentDataView = getSavedSearch().searchSource.getField('index'); - sendTimeRangeMsg(dataSubjects.main$, timefilter.getAbsoluteTime(), timefilter.getTime(), query); if (isOfAggregateQueryType(query)) { const nextDataView = await getEsqlDataView(query, currentDataView, services); From afe95ca4cba9db3722f585387684876cd9ec5097 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 9 Dec 2024 11:13:58 +0100 Subject: [PATCH 6/9] Rename to sendFetchStartMsg --- .../application/main/hooks/use_saved_search_messages.ts | 4 ++-- .../main/state_management/discover_data_state_container.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts index 9f4b834040d2b..a1d1b400679e1 100644 --- a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts +++ b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts @@ -47,9 +47,9 @@ export function sendCompleteMsg(main$: DataMain$, foundDocuments = true) { } /** - * Send message containing timeRange via main observable + * Send message when data fetching starts via main observable */ -export function sendTimeRangeMsg( +export function sendFetchStartMsg( main$: DataMain$, timeRange: TimeRange, timeRangeRelative?: TimeRange, diff --git a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts index 46d0e4107ddae..b65c7e4973c8b 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts @@ -26,7 +26,7 @@ import type { DiscoverSearchSessionManager } from './discover_search_session'; import { FetchStatus } from '../../types'; import { validateTimeRange } from './utils/validate_time_range'; import { fetchAll, fetchMoreDocuments } from '../data_fetching/fetch_all'; -import { sendResetMsg, sendTimeRangeMsg } from '../hooks/use_saved_search_messages'; +import { sendResetMsg, sendFetchStartMsg } from '../hooks/use_saved_search_messages'; import { getFetch$ } from '../data_fetching/get_fetch_observable'; import type { DiscoverInternalStateContainer } from './discover_internal_state_container'; import { getDefaultProfileState } from './utils/get_default_profile_state'; @@ -238,7 +238,7 @@ export function getDataStateContainer({ abortController?.abort(); abortControllerFetchMore?.abort(); - sendTimeRangeMsg( + sendFetchStartMsg( dataSubjects.main$, timefilter.getAbsoluteTime(), timefilter.getTime(), From b966289c8b4a7042111633678a7193ffb90d8b39 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 9 Dec 2024 14:11:19 +0100 Subject: [PATCH 7/9] Refactor --- .../layout/discover_histogram_layout.test.tsx | 7 ++++-- .../layout/discover_layout.test.tsx | 14 +++++------ .../layout/use_discover_histogram.ts | 18 +++++++-------- .../main/data_fetching/fetch_all.ts | 4 ++-- .../main/hooks/use_saved_search_messages.ts | 13 ++++------- .../discover_data_state_container.ts | 23 +++++++++++++------ 6 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/plugins/discover/public/application/main/components/layout/discover_histogram_layout.test.tsx b/src/plugins/discover/public/application/main/components/layout/discover_histogram_layout.test.tsx index 7107046d73ef1..0d3ac14e93bdc 100644 --- a/src/plugins/discover/public/application/main/components/layout/discover_histogram_layout.test.tsx +++ b/src/plugins/discover/public/application/main/components/layout/discover_histogram_layout.test.tsx @@ -80,8 +80,11 @@ const mountComponent = async ({ const main$ = new BehaviorSubject({ fetchStatus: FetchStatus.COMPLETE, foundDocuments: true, - timeRange: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, - timeRangeRelative: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, + params: { + dataView, + timeRange: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, + timeRangeRelative: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, + }, }) as DataMain$; const documents$ = new BehaviorSubject({ diff --git a/src/plugins/discover/public/application/main/components/layout/discover_layout.test.tsx b/src/plugins/discover/public/application/main/components/layout/discover_layout.test.tsx index d23a28aa7752d..8499aa6e77d37 100644 --- a/src/plugins/discover/public/application/main/components/layout/discover_layout.test.tsx +++ b/src/plugins/discover/public/application/main/components/layout/discover_layout.test.tsx @@ -53,23 +53,21 @@ async function mountComponent( main$: DataMain$ = new BehaviorSubject({ fetchStatus: FetchStatus.COMPLETE, foundDocuments: true, - timeRange: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, - timeRangeRelative: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, + params: { + dataView, + timeRange: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, + timeRangeRelative: { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }, + }, }) as DataMain$ ) { const searchSourceMock = createSearchSourceMock({ index: dataView }); const services = createDiscoverServicesMock(); - const time = { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }; (services.data.query.queryString.getDefaultQuery as jest.Mock).mockReturnValue({ language: 'kuery', query: '', }); - (services.data.query.getState as jest.Mock).mockReturnValue({ - filters: [], - query, - time, - }); + (searchSourceInstanceMock.fetch$ as jest.Mock).mockImplementation( jest.fn().mockReturnValue(of({ rawResponse: { hits: { total: 2 } } })) ); diff --git a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts index 90a8c71838c89..1eb96c8c6f716 100644 --- a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts +++ b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts @@ -7,7 +7,6 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { useQuerySubscriber } from '@kbn/unified-field-list/src/hooks/use_query_subscriber'; import { canImportVisContext, UnifiedHistogramApi, @@ -42,7 +41,6 @@ import type { InspectorAdapters } from '../../hooks/use_inspector'; import { checkHitCount, sendErrorTo } from '../../hooks/use_saved_search_messages'; import type { DiscoverStateContainer } from '../../state_management/discover_state'; import { addLog } from '../../../../utils/add_log'; -import { useInternalStateSelector } from '../../state_management/discover_internal_state_container'; import { useAppStateSelector, type DiscoverAppState, @@ -216,10 +214,12 @@ export const useDiscoverHistogram = ({ /** * Request params */ - const { filters } = useQuerySubscriber({ data: services.data }); - const customFilters = useInternalStateSelector((state) => state.customFilters); - const query = main$.getValue().query; - const { timeRangeRelative, timeRange } = useObservable(main$, main$.getValue()); + const { params } = useObservable( + main$.pipe(filter(({ fetchStatus }) => fetchStatus === FetchStatus.LOADING)), + main$.getValue() + ); + const { timeRangeRelative, timeRange, customFilters, query, filters, dataView } = params || {}; + console.log('histogram params', params?.timeRange); // When in ES|QL mode, update the data view, query, and // columns only when documents are done fetching so the Lens suggestions @@ -324,12 +324,10 @@ export const useDiscoverHistogram = ({ }; }, [isEsqlMode, stateContainer.dataState.fetchChart$, esqlFetchComplete$, unifiedHistogram]); - const dataView = useInternalStateSelector((state) => state.dataView!); - const histogramCustomization = useDiscoverCustomization('unified_histogram'); const filtersMemoized = useMemo(() => { - const allFilters = [...(filters ?? []), ...customFilters]; + const allFilters = [...(filters ?? []), ...(customFilters ?? [])]; return allFilters.length ? allFilters : EMPTY_FILTERS; }, [filters, customFilters]); @@ -389,7 +387,7 @@ export const useDiscoverHistogram = ({ ref, getCreationOptions, services, - dataView: isEsqlMode ? esqlDataView : dataView, + dataView: isEsqlMode ? esqlDataView : dataView!, query: isEsqlMode ? esqlQuery : query, filters: filtersMemoized, timeRange, diff --git a/src/plugins/discover/public/application/main/data_fetching/fetch_all.ts b/src/plugins/discover/public/application/main/data_fetching/fetch_all.ts index fd0689abea7dd..42abf25ee2ddc 100644 --- a/src/plugins/discover/public/application/main/data_fetching/fetch_all.ts +++ b/src/plugins/discover/public/application/main/data_fetching/fetch_all.ts @@ -97,7 +97,7 @@ export function fetchAll( services, sort: getAppState().sort as SortOrder[], customFilters: getInternalState().customFilters, - inputTimeRange: dataSubjects.main$.getValue().timeRange, + inputTimeRange: dataSubjects.main$.getValue().params?.timeRange, }); } @@ -118,7 +118,7 @@ export function fetchAll( data, expressions, profilesManager, - inputTimeRange: dataSubjects.main$.getValue().timeRange, + inputTimeRange: dataSubjects.main$.getValue().params?.timeRange, }) : fetchDocuments(searchSource, fetchDeps); const fetchType = isEsqlQuery ? 'fetchTextBased' : 'fetchDocuments'; diff --git a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts index a1d1b400679e1..bd3dc2b37d9aa 100644 --- a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts +++ b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts @@ -10,11 +10,11 @@ import type { BehaviorSubject } from 'rxjs'; import type { DataTableRecord } from '@kbn/discover-utils/src/types'; import type { SearchResponseWarning } from '@kbn/search-response-warnings'; -import { AggregateQuery, Query, TimeRange } from '@kbn/es-query'; import { FetchStatus } from '../../types'; -import type { +import { DataDocuments$, DataMain$, + DataMainMsgParams, DataMsg, DataTotalHits$, SavedSearchData, @@ -49,13 +49,8 @@ export function sendCompleteMsg(main$: DataMain$, foundDocuments = true) { /** * Send message when data fetching starts via main observable */ -export function sendFetchStartMsg( - main$: DataMain$, - timeRange: TimeRange, - timeRangeRelative?: TimeRange, - query?: AggregateQuery | Query | undefined -) { - main$.next({ ...main$.getValue(), timeRange, timeRangeRelative, query }); +export function sendFetchStartMsg(main$: DataMain$, params: DataMainMsgParams) { + main$.next({ ...main$.getValue(), params, fetchStatus: FetchStatus.LOADING }); } /** diff --git a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts index b65c7e4973c8b..ad2370e1ca8ce 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts @@ -53,9 +53,16 @@ export interface DataMsg { export interface DataMainMsg extends DataMsg { foundDocuments?: boolean; + params?: DataMainMsgParams; +} + +export interface DataMainMsgParams { + customFilters?: Filter[]; + dataView?: DataView; + filters?: Filter[]; timeRange?: TimeRange; timeRangeRelative?: TimeRange; - filter?: Filter[]; + query?: AggregateQuery | Query | undefined; } export interface DataDocumentsMsg extends DataMsg { @@ -238,12 +245,14 @@ export function getDataStateContainer({ abortController?.abort(); abortControllerFetchMore?.abort(); - sendFetchStartMsg( - dataSubjects.main$, - timefilter.getAbsoluteTime(), - timefilter.getTime(), - appStateContainer.getState().query - ); + sendFetchStartMsg(dataSubjects.main$, { + customFilters: internalStateContainer.getState().customFilters, + dataView: internalStateContainer.getState().dataView, + filters: appStateContainer.getState().filters, + query: appStateContainer.getState().query, + timeRange: timefilter.getAbsoluteTime(), + timeRangeRelative: timefilter.getTime(), + }); if (options.fetchMore) { abortControllerFetchMore = new AbortController(); From baa4b717ebdd7add4c2123aded293518704d65e2 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 10 Dec 2024 06:45:31 +0100 Subject: [PATCH 8/9] improve code --- .../main/components/layout/use_discover_histogram.ts | 7 +++++-- .../application/main/hooks/use_saved_search_messages.ts | 8 ++++++-- .../state_management/discover_data_state_container.ts | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts index 1eb96c8c6f716..443e0f9e3be1d 100644 --- a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts +++ b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts @@ -22,6 +22,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { debounceTime, distinctUntilChanged, + distinctUntilKeyChanged, filter, map, merge, @@ -215,11 +216,13 @@ export const useDiscoverHistogram = ({ * Request params */ const { params } = useObservable( - main$.pipe(filter(({ fetchStatus }) => fetchStatus === FetchStatus.LOADING)), + main$.pipe( + filter(({ fetchStatus }) => fetchStatus === FetchStatus.LOADING), + distinctUntilKeyChanged('fetchTime') + ), main$.getValue() ); const { timeRangeRelative, timeRange, customFilters, query, filters, dataView } = params || {}; - console.log('histogram params', params?.timeRange); // When in ES|QL mode, update the data view, query, and // columns only when documents are done fetching so the Lens suggestions diff --git a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts index bd3dc2b37d9aa..d8c67a693a26f 100644 --- a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts +++ b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts @@ -50,7 +50,12 @@ export function sendCompleteMsg(main$: DataMain$, foundDocuments = true) { * Send message when data fetching starts via main observable */ export function sendFetchStartMsg(main$: DataMain$, params: DataMainMsgParams) { - main$.next({ ...main$.getValue(), params, fetchStatus: FetchStatus.LOADING }); + main$.next({ + ...main$.getValue(), + params, + fetchStatus: FetchStatus.LOADING, + fetchTime: new Date().toISOString(), + }); } /** @@ -122,7 +127,6 @@ export function sendErrorMsg(data$: DataMain$ | DataDocuments$ | DataTotalHits$, */ export function sendResetMsg(data: SavedSearchData, initialFetchStatus: FetchStatus) { data.main$.next({ - ...data.main$.getValue(), fetchStatus: initialFetchStatus, foundDocuments: undefined, }); diff --git a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts index ad2370e1ca8ce..d6cf1192ceeeb 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts @@ -54,6 +54,7 @@ export interface DataMsg { export interface DataMainMsg extends DataMsg { foundDocuments?: boolean; params?: DataMainMsgParams; + fetchTime?: string; } export interface DataMainMsgParams { From 06f6aee0afbd3ba35065cac9e15dd883344dbf2b Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 10 Dec 2024 09:26:04 +0100 Subject: [PATCH 9/9] iterate --- .../components/layout/discover_documents.tsx | 2 +- .../layout/use_discover_histogram.ts | 30 +++++++++---------- .../main/hooks/use_saved_search_messages.ts | 4 +++ .../discover_data_state_container.ts | 4 +++ 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/plugins/discover/public/application/main/components/layout/discover_documents.tsx b/src/plugins/discover/public/application/main/components/layout/discover_documents.tsx index 77abd46333c68..376b1f3e2476b 100644 --- a/src/plugins/discover/public/application/main/components/layout/discover_documents.tsx +++ b/src/plugins/discover/public/application/main/components/layout/discover_documents.tsx @@ -277,7 +277,7 @@ function DiscoverDocumentsComponent({ dataView, query, filters, - timeRange: main$.getValue().timeRange, + timeRange: main$.getValue().params?.timeRange, }); const renderDocumentView = useCallback( diff --git a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts index 443e0f9e3be1d..bb992f3eee4f0 100644 --- a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts +++ b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.ts @@ -46,7 +46,7 @@ import { useAppStateSelector, type DiscoverAppState, } from '../../state_management/discover_app_state_container'; -import { DataDocumentsMsg } from '../../state_management/discover_data_state_container'; +import { DataDocumentsMsg, DataMain$ } from '../../state_management/discover_data_state_container'; import { useSavedSearch } from '../../state_management/discover_state_provider'; import { useIsEsqlMode } from '../../hooks/use_is_esql_mode'; @@ -132,22 +132,20 @@ export const useDiscoverHistogram = ({ */ useEffect(() => { - const subscription = createAppStateObservable(stateContainer.appState.state$).subscribe( - (changes) => { - if ('timeInterval' in changes && changes.timeInterval) { - unifiedHistogram?.setTimeInterval(changes.timeInterval); - } + const subscription = createAppStateObservable(main$).subscribe((changes) => { + if ('timeInterval' in changes && changes.timeInterval) { + unifiedHistogram?.setTimeInterval(changes.timeInterval); + } - if ('chartHidden' in changes && typeof changes.chartHidden === 'boolean') { - unifiedHistogram?.setChartHidden(changes.chartHidden); - } + if ('chartHidden' in changes && typeof changes.chartHidden === 'boolean') { + unifiedHistogram?.setChartHidden(changes.chartHidden); } - ); + }); return () => { subscription?.unsubscribe(); }; - }, [stateContainer.appState.state$, unifiedHistogram]); + }, [unifiedHistogram, main$]); /** * Total hits @@ -446,7 +444,7 @@ const createUnifiedHistogramStateObservable = (state$?: Observable) => { +const createAppStateObservable = (state$: DataMain$) => { return state$.pipe( startWith(undefined), pairwise(), @@ -457,12 +455,12 @@ const createAppStateObservable = (state$: Observable) => { return changes; } - if (prev?.interval !== curr.interval) { - changes.timeInterval = curr.interval; + if (prev?.params?.timeInterval !== curr.params?.timeInterval) { + changes.timeInterval = curr.params?.timeInterval; } - if (prev?.hideChart !== curr.hideChart) { - changes.chartHidden = curr.hideChart; + if (prev?.params?.hideChart !== curr.params?.hideChart) { + changes.chartHidden = curr.params?.hideChart; } return changes; diff --git a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts index d8c67a693a26f..c840d4e14bd5e 100644 --- a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts +++ b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.ts @@ -126,9 +126,13 @@ export function sendErrorMsg(data$: DataMain$ | DataDocuments$ | DataTotalHits$, * Needed when data view is switched or a new runtime field is added */ export function sendResetMsg(data: SavedSearchData, initialFetchStatus: FetchStatus) { + const dataView = data.main$.getValue().params?.dataView; data.main$.next({ fetchStatus: initialFetchStatus, foundDocuments: undefined, + params: { + dataView, + }, }); data.documents$.next({ fetchStatus: initialFetchStatus, result: [] }); data.totalHits$.next({ fetchStatus: initialFetchStatus, result: undefined }); diff --git a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts index d6cf1192ceeeb..d243f7bc9c990 100644 --- a/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts +++ b/src/plugins/discover/public/application/main/state_management/discover_data_state_container.ts @@ -61,8 +61,10 @@ export interface DataMainMsgParams { customFilters?: Filter[]; dataView?: DataView; filters?: Filter[]; + hideChart?: boolean; timeRange?: TimeRange; timeRangeRelative?: TimeRange; + timeInterval?: string; query?: AggregateQuery | Query | undefined; } @@ -250,9 +252,11 @@ export function getDataStateContainer({ customFilters: internalStateContainer.getState().customFilters, dataView: internalStateContainer.getState().dataView, filters: appStateContainer.getState().filters, + hideChart: appStateContainer.getState().hideChart, query: appStateContainer.getState().query, timeRange: timefilter.getAbsoluteTime(), timeRangeRelative: timefilter.getTime(), + timeInterval: appStateContainer.getState().interval, }); if (options.fetchMore) {