mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-06 19:19:19 +02:00
feat: system page caching via SystemCache + system_cache SQLite table
Add a TTL-based cache for the expensive checks on the admin system page,
eliminating repeated systemctl subprocess calls (~4×~100ms), curl self-pings
(~200-500ms), disk_*_space() and PHP ini reads on every page load.
Changes:
- storage/migrations/007_system_cache.sql: new migration creating the
system_cache table (key TEXT PK, value TEXT, updated_at INTEGER)
- storage/schema.sql: system_cache table added before pages table
- Applied migration to live storage/posterg.db
- src/SystemCache.php: new class with get/set/isStale/ageSeconds/invalidate;
uses SQLite INSERT … ON CONFLICT upsert; no external dependencies
- src/Database.php: added getDatabasePath(): string accessor
- public/admin/system.php:
- Bootstrap SystemCache at request start using the existing DB PDO handle
- system_status: cached with 2-min TTL (systemctl + curl checks)
- php_info: cached with 1-hour TTL (PHP ini values are runtime-constant)
- disk_info: cached with 5-min TTL (total/free/used/pct tuple)
- Logs section: unchanged — always reads live log tail per active tab
- ?refresh=1 GET param invalidates all three cache keys before rendering
- Status panel heading shows cache badge: '⚡ Cache — il y a Xs' (hit)
or '⟳ Actualisé' (miss/fresh), styled via new .sys-cache-badge rules
- public/assets/css/system.css: .sys-cache-badge / --hit / --miss styles
This commit is contained in:
@@ -1,10 +1,24 @@
|
||||
<?php
|
||||
require_once __DIR__ . "/../../config/bootstrap.php";
|
||||
require_once __DIR__ . '/../../src/AdminAuth.php';
|
||||
require_once APP_ROOT . '/src/Database.php';
|
||||
require_once APP_ROOT . '/src/SystemCache.php';
|
||||
AdminAuth::requireLogin();
|
||||
|
||||
$pageTitle = "Système";
|
||||
|
||||
// Bootstrap cache (uses the same SQLite DB as the app)
|
||||
$_db = new Database();
|
||||
$_cache = new SystemCache($_db->getPDO());
|
||||
|
||||
// ?refresh=1 force-busts all cached sections
|
||||
$forceRefresh = isset($_GET['refresh']) && $_GET['refresh'] === '1';
|
||||
if ($forceRefresh) {
|
||||
$_cache->invalidate('system_status');
|
||||
$_cache->invalidate('disk_info');
|
||||
$_cache->invalidate('php_info');
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
// SECTION 1 — STATUS DATA
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
@@ -71,111 +85,141 @@ function statusClass(string $status): string {
|
||||
};
|
||||
}
|
||||
|
||||
$checks = [];
|
||||
// ── system_status cache (2-minute TTL: systemctl + curl checks) ─────────────
|
||||
$statusCacheAge = $_cache->ageSeconds('system_status');
|
||||
$checksFromCache = $_cache->get('system_status', 120);
|
||||
|
||||
// nginx
|
||||
$nginxStatus = systemdStatus('nginx');
|
||||
$nginxVersion = safeExec('nginx -v 2>&1 | head -1');
|
||||
$checks['nginx'] = [
|
||||
'label' => 'nginx',
|
||||
'status' => $nginxStatus,
|
||||
'detail' => $nginxVersion,
|
||||
];
|
||||
if ($checksFromCache !== null) {
|
||||
$checks = $checksFromCache;
|
||||
$statusCached = true;
|
||||
} else {
|
||||
$statusCached = false;
|
||||
$checks = [];
|
||||
|
||||
// php-fpm
|
||||
$phpFpmStatus = null;
|
||||
$phpFpmUnit = null;
|
||||
foreach (['php8.3-fpm', 'php8.2-fpm', 'php8.1-fpm', 'php-fpm'] as $unit) {
|
||||
$s = systemdStatus($unit);
|
||||
if ($s !== null && $s !== 'unknown') {
|
||||
$phpFpmStatus = $s;
|
||||
$phpFpmUnit = $unit;
|
||||
break;
|
||||
// nginx
|
||||
$nginxStatus = systemdStatus('nginx');
|
||||
$nginxVersion = safeExec('nginx -v 2>&1 | head -1');
|
||||
$checks['nginx'] = [
|
||||
'label' => 'nginx',
|
||||
'status' => $nginxStatus,
|
||||
'detail' => $nginxVersion,
|
||||
];
|
||||
|
||||
// php-fpm
|
||||
$phpFpmStatus = null;
|
||||
$phpFpmUnit = null;
|
||||
foreach (['php8.3-fpm', 'php8.2-fpm', 'php8.1-fpm', 'php-fpm'] as $unit) {
|
||||
$s = systemdStatus($unit);
|
||||
if ($s !== null && $s !== 'unknown') {
|
||||
$phpFpmStatus = $s;
|
||||
$phpFpmUnit = $unit;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$checks['php_fpm'] = [
|
||||
'label' => 'php-fpm' . ($phpFpmUnit ? " ($phpFpmUnit)" : ''),
|
||||
'status' => $phpFpmStatus,
|
||||
'detail' => null,
|
||||
];
|
||||
$checks['php_fpm'] = [
|
||||
'label' => 'php-fpm' . ($phpFpmUnit ? " ($phpFpmUnit)" : ''),
|
||||
'status' => $phpFpmStatus,
|
||||
'detail' => null,
|
||||
];
|
||||
|
||||
// Site HTTP ping
|
||||
$siteUrl = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? 'localhost') . '/';
|
||||
$httpResult = localHttpCheck($siteUrl);
|
||||
$checks['site_http'] = [
|
||||
'label' => 'Site HTTP',
|
||||
'status' => $httpResult !== null ? ($httpResult[0] < 500 ? 'active' : 'failed') : null,
|
||||
'detail' => $httpResult !== null ? "HTTP {$httpResult[0]} — {$httpResult[1]} ms" : 'curl indisponible',
|
||||
];
|
||||
// Site HTTP ping
|
||||
$siteUrl = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? 'localhost') . '/';
|
||||
$httpResult = localHttpCheck($siteUrl);
|
||||
$checks['site_http'] = [
|
||||
'label' => 'Site HTTP',
|
||||
'status' => $httpResult !== null ? ($httpResult[0] < 500 ? 'active' : 'failed') : null,
|
||||
'detail' => $httpResult !== null ? "HTTP {$httpResult[0]} — {$httpResult[1]} ms" : 'curl indisponible',
|
||||
];
|
||||
|
||||
// Database
|
||||
require_once APP_ROOT . '/src/Database.php';
|
||||
$dbPath = APP_ROOT . '/storage/test.db';
|
||||
$dbExists = file_exists($dbPath);
|
||||
$dbWritable = $dbExists && is_writable($dbPath);
|
||||
$dbSizeBytes = $dbExists ? filesize($dbPath) : null;
|
||||
$dbSizeHuman = $dbSizeBytes !== null
|
||||
? ($dbSizeBytes > 1048576
|
||||
? number_format($dbSizeBytes / 1048576, 1) . ' MB'
|
||||
: number_format($dbSizeBytes / 1024, 1) . ' KB')
|
||||
: 'N/A';
|
||||
|
||||
$dbRowCount = null;
|
||||
if ($dbExists) {
|
||||
try {
|
||||
$db = new Database();
|
||||
$dbRowCount = $db->getThesisCount();
|
||||
} catch (Throwable $e) {
|
||||
$dbRowCount = null;
|
||||
// Database (DB object already created above, reuse it)
|
||||
$dbPath = $_db->getDatabasePath();
|
||||
$dbExists = file_exists($dbPath);
|
||||
$dbWritable = $dbExists && is_writable($dbPath);
|
||||
$dbSizeBytes = $dbExists ? filesize($dbPath) : null;
|
||||
$dbSizeHuman = $dbSizeBytes !== null
|
||||
? ($dbSizeBytes > 1048576
|
||||
? number_format($dbSizeBytes / 1048576, 1) . ' MB'
|
||||
: number_format($dbSizeBytes / 1024, 1) . ' KB')
|
||||
: 'N/A';
|
||||
$dbRowCount = null;
|
||||
if ($dbExists) {
|
||||
try {
|
||||
$dbRowCount = $_db->getThesisCount();
|
||||
} catch (Throwable $e) {
|
||||
$dbRowCount = null;
|
||||
}
|
||||
}
|
||||
$checks['database'] = [
|
||||
'label' => 'Base de données SQLite',
|
||||
'status' => $dbExists ? ($dbWritable ? 'active' : 'inactive') : 'failed',
|
||||
'detail' => $dbExists
|
||||
? ($dbRowCount !== null ? "$dbRowCount thèses — $dbSizeHuman" : "Lecture impossible — $dbSizeHuman")
|
||||
: 'Fichier introuvable',
|
||||
];
|
||||
|
||||
// Storage directory
|
||||
$storageDir = APP_ROOT . '/storage';
|
||||
$storageWritable = is_dir($storageDir) && is_writable($storageDir);
|
||||
$bannersDir = $storageDir . '/banners';
|
||||
$coversDir = $storageDir . '/covers';
|
||||
$checks['storage'] = [
|
||||
'label' => 'Répertoire storage',
|
||||
'status' => $storageWritable ? 'active' : ($storageDir ? 'inactive' : 'failed'),
|
||||
'detail' => $storageWritable
|
||||
? implode(' · ', array_filter([
|
||||
is_dir($bannersDir) ? ('banners/ ' . count(array_diff(scandir($bannersDir), ['.','..'])) . ' fichiers') : null,
|
||||
is_dir($coversDir) ? ('covers/ ' . count(array_diff(scandir($coversDir), ['.','..'])) . ' fichiers') : null,
|
||||
]))
|
||||
: 'Non accessible en écriture',
|
||||
];
|
||||
|
||||
// Maintenance mode
|
||||
$maintenanceOn = file_exists(APP_ROOT . '/storage/maintenance.flag');
|
||||
$checks['maintenance'] = [
|
||||
'label' => 'Mode maintenance',
|
||||
'status' => $maintenanceOn ? 'warn' : 'active',
|
||||
'detail' => $maintenanceOn ? 'Activé — site public inaccessible' : 'Désactivé',
|
||||
];
|
||||
|
||||
$_cache->set('system_status', $checks);
|
||||
$statusCacheAge = 0;
|
||||
}
|
||||
$checks['database'] = [
|
||||
'label' => 'Base de données SQLite',
|
||||
'status' => $dbExists ? ($dbWritable ? 'active' : 'inactive') : 'failed',
|
||||
'detail' => $dbExists
|
||||
? ($dbRowCount !== null ? "$dbRowCount thèses — $dbSizeHuman" : "Lecture impossible — $dbSizeHuman")
|
||||
: 'Fichier introuvable',
|
||||
];
|
||||
|
||||
// Storage directory
|
||||
$storageDir = APP_ROOT . '/storage';
|
||||
$storageWritable = is_dir($storageDir) && is_writable($storageDir);
|
||||
$bannersDir = $storageDir . '/banners';
|
||||
$coversDir = $storageDir . '/covers';
|
||||
$checks['storage'] = [
|
||||
'label' => 'Répertoire storage',
|
||||
'status' => $storageWritable ? 'active' : ($storageDir ? 'inactive' : 'failed'),
|
||||
'detail' => $storageWritable
|
||||
? implode(' · ', array_filter([
|
||||
is_dir($bannersDir) ? ('banners/ ' . count(array_diff(scandir($bannersDir), ['.','..'])) . ' fichiers') : null,
|
||||
is_dir($coversDir) ? ('covers/ ' . count(array_diff(scandir($coversDir), ['.','..'])) . ' fichiers') : null,
|
||||
]))
|
||||
: 'Non accessible en écriture',
|
||||
];
|
||||
// ── php_info cache (1-hour TTL: PHP ini values don't change at runtime) ───────
|
||||
$phpInfoFromCache = $_cache->get('php_info', 3600);
|
||||
if ($phpInfoFromCache !== null) {
|
||||
$phpInfo = $phpInfoFromCache;
|
||||
} else {
|
||||
$phpInfo = [
|
||||
'version' => PHP_VERSION,
|
||||
'sapi' => PHP_SAPI,
|
||||
'memory_limit' => ini_get('memory_limit'),
|
||||
'upload_max' => ini_get('upload_max_filesize'),
|
||||
'post_max' => ini_get('post_max_size'),
|
||||
'max_exec' => ini_get('max_execution_time') . 's',
|
||||
];
|
||||
$_cache->set('php_info', $phpInfo);
|
||||
}
|
||||
|
||||
// Maintenance mode
|
||||
$maintenanceOn = file_exists(APP_ROOT . '/storage/maintenance.flag');
|
||||
$checks['maintenance'] = [
|
||||
'label' => 'Mode maintenance',
|
||||
'status' => $maintenanceOn ? 'warn' : 'active',
|
||||
'detail' => $maintenanceOn ? 'Activé — site public inaccessible' : 'Désactivé',
|
||||
];
|
||||
|
||||
// PHP info
|
||||
$phpInfo = [
|
||||
'version' => PHP_VERSION,
|
||||
'sapi' => PHP_SAPI,
|
||||
'memory_limit' => ini_get('memory_limit'),
|
||||
'upload_max' => ini_get('upload_max_filesize'),
|
||||
'post_max' => ini_get('post_max_size'),
|
||||
'max_exec' => ini_get('max_execution_time') . 's',
|
||||
];
|
||||
|
||||
// Disk
|
||||
$diskTotal = disk_total_space(APP_ROOT);
|
||||
$diskFree = disk_free_space(APP_ROOT);
|
||||
$diskUsed = $diskTotal - $diskFree;
|
||||
$diskPct = $diskTotal > 0 ? (int) round($diskUsed / $diskTotal * 100) : 0;
|
||||
// ── disk_info cache (5-minute TTL) ────────────────────────────────────────────
|
||||
$diskFromCache = $_cache->get('disk_info', 300);
|
||||
if ($diskFromCache !== null) {
|
||||
$diskTotal = $diskFromCache['total'];
|
||||
$diskFree = $diskFromCache['free'];
|
||||
$diskUsed = $diskFromCache['used'];
|
||||
$diskPct = $diskFromCache['pct'];
|
||||
} else {
|
||||
$diskTotal = disk_total_space(APP_ROOT);
|
||||
$diskFree = disk_free_space(APP_ROOT);
|
||||
$diskUsed = $diskTotal - $diskFree;
|
||||
$diskPct = $diskTotal > 0 ? (int) round($diskUsed / $diskTotal * 100) : 0;
|
||||
$_cache->set('disk_info', [
|
||||
'total' => $diskTotal,
|
||||
'free' => $diskFree,
|
||||
'used' => $diskUsed,
|
||||
'pct' => $diskPct,
|
||||
]);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
// SECTION 2 — LOGS DATA
|
||||
@@ -377,7 +421,8 @@ require_once APP_ROOT . '/templates/head.php';
|
||||
|
||||
<p class="sys-refresh-note">
|
||||
Affiché le <?= date('d/m/Y à H:i:s') ?> —
|
||||
<a href="?tab=<?= htmlspecialchars($activeTab) ?>&n=<?= $selectedN ?>">Rafraîchir</a>
|
||||
<a href="?tab=<?= htmlspecialchars($activeTab) ?>&n=<?= $selectedN ?>">Rafraîchir</a> —
|
||||
<a href="?tab=<?= htmlspecialchars($activeTab) ?>&n=<?= $selectedN ?>&refresh=1">Forcer actualisation</a>
|
||||
</p>
|
||||
|
||||
<!-- ── Tab bar ─────────────────────────────────────────────────────── -->
|
||||
@@ -399,7 +444,17 @@ require_once APP_ROOT . '/templates/head.php';
|
||||
STATUS PANEL
|
||||
════════════════════════════════════════════════════════════════════ -->
|
||||
|
||||
<h2 class="srv-section-title">Services</h2>
|
||||
<h2 class="srv-section-title">Services
|
||||
<?php if ($statusCached && $statusCacheAge !== null): ?>
|
||||
<span class="sys-cache-badge sys-cache-badge--hit" title="Données en cache">
|
||||
⚡ Cache — il y a <?= $statusCacheAge ?>s
|
||||
</span>
|
||||
<?php else: ?>
|
||||
<span class="sys-cache-badge sys-cache-badge--miss" title="Données fraîches">
|
||||
⟳ Actualisé
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
</h2>
|
||||
<div class="srv-grid">
|
||||
<?php foreach ($checks as $check): ?>
|
||||
<?php $st = $check['status'] ?? 'unknown'; ?>
|
||||
|
||||
Reference in New Issue
Block a user