[Logs UI]: Fix log error on fetching previous entries (#154459)

## 📓 Summary

Closes #151700 

As shown in the video on [this
comment](https://github.com/elastic/kibana/issues/151700#issuecomment-1497246729),
the issue was reproduced when jumping to the lowest possible edge of the
available logs in the lateral timeline.
In case there are no existing logs before the selected time cursor, the
`topCursor` value required for the previous logs call is set to `null`
and the error was triggered.

I believe in this case the conditional to trigger the error should not
only rely on the missing `topCursor` variable existence, but we should
also check whether previous logs exist with the `hasMoreBefore` state
value.

These are the conditionals in order of execution:
- Top cursor missing but has more previous entries ➡️ trigger the Error
- No more previous entries and the request is not forced ➡️ Short
circuit data fetching
- Top cursor exists => Trigger the request for previous entries.

These changes also spotted an existing bug where the `hasMoreBefore` was
never set to the API response value, but was always `true`, preventing
also showing the "Expand time range" call to action on the scenario
described above.

## 🧪 Testing

- Navigate to Logs stream
- Jump to a point in time that has no logs previously ingested
- Verify the error is not logged when scrolling and that the "extend
range" button is shown


https://user-images.githubusercontent.com/34506779/230292210-c2690c37-efa1-4b05-a237-f14eb78d26eb.mov

---------

Co-authored-by: Marco Antonio Ghiani <marcoantonio.ghiani@elastic.co>
Co-authored-by: Carlos Crespo <crespocarlos@users.noreply.github.com>
This commit is contained in:
Marco Antonio Ghiani 2023-04-18 12:03:24 +02:00 committed by GitHub
parent 779e553f81
commit e472ed5fba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -109,7 +109,7 @@ export function useLogStream({
...(resetOnSuccess ? INITIAL_STATE : prevState),
entries: combined.entries,
hasMoreAfter: combined.hasMoreAfter ?? prevState.hasMoreAfter,
hasMoreBefore: combined.hasMoreAfter ?? prevState.hasMoreAfter,
hasMoreBefore: combined.hasMoreBefore ?? prevState.hasMoreBefore,
bottomCursor: combined.bottomCursor,
topCursor: combined.topCursor,
lastLoadedTime: new Date(),
@ -151,9 +151,9 @@ export function useLogStream({
const fetchPreviousEntries = useCallback<FetchPageCallback>(
(params) => {
if (state.topCursor === null) {
if (state.topCursor === null && state.hasMoreBefore) {
throw new Error(
'useLogStream: Cannot fetch previous entries. No cursor is set.\nEnsure you have called `fetchEntries` at least once.'
'useLogStream: Cannot fetch previous entries.\nIt seems there are more entries available, but no cursor is set.\nEnsure you have called `fetchEntries` at least once.'
);
}
@ -161,10 +161,12 @@ export function useLogStream({
return;
}
fetchLogEntriesBefore(state.topCursor, {
size: LOG_ENTRIES_CHUNK_SIZE,
extendTo: params?.extendTo,
});
if (state.topCursor !== null) {
fetchLogEntriesBefore(state.topCursor, {
size: LOG_ENTRIES_CHUNK_SIZE,
extendTo: params?.extendTo,
});
}
},
[fetchLogEntriesBefore, state.topCursor, state.hasMoreBefore]
);
@ -198,7 +200,7 @@ export function useLogStream({
const fetchNextEntries = useCallback<FetchPageCallback>(
(params) => {
if (state.bottomCursor === null) {
if (state.bottomCursor === null && state.hasMoreAfter) {
throw new Error(
'useLogStream: Cannot fetch next entries. No cursor is set.\nEnsure you have called `fetchEntries` at least once.'
);
@ -208,10 +210,12 @@ export function useLogStream({
return;
}
fetchLogEntriesAfter(state.bottomCursor, {
size: LOG_ENTRIES_CHUNK_SIZE,
extendTo: params?.extendTo,
});
if (state.bottomCursor !== null) {
fetchLogEntriesAfter(state.bottomCursor, {
size: LOG_ENTRIES_CHUNK_SIZE,
extendTo: params?.extendTo,
});
}
},
[fetchLogEntriesAfter, state.bottomCursor, state.hasMoreAfter]
);