aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/routes/+page.ts
blob: ba7016953f3b3c9fb90ef39762f9284253bbaaf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import type { Service, Heartbeat } from '$lib/types';

export interface EnhancedService extends Service {
	heartbeats: Heartbeat[];
}

export async function load({ fetch }) {
	let services: EnhancedService[] = [];
	let error = '';

	try {
		const res = await fetch('/api/services');
		if (!res.ok) throw new Error(`HTTP ${res.status}`);
		const list: Service[] = await res.json();

		services = await Promise.all(
			list.map(async (svc) => {
				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();
					return { ...svc, heartbeats };
				} catch {
					return { ...svc, heartbeats: [] };
				}
			})
		);
	} catch (e) {
		error = 'Não foi possível conectar ao servidor';
	}

	return { services, error };
}