aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/routes/(admin)/admin/+page.svelte
blob: 24b56db17bc9b42bbec56cba6c0334c020b92a72 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<script lang="ts">
	const API = 'http://localhost:8080/api';

	let { data } = $props();

	let initialized = $state(false);
	let token = $state('');
	let services = $state<any[]>([]);
	let showForm = $state(false);

	$effect(() => {
		if (!initialized && data) {
			token = data.token;
			services = data.services ?? [];
			initialized = true;
		}
	});
	let editingSvc: any = $state(null);
	let saving = $state(false);
	let formError = $state('');
	let testing = $state<Record<number, boolean>>({});
	let toggling = $state<Record<number, boolean>>({});

	function apiHeaders() {
		return { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` };
	}

	async function handleUnauthorized(res: Response) {
		if (res.status === 401) {
			window.location.href = '/login';
			return true;
		}
		return false;
	}

	function openCreate() {
		editingSvc = null;
		formError = '';
		showForm = true;
	}

	function openEdit(svc: any) {
		editingSvc = { ...svc };
		formError = '';
		showForm = true;
	}

	function closeForm() {
		showForm = false;
		editingSvc = null;
		formError = '';
	}

	async function reFetch() {
		try {
			const res = await fetch(`${API}/services`, { headers: { Authorization: `Bearer ${token}` } });
			if (await handleUnauthorized(res)) return;
			if (res.ok) {
				const list = await res.json();
				const withStats = await Promise.all(
					list.map(async (svc: any) => {
						try {
							const sr = await fetch(`${API}/services/${svc.id}/stats`, {
								headers: { Authorization: `Bearer ${token}` }
							});
							if (sr.ok) svc.stats = await sr.json();
						} catch {}
						return svc;
					})
				);
				services = withStats;
			}
		} catch {}
	}

	async function handleSubmit(e: Event) {
		e.preventDefault();
		saving = true;
		formError = '';

		const form = e.target as HTMLFormElement;
		const fd = new FormData(form);

		const body: Record<string, any> = {
			name: fd.get('name'),
			url: fd.get('url'),
			group_name: (fd.get('group_name') as string) || 'Geral',
			interval_seconds: parseInt((fd.get('interval_seconds') as string) || '60', 10),
		};
		const keyword = fd.get('keyword_to_find') as string;
		if (keyword) body.keyword_to_find = keyword;
		const discord = fd.get('discord_webhook_url') as string;
		if (discord) body.discord_webhook_url = discord;
		const email = fd.get('alert_email') as string;
		if (email) body.alert_email = email;

		try {
			const id = fd.get('id');
			const method = id ? 'PUT' : 'POST';
			const url = id ? `${API}/services/${id}` : `${API}/services`;
			const res = await fetch(url, {
				method,
				headers: apiHeaders(),
				body: JSON.stringify(body),
			});
			if (await handleUnauthorized(res)) return;
			if (!res.ok) {
				const err = await res.json().catch(() => ({}));
				throw new Error(err.error || 'Erro ao salvar');
			}
			await reFetch();
			closeForm();
		} catch (e: any) {
			formError = e.message;
		} finally {
			saving = false;
		}
	}

	async function handleDelete(id: number) {
		try {
			const res = await fetch(`${API}/services/${id}`, {
				method: 'DELETE',
				headers: { Authorization: `Bearer ${token}` }
			});
			if (await handleUnauthorized(res)) return;
			if (res.ok) await reFetch();
		} catch {}
	}

	async function handleToggle(id: number) {
		toggling = { ...toggling, [id]: true };
		try {
			const res = await fetch(`${API}/services/${id}/toggle`, {
				method: 'PATCH',
				headers: { Authorization: `Bearer ${token}` }
			});
			if (await handleUnauthorized(res)) return;
			if (res.ok) {
				const updated = await res.json();
				services = services.map((s) => (s.id === id ? { ...s, ...updated } : s));
			}
		} catch { /* ignore */ }
		toggling = { ...toggling, [id]: false };
	}

	async function handleTest(id: number) {
		testing = { ...testing, [id]: true };
		try {
			const res = await fetch(`${API}/services/${id}/test`, {
				method: 'POST',
				headers: { Authorization: `Bearer ${token}` }
			});
			if (await handleUnauthorized(res)) return;
			if (res.ok) {
				const hb = await res.json();
				services = services.map((s) =>
					s.id === id ? { ...s, last_heartbeat: hb } : s
				);
			}
		} catch { /* ignore */ }
		testing = { ...testing, [id]: false };
	}
</script>

<svelte:head>
	<title>Admin — YAUM</title>
</svelte:head>

<div class="mb-8 flex items-center justify-between">
	<div>
		<h1 class="text-2xl font-semibold tracking-tight text-white">Painel de Controle</h1>
		<p class="mt-1 text-sm text-[var(--text-muted)]">Gerenciamento de serviços monitorados</p>
	</div>
	<a
		href="/"
		class="text-xs text-[var(--text-muted)] underline transition-colors hover:text-white"
	>
		← Dashboard
	</a>
</div>

<div class="mb-8">
	<button
		onclick={openCreate}
		class="flex items-center gap-2 rounded-xl border border-[var(--border-color)] bg-[var(--bg-card)] px-4 py-2.5 text-sm font-medium text-[var(--text-primary)] transition-all hover:border-[var(--green)]/50 hover:text-[var(--green)] hover:shadow-[0_0_20px_rgba(34,240,106,0.08)]"
	>
		<svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
			<path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4" />
		</svg>
		Novo Serviço
	</button>
</div>

{#if data.error}
	<div class="rounded-xl border border-[var(--red)]/20 bg-[var(--red)]/5 p-6 text-center">
		<p class="text-sm text-[var(--red)]">{data.error}</p>
	</div>
{:else if services.length === 0}
	<div class="rounded-xl border border-dashed border-[var(--border-color)] p-12 text-center">
		<p class="text-sm text-[var(--text-muted)]">Nenhum serviço cadastrado</p>
	</div>
{:else}
	{#each [...new Set(services.map((s) => s.group_name || 'Geral'))].sort() as group}
		<div class="mb-6">
			<h2 class="mb-3 text-xs font-semibold uppercase tracking-wider text-[var(--text-secondary)]">{group}</h2>
			<div class="overflow-hidden rounded-xl border border-[var(--border-color)]">
				<table class="w-full text-left text-sm">
					<thead>
						<tr class="border-b border-[var(--border-color)] bg-[var(--bg-secondary)]/30">
							<th class="px-4 py-3 text-[10px] font-semibold uppercase tracking-wider text-[var(--text-muted)]">ID</th>
							<th class="px-4 py-3 text-[10px] font-semibold uppercase tracking-wider text-[var(--text-muted)]">Nome</th>
							<th class="hidden px-4 py-3 text-[10px] font-semibold uppercase tracking-wider text-[var(--text-muted)] md:table-cell">URL</th>
							<th class="px-4 py-3 text-[10px] font-semibold uppercase tracking-wider text-[var(--text-muted)]">Status</th>
							<th class="px-4 py-3 text-[10px] font-semibold uppercase tracking-wider text-[var(--text-muted)]">SLA 30d</th>
							<th class="px-4 py-3 text-[10px] font-semibold uppercase tracking-wider text-[var(--text-muted)]">Ações</th>
						</tr>
					</thead>
					<tbody>
						{#each services.filter((s) => (s.group_name || 'Geral') === group) as svc (svc.id)}
							<tr class="border-b border-[var(--border-color)] transition-colors hover:bg-[var(--bg-secondary)]/20">
						<td class="px-4 py-3 font-mono text-xs text-[var(--text-muted)]">{svc.id}</td>
						<td class="px-4 py-3 font-medium text-white">{svc.name}</td>
						<td class="hidden max-w-[200px] truncate px-4 py-3 font-mono text-xs text-[var(--text-muted)] md:table-cell">{svc.url}</td>
						<td class="px-4 py-3">
							<span
								class="inline-flex items-center gap-1 rounded-md px-2 py-0.5 text-[10px] font-semibold"
								style="background-color: {svc.last_heartbeat?.is_up ?? true ? 'rgba(34,240,106,0.1)' : 'rgba(255,64,96,0.1)'}; color: {svc.last_heartbeat?.is_up ?? true ? 'var(--green)' : 'var(--red)'}"
							>
								{svc.last_heartbeat?.is_up ?? true ? 'UP' : 'DOWN'}
							</span>
						</td>
						<td class="px-4 py-3 font-mono text-xs tabular-nums">
							{svc.stats ? svc.stats.uptime_30d.toFixed(2) + '%' : '—'}
						</td>
						<td class="px-4 py-3">
							<div class="flex flex-wrap gap-1.5">
								<button
									onclick={() => openEdit(svc)}
									class="rounded-lg px-2.5 py-1 text-[10px] font-medium text-[var(--text-secondary)] transition-colors hover:bg-[var(--border-color)] hover:text-white"
								>
									Editar
								</button>
								<button
									onclick={() => handleToggle(svc.id)}
									disabled={toggling[svc.id] ?? false}
									class="rounded-lg px-2.5 py-1 text-[10px] font-medium transition-colors"
									style="color: {svc.is_active ?? true ? 'var(--green)' : 'var(--red)'}; {(svc.is_active ?? true) ? 'border:1px solid rgba(34,240,106,0.3)' : 'border:1px solid rgba(255,64,96,0.3)'}; {toggling[svc.id] ? 'opacity:0.5' : ''}"
								>
									{svc.is_active ?? true ? 'Ativo' : 'Pausado'}
								</button>
								<button
									onclick={() => handleTest(svc.id)}
									disabled={testing[svc.id] ?? false}
									class="rounded-lg px-2.5 py-1 text-[10px] font-medium text-[var(--text-secondary)] transition-colors hover:bg-[var(--border-color)] hover:text-white"
									style={testing[svc.id] ? 'opacity:0.5' : ''}
								>
									{testing[svc.id] ? '...' : 'Testar'}
								</button>
								<button
									onclick={() => handleDelete(svc.id)}
									class="rounded-lg px-2.5 py-1 text-[10px] font-medium text-[var(--red)] transition-colors hover:bg-[var(--red)]/10"
								>
									Excluir
								</button>
							</div>
						</td>
					</tr>
				{/each}
			</tbody>
		</table>
	</div>
	</div>
	{/each}
{/if}

<!-- Modal create / edit -->
{#if showForm}
	<!-- svelte-ignore a11y_click_events_have_key_events -->
	<div
		role="button"
		tabindex="-1"
		aria-label="Fechar"
		class="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm"
		onclick={closeForm}
	>
		<div
			class="w-full max-w-md animate-[modalIn_0.2s_ease-out] rounded-2xl border border-[var(--border-color)] bg-[var(--bg-secondary)] p-6 shadow-2xl"
			onclick={(e) => e.stopPropagation()}
		>
			<div class="mb-5 flex items-center justify-between">
				<h2 class="text-sm font-semibold uppercase tracking-wider text-[var(--text-secondary)]">
					{editingSvc ? 'Editar Serviço' : 'Novo Serviço'}
				</h2>
				<button
					onclick={closeForm}
					class="rounded-lg p-1.5 text-[var(--text-muted)] transition-colors hover:bg-[var(--border-color)] hover:text-white"
					aria-label="Fechar"
				>
					<svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
						<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
					</svg>
				</button>
			</div>

			<form onsubmit={handleSubmit} class="space-y-4">
				{#if editingSvc}
					<input type="hidden" name="id" value={editingSvc.id} />
				{/if}

				<div>
					<label for="admin-name" class="mb-1.5 block text-xs font-medium text-[var(--text-secondary)]">Nome do Serviço</label>
					<input
						id="admin-name"
						name="name"
						type="text"
						value={editingSvc?.name ?? ''}
						required
						placeholder="Meu Site"
						class="w-full rounded-xl border border-[var(--border-color)] bg-[var(--bg-card)] px-3.5 py-2.5 text-sm text-white outline-none transition-all placeholder:text-[var(--text-muted)] focus:border-[var(--green)]/50 focus:ring-1 focus:ring-[var(--green)]/20"
					/>
				</div>

				<div>
					<label for="admin-url" class="mb-1.5 block text-xs font-medium text-[var(--text-secondary)]">URL</label>
	<input
		id="admin-url"
		name="url"
		type="url"
		value={editingSvc?.url ?? ''}
		required
		placeholder="https://exemplo.com"
		class="w-full rounded-xl border border-[var(--border-color)] bg-[var(--bg-card)] px-3.5 py-2.5 text-sm text-white outline-none transition-all placeholder:text-[var(--text-muted)] focus:border-[var(--green)]/50 focus:ring-1 focus:ring-[var(--green)]/20"
	/>
</div>

<div>
	<label for="admin-group" class="mb-1.5 block text-xs font-medium text-[var(--text-secondary)]">Grupo / Categoria</label>
	<input
		id="admin-group"
		name="group_name"
		type="text"
		value={editingSvc?.group_name ?? 'Geral'}
		placeholder="APIs, Websites, Servidores..."
		class="w-full rounded-xl border border-[var(--border-color)] bg-[var(--bg-card)] px-3.5 py-2.5 text-sm text-white outline-none transition-all placeholder:text-[var(--text-muted)] focus:border-[var(--green)]/50 focus:ring-1 focus:ring-[var(--green)]/20"
	/>
</div>

<div>
					<label for="admin-interval" class="mb-1.5 block text-xs font-medium text-[var(--text-secondary)]">Intervalo (segundos)</label>
					<input
						id="admin-interval"
						name="interval_seconds"
						type="number"
						value={editingSvc?.interval_seconds ?? 60}
						min="10"
						max="3600"
						class="w-full rounded-xl border border-[var(--border-color)] bg-[var(--bg-card)] px-3.5 py-2.5 text-sm text-white outline-none transition-all placeholder:text-[var(--text-muted)] focus:border-[var(--green)]/50 focus:ring-1 focus:ring-[var(--green)]/20"
					/>
				</div>

				<div class="border-t border-[var(--border-color)] pt-4">
					<p class="mb-3 text-xs font-semibold uppercase tracking-wider text-[var(--text-muted)]">Opcionais</p>

					<div>
						<label for="admin-keyword" class="mb-1.5 block text-xs font-medium text-[var(--text-secondary)]">Palavra-chave</label>
						<input
							id="admin-keyword"
							name="keyword_to_find"
							type="text"
							value={editingSvc?.keyword_to_find ?? ''}
							placeholder="Bem-vindo, Login..."
							class="w-full rounded-xl border border-[var(--border-color)] bg-[var(--bg-card)] px-3.5 py-2.5 text-sm text-white outline-none transition-all placeholder:text-[var(--text-muted)] focus:border-[var(--green)]/50 focus:ring-1 focus:ring-[var(--green)]/20"
						/>
					</div>

					<div class="mt-3">
						<label for="admin-webhook" class="mb-1.5 block text-xs font-medium text-[var(--text-secondary)]">Webhook Discord</label>
						<input
							id="admin-webhook"
							name="discord_webhook_url"
							type="url"
							value={editingSvc?.discord_webhook_url ?? ''}
							placeholder="https://discord.com/api/webhooks/..."
							class="w-full rounded-xl border border-[var(--border-color)] bg-[var(--bg-card)] px-3.5 py-2.5 text-sm text-white outline-none transition-all placeholder:text-[var(--text-muted)] focus:border-[var(--green)]/50 focus:ring-1 focus:ring-[var(--green)]/20"
						/>
					</div>

					<div class="mt-3">
						<label for="admin-email" class="mb-1.5 block text-xs font-medium text-[var(--text-secondary)]">E-mail de Alerta</label>
						<input
							id="admin-email"
							name="alert_email"
							type="email"
							value={editingSvc?.alert_email ?? ''}
							placeholder="admin@exemplo.com"
							class="w-full rounded-xl border border-[var(--border-color)] bg-[var(--bg-card)] px-3.5 py-2.5 text-sm text-white outline-none transition-all placeholder:text-[var(--text-muted)] focus:border-[var(--green)]/50 focus:ring-1 focus:ring-[var(--green)]/20"
						/>
					</div>
				</div>

				{#if formError}
					<p class="rounded-lg bg-[var(--red)]/5 px-3 py-2 text-xs text-[var(--red)]">{formError}</p>
				{/if}

				<div class="flex gap-3 pt-1">
					<button
						type="button"
						onclick={closeForm}
						class="flex-1 rounded-xl border border-[var(--border-color)] px-4 py-2.5 text-sm font-medium text-[var(--text-muted)] transition-colors hover:border-[var(--text-muted)]/30 hover:text-white"
					>
						Cancelar
					</button>
					<button
						type="submit"
						disabled={saving}
						class="flex-1 rounded-xl border border-[var(--green)] bg-[var(--green)]/10 px-4 py-2.5 text-sm font-medium text-[var(--green)] transition-all hover:bg-[var(--green)]/20 disabled:cursor-not-allowed disabled:opacity-40"
					>
						{saving ? 'Salvando…' : editingSvc ? 'Salvar' : 'Adicionar'}
					</button>
				</div>
			</form>
		</div>
	</div>
{/if}

<style>
	@keyframes modalIn {
		from { opacity: 0; transform: scale(0.95) translateY(-8px); }
		to   { opacity: 1; transform: scale(1) translateY(0); }
	}
</style>