aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/routes/+page.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/routes/+page.ts')
-rw-r--r--frontend/src/routes/+page.ts44
1 files changed, 42 insertions, 2 deletions
diff --git a/frontend/src/routes/+page.ts b/frontend/src/routes/+page.ts
index ba70169..4f8b3f9 100644
--- a/frontend/src/routes/+page.ts
+++ b/frontend/src/routes/+page.ts
@@ -4,9 +4,19 @@ export interface EnhancedService extends Service {
heartbeats: Heartbeat[];
}
+export interface InitialLogEntry {
+ kind: 'check' | 'created' | 'deleted' | 'transition';
+ serviceName: string;
+ serviceUrl: string;
+ timestamp: Date;
+ hb?: Heartbeat;
+ wasUp?: boolean;
+}
+
export async function load({ fetch }) {
let services: EnhancedService[] = [];
let error = '';
+ let initialLog: InitialLogEntry[] = [];
try {
const res = await fetch('/api/services');
@@ -18,16 +28,46 @@ export async function load({ fetch }) {
try {
const hres = await fetch(`/api/services/${svc.id}/history`);
if (!hres.ok) throw new Error(`HTTP ${hres.status}`);
- const heartbeats: Heartbeat[] = await hres.json();
+ const json = await hres.json();
+ const heartbeats: Heartbeat[] = Array.isArray(json) ? json : (json.data ?? []);
return { ...svc, heartbeats };
} catch {
return { ...svc, heartbeats: [] };
}
})
);
+
+ initialLog = [];
+ for (const svc of services) {
+ const hbs = svc.heartbeats;
+ if (hbs.length === 0) continue;
+ const last = hbs[hbs.length - 1];
+ initialLog.push({
+ kind: 'check',
+ serviceName: svc.name,
+ serviceUrl: svc.url,
+ timestamp: new Date(last.tested_at),
+ hb: last
+ });
+ if (hbs.length >= 2) {
+ const prev = hbs[hbs.length - 2];
+ if (prev.is_up !== last.is_up) {
+ initialLog.push({
+ kind: 'transition',
+ serviceName: svc.name,
+ serviceUrl: svc.url,
+ timestamp: new Date(last.tested_at),
+ hb: last,
+ wasUp: prev.is_up
+ });
+ }
+ }
+ }
+ initialLog.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());
+ initialLog = initialLog.slice(0, 50);
} catch (e) {
error = 'Não foi possível conectar ao servidor';
}
- return { services, error };
+ return { services, error, initialLog };
}