From 630aa45c8743f6334c2856c5749c18fcdd47d067 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 18 Apr 2022 00:42:08 +0200 Subject: [PATCH] fix: Application logs paginated --- .../applications/[id]/logs/index.json.ts | 1 + .../applications/[id]/logs/index.svelte | 102 ++++++++++++++++-- 2 files changed, 96 insertions(+), 7 deletions(-) diff --git a/src/routes/applications/[id]/logs/index.json.ts b/src/routes/applications/[id]/logs/index.json.ts index f3ab6b9ca..4fa0c3d86 100644 --- a/src/routes/applications/[id]/logs/index.json.ts +++ b/src/routes/applications/[id]/logs/index.json.ts @@ -27,6 +27,7 @@ export const get: RequestHandler = async (event) => { .split('\n') .map((l) => l.slice(8)) .filter((a) => a) + .reverse() } }; } diff --git a/src/routes/applications/[id]/logs/index.svelte b/src/routes/applications/[id]/logs/index.svelte index 30b6be9a0..505bfbb8d 100644 --- a/src/routes/applications/[id]/logs/index.svelte +++ b/src/routes/applications/[id]/logs/index.svelte @@ -24,19 +24,22 @@ export let application; import { page } from '$app/stores'; import LoadingLogs from './_Loading.svelte'; - import { getDomain } from '$lib/components/common'; import { get } from '$lib/api'; import { errorNotification } from '$lib/form'; let loadLogsInterval = null; + let allLogs = []; let logs = []; + let currentPage = 1; + let endOfLogs = false; + let startOfLogs = true; let followingBuild; let followingInterval; let logsEl; const { id } = $page.params; onMount(async () => { - loadLogs(); + loadAllLogs(); loadLogsInterval = setInterval(() => { loadLogs(); }, 1000); @@ -45,10 +48,20 @@ clearInterval(loadLogsInterval); clearInterval(followingInterval); }); + async function loadAllLogs() { + try { + const data = await get(`/applications/${id}/logs.json`); + allLogs = data.logs; + logs = data.logs.slice(0, 100); + return; + } catch ({ error }) { + return errorNotification(error); + } + } async function loadLogs() { try { const newLogs = await get(`/applications/${id}/logs.json`); - logs = newLogs.logs; + logs = newLogs.logs.slice(0, 100); return; } catch ({ error }) { return errorNotification(error); @@ -66,6 +79,32 @@ window.clearInterval(followingInterval); } } + async function loadOlderLogs() { + clearInterval(loadLogsInterval); + loadLogsInterval = null; + if (logs.length < 100) { + endOfLogs = true; + return; + } + startOfLogs = false; + endOfLogs = false; + currentPage += 1; + logs = allLogs.slice(currentPage * 100 - 100, currentPage * 100); + } + async function loadNewerLogs() { + if (currentPage !== 1) { + clearInterval(loadLogsInterval); + endOfLogs = false; + loadLogsInterval = null; + currentPage -= 1; + logs = allLogs.slice(currentPage * 100 - 100, currentPage * 100); + } else { + startOfLogs = true; + loadLogsInterval = setInterval(() => { + loadLogs(); + }, 1000); + } + }
@@ -145,9 +184,58 @@
Waiting for the logs...
{:else}
- -
+
+ {#if loadLogsInterval} + + {/if} +
+ +
-
+
{#each logs as log} {log + '\n'} {/each}