[Logs UI] Ensure live stream always gets latest entries (#67935)

LogPositionState doesn't always reevaluate the value of `endTimestamp`
when live stream is on. The [dependencies][1] for it to change rely on the
scroll position to update. If there's less than one scroll page or the
previous API call didn't return any entries, the `endTimestamp` would
not update.

We force `Date.now()` as an `endTimestamp` on the API call to ensure it
always gets the latest entries possible, regardless of the state. This
introduces some inconsistency that will be fixed once work beings on #65493.

[1]: fe4c164681/x-pack/plugins/infra/public/containers/logs/log_position/log_position_state.ts (L160-L173)
This commit is contained in:
Alejandro Fernández 2020-06-02 17:36:47 +02:00 committed by GitHub
parent ce7940adc2
commit 77e7e0bb49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -194,7 +194,10 @@ const useFetchEntriesEffect = (
} }
}; };
const runFetchMoreEntriesRequest = async (direction: ShouldFetchMoreEntries) => { const runFetchMoreEntriesRequest = async (
direction: ShouldFetchMoreEntries,
overrides: Partial<LogEntriesProps> = {}
) => {
if (!props.startTimestamp || !props.endTimestamp) { if (!props.startTimestamp || !props.endTimestamp) {
return; return;
} }
@ -209,10 +212,10 @@ const useFetchEntriesEffect = (
try { try {
const commonFetchArgs: LogEntriesBaseRequest = { const commonFetchArgs: LogEntriesBaseRequest = {
sourceId: props.sourceId, sourceId: overrides.sourceId || props.sourceId,
startTimestamp: props.startTimestamp, startTimestamp: overrides.startTimestamp || props.startTimestamp,
endTimestamp: props.endTimestamp, endTimestamp: overrides.endTimestamp || props.endTimestamp,
query: props.filterQuery, query: overrides.filterQuery || props.filterQuery,
}; };
const fetchArgs: LogEntriesRequest = getEntriesBefore const fetchArgs: LogEntriesRequest = getEntriesBefore
@ -279,10 +282,10 @@ const useFetchEntriesEffect = (
const streamEntriesEffect = () => { const streamEntriesEffect = () => {
(async () => { (async () => {
if (props.isStreaming && !state.isLoadingMore && !state.isReloading) { if (props.isStreaming && !state.isLoadingMore && !state.isReloading) {
const endTimestamp = Date.now();
if (startedStreaming) { if (startedStreaming) {
await new Promise((res) => setTimeout(res, LIVE_STREAM_INTERVAL)); await new Promise((res) => setTimeout(res, LIVE_STREAM_INTERVAL));
} else { } else {
const endTimestamp = Date.now();
props.jumpToTargetPosition({ tiebreaker: 0, time: endTimestamp }); props.jumpToTargetPosition({ tiebreaker: 0, time: endTimestamp });
setStartedStreaming(true); setStartedStreaming(true);
if (state.hasMoreAfterEnd) { if (state.hasMoreAfterEnd) {
@ -290,7 +293,9 @@ const useFetchEntriesEffect = (
return; return;
} }
} }
const newEntriesEnd = await runFetchMoreEntriesRequest(ShouldFetchMoreEntries.After); const newEntriesEnd = await runFetchMoreEntriesRequest(ShouldFetchMoreEntries.After, {
endTimestamp,
});
if (newEntriesEnd) { if (newEntriesEnd) {
props.jumpToTargetPosition(newEntriesEnd); props.jumpToTargetPosition(newEntriesEnd);
} }