diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/charts/chart.tsx b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/charts/chart.tsx index 366b11bf0cb11..0c1067499a033 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/charts/chart.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/charts/chart.tsx @@ -15,7 +15,7 @@ import { buildCombinedAssetFilter } from '../../../utils/filters/build'; import { type BrushEndArgs, LensChart, type OnFilterEvent, LensChartProps } from '../../lens'; import { useDatePickerContext } from '../hooks/use_date_picker'; import { extractRangeFromChartFilterEvent } from './chart_utils'; -import { useSearchSessionContext } from '../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../hooks/use_reload_request_time'; export type ChartProps = Pick & { id: string; @@ -34,7 +34,7 @@ export const Chart = ({ lensAttributes, }: ChartProps) => { const { setDateRange } = useDatePickerContext(); - const { searchSessionId } = useSearchSessionContext(); + const { reloadRequestTime } = useReloadRequestTimeContext(); const { services: { dataViews }, } = useKibanaContextForPlugin(); @@ -86,7 +86,7 @@ export const Chart = ({ borderRadius="m" dateRange={dateRange} height={METRIC_CHART_HEIGHT} - searchSessionId={searchSessionId} + lastReloadRequestTime={reloadRequestTime} filters={filters} lensAttributes={lensAttributes} overrides={overrides} diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/container_kpi_charts.tsx b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/container_kpi_charts.tsx index 95291d157745e..2420c0372c903 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/container_kpi_charts.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/container_kpi_charts.tsx @@ -22,7 +22,7 @@ export interface ContainerKpiChartsProps { dateRange: TimeRange; query?: Query; filters?: Filter[]; - searchSessionId?: string; + lastReloadRequestTime?: number; options?: { getSubtitle?: (formulaValue: string) => string; }; @@ -34,7 +34,7 @@ export const ContainerKpiCharts = ({ dataView, filters, query, - searchSessionId, + lastReloadRequestTime, loading = false, }: ContainerKpiChartsProps) => { const isDockerContainer = useIntegrationCheck({ dependsOn: INTEGRATIONS.docker }); @@ -53,7 +53,7 @@ export const ContainerKpiCharts = ({ dataView={dataView} filters={filters} query={query} - searchSessionId={searchSessionId} + lastReloadRequestTime={lastReloadRequestTime} loading={loading} /> )} @@ -63,7 +63,7 @@ export const ContainerKpiCharts = ({ dataView={dataView} filters={filters} query={query} - searchSessionId={searchSessionId} + lastReloadRequestTime={lastReloadRequestTime} loading={loading} /> )} @@ -76,7 +76,7 @@ const DockerKpiCharts = ({ dataView, filters, query, - searchSessionId, + lastReloadRequestTime, loading = false, }: ContainerKpiChartsProps) => { const charts = useDockerContainerKpiCharts({ @@ -92,7 +92,7 @@ const DockerKpiCharts = ({ dateRange={dateRange} filters={filters} query={query} - searchSessionId={searchSessionId} + lastReloadRequestTime={lastReloadRequestTime} loading={loading} /> @@ -107,7 +107,7 @@ const KubernetesKpiCharts = ({ filters, options, query, - searchSessionId, + lastReloadRequestTime, loading = false, }: ContainerKpiChartsProps) => { const charts = useK8sContainerKpiCharts({ @@ -123,7 +123,7 @@ const KubernetesKpiCharts = ({ dateRange={dateRange} filters={filters} query={query} - searchSessionId={searchSessionId} + lastReloadRequestTime={lastReloadRequestTime} loading={loading} /> diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/host_kpi_charts.tsx b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/host_kpi_charts.tsx index 64345efb1af8a..2b68627176a0c 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/host_kpi_charts.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/host_kpi_charts.tsx @@ -17,7 +17,7 @@ export interface HostKpiChartsProps { dateRange: TimeRange; query?: Query; filters?: Filter[]; - searchSessionId?: string; + lastReloadRequestTime?: number; getSubtitle?: (formulaValue: string) => string; loading?: boolean; } @@ -28,7 +28,7 @@ export const HostKpiCharts = ({ filters, getSubtitle, query, - searchSessionId, + lastReloadRequestTime, loading = false, }: HostKpiChartsProps) => { const charts = useHostKpiCharts({ @@ -45,7 +45,7 @@ export const HostKpiCharts = ({ dateRange={dateRange} filters={filters} query={query} - searchSessionId={searchSessionId} + lastReloadRequestTime={lastReloadRequestTime} loading={loading} /> diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/kpi.tsx b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/kpi.tsx index 3545a54d813db..6143133b561e5 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/kpi.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/kpi.tsx @@ -15,7 +15,7 @@ export const Kpi = ({ dateRange, query, filters, - searchSessionId, + lastReloadRequestTime, loading, ...chartProps }: LensConfig & { @@ -25,6 +25,7 @@ export const Kpi = ({ filters?: Filter[]; searchSessionId?: string; loading?: boolean; + lastReloadRequestTime?: number; }) => { const tooltipContent = useMemo( () => @@ -44,7 +45,7 @@ export const Kpi = ({ query={query} loading={loading} toolTip={tooltipContent} - searchSessionId={searchSessionId} + lastReloadRequestTime={lastReloadRequestTime} disableTriggers hidePanelTitles /> diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/context_providers.tsx b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/context_providers.tsx index 87220691b50fc..d7b03769d20c1 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/context_providers.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/context_providers.tsx @@ -6,7 +6,7 @@ */ import React from 'react'; -import { SearchSessionProvider } from '../../hooks/use_search_session'; +import { ReloadRequestTimeProvider } from '../../hooks/use_reload_request_time'; import { AssetDetailsRenderPropsProvider } from './hooks/use_asset_details_render_props'; import { DatePickerProvider } from './hooks/use_date_picker'; import { LoadingStateProvider } from './hooks/use_loading_state'; @@ -22,7 +22,7 @@ const RenderWithOptionalSearchSessionProvider = ({ }) => { if (renderMode.mode === 'flyout') { // flyout mode requires its own search session so that it doesn't interfere with the main page - return {children}; + return {children}; } return <>{children}; }; diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_date_picker.ts b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_date_picker.ts index 4cb20bfc4f3d5..11ee8e2378751 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_date_picker.ts +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_date_picker.ts @@ -10,7 +10,7 @@ import createContainer from 'constate'; import { useCallback, useMemo, useState } from 'react'; import useEffectOnce from 'react-use/lib/useEffectOnce'; import { BehaviorSubject } from 'rxjs'; -import { useSearchSessionContext } from '../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../hooks/use_reload_request_time'; import { parseDateRange } from '../../../utils/datemath'; import { AssetDetailsProps } from '../types'; import { getDefaultDateRange, toTimestampRange } from '../utils'; @@ -22,7 +22,7 @@ export function useDatePicker({ dateRange = getDefaultDateRange(), autoRefresh, }: UseDateRangeProviderProps) { - const { updateSearchSessionId } = useSearchSessionContext(); + const { updateReloadRequestTime } = useReloadRequestTimeContext(); const autoRefreshTick$ = useMemo(() => new BehaviorSubject(null), []); const autoRefreshConfig$ = useMemo( () => new BehaviorSubject(undefined), @@ -52,9 +52,9 @@ export function useDatePicker({ (newDateRange: TimeRange) => { setUrlState({ dateRange: newDateRange }); setParsedDateRange(parseDateRange(newDateRange)); - updateSearchSessionId(); + updateReloadRequestTime(); }, - [setUrlState, updateSearchSessionId] + [setUrlState, updateReloadRequestTime] ); const onRefresh = useCallback( diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_loading_state.test.ts b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_loading_state.test.ts index 2475c97cec0e8..17d62a48caeb1 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_loading_state.test.ts +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_loading_state.test.ts @@ -12,12 +12,12 @@ import { BehaviorSubject, EMPTY, of, Subject, Subscription, skip } from 'rxjs'; import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; import { coreMock } from '@kbn/core/public/mocks'; import { SearchSessionState, waitUntilNextSessionCompletes$ } from '@kbn/data-plugin/public'; -import { useSearchSessionContext } from '../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../hooks/use_reload_request_time'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; jest.mock('./use_date_picker'); jest.mock('../../../hooks/use_kibana'); -jest.mock('../../../hooks/use_search_session'); +jest.mock('../../../hooks/use_reload_request_time'); jest.mock('@kbn/data-plugin/public', () => ({ ...jest.requireActual('@kbn/data-plugin/public'), @@ -36,8 +36,8 @@ const waitUntilNextSessionCompletesMock$ = waitUntilNextSessionCompletes$ as jes typeof waitUntilNextSessionCompletes$ >; -const useSearchSessionContextMock = useSearchSessionContext as jest.MockedFunction< - typeof useSearchSessionContext +const useRequestTimeContextMock = useReloadRequestTimeContext as jest.MockedFunction< + typeof useReloadRequestTimeContext >; describe('useLoadingState', () => { @@ -50,12 +50,12 @@ describe('useLoadingState', () => { const sessionState$ = new BehaviorSubject(SearchSessionState.None); - const updateSearchSessionIdMock = jest.fn(); + const updateReloadRequestTimeMock = jest.fn(); - const mockSearchSessionContext = () => { - useSearchSessionContextMock.mockReturnValue({ - updateSearchSessionId: updateSearchSessionIdMock, - searchSessionId: '', + const mockRequestTimeContext = () => { + useRequestTimeContextMock.mockReturnValue({ + updateReloadRequestTime: updateReloadRequestTimeMock, + reloadRequestTime: 0, }); }; @@ -89,7 +89,7 @@ describe('useLoadingState', () => { subscription = new Subscription(); jest.useFakeTimers(); waitUntilNextSessionCompletesMock$.mockReturnValue(of(SearchSessionState.None)); - mockSearchSessionContext(); + mockRequestTimeContext(); mockUseKibana(); mockDatePickerContext(); }); @@ -152,7 +152,7 @@ describe('useLoadingState', () => { unmount(); }); - it('should not call updateSearchSessionId if waitUntilNextSessionCompletesMock$ returns empty', async () => { + it('should not call updateRequestTime if waitUntilNextSessionCompletesMock$ returns empty', async () => { const { unmount } = renderHook(() => useLoadingState()); // waitUntilNextSessionCompletes$ returns EMPTY when the status is loading or none @@ -165,12 +165,12 @@ describe('useLoadingState', () => { }); // only the mount call must happen - await waitFor(() => expect(updateSearchSessionIdMock).toHaveBeenCalledTimes(1)); + await waitFor(() => expect(updateReloadRequestTimeMock).toHaveBeenCalledTimes(1)); unmount(); }); - it('should call updateSearchSessionId when waitUntilNextSessionCompletesMock$ returns', async () => { + it('should call updateRequestTime when waitUntilNextSessionCompletesMock$ returns', async () => { const { unmount } = renderHook(() => useLoadingState()); // waitUntilNextSessionCompletes$ returns something when the status is Completed or BackgroundCompleted @@ -182,7 +182,7 @@ describe('useLoadingState', () => { jest.runOnlyPendingTimers(); }); - await waitFor(() => expect(updateSearchSessionIdMock).toHaveBeenCalledTimes(2)); + await waitFor(() => expect(updateReloadRequestTimeMock).toHaveBeenCalledTimes(2)); unmount(); }); diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_loading_state.ts b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_loading_state.ts index f3098b6dca8f8..a8228b821dd6f 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_loading_state.ts +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_loading_state.ts @@ -26,7 +26,7 @@ import { useCallback, useEffect, useMemo } from 'react'; import { SearchSessionState, waitUntilNextSessionCompletes$ } from '@kbn/data-plugin/public'; import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; import { useDatePickerContext } from './use_date_picker'; -import { useSearchSessionContext } from '../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../hooks/use_reload_request_time'; export type RequestState = 'running' | 'done' | 'error'; const WAIT_MS = 1000; @@ -37,15 +37,15 @@ export const useLoadingState = () => { const { data: { search }, } = services; - const { updateSearchSessionId } = useSearchSessionContext(); + const { updateReloadRequestTime } = useReloadRequestTimeContext(); const isAutoRefreshRequestPending$ = useMemo(() => new BehaviorSubject(false), []); const requestsCount$ = useMemo(() => new BehaviorSubject(0), []); const requestState$ = useMemo(() => new BehaviorSubject(null), []); useEffect(() => { - updateSearchSessionId(); - }, [updateSearchSessionId]); + updateReloadRequestTime(); + }, [updateReloadRequestTime]); const waitUntilRequestsCompletes$ = useCallback( () => @@ -140,7 +140,7 @@ export const useLoadingState = () => { // This will only be called when Lens is used in the Asset Details page return waitUntilNextSessionCompletes$(search.session).pipe( tap(() => { - updateSearchSessionId(); + updateReloadRequestTime(); }) ); } @@ -149,7 +149,7 @@ export const useLoadingState = () => { return of(null).pipe( tap(() => { if (!isAutoRefreshRequestPending) { - updateSearchSessionId(); + updateReloadRequestTime(); } }) ); @@ -169,7 +169,7 @@ export const useLoadingState = () => { requestState$, requestsCount$, search.session, - updateSearchSessionId, + updateReloadRequestTime, waitUntilRequestsCompletes$, ]); diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_request_observable.test.ts b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_request_observable.test.ts index 9be24cbfcf3f4..8ea36562c04a4 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_request_observable.test.ts +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_request_observable.test.ts @@ -9,12 +9,12 @@ import { act, waitFor, renderHook } from '@testing-library/react'; import { useRequestObservable } from './use_request_observable'; import { type RequestState, useLoadingStateContext } from './use_loading_state'; import { useDatePickerContext, type UseDateRangeProviderProps } from './use_date_picker'; -import { useSearchSessionContext } from '../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../hooks/use_reload_request_time'; import { BehaviorSubject } from 'rxjs'; jest.mock('./use_loading_state'); jest.mock('./use_date_picker'); -jest.mock('../../../hooks/use_search_session'); +jest.mock('../../../hooks/use_reload_request_time'); const useLoadingStateContextMock = useLoadingStateContext as jest.MockedFunction< typeof useLoadingStateContext @@ -23,8 +23,8 @@ const useDatePickerContextMock = useDatePickerContext as jest.MockedFunction< typeof useDatePickerContext >; -const useSearchSessionMock = useSearchSessionContext as jest.MockedFunction< - typeof useSearchSessionContext +const useReloadRequestTimeMock = useReloadRequestTimeContext as jest.MockedFunction< + typeof useReloadRequestTimeContext >; describe('useRequestObservable', () => { @@ -38,10 +38,10 @@ describe('useRequestObservable', () => { // needed to spy on `next` function requestStateMock$.next = jest.fn(); - const mockUseSearchSessionMock = () => { - useSearchSessionMock.mockReturnValue({ - updateSearchSessionId: jest.fn(() => {}), - searchSessionId: '', + const mockUseRequestTimeMock = () => { + useReloadRequestTimeMock.mockReturnValue({ + updateReloadRequestTime: jest.fn(() => {}), + reloadRequestTime: 0, }); }; @@ -59,12 +59,14 @@ describe('useRequestObservable', () => { }; beforeEach(() => { + jest.useFakeTimers(); mockDatePickerContext(); - mockUseSearchSessionMock(); + mockUseRequestTimeMock(); mockUseLoadingStateContextMock(); }); afterEach(() => { + jest.useRealTimers(); jest.clearAllMocks(); }); diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/metadata/metadata.test.tsx b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/metadata/metadata.test.tsx index 37b72f03e80b3..d8bd93e03ca40 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/metadata/metadata.test.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/metadata/metadata.test.tsx @@ -16,18 +16,18 @@ import { ContextProviders } from '../../context_providers'; import { coreMock } from '@kbn/core/public/mocks'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana'; -import { useSearchSessionContext } from '../../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../../hooks/use_reload_request_time'; jest.mock('../../../../containers/metrics_source'); jest.mock('../../hooks/use_metadata'); jest.mock('../../../../hooks/use_kibana'); -jest.mock('../../../../hooks/use_search_session'); +jest.mock('../../../../hooks/use_reload_request_time'); const useKibanaMock = useKibanaContextForPlugin as jest.MockedFunction< typeof useKibanaContextForPlugin >; -const useSearchSessionContextMock = useSearchSessionContext as jest.MockedFunction< - typeof useSearchSessionContext +const useRequestTimeContextMock = useReloadRequestTimeContext as jest.MockedFunction< + typeof useReloadRequestTimeContext >; const mockUseKibana = () => { @@ -39,10 +39,10 @@ const mockUseKibana = () => { } as unknown as ReturnType); }; -const mockSearchSessionContext = () => { - useSearchSessionContextMock.mockReturnValue({ - updateSearchSessionId: jest.fn(), - searchSessionId: '', +const mockRequestTimeContext = () => { + useRequestTimeContextMock.mockReturnValue({ + updateReloadRequestTime: jest.fn(), + reloadRequestTime: 0, }); }; @@ -73,11 +73,13 @@ const renderHostMetadata = () => ); beforeEach(() => { + jest.useFakeTimers(); mockUseKibana(); - mockSearchSessionContext(); + mockRequestTimeContext(); }); afterEach(() => { + jest.useRealTimers(); jest.clearAllMocks(); }); diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/overview/kpis/kpi_grid.tsx b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/overview/kpis/kpi_grid.tsx index a908e5c1f6234..2e731ad9fb828 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/overview/kpis/kpi_grid.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/overview/kpis/kpi_grid.tsx @@ -16,7 +16,7 @@ import { import { buildCombinedAssetFilter } from '../../../../../utils/filters/build'; import { HostKpiCharts } from '../../../components/kpis/host_kpi_charts'; import { ContainerKpiCharts } from '../../../components/kpis/container_kpi_charts'; -import { useSearchSessionContext } from '../../../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../../../hooks/use_reload_request_time'; interface Props { dataView?: DataView; @@ -26,7 +26,7 @@ interface Props { } export const KPIGrid = ({ assetId, assetType, dataView, dateRange }: Props) => { - const { searchSessionId } = useSearchSessionContext(); + const { reloadRequestTime } = useReloadRequestTimeContext(); const filters = useMemo(() => { return [ @@ -45,14 +45,14 @@ export const KPIGrid = ({ assetId, assetType, dataView, dateRange }: Props) => { dataView={dataView} filters={filters} dateRange={dateRange} - searchSessionId={searchSessionId} + lastReloadRequestTime={reloadRequestTime} /> ) : ( )} diff --git a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/overview/logs.tsx b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/overview/logs.tsx index 5ca334acf20ec..df2979fe4feb6 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/overview/logs.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/asset_details/tabs/overview/logs.tsx @@ -14,7 +14,7 @@ import { type InventoryItemType, } from '@kbn/metrics-data-access-plugin/common'; import { buildCombinedAssetFilter } from '../../../../utils/filters/build'; -import { useSearchSessionContext } from '../../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../../hooks/use_reload_request_time'; import { useLogsCharts } from '../../hooks/use_log_charts'; import { Kpi } from '../../components/kpis/kpi'; @@ -26,7 +26,7 @@ interface Props { } export const LogsContent = ({ assetId, assetType, dataView, dateRange }: Props) => { - const { searchSessionId } = useSearchSessionContext(); + const { reloadRequestTime } = useReloadRequestTimeContext(); const filters = useMemo(() => { return [ @@ -50,7 +50,7 @@ export const LogsContent = ({ assetId, assetType, dataView, dateRange }: Props) {...chartProps} dateRange={dateRange} filters={filters} - searchSessionId={searchSessionId} + lastReloadRequestTime={reloadRequestTime} /> ))} diff --git a/x-pack/solutions/observability/plugins/infra/public/components/lens/lens_chart.tsx b/x-pack/solutions/observability/plugins/infra/public/components/lens/lens_chart.tsx index 56d69fa9b6c7c..eed4f16c2978c 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/lens/lens_chart.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/lens/lens_chart.tsx @@ -35,7 +35,7 @@ const DEFAULT_DISABLED_ACTIONS = [ export type LensChartProps = BaseChartProps & Pick & { toolTip?: React.ReactElement; - searchSessionId?: string; + reloadRequestTime?: number; description?: string; } & { lensAttributes: UseLensAttributesParams; @@ -55,7 +55,7 @@ export const LensChart = React.memo( onFilter, overrides, toolTip, - searchSessionId, + reloadRequestTime, disableTriggers = false, height = MIN_HEIGHT, loading = false, @@ -71,7 +71,7 @@ export const LensChart = React.memo( timeRange: dateRange, query, filters, - searchSessionId, + lastReloadRequestTime: reloadRequestTime, }); const handleBeforeBadgesRender = useCallback((messages: UserMessage[]) => { @@ -141,7 +141,7 @@ export const LensChart = React.memo( query={query} overrides={overrides} onBrushEnd={onBrushEnd} - searchSessionId={searchSessionId} + lastReloadRequestTime={reloadRequestTime} onFilter={onFilter} onBeforeBadgesRender={handleBeforeBadgesRender} /> diff --git a/x-pack/solutions/observability/plugins/infra/public/components/lens/lens_wrapper.tsx b/x-pack/solutions/observability/plugins/infra/public/components/lens/lens_wrapper.tsx index 01ce60b593462..02b38cc39bc9b 100644 --- a/x-pack/solutions/observability/plugins/infra/public/components/lens/lens_wrapper.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/components/lens/lens_wrapper.tsx @@ -18,10 +18,10 @@ export const LensWrapper = ({ attributes, dateRange, filters, - searchSessionId, loading = false, onLoad, query, + lastReloadRequestTime, ...props }: LensWrapperProps) => { const { euiTheme } = useEuiTheme(); @@ -33,7 +33,7 @@ export const LensWrapper = ({ dateRange, filters, query, - searchSessionId, + lastReloadRequestTime, }); const ref = useRef(null); @@ -62,7 +62,7 @@ export const LensWrapper = ({ dateRange, filters, query, - searchSessionId, + lastReloadRequestTime, }); } }, [ @@ -71,7 +71,7 @@ export const LensWrapper = ({ filters, intersectionObserverEntry?.isIntersecting, query, - searchSessionId, + lastReloadRequestTime, ]); const handleOnLoad = useCallback( @@ -109,7 +109,7 @@ export const LensWrapper = ({ {isLoading && } Pro data: undefined, status: FETCH_STATUS.NOT_INITIATED, }); - const { searchSessionId } = useSearchSessionContext(); - const [cachedSearchSessionId, setCachedSearchSessionId] = useState(''); + const { reloadRequestTime } = useReloadRequestTimeContext(); + const [cachedReloadRequestTime, setCachedReloadRequestTime] = useState(reloadRequestTime); const autoFetchRef = useRef(autoFetch); const controller = useRef(new AbortController()); @@ -197,10 +197,10 @@ export function useFetcher Pro useEffect(() => { // Allows the caller of useFetcher to control when the fetch can be triggered if (autoFetch) { - setCachedSearchSessionId(searchSessionId); + setCachedReloadRequestTime(reloadRequestTime); } autoFetchRef.current = autoFetch; - }, [autoFetch, searchSessionId]); + }, [autoFetch, reloadRequestTime]); useEffect(() => { return () => { @@ -212,7 +212,7 @@ export function useFetcher Pro if (autoFetchRef.current) { triggerFetch(); } - }, [autoFetchRef, fetchWithAbort, cachedSearchSessionId, triggerFetch]); + }, [autoFetchRef, fetchWithAbort, cachedReloadRequestTime, triggerFetch]); return useMemo( () => ({ diff --git a/x-pack/solutions/observability/plugins/infra/public/hooks/use_lens_attributes.ts b/x-pack/solutions/observability/plugins/infra/public/hooks/use_lens_attributes.ts index b1248a1f05e1e..155e404ff8cc8 100644 --- a/x-pack/solutions/observability/plugins/infra/public/hooks/use_lens_attributes.ts +++ b/x-pack/solutions/observability/plugins/infra/public/hooks/use_lens_attributes.ts @@ -65,12 +65,12 @@ export const useLensAttributes = (params: UseLensAttributesParams) => { timeRange, query, filters, - searchSessionId, + lastReloadRequestTime, }: { timeRange: TimeRange; filters: Filter[]; query: Query | AggregateQuery; - searchSessionId?: string; + lastReloadRequestTime?: number; }) => () => { const injectedAttributes = injectFilters({ filters, query }); @@ -80,7 +80,7 @@ export const useLensAttributes = (params: UseLensAttributesParams) => { id: '', timeRange, attributes: injectedAttributes, - searchSessionId, + lastReloadRequestTime, }, { openInNewTab: true, @@ -96,15 +96,15 @@ export const useLensAttributes = (params: UseLensAttributesParams) => { timeRange, filters = [], query = { language: 'kuery', query: '' }, - searchSessionId, + lastReloadRequestTime, }: { timeRange: TimeRange; filters?: Filter[]; query?: Query | AggregateQuery; - searchSessionId?: string; + lastReloadRequestTime?: number; }) => { const openInLens = getOpenInLensAction( - openInLensAction({ timeRange, filters, query, searchSessionId }) + openInLensAction({ timeRange, filters, query, lastReloadRequestTime }) ); return [openInLens]; }, diff --git a/x-pack/solutions/observability/plugins/infra/public/hooks/use_reload_request_time.ts b/x-pack/solutions/observability/plugins/infra/public/hooks/use_reload_request_time.ts new file mode 100644 index 0000000000000..4b3f3e32e80e8 --- /dev/null +++ b/x-pack/solutions/observability/plugins/infra/public/hooks/use_reload_request_time.ts @@ -0,0 +1,25 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import createContainer from 'constate'; +import { useCallback, useState } from 'react'; + +export const useReloadRequestTime = () => { + const [reloadRequestTime, setReloadRequestTime] = useState(Date.now()); + + const updateReloadRequestTime = useCallback(() => { + setReloadRequestTime(Date.now()); + }, []); + + return { + updateReloadRequestTime, + reloadRequestTime, + }; +}; + +export const [ReloadRequestTimeProvider, useReloadRequestTimeContext] = + createContainer(useReloadRequestTime); diff --git a/x-pack/solutions/observability/plugins/infra/public/hooks/use_search_session.ts b/x-pack/solutions/observability/plugins/infra/public/hooks/use_search_session.ts deleted file mode 100644 index d0d818cf4bf93..0000000000000 --- a/x-pack/solutions/observability/plugins/infra/public/hooks/use_search_session.ts +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import createContainer from 'constate'; -import { useCallback, useEffect, useState } from 'react'; -import { useKibanaContextForPlugin } from './use_kibana'; - -export const useSearchSession = () => { - const { services } = useKibanaContextForPlugin(); - const { - data: { search }, - } = services; - const [searchSessionId, setSearchSessionId] = useState(''); - - const updateSearchSessionId = useCallback(() => { - const sessionId = search.session.start(); - setSearchSessionId(sessionId); - }, [search.session]); - - useEffect(() => { - updateSearchSessionId(); - }, [updateSearchSessionId]); - - return { - updateSearchSessionId, - searchSessionId, - }; -}; - -export const [SearchSessionProvider, useSearchSessionContext] = createContainer(useSearchSession); diff --git a/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/kpis/kpi_charts.tsx b/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/kpis/kpi_charts.tsx index dc945af7d0d37..23031bda3f912 100644 --- a/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/kpis/kpi_charts.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/kpis/kpi_charts.tsx @@ -6,7 +6,7 @@ */ import React from 'react'; import { i18n } from '@kbn/i18n'; -import { useSearchSessionContext } from '../../../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../../../hooks/use_reload_request_time'; import { HostKpiCharts } from '../../../../../components/asset_details'; import { buildCombinedAssetFilter } from '../../../../../utils/filters/build'; import { useUnifiedSearchContext } from '../../hooks/use_unified_search'; @@ -17,7 +17,7 @@ import { useMetricsDataViewContext } from '../../../../../containers/metrics_sou export const KpiCharts = () => { const { searchCriteria } = useUnifiedSearchContext(); - const { searchSessionId } = useSearchSessionContext(); + const { reloadRequestTime } = useReloadRequestTimeContext(); const { hostNodes, loading: hostsLoading } = useHostsViewContext(); const { loading: hostCountLoading, count: hostCount } = useHostCountContext(); const { metricsView } = useMetricsDataViewContext(); @@ -60,14 +60,14 @@ export const KpiCharts = () => { }); }; - // prevents requestTs and searchCriteria state from reloading the chart + // prevents requests and searchCriteria state from reloading the chart // we want it to reload only once the table has finished loading. // attributes passed to useAfterLoadedState don't need to be memoized const { afterLoadedState } = useAfterLoadedState(loading, { dateRange: searchCriteria.dateRange, query: shouldUseSearchCriteria ? searchCriteria.query : undefined, filters, - searchSessionId, + reloadRequestTime, getSubtitle, }); @@ -77,7 +77,7 @@ export const KpiCharts = () => { dateRange={afterLoadedState.dateRange} filters={afterLoadedState.filters} query={afterLoadedState.query} - searchSessionId={afterLoadedState.searchSessionId} + lastReloadRequestTime={afterLoadedState.reloadRequestTime} getSubtitle={afterLoadedState.getSubtitle} loading={loading} /> diff --git a/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/tabs/metrics/chart.tsx b/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/tabs/metrics/chart.tsx index cf282d4ed3f29..d78fd1ee30b3f 100644 --- a/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/tabs/metrics/chart.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/tabs/metrics/chart.tsx @@ -7,7 +7,7 @@ import React from 'react'; import type { LensConfig, LensDataviewDataset } from '@kbn/lens-embeddable-utils/config_builder'; import useAsync from 'react-use/lib/useAsync'; -import { useSearchSessionContext } from '../../../../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../../../../hooks/use_reload_request_time'; import { resolveDataView } from '../../../../../../utils/data_view'; import { useKibanaContextForPlugin } from '../../../../../../hooks/use_kibana'; import { HOST_NAME_FIELD } from '../../../../../../../common/constants'; @@ -26,7 +26,7 @@ export type ChartProps = LensConfig & { export const Chart = ({ id, ...chartProps }: ChartProps) => { const { searchCriteria } = useUnifiedSearchContext(); const { loading } = useHostsViewContext(); - const { searchSessionId } = useSearchSessionContext(); + const { reloadRequestTime } = useReloadRequestTimeContext(); const { currentPage } = useHostsTableContext(); const { services: { dataViews }, @@ -40,7 +40,7 @@ export const Chart = ({ id, ...chartProps }: ChartProps) => { const { afterLoadedState } = useAfterLoadedState(loading, { dateRange: searchCriteria.dateRange, query: shouldUseSearchCriteria ? searchCriteria.query : undefined, - searchSessionId, + reloadRequestTime, }); const { value: filters = [] } = useAsync(async () => { @@ -77,7 +77,7 @@ export const Chart = ({ id, ...chartProps }: ChartProps) => { loading={loading} filters={filters} query={afterLoadedState.query} - searchSessionId={afterLoadedState.searchSessionId} + lastReloadRequestTime={afterLoadedState.reloadRequestTime} /> ); }; diff --git a/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/hooks/use_unified_search.ts b/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/hooks/use_unified_search.ts index 291f95554e89c..61b4c14f9072a 100644 --- a/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/hooks/use_unified_search.ts +++ b/x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/hooks/use_unified_search.ts @@ -12,7 +12,7 @@ import deepEqual from 'fast-deep-equal'; import useEffectOnce from 'react-use/lib/useEffectOnce'; import { useKibanaQuerySettings } from '@kbn/observability-shared-plugin/public'; import { useTimeRange } from '../../../../hooks/use_time_range'; -import { useSearchSessionContext } from '../../../../hooks/use_search_session'; +import { useReloadRequestTimeContext } from '../../../../hooks/use_reload_request_time'; import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana'; import { telemetryTimeRangeFormatter } from '../../../../../common/formatters/telemetry_time_range'; import { useMetricsDataViewContext } from '../../../../containers/metrics_source'; @@ -42,7 +42,7 @@ export const useUnifiedSearch = () => { const [error, setError] = useState(null); const [searchCriteria, setSearch] = useHostsUrlState(); const { metricsView } = useMetricsDataViewContext(); - const { updateSearchSessionId } = useSearchSessionContext(); + const { updateReloadRequestTime } = useReloadRequestTimeContext(); const { services } = useKibanaContextForPlugin(); const kibanaQuerySettings = useKibanaQuerySettings(); @@ -72,33 +72,33 @@ export const useUnifiedSearch = () => { const onFiltersChange = useCallback( (filters: Filter[]) => { setSearch({ type: 'SET_FILTERS', filters }); - updateSearchSessionId(); + updateReloadRequestTime(); }, - [setSearch, updateSearchSessionId] + [setSearch, updateReloadRequestTime] ); const onPanelFiltersChange = useCallback( (panelFilters: Filter[]) => { setSearch({ type: 'SET_PANEL_FILTERS', panelFilters }); - updateSearchSessionId(); + updateReloadRequestTime(); }, - [setSearch, updateSearchSessionId] + [setSearch, updateReloadRequestTime] ); const onLimitChange = useCallback( (limit: number) => { setSearch({ type: 'SET_LIMIT', limit }); - updateSearchSessionId(); + updateReloadRequestTime(); }, - [setSearch, updateSearchSessionId] + [setSearch, updateReloadRequestTime] ); const onDateRangeChange = useCallback( (dateRange: StringDateRange) => { setSearch({ type: 'SET_DATE_RANGE', dateRange }); - updateSearchSessionId(); + updateReloadRequestTime(); }, - [setSearch, updateSearchSessionId] + [setSearch, updateReloadRequestTime] ); const onQueryChange = useCallback( @@ -107,12 +107,12 @@ export const useUnifiedSearch = () => { setError(null); validateQuery(query); setSearch({ type: 'SET_QUERY', query }); - updateSearchSessionId(); + updateReloadRequestTime(); } catch (err) { setError(err); } }, - [validateQuery, setSearch, updateSearchSessionId] + [validateQuery, setSearch, updateReloadRequestTime] ); const onSubmit = useCallback( diff --git a/x-pack/solutions/observability/plugins/infra/public/pages/metrics/index.tsx b/x-pack/solutions/observability/plugins/infra/public/pages/metrics/index.tsx index 794da589f9e0b..4b76a883ff8d0 100644 --- a/x-pack/solutions/observability/plugins/infra/public/pages/metrics/index.tsx +++ b/x-pack/solutions/observability/plugins/infra/public/pages/metrics/index.tsx @@ -38,7 +38,7 @@ import { NotFoundPage } from '../404'; import { ReactQueryProvider } from '../../containers/react_query_provider'; import { usePluginConfig } from '../../containers/plugin_config_context'; import { RedirectWithQueryParams } from '../../utils/redirect_with_query_params'; -import { SearchSessionProvider } from '../../hooks/use_search_session'; +import { ReloadRequestTimeProvider } from '../../hooks/use_reload_request_time'; import { OnboardingFlow } from '../../components/shared/templates/no_data_config'; const ADD_DATA_LABEL = i18n.translate('xpack.infra.metricsHeaderAddDataButtonLabel', { @@ -79,7 +79,7 @@ export const InfrastructurePage = () => { - + { /> - +