diff --git a/x-pack/plugins/infra/public/containers/logs/log_entries/index.ts b/x-pack/plugins/infra/public/containers/logs/log_entries/index.ts index 2ec1206e0dba..b9a5c4068e16 100644 --- a/x-pack/plugins/infra/public/containers/logs/log_entries/index.ts +++ b/x-pack/plugins/infra/public/containers/logs/log_entries/index.ts @@ -69,6 +69,7 @@ export interface LogEntriesStateParams { entries: LogEntriesResponse['data']['entries']; topCursor: LogEntriesResponse['data']['topCursor'] | null; bottomCursor: LogEntriesResponse['data']['bottomCursor'] | null; + centerCursor: TimeKey | null; isReloading: boolean; isLoadingMore: boolean; lastLoadedTime: Date | null; @@ -88,6 +89,7 @@ export const logEntriesInitialState: LogEntriesStateParams = { entries: [], topCursor: null, bottomCursor: null, + centerCursor: null, isReloading: true, isLoadingMore: false, lastLoadedTime: null, @@ -348,7 +350,7 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action return { ...prevState, ...action.payload, - entries: action.payload.entries, + centerCursor: getCenterCursor(action.payload.entries), lastLoadedTime: new Date(), isReloading: false, @@ -360,13 +362,15 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action case Action.ReceiveEntriesBefore: { const newEntries = action.payload.entries; const prevEntries = cleanDuplicateItems(prevState.entries, newEntries); + const entries = [...newEntries, ...prevEntries]; const update = { - entries: [...newEntries, ...prevEntries], + entries, isLoadingMore: false, hasMoreBeforeStart: newEntries.length > 0, // Keep the previous cursor if request comes empty, to easily extend the range. topCursor: newEntries.length > 0 ? action.payload.topCursor : prevState.topCursor, + centerCursor: getCenterCursor(entries), lastLoadedTime: new Date(), }; @@ -375,13 +379,15 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action case Action.ReceiveEntriesAfter: { const newEntries = action.payload.entries; const prevEntries = cleanDuplicateItems(prevState.entries, newEntries); + const entries = [...prevEntries, ...newEntries]; const update = { - entries: [...prevEntries, ...newEntries], + entries, isLoadingMore: false, hasMoreAfterEnd: newEntries.length > 0, // Keep the previous cursor if request comes empty, to easily extend the range. bottomCursor: newEntries.length > 0 ? action.payload.bottomCursor : prevState.bottomCursor, + centerCursor: getCenterCursor(entries), lastLoadedTime: new Date(), }; @@ -394,6 +400,7 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action entries: [], topCursor: null, bottomCursor: null, + centerCursor: null, hasMoreBeforeStart: true, hasMoreAfterEnd: true, }; @@ -419,4 +426,8 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action } }; +function getCenterCursor(entries: LogEntry[]): TimeKey | null { + return entries.length > 0 ? entries[Math.floor(entries.length / 2)].cursor : null; +} + export const LogEntriesState = createContainer(useLogEntriesState); diff --git a/x-pack/plugins/infra/public/containers/logs/log_highlights/log_highlights.tsx b/x-pack/plugins/infra/public/containers/logs/log_highlights/log_highlights.tsx index f5538d90f087..941e89848131 100644 --- a/x-pack/plugins/infra/public/containers/logs/log_highlights/log_highlights.tsx +++ b/x-pack/plugins/infra/public/containers/logs/log_highlights/log_highlights.tsx @@ -18,7 +18,7 @@ const FETCH_THROTTLE_INTERVAL = 3000; interface UseLogHighlightsStateProps { sourceId: string; sourceVersion: string | undefined; - centerPoint: TimeKey | null; + centerCursor: TimeKey | null; size: number; filterQuery: string | null; } @@ -26,7 +26,7 @@ interface UseLogHighlightsStateProps { export const useLogHighlightsState = ({ sourceId, sourceVersion, - centerPoint, + centerCursor, size, filterQuery, }: UseLogHighlightsStateProps) => { @@ -47,7 +47,7 @@ export const useLogHighlightsState = ({ sourceVersion, throttledStartTimestamp, throttledEndTimestamp, - centerPoint, + centerCursor, size, filterQuery, highlightTerms diff --git a/x-pack/plugins/infra/public/pages/logs/stream/page_providers.tsx b/x-pack/plugins/infra/public/pages/logs/stream/page_providers.tsx index 882b230ffdba..e4ccdaf7c574 100644 --- a/x-pack/plugins/infra/public/pages/logs/stream/page_providers.tsx +++ b/x-pack/plugins/infra/public/pages/logs/stream/page_providers.tsx @@ -70,17 +70,15 @@ const LogEntriesStateProvider: React.FC = ({ children }) => { const LogHighlightsStateProvider: React.FC = ({ children }) => { const { sourceId, version } = useContext(Source.Context); - const [{ topCursor, bottomCursor, entries }] = useContext(LogEntriesState.Context); + const [{ topCursor, bottomCursor, centerCursor, entries }] = useContext(LogEntriesState.Context); const { filterQuery } = useContext(LogFilterState.Context); - const centerPoint = entries.length > 0 ? entries[Math.floor(entries.length / 2)].cursor : null; - const highlightsProps = { sourceId, sourceVersion: version, entriesStart: topCursor, entriesEnd: bottomCursor, - centerPoint, + centerCursor, size: entries.length, filterQuery, };