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>
181 lines
6.5 KiB
Vue
181 lines
6.5 KiB
Vue
<template>
|
|
<div class="config-wrapper">
|
|
<div class="config-card">
|
|
<div class="config-header">
|
|
<h1>Server Process Management</h1>
|
|
</div>
|
|
|
|
|
|
<!-- Stats Grid -->
|
|
<!-- User requested specific order: CPU (full), Status, PID, Memory, Uptime -->
|
|
<div class="stats-grid mb-4">
|
|
<!-- CPU Usage (Full Width) -->
|
|
<div class="stat-item full-width cpu-progress">
|
|
<label class="stat-label d-flex justify-content-between">
|
|
<span>CPU Usage</span>
|
|
<span>{{ stats.cpu_percent }}%</span>
|
|
</label>
|
|
<div class="progress">
|
|
<div class="progress-bar progress-bar-brand" role="progressbar"
|
|
:style="{ width: Math.min(stats.cpu_percent, 100) + '%' }"
|
|
:aria-valuenow="stats.cpu_percent" aria-valuemin="0" aria-valuemax="100"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Status -->
|
|
<div class="stat-item">
|
|
<label class="stat-label">Status</label>
|
|
<div class="d-flex align-items-center">
|
|
<span :class="['status-badge', 'status-' + (stats.status === 'running' ? 'active' : 'revoked')]">
|
|
{{ stats.status.toUpperCase() }}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- PID -->
|
|
<div class="stat-item">
|
|
<label class="stat-label">Process ID</label>
|
|
<div class="stat-value">{{ stats.pid || 'N/A' }}</div>
|
|
</div>
|
|
|
|
<!-- Memory -->
|
|
<div class="stat-item">
|
|
<label class="stat-label">Memory (RSS)</label>
|
|
<div class="stat-value">{{ stats.memory_mb }} MB</div>
|
|
</div>
|
|
|
|
<!-- Uptime -->
|
|
<div class="stat-item">
|
|
<label class="stat-label">Uptime</label>
|
|
<div class="stat-value monospace">{{ stats.uptime || 'N/A' }}</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Action Buttons -->
|
|
<div class="btn-action-group justify-content-center">
|
|
<!-- Start -->
|
|
<button type="button" class="btn-action btn-action-primary"
|
|
@click="handleControl('start')"
|
|
:disabled="isLoading || stats.status === 'running' || !pkiStatus.server_ready"
|
|
:title="startHint">
|
|
<i v-if="isLoading && action==='start'" class="fas fa-spinner fa-spin"></i>
|
|
<i v-else class="fas fa-play"></i>
|
|
Start Service
|
|
</button>
|
|
|
|
<!-- Restart -->
|
|
<button type="button" class="btn-action btn-action-save"
|
|
@click="handleControl('restart')"
|
|
:disabled="isLoading || !pkiStatus.server_ready"
|
|
:title="startHint">
|
|
<i v-if="isLoading && action==='restart'" class="fas fa-spinner fa-spin"></i>
|
|
<i v-else class="fas fa-sync-alt"></i>
|
|
Restart Service
|
|
</button>
|
|
|
|
<!-- Stop -->
|
|
<button type="button" class="btn-action btn-action-danger"
|
|
@click="handleControl('stop')"
|
|
:disabled="isLoading || stats.status !== 'running'">
|
|
<i v-if="isLoading && action==='stop'" class="fas fa-spinner fa-spin"></i>
|
|
<i v-else class="fas fa-stop"></i>
|
|
Stop Service
|
|
</button>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<ConfirmModal ref="confirmModal" @confirm="runControl" />
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, reactive, computed, onMounted, onUnmounted } from 'vue';
|
|
import { useApi } from '../composables/useApi';
|
|
import { useToast } from '../composables/useToast';
|
|
import { usePkiStatus } from '../composables/usePkiStatus';
|
|
import ConfirmModal from '../components/ConfirmModal.vue';
|
|
|
|
const { profilesApiClient } = useApi();
|
|
const toast = useToast();
|
|
const { status: pkiStatus, refresh: refreshPkiStatus } = usePkiStatus();
|
|
|
|
// Start/Restart need the PKI server files (Stop stays available)
|
|
const startHint = computed(() => {
|
|
if (pkiStatus.server_ready) return '';
|
|
return pkiStatus.pki_ready && pkiStatus.missing.length
|
|
? `Missing server files: ${pkiStatus.missing.join(', ')}`
|
|
: 'Initialize PKI first';
|
|
});
|
|
|
|
// State
|
|
const stats = reactive({
|
|
status: 'unknown',
|
|
pid: null,
|
|
cpu_percent: 0.0,
|
|
memory_mb: 0.0,
|
|
uptime: null
|
|
});
|
|
|
|
const confirmModal = ref(null);
|
|
const isLoading = ref(false);
|
|
const action = ref(''); // 'start', 'stop', 'restart'
|
|
let pollInterval = null;
|
|
|
|
// Methods
|
|
const showMessage = (text, type = 'success') => toast.show(text, type);
|
|
|
|
const fetchStats = async () => {
|
|
try {
|
|
const res = await profilesApiClient.get('/server/process/stats');
|
|
Object.assign(stats, res.data);
|
|
} catch (err) {
|
|
console.error("Failed to fetch stats:", err);
|
|
// Don't show error toast on every poll failure to avoid spam
|
|
}
|
|
};
|
|
|
|
const CONFIRM_CLASSES = { start: 'btn-action-primary', restart: 'btn-action-save', stop: 'btn-action-danger' };
|
|
|
|
const handleControl = (cmd) => {
|
|
confirmModal.value.open({
|
|
title: `${cmd.charAt(0).toUpperCase()}${cmd.slice(1)} the service?`,
|
|
message: `Are you sure you want to ${cmd.toUpperCase()} the OpenVPN service?`,
|
|
confirmText: `Yes, ${cmd}`,
|
|
confirmBtnClass: CONFIRM_CLASSES[cmd],
|
|
data: cmd
|
|
});
|
|
};
|
|
|
|
const runControl = async (cmd) => {
|
|
isLoading.value = true;
|
|
action.value = cmd;
|
|
|
|
try {
|
|
const res = await profilesApiClient.post(`/server/process/${cmd}`);
|
|
showMessage(res.data.message || `Service ${cmd} successful`, 'success');
|
|
|
|
// Refresh stats immediately after action
|
|
setTimeout(fetchStats, 1000);
|
|
} catch (err) {
|
|
const errMsg = err.response?.data?.detail || err.message;
|
|
showMessage(`Failed to ${cmd}: ${errMsg}`, 'error');
|
|
} finally {
|
|
isLoading.value = false;
|
|
action.value = '';
|
|
}
|
|
};
|
|
|
|
onMounted(() => {
|
|
refreshPkiStatus();
|
|
fetchStats();
|
|
// Poll every 5 seconds
|
|
pollInterval = setInterval(fetchStats, 5000);
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
if (pollInterval) clearInterval(pollInterval);
|
|
});
|
|
</script>
|
|
|