From a472a2a5249d671c065c8826be6da031f3288d6b Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 6 Dec 2024 10:04:20 +0100 Subject: [PATCH 01/11] 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 02/11] 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 03/11] [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 04/11] 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 05/11] 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 06/11] 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 81af64a9bf16a67a8079607523ff57d3f1195910 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 10 Dec 2024 12:09:15 +0100 Subject: [PATCH 07/11] Update sendResetMsg --- .../application/main/hooks/use_saved_search_messages.ts | 6 +----- 1 file changed, 1 insertion(+), 5 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 a1d1b400679e1..2c5df2129d62a 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,11 +126,7 @@ 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({ - ...data.main$.getValue(), - fetchStatus: initialFetchStatus, - foundDocuments: undefined, - }); + data.main$.next({ fetchStatus: initialFetchStatus, foundDocuments: undefined }); data.documents$.next({ fetchStatus: initialFetchStatus, result: [] }); data.totalHits$.next({ fetchStatus: initialFetchStatus, result: undefined }); } From 19bdbff300517cd78af27c5c1bb856996ae4ebde Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 10 Dec 2024 12:10:22 +0100 Subject: [PATCH 08/11] Update use_discover_histogram.ts --- .../main/components/layout/use_discover_histogram.ts | 3 +-- 1 file changed, 1 insertion(+), 2 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 90a8c71838c89..91ed4b68847db 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,7 @@ export const useDiscoverHistogram = ({ */ 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 { timeRangeRelative, timeRange, query } = 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 From 1023a8ebd5e94ce59cf05a950787ebc492262252 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 10 Dec 2024 15:50:56 +0100 Subject: [PATCH 09/11] iterate --- .../data_fetching/update_search_source.ts | 6 +++--- .../hooks/use_saved_search_messages.test.ts | 20 +++++++++++++++++++ .../main/hooks/use_saved_search_messages.ts | 11 +++++++--- .../discover_data_state_container.ts | 3 +-- 4 files changed, 32 insertions(+), 8 deletions(-) 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 115c8c782ec89..05ba512d0e716 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 @@ -7,9 +7,9 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { ISearchSource } from '@kbn/data-plugin/public'; -import { DataViewType, DataView } from '@kbn/data-views-plugin/public'; -import { Filter, TimeRange } from '@kbn/es-query'; +import type { ISearchSource } from '@kbn/data-plugin/public'; +import { DataViewType, type DataView } from '@kbn/data-views-plugin/public'; +import type { Filter, TimeRange } from '@kbn/es-query'; import type { SortOrder } from '@kbn/saved-search-plugin/public'; import { SORT_DEFAULT_ORDER_SETTING } from '@kbn/discover-utils'; import { DiscoverServices } from '../../../build_services'; diff --git a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.test.ts b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.test.ts index 38f9209d10437..67c6dca4bcac9 100644 --- a/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.test.ts +++ b/src/plugins/discover/public/application/main/hooks/use_saved_search_messages.test.ts @@ -17,6 +17,7 @@ import { sendLoadingMoreFinishedMsg, sendNoResultsFoundMsg, sendPartialMsg, + sendFetchStartMsg, } from './use_saved_search_messages'; import { FetchStatus } from '../../types'; import { BehaviorSubject } from 'rxjs'; @@ -188,4 +189,23 @@ describe('test useSavedSearch message generators', () => { }); checkHitCount(main$, 0); }); + + test('sendFetchStartMsg', (done) => { + const main$ = new BehaviorSubject({ + fetchStatus: FetchStatus.COMPLETE, + }); + const timeRange = { from: 'fromAbs', to: 'toAbs' }; + const timeRangeRelative = { from: 'fromRel', to: 'toRel' }; + const query = { query: 'query', language: 'test' }; + main$.subscribe((value) => { + if (value.fetchStatus === FetchStatus.LOADING) { + expect(value.fetchStatus).toBe(FetchStatus.LOADING); + expect(value.timeRange).toBe(timeRange); + expect(value.timeRangeRelative).toBe(timeRangeRelative); + expect(value.query).toBe(query); + done(); + } + }); + sendFetchStartMsg(main$, timeRange, timeRangeRelative, 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 2c5df2129d62a..0ea684107600d 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 @@ -52,10 +52,15 @@ export function sendCompleteMsg(main$: DataMain$, foundDocuments = true) { export function sendFetchStartMsg( main$: DataMain$, timeRange: TimeRange, - timeRangeRelative?: TimeRange, - query?: AggregateQuery | Query | undefined + timeRangeRelative: TimeRange, + query: AggregateQuery | Query | undefined ) { - main$.next({ ...main$.getValue(), timeRange, timeRangeRelative, query }); + main$.next({ + ...main$.getValue(), + timeRange, + timeRangeRelative, + query, + }); } /** 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..04d9902321a04 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, Filter, isOfAggregateQueryType, Query, TimeRange } from '@kbn/es-query'; +import { AggregateQuery, 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'; @@ -55,7 +55,6 @@ export interface DataMainMsg extends DataMsg { foundDocuments?: boolean; timeRange?: TimeRange; timeRangeRelative?: TimeRange; - filter?: Filter[]; } export interface DataDocumentsMsg extends DataMsg { From 87a3fc4c03760870717b9573503d8dc763c98c0e Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Wed, 11 Dec 2024 05:00:46 +0100 Subject: [PATCH 10/11] Add tests --- .../layout/use_discover_histogram.test.tsx | 23 ++++++++++++++++++- .../main/data_fetching/fetch_all.ts | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.test.tsx b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.test.tsx index c8f829d442444..3134bf67ef12c 100644 --- a/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.test.tsx +++ b/src/plugins/discover/public/application/main/components/layout/use_discover_histogram.test.tsx @@ -24,7 +24,11 @@ import { UnifiedHistogramState, } from '@kbn/unified-histogram-plugin/public'; import { createMockUnifiedHistogramApi } from '@kbn/unified-histogram-plugin/public/mocks'; -import { checkHitCount, sendErrorTo } from '../../hooks/use_saved_search_messages'; +import { + checkHitCount, + sendErrorTo, + sendFetchStartMsg, +} from '../../hooks/use_saved_search_messages'; import type { InspectorAdapters } from '../../hooks/use_inspector'; import { UnifiedHistogramCustomization } from '../../../../customizations/customization_types/histogram_customization'; import { useDiscoverCustomization } from '../../../../customizations'; @@ -379,6 +383,23 @@ describe('useDiscoverHistogram', () => { }); expect(hook.result.current.isChartLoading).toBe(true); }); + + it('should use timerange + timeRangeRelative + query given by the data main$ observable', async () => { + const fetch$ = new Subject(); + const stateContainer = getStateContainer(); + const timeRange = { from: '2021-05-01T20:00:00Z', to: '2021-05-02T20:00:00Z' }; + const timeRangeRelative = { from: 'now-15m', to: 'now' }; + const query = { esql: 'test' }; + sendFetchStartMsg(stateContainer.dataState.data$.main$, timeRange, timeRangeRelative, query); + + const { hook } = await renderUseDiscoverHistogram({ stateContainer }); + act(() => { + fetch$.next(); + }); + expect(hook.result.current.timeRange).toBe(timeRange); + expect(hook.result.current.relativeTimeRange).toBe(timeRangeRelative); + expect(hook.result.current.query).toBe(query); + }); }); describe('refetching', () => { 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..5d87ccdff73b9 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 @@ -247,6 +247,7 @@ export async function fetchMoreDocuments( services, sort: getAppState().sort as SortOrder[], customFilters: getInternalState().customFilters, + inputTimeRange: dataSubjects.main$.getValue().timeRange, }); // Fetch more documents From 7a5543d22f95fbc34b1cc1c88414caae76d8c303 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Wed, 11 Dec 2024 13:51:06 +0100 Subject: [PATCH 11/11] dive into use_request_params --- .../public/hooks/use_request_params.tsx | 11 +++++++---- .../unified_histogram/public/layout/layout.tsx | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/plugins/unified_histogram/public/hooks/use_request_params.tsx b/src/plugins/unified_histogram/public/hooks/use_request_params.tsx index 7edd134afaeee..6eee29657052a 100644 --- a/src/plugins/unified_histogram/public/hooks/use_request_params.tsx +++ b/src/plugins/unified_histogram/public/hooks/use_request_params.tsx @@ -26,11 +26,13 @@ export const useRequestParams = ({ query: originalQuery, filters: originalFilters, timeRange: originalTimeRange, + relativeTimeRange: originalRelativeTimeRange, }: { services: UnifiedHistogramServices; query?: Query | AggregateQuery; filters?: Filter[]; timeRange?: TimeRange; + relativeTimeRange?: TimeRange; }): UseRequestParamsResult => { const { data } = services; @@ -42,14 +44,15 @@ export const useRequestParams = ({ ); const relativeTimeRange = useMemo( - () => originalTimeRange ?? data.query.timefilter.timefilter.getTimeDefaults(), - [data.query.timefilter.timefilter, originalTimeRange] + () => originalRelativeTimeRange ?? data.query.timefilter.timefilter.getTimeDefaults(), + [data.query.timefilter.timefilter, originalRelativeTimeRange] ); - const timeRange = useRef(getAbsoluteTimeRange(relativeTimeRange)); + const timeRange = useRef(originalTimeRange ?? getAbsoluteTimeRange(relativeTimeRange)); + const getTimeRange = useCallback(() => timeRange.current, []); const updateTimeRange = useStableCallback(() => { - timeRange.current = getAbsoluteTimeRange(relativeTimeRange); + timeRange.current = originalTimeRange ?? getAbsoluteTimeRange(relativeTimeRange); }); return { filters, query, getTimeRange, updateTimeRange, relativeTimeRange }; diff --git a/src/plugins/unified_histogram/public/layout/layout.tsx b/src/plugins/unified_histogram/public/layout/layout.tsx index b9d9f6fbc446f..7098958f37d54 100644 --- a/src/plugins/unified_histogram/public/layout/layout.tsx +++ b/src/plugins/unified_histogram/public/layout/layout.tsx @@ -255,6 +255,7 @@ export const UnifiedHistogramLayout = ({ query: originalQuery, filters: originalFilters, timeRange: originalTimeRange, + relativeTimeRange, }); const [lensVisService] = useState(() => new LensVisService({ services, lensSuggestionsApi }));