useTrafficChart composable replaces the duplicated chart code in Analytics and HistoryModal; theme observer is released on unmount; blob URLs are revoked. ConfirmModal gets a unique id; confirm() in Server/PKI and SweetAlert2 in Account are replaced by ConfirmModal and toasts, sweetalert2 and sass removed. Separate authApiClient for /api/auth. 404 redirect, lazy Clients, token-based isAuthenticated, theme from prefers-color-scheme set before mount. BaseModal reactive size, NewClientModal name check and focus on opened, HistoryModal ignores stale answers. Login.vue.bak, the dead global axios interceptor and empty <p> removed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
309 lines
11 KiB
Vue
309 lines
11 KiB
Vue
<template>
|
|
<div class="kpi-grid mb-4">
|
|
<div class="stat-item">
|
|
<div class="stat-content">
|
|
<h3>{{ kpi.concurrentUsers }}</h3>
|
|
<p class="stat-label">Concurrent Users (Peak)</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="stat-item">
|
|
<div class="stat-content">
|
|
<h3>{{ formatBytes(kpi.totalTraffic) }}</h3>
|
|
<p class="stat-label">Traffic Volume (Total)</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="stat-item">
|
|
<div class="stat-content">
|
|
<h3>{{ kpi.expiringCerts }}</h3>
|
|
<p class="stat-label">Expiring Soon (In 45 Days)</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header d-flex justify-content-between align-items-center flex-wrap">
|
|
<span><i class="fas fa-chart-area me-2"></i>Traffic Overview</span>
|
|
|
|
<div class="chart-header-controls">
|
|
<select class="form-select form-select-sm" style="width:auto;" v-model="range" @change="loadAnalytics">
|
|
<option value="24h">Last 24 Hours</option>
|
|
<option value="7d">Last 7 Days</option>
|
|
<option value="30d">Last 1 Month</option>
|
|
</select>
|
|
|
|
<div class="d-flex align-items-center">
|
|
<div class="toggle-wrapper me-2">
|
|
<label class="toggle-switch">
|
|
<input type="checkbox" id="vizToggle" v-model="isSpeedMode" @change="renderMainChart">
|
|
<span class="toggle-slider"></span>
|
|
</label>
|
|
</div>
|
|
<label class="small fw-bold user-select-none" style="color: var(--text-heading); cursor: pointer;" for="vizToggle">
|
|
Speed
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="chart-container">
|
|
<canvas ref="mainChartRef"></canvas>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-lg-7">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<i class="fas fa-trophy me-2"></i>TOP-10 Active Clients
|
|
</div>
|
|
<div class="card-body p-0">
|
|
<div class="table-responsive">
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Client Name</th>
|
|
<th>Total Data</th>
|
|
<th>Activity Share</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-if="loading.analytics">
|
|
<td colspan="3" class="text-center py-4 text-muted">Loading...</td>
|
|
</tr>
|
|
<tr v-else-if="topClients.length === 0">
|
|
<td colspan="3" class="text-center py-4 text-muted">No data available</td>
|
|
</tr>
|
|
<tr v-else v-for="c in topClients" :key="c.name">
|
|
<td>
|
|
<span class="user-select-all fw-bold" style="color: var(--text-heading);">{{ c.name }}</span>
|
|
</td>
|
|
<td class="font-monospace text-muted">{{ formatBytes(c.total) }}</td>
|
|
<td>
|
|
<div class="d-flex align-items-center">
|
|
<div class="progress flex-grow-1" style="height: 6px;">
|
|
<div class="progress-bar" role="progressbar" :style="{ width: c.percent + '%', backgroundColor: '#EC7C31' }"></div>
|
|
</div>
|
|
<span class="ms-2 small text-muted w-25 text-end">{{ c.percent }}%</span>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-lg-5">
|
|
<div class="card mb-4">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<span><i class="fas fa-exclamation-circle me-2"></i>Certificate Alerts</span>
|
|
<span class="badge bg-secondary" style="font-size: 0.7em;">Next 45 Days</span>
|
|
</div>
|
|
<div class="card-body p-3" style="min-height: 120px; max-height: 200px; overflow-y: auto;">
|
|
<p v-if="loading.certs" class="text-muted text-center py-3 mb-0">Checking certificates...</p>
|
|
<p v-else-if="expiringCertsList.length === 0" class="text-muted text-center py-3 mb-0">
|
|
<i class="fas fa-check-circle text-success me-2"></i>All Good
|
|
</p>
|
|
<div v-else class="list-group list-group-flush">
|
|
<div v-for="cert in expiringCertsList" :key="cert.username" class="list-group-item px-0 py-2 d-flex justify-content-between align-items-center border-0">
|
|
<div>
|
|
<div class="fw-bold small">{{ cert.username }}</div>
|
|
<div class="text-muted" style="font-size: 0.75rem;">Expires: {{ cert.expiration_date }}</div>
|
|
</div>
|
|
<span class="badge status-warning text-dark">{{ cert.days_remaining }} days</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<i class="fas fa-chart-pie me-2"></i>Traffic Distribution
|
|
</div>
|
|
<div class="card-body d-flex align-items-center justify-content-around p-4" style="min-height: 200px;">
|
|
<div class="pie-container" style="width: 140px; height: 140px; flex: 0 0 auto;">
|
|
<canvas ref="pieChartRef"></canvas>
|
|
</div>
|
|
<div class="ms-3 flex-grow-1">
|
|
<div class="mb-3">
|
|
<div class="small text-muted mb-1"><span class="legend-dot" style="background:#1652B8"></span>Download
|
|
</div>
|
|
<div class="h5 mb-0" style="color: var(--text-heading);">{{ kpi.totalReceivedString }}</div>
|
|
</div>
|
|
<div>
|
|
<div class="small text-muted mb-1"><span class="legend-dot" style="background:#EC7C31"></span>Upload</div>
|
|
<div class="h5 mb-0" style="color: var(--text-heading);">{{ kpi.totalSentString }}</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, reactive, onMounted, onUnmounted } from 'vue';
|
|
import Chart from 'chart.js/auto';
|
|
import { useApi } from '../composables/useApi';
|
|
import { useFormatters } from '../composables/useFormatters';
|
|
import { useAppConfig } from '../composables/useAppConfig';
|
|
import { useThemeObserver } from '../composables/useTheme';
|
|
import { useTrafficChart, getThemeColors, COLOR_RX, COLOR_TX } from '../composables/useTrafficChart';
|
|
|
|
const { fetchAnalytics, fetchCertificates } = useApi();
|
|
const { formatBytes } = useFormatters();
|
|
const { config } = useAppConfig();
|
|
|
|
// Refs
|
|
const mainChartRef = ref(null);
|
|
const pieChartRef = ref(null);
|
|
let pieChartInstance = null;
|
|
const mainChart = useTrafficChart(mainChartRef, { maxPoints: 96 });
|
|
|
|
// State
|
|
const loading = reactive({ analytics: true, certs: true });
|
|
const range = ref('24h');
|
|
const isSpeedMode = ref(false);
|
|
|
|
const kpi = reactive({
|
|
concurrentUsers: '-',
|
|
totalTraffic: 0,
|
|
expiringCerts: '-',
|
|
totalReceived: 0,
|
|
totalSent: 0,
|
|
totalReceivedString: '-',
|
|
totalSentString: '-'
|
|
});
|
|
|
|
const topClients = ref([]);
|
|
const expiringCertsList = ref([]);
|
|
let cachedHistory = [];
|
|
|
|
const loadAnalytics = async () => {
|
|
loading.analytics = true;
|
|
try {
|
|
const res = await fetchAnalytics(range.value);
|
|
if (res.success) {
|
|
processData(res.data);
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
} finally {
|
|
loading.analytics = false;
|
|
}
|
|
};
|
|
|
|
const loadCerts = async () => {
|
|
loading.certs = true;
|
|
try {
|
|
// fetchCertificates returns the plain array of profiles
|
|
const profiles = await fetchCertificates();
|
|
const list = profiles.filter(cert => {
|
|
// Only active, non-revoked, and expiring soon (within 45 days)
|
|
return !cert.is_revoked && !cert.is_expired &&
|
|
cert.days_remaining != null && cert.days_remaining <= 45;
|
|
});
|
|
|
|
kpi.expiringCerts = list.length;
|
|
expiringCertsList.value = list.sort((a, b) => a.days_remaining - b.days_remaining);
|
|
} catch (e) {
|
|
console.error(e);
|
|
} finally {
|
|
loading.certs = false;
|
|
}
|
|
};
|
|
|
|
const processData = (data) => {
|
|
kpi.concurrentUsers = data.max_concurrent_24h || 0;
|
|
|
|
// Traffic Distribution
|
|
const dist = data.traffic_distribution || { rx: 0, tx: 0 };
|
|
kpi.totalReceived = Number(dist.rx || 0);
|
|
kpi.totalSent = Number(dist.tx || 0);
|
|
kpi.totalTraffic = kpi.totalReceived + kpi.totalSent;
|
|
|
|
kpi.totalReceivedString = formatBytes(kpi.totalReceived);
|
|
kpi.totalSentString = formatBytes(kpi.totalSent);
|
|
|
|
// Top Clients
|
|
topClients.value = (data.top_clients_24h || []).map(c => ({
|
|
name: c.common_name,
|
|
total: Number(c.total_traffic || 0),
|
|
percent: kpi.totalTraffic > 0 ? ((Number(c.total_traffic) / kpi.totalTraffic) * 100).toFixed(1) : 0
|
|
}));
|
|
|
|
// Map the raw fields to the point format of useTrafficChart
|
|
cachedHistory = (data.global_history_24h || []).map(p => ({
|
|
timestamp: p.timestamp,
|
|
bytes_received: p.total_rx,
|
|
bytes_sent: p.total_tx,
|
|
rx_rate_mbps: p.total_rx_rate,
|
|
tx_rate_mbps: p.total_tx_rate
|
|
}));
|
|
|
|
renderMainChart();
|
|
renderPieChart();
|
|
};
|
|
|
|
const renderMainChart = () => {
|
|
mainChart.render(cachedHistory, { range: range.value, speedMode: isSpeedMode.value });
|
|
};
|
|
|
|
const renderPieChart = () => {
|
|
if (!pieChartRef.value) return;
|
|
if (pieChartInstance) pieChartInstance.destroy();
|
|
|
|
const ctx = pieChartRef.value.getContext('2d');
|
|
const bgColor = getThemeColors().isDark ? '#161b22' : '#ffffff';
|
|
|
|
pieChartInstance = new Chart(ctx, {
|
|
type: 'doughnut',
|
|
data: {
|
|
labels: ['Received', 'Sent'],
|
|
datasets: [{
|
|
data: [kpi.totalReceived, kpi.totalSent],
|
|
backgroundColor: [COLOR_RX, COLOR_TX],
|
|
borderColor: bgColor,
|
|
borderWidth: 2,
|
|
hoverOffset: 4
|
|
}]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
cutout: '70%',
|
|
plugins: { legend: { display: false } }
|
|
}
|
|
});
|
|
};
|
|
|
|
// Lifecycle
|
|
let intervalId;
|
|
|
|
// Redraw the charts when the theme is toggled
|
|
useThemeObserver(() => {
|
|
renderMainChart();
|
|
renderPieChart();
|
|
});
|
|
|
|
onMounted(() => {
|
|
loadAnalytics();
|
|
loadCerts();
|
|
|
|
const refreshTime = config.value?.refresh_interval || 30000;
|
|
intervalId = setInterval(() => {
|
|
loadAnalytics();
|
|
loadCerts();
|
|
}, refreshTime);
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
if (intervalId) clearInterval(intervalId);
|
|
if (pieChartInstance) pieChartInstance.destroy();
|
|
});
|
|
</script>
|