Skip to content

Commit 44fbe82

Browse files
committed
feat: adds scroll up for multiple container views
1 parent 8057bff commit 44fbe82

File tree

1 file changed

+71
-20
lines changed

1 file changed

+71
-20
lines changed

assets/composable/eventStreams.ts

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function useLogStream(url: Ref<string>, container?: Ref<Container>) {
7373
const loading = ref(true);
7474
const error = ref(false);
7575
const { paused: scrollingPaused } = useScrollContext();
76-
const { streamConfig, hasComplexLogs, levels, loadingMore } = useLoggingContext();
76+
const { streamConfig, hasComplexLogs, levels, loadingMore, containers } = useLoggingContext();
7777
let initial = true;
7878

7979
function flushNow() {
@@ -105,7 +105,7 @@ function useLogStream(url: Ref<string>, container?: Ref<Container>) {
105105
// sort the buffer the very first time because of multiple logs in parallel
106106
buffer.value.sort((a, b) => a.date.getTime() - b.date.getTime());
107107

108-
if (container) {
108+
if (container || containers.value.length > 0) {
109109
const loadMoreItem = new LoadMoreLogEntry(new Date(), loadOlderLogs);
110110
messages.value = [loadMoreItem];
111111
}
@@ -196,27 +196,78 @@ function useLogStream(url: Ref<string>, container?: Ref<Container>) {
196196

197197
async function loadOlderLogs(entry: LoadMoreLogEntry) {
198198
if (!(messages.value[0] instanceof LoadMoreLogEntry)) throw new Error("No loadMoreLogEntry on first item");
199-
if (!container) throw new Error("No container");
200199

201200
const [loader, ...existingLogs] = messages.value;
202-
const to = existingLogs[0].date;
203-
const lastSeenId = existingLogs[0].id;
204-
const last = messages.value[Math.min(messages.value.length - 1, 300)].date;
205-
const delta = to.getTime() - last.getTime();
206-
const from = new Date(to.getTime() + delta);
207-
try {
208-
loadingMore.value = true;
209-
const { logs: newLogs, signal } = await loadBetween(container, params, from, to, {
210-
min: 100,
211-
lastSeenId,
212-
});
213-
if (newLogs && signal.aborted === false) {
214-
messages.value = [loader, ...newLogs, ...existingLogs];
201+
202+
if (container) {
203+
const to = existingLogs[0].date;
204+
const lastSeenId = existingLogs[0].id;
205+
const last = messages.value[Math.min(messages.value.length - 1, 300)].date;
206+
const delta = to.getTime() - last.getTime();
207+
const from = new Date(to.getTime() + delta);
208+
try {
209+
loadingMore.value = true;
210+
const { logs: newLogs, signal } = await loadBetween(container, params, from, to, {
211+
min: 100,
212+
lastSeenId,
213+
});
214+
if (newLogs && signal.aborted === false) {
215+
messages.value = [loader, ...newLogs, ...existingLogs];
216+
}
217+
} catch (error) {
218+
console.error(error);
219+
} finally {
220+
loadingMore.value = false;
221+
}
222+
} else if (containers.value.length > 0) {
223+
const earliestByContainer = new Map<string, LogEntry<LogMessage>>();
224+
for (const log of existingLogs) {
225+
if (!log.containerID) continue;
226+
const current = earliestByContainer.get(log.containerID);
227+
if (!current || log.date < current.date) {
228+
earliestByContainer.set(log.containerID, log);
229+
}
230+
}
231+
232+
const firstDate = existingLogs[0].date;
233+
const lastDate = existingLogs[Math.min(existingLogs.length - 1, 300)].date;
234+
const delta = firstDate.getTime() - lastDate.getTime();
235+
236+
try {
237+
loadingMore.value = true;
238+
const minPerContainer = Math.ceil(100 / containers.value.length);
239+
240+
const results = await Promise.all(
241+
containers.value.map((c) => {
242+
const earliest = earliestByContainer.get(c.id);
243+
const to = earliest?.date ?? existingLogs[0].date;
244+
const from = new Date(to.getTime() + delta);
245+
return loadBetween(
246+
computed(() => c),
247+
params,
248+
from,
249+
to,
250+
{
251+
min: minPerContainer,
252+
lastSeenId: earliest?.id,
253+
},
254+
);
255+
}),
256+
);
257+
258+
const allNewLogs = results
259+
.filter(({ signal }) => !signal.aborted)
260+
.flatMap(({ logs }) => logs)
261+
.sort((a, b) => a.date.getTime() - b.date.getTime());
262+
263+
if (allNewLogs.length > 0) {
264+
messages.value = [loader, ...allNewLogs, ...existingLogs];
265+
}
266+
} catch (error) {
267+
console.error(error);
268+
} finally {
269+
loadingMore.value = false;
215270
}
216-
} catch (error) {
217-
console.error(error);
218-
} finally {
219-
loadingMore.value = false;
220271
}
221272
}
222273

0 commit comments

Comments
 (0)