Commit 60343693 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix DebugBar production crash and guard Database instrumentation

DebugBar used self:: in heredoc causing undefined variable fatal error.
Database query instrumentation now wrapped in try/catch so logging/debug
can never crash actual queries.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 520392ae
...@@ -41,18 +41,21 @@ class Database ...@@ -41,18 +41,21 @@ class Database
$stmt->execute($params); $stmt->execute($params);
$elapsed = microtime(true) - $start; $elapsed = microtime(true) - $start;
DebugBar::recordQuery($elapsed); try {
DebugBar::recordQuery($elapsed);
if ($elapsed > 0.1) { if ($elapsed > 0.1) {
Logger::warning("Slow query ({$elapsed}s)", ['sql' => substr($sql, 0, 500)]); Logger::warning("Slow query ({$elapsed}s)", ['sql' => substr($sql, 0, 500)]);
} }
if (preg_match('/(?:FROM|INTO|UPDATE)\s+`?(\w+)`?/i', $sql, $m)) { if (preg_match('/(?:FROM|INTO|UPDATE)\s+`?(\w+)`?/i', $sql, $m)) {
$table = $m[1]; $table = $m[1];
$this->tableQueryCounts[$table] = ($this->tableQueryCounts[$table] ?? 0) + 1; $this->tableQueryCounts[$table] = ($this->tableQueryCounts[$table] ?? 0) + 1;
if ($this->tableQueryCounts[$table] === 11) { if ($this->tableQueryCounts[$table] === 11) {
Logger::warning("N+1 detected: table '{$table}' queried 11+ times in single request"); Logger::warning("N+1 detected: table '{$table}' queried 11+ times in single request");
}
} }
} catch (\Throwable $e) {
} }
return $stmt; return $stmt;
......
...@@ -5,7 +5,7 @@ namespace App\Core; ...@@ -5,7 +5,7 @@ namespace App\Core;
final class DebugBar final class DebugBar
{ {
private static float $startTime; private static float $startTime = 0;
private static int $queryCount = 0; private static int $queryCount = 0;
private static float $queryTime = 0.0; private static float $queryTime = 0.0;
...@@ -22,13 +22,12 @@ final class DebugBar ...@@ -22,13 +22,12 @@ final class DebugBar
public static function render(): string public static function render(): string
{ {
$totalTime = round((microtime(true) - self::$startTime) * 1000); $totalTime = self::$startTime > 0 ? round((microtime(true) - self::$startTime) * 1000) : 0;
$memory = round(memory_get_peak_usage(true) / 1024 / 1024, 1); $memory = round(memory_get_peak_usage(true) / 1024 / 1024, 1);
$queryCount = self::$queryCount;
$queryTime = round(self::$queryTime * 1000); $queryTime = round(self::$queryTime * 1000);
$app = App::getInstance(); $app = App::getInstance();
$route = '-';
$permission = '-';
$employee = '-'; $employee = '-';
try { try {
...@@ -43,7 +42,7 @@ final class DebugBar ...@@ -43,7 +42,7 @@ final class DebugBar
<div id="debug-bar" style="position:fixed;bottom:0;left:0;right:0;background:#1a1a2e;color:#e0e0e0;font-family:monospace;font-size:11px;padding:6px 15px;z-index:99999;direction:ltr;text-align:left;display:flex;gap:20px;align-items:center;border-top:2px solid #0D7377;"> <div id="debug-bar" style="position:fixed;bottom:0;left:0;right:0;background:#1a1a2e;color:#e0e0e0;font-family:monospace;font-size:11px;padding:6px 15px;z-index:99999;direction:ltr;text-align:left;display:flex;gap:20px;align-items:center;border-top:2px solid #0D7377;">
<span style="color:#4ecdc4;font-weight:bold;">DEBUG</span> <span style="color:#4ecdc4;font-weight:bold;">DEBUG</span>
<span>Time: <b>{$totalTime}ms</b></span> <span>Time: <b>{$totalTime}ms</b></span>
<span>Queries: <b>{self::$queryCount}</b> ({$queryTime}ms)</span> <span>Queries: <b>{$queryCount}</b> ({$queryTime}ms)</span>
<span>Memory: <b>{$memory}MB</b></span> <span>Memory: <b>{$memory}MB</b></span>
<span>Employee: <b>{$employee}</b></span> <span>Employee: <b>{$employee}</b></span>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment