summaryrefslogtreecommitdiffstats
path: root/assets/index.js
blob: 246364fff884772994cca2ab56e7af6ea6aa6a97 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * @typedef {Object} Check
 * @property {String} status - 'pass'|'fail'|'warn'
 * @property {String} output - Details. Not present if 'pass'
 */

/**
 * @typedef {Check} HealthCheck
 * @property {Map<String, Check>} checks
 */

async function getHealthCheck() {
	const url = "api/healthcheck";
	try {
		const response = await fetch(url);
		if (!response.ok) {
			throw new Error(`Response status: ${response.status}`);
		}

		const json = await response.json();
		return json;
	} catch (error) {
		console.error(error.message);
	}
}

function updateStatus(check) {
	const statusElm = document.getElementById("status");
	const issuesElm = document.getElementById("issues");
	switch (check.status) {
		case "pass":
			issuesElm.textContent = "No issues detected";
			statusElm.setAttribute("class", "ok");
			break;
		case "fail":
			issuesElm.textContent = check.output;
			statusElm.setAttribute("class", "error");
			break;
		case "warn":
			issuesElm.textContent = check.output;
			statusElm.setAttribute("class", "warning");
			break;
		default:
			issuesElm.textContent = "Unknown";
			statusElm.setAttribute("class", "warning");
	}
}

getHealthCheck().then((healthCheck) => {
	const table = document.getElementById("services");
	const evtSource = new EventSource("sse");
	updateStatus(healthCheck);

	for (const [service, check] of Object.entries(healthCheck.checks)) {
		const row = table.insertRow();

		const nameNode = row.insertCell();
		nameNode.textContent = service;

		const stateNode = row.insertCell();
		switch (check.status) {
			case "pass":
				stateNode.textContent = "Operational";
				stateNode.setAttribute("class", "ok");
				break;
			case "fail":
				stateNode.textContent = "Down";
				stateNode.title = check.output;
				stateNode.setAttribute("class", "error");
				break;
			case "warn":
				stateNode.textContent = "Warning";
				stateNode.title = check.output;
				stateNode.setAttribute("class", "warning");
				break;
			default:
				stateNode.textContent = "Unknown";
				statusElm.setAttribute("class", "warning");
		}

		evtSource.addEventListener(service, (event) => {
			const status = JSON.parse(event.data);
			stateNode.textContent = status.state;
			stateNode.title = status.output;
		});
	}
});