Skip to content

Commit

Permalink
[Logs UI] Ensure live stream always gets latest entries (#67935)
Browse files Browse the repository at this point in the history
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]: https://github.com/elastic/kibana/blob/fe4c164681e92ef5bf0c28f7ab3dfe00a5aacd6f/x-pack/plugins/infra/public/containers/logs/log_position/log_position_state.ts#L160-L173
  • Loading branch information
Alejandro Fernández committed Jun 2, 2020
1 parent ce7940a commit 77e7e0b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions x-pack/plugins/infra/public/containers/logs/log_entries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ const useFetchEntriesEffect = (
}
};

const runFetchMoreEntriesRequest = async (direction: ShouldFetchMoreEntries) => {
const runFetchMoreEntriesRequest = async (
direction: ShouldFetchMoreEntries,
overrides: Partial<LogEntriesProps> = {}
) => {
if (!props.startTimestamp || !props.endTimestamp) {
return;
}
Expand All @@ -209,10 +212,10 @@ const useFetchEntriesEffect = (

try {
const commonFetchArgs: LogEntriesBaseRequest = {
sourceId: props.sourceId,
startTimestamp: props.startTimestamp,
endTimestamp: props.endTimestamp,
query: props.filterQuery,
sourceId: overrides.sourceId || props.sourceId,
startTimestamp: overrides.startTimestamp || props.startTimestamp,
endTimestamp: overrides.endTimestamp || props.endTimestamp,
query: overrides.filterQuery || props.filterQuery,
};

const fetchArgs: LogEntriesRequest = getEntriesBefore
Expand Down Expand Up @@ -279,18 +282,20 @@ const useFetchEntriesEffect = (
const streamEntriesEffect = () => {
(async () => {
if (props.isStreaming && !state.isLoadingMore && !state.isReloading) {
const endTimestamp = Date.now();
if (startedStreaming) {
await new Promise((res) => setTimeout(res, LIVE_STREAM_INTERVAL));
} else {
const endTimestamp = Date.now();
props.jumpToTargetPosition({ tiebreaker: 0, time: endTimestamp });
setStartedStreaming(true);
if (state.hasMoreAfterEnd) {
runFetchNewEntriesRequest({ endTimestamp });
return;
}
}
const newEntriesEnd = await runFetchMoreEntriesRequest(ShouldFetchMoreEntries.After);
const newEntriesEnd = await runFetchMoreEntriesRequest(ShouldFetchMoreEntries.After, {
endTimestamp,
});
if (newEntriesEnd) {
props.jumpToTargetPosition(newEntriesEnd);
}
Expand Down

0 comments on commit 77e7e0b

Please sign in to comment.