File size: 5,419 Bytes
b66240d |
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 |
import apiClient from './apiClient.js';
class AdminDashboard {
constructor() {
this.providersContainer = document.querySelector('[data-admin-providers]');
this.tableBody = document.querySelector('[data-admin-table]');
this.refreshBtn = document.querySelector('[data-admin-refresh]');
this.healthBadge = document.querySelector('[data-admin-health]');
this.latencyChartCanvas = document.querySelector('#provider-latency-chart');
this.statusChartCanvas = document.querySelector('#provider-status-chart');
this.latencyChart = null;
this.statusChart = null;
}
init() {
this.loadProviders();
if (this.refreshBtn) {
this.refreshBtn.addEventListener('click', () => this.loadProviders());
}
}
async loadProviders() {
if (this.tableBody) {
this.tableBody.innerHTML = '<tr><td colspan="5">Loading providers...</td></tr>';
}
const result = await apiClient.getProviders();
if (!result.ok) {
this.providersContainer.innerHTML = `<div class="inline-message inline-error">${result.error}</div>`;
this.tableBody.innerHTML = '';
return;
}
const providers = result.data || [];
this.renderCards(providers);
this.renderTable(providers);
this.renderCharts(providers);
}
renderCards(providers) {
if (!this.providersContainer) return;
const healthy = providers.filter((p) => p.status === 'healthy').length;
const failing = providers.length - healthy;
const avgLatency = (
providers.reduce((sum, provider) => sum + Number(provider.latency || 0), 0) / (providers.length || 1)
).toFixed(0);
this.providersContainer.innerHTML = `
<div class="glass-card stat-card">
<h3>Total Providers</h3>
<div class="stat-value">${providers.length}</div>
</div>
<div class="glass-card stat-card">
<h3>Healthy</h3>
<div class="stat-value text-success">${healthy}</div>
</div>
<div class="glass-card stat-card">
<h3>Issues</h3>
<div class="stat-value text-danger">${failing}</div>
</div>
<div class="glass-card stat-card">
<h3>Avg Latency</h3>
<div class="stat-value">${avgLatency} ms</div>
</div>
`;
if (this.healthBadge) {
this.healthBadge.dataset.state = failing ? 'warn' : 'ok';
this.healthBadge.querySelector('span').textContent = failing ? 'degraded' : 'optimal';
}
}
renderTable(providers) {
if (!this.tableBody) return;
this.tableBody.innerHTML = providers
.map(
(provider) => `
<tr>
<td>${provider.name}</td>
<td>${provider.category || '—'}</td>
<td>${provider.latency || '—'} ms</td>
<td>
<span class="badge ${provider.status === 'healthy' ? 'badge-success' : 'badge-danger'}">
${provider.status}
</span>
</td>
<td>${provider.endpoint || provider.url || ''}</td>
</tr>
`,
)
.join('');
}
renderCharts(providers) {
if (this.latencyChartCanvas) {
const labels = providers.map((p) => p.name);
const data = providers.map((p) => p.latency || 0);
if (this.latencyChart) this.latencyChart.destroy();
this.latencyChart = new Chart(this.latencyChartCanvas, {
type: 'bar',
data: {
labels,
datasets: [
{
label: 'Latency (ms)',
data,
backgroundColor: '#38bdf8',
},
],
},
options: {
plugins: { legend: { display: false } },
scales: {
x: { ticks: { color: 'var(--text-muted)' } },
y: { ticks: { color: 'var(--text-muted)' } },
},
},
});
}
if (this.statusChartCanvas) {
const healthy = providers.filter((p) => p.status === 'healthy').length;
const degraded = providers.length - healthy;
if (this.statusChart) this.statusChart.destroy();
this.statusChart = new Chart(this.statusChartCanvas, {
type: 'doughnut',
data: {
labels: ['Healthy', 'Degraded'],
datasets: [
{
data: [healthy, degraded],
backgroundColor: ['#22c55e', '#f59e0b'],
},
],
},
options: {
plugins: { legend: { labels: { color: 'var(--text-primary)' } } },
},
});
}
}
}
window.addEventListener('DOMContentLoaded', () => {
const dashboard = new AdminDashboard();
dashboard.init();
});
|