aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/debug/clusters.php98
-rw-r--r--api/debug/config.php59
-rw-r--r--api/debug/dashboard.php200
-rw-r--r--api/debug/error.php36
-rw-r--r--api/debug/errors.php43
-rw-r--r--api/debug/health.php20
-rw-r--r--api/debug/ingest.php117
-rw-r--r--api/debug/stats.php55
8 files changed, 628 insertions, 0 deletions
diff --git a/api/debug/clusters.php b/api/debug/clusters.php
new file mode 100644
index 0000000..5174317
--- /dev/null
+++ b/api/debug/clusters.php
@@ -0,0 +1,98 @@
+<?php
+require_once __DIR__ . '/config.php';
+
+$errors = load_json(ERRORS_FILE);
+$rebuildFlag = DEBUG_DATA_DIR . '/.rebuild_clusters';
+
+// Check if cached clusters exist and are fresh
+if (file_exists(CLUSTERS_FILE) && !file_exists($rebuildFlag)) {
+ $clusters = load_json(CLUSTERS_FILE);
+ json_response([
+ 'success' => true,
+ 'cached' => true,
+ 'clusters' => $clusters,
+ 'total_errors' => count($errors),
+ ]);
+}
+
+// Rebuild clusters
+$normalized = [];
+foreach ($errors as $e) {
+ $msg = strtolower($e['message'] ?? '');
+ $msg = preg_replace('/\b0x[a-f0-9]+\b/', '0xHEX', $msg);
+ $msg = preg_replace('/\b\d+\b/', 'N', $msg);
+ $msg = preg_replace('/["\'].*?["\']/', '"STR"', $msg);
+ $msg = preg_replace('/[^\w\s]/', ' ', $msg);
+ $msg = preg_replace('/\s+/', ' ', $msg);
+ $normalized[] = trim($msg);
+}
+
+$clusters = [];
+$assigned = [];
+$threshold = CLUSTER_SIMILARITY_THRESHOLD;
+
+for ($i = 0; $i < count($errors); $i++) {
+ if (isset($assigned[$i])) continue;
+
+ $cluster = [
+ 'errors' => [$i],
+ 'messages' => [$errors[$i]['message']],
+ 'count' => $errors[$i]['count'] ?? 1,
+ 'types' => [$errors[$i]['type'] ?? 'unknown'],
+ 'categories' => [$errors[$i]['category'] ?? 'runtime'],
+ 'label' => $errors[$i]['message'] ?? '',
+ ];
+ $assigned[$i] = true;
+
+ for ($j = $i + 1; $j < count($errors); $j++) {
+ if (isset($assigned[$j])) continue;
+
+ // Jaccard similarity
+ $tokensA = array_unique(array_filter(explode(' ', $normalized[$i])));
+ $tokensB = array_unique(array_filter(explode(' ', $normalized[$j])));
+ if (empty($tokensA) && empty($tokensB)) {
+ $similarity = 1;
+ } else {
+ $intersection = count(array_intersect($tokensA, $tokensB));
+ $union = count($tokensA) + count($tokensB) - $intersection;
+ $similarity = $union > 0 ? $intersection / $union : 0;
+ }
+
+ if ($similarity >= $threshold) {
+ $cluster['errors'][] = $j;
+ $cluster['messages'][] = $errors[$j]['message'];
+ $cluster['count'] += $errors[$j]['count'] ?? 1;
+ if (!in_array($errors[$j]['type'] ?? 'unknown', $cluster['types'])) {
+ $cluster['types'][] = $errors[$j]['type'] ?? 'unknown';
+ }
+ if (!in_array($errors[$j]['category'] ?? 'runtime', $cluster['categories'])) {
+ $cluster['categories'][] = $errors[$j]['category'] ?? 'runtime';
+ }
+ $assigned[$j] = true;
+ }
+ }
+
+ // Most frequent message as label
+ $freq = array_count_values($cluster['messages']);
+ arsort($freq);
+ $cluster['label'] = substr(key($freq) ?: $cluster['label'], 0, 200);
+ $cluster['error_count'] = count($cluster['errors']);
+ unset($cluster['errors']);
+ unset($cluster['messages']);
+
+ $clusters[] = $cluster;
+}
+
+usort($clusters, fn($a, $b) => $b['count'] - $a['count']);
+
+// Cache
+save_json(CLUSTERS_FILE, $clusters);
+if (file_exists($rebuildFlag)) unlink($rebuildFlag);
+
+json_response([
+ 'success' => true,
+ 'cached' => false,
+ 'clusters' => array_slice($clusters, 0, 50),
+ 'total_errors' => count($errors),
+ 'total_clusters' => count($clusters),
+]);
diff --git a/api/debug/config.php b/api/debug/config.php
new file mode 100644
index 0000000..4a2bd05
--- /dev/null
+++ b/api/debug/config.php
@@ -0,0 +1,59 @@
+<?php
+// api/debug/config.php — Shared config for debug backend
+header('Content-Type: application/json; charset=utf-8');
+header('Access-Control-Allow-Origin: https://webx11.duckdns.org');
+header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
+header('Access-Control-Allow-Headers: Content-Type');
+header('Access-Control-Max-Age: 86400');
+
+// Handle preflight
+if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
+ http_response_code(204);
+ exit;
+}
+
+date_default_timezone_set('UTC');
+
+define('DEBUG_DATA_DIR', __DIR__ . '/../../data/debug');
+define('MAX_ERRORS_PER_BATCH', 100);
+define('MAX_BATCH_SIZE_KB', 64);
+define('CLUSTER_SIMILARITY_THRESHOLD', 0.35);
+define('ERRORS_FILE', DEBUG_DATA_DIR . '/errors.json');
+define('SESSIONS_FILE', DEBUG_DATA_DIR . '/sessions.json');
+define('CLUSTERS_FILE', DEBUG_DATA_DIR . '/clusters.json');
+
+if (!is_dir(DEBUG_DATA_DIR)) {
+ mkdir(DEBUG_DATA_DIR, 0755, true);
+}
+
+function load_json($path) {
+ if (!file_exists($path)) return [];
+ $data = json_decode(file_get_contents($path), true);
+ return is_array($data) ? $data : [];
+}
+
+function save_json($path, $data) {
+ $encoded = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
+ $fp = fopen($path, 'c');
+ if ($fp && flock($fp, LOCK_EX)) {
+ ftruncate($fp, 0);
+ fwrite($fp, $encoded);
+ fflush($fp);
+ flock($fp, LOCK_UN);
+ fclose($fp);
+ } else {
+ // Fallback: write without lock
+ file_put_contents($path, $encoded);
+ }
+ @chmod($path, 0664);
+}
+
+function json_response($data, $code = 200) {
+ http_response_code($code);
+ echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
+ exit;
+}
+
+function error_response($msg, $code = 400) {
+ json_response(['success' => false, 'error' => $msg], $code);
+}
diff --git a/api/debug/dashboard.php b/api/debug/dashboard.php
new file mode 100644
index 0000000..7a46661
--- /dev/null
+++ b/api/debug/dashboard.php
@@ -0,0 +1,200 @@
+<?php
+require_once __DIR__ . '/config.php';
+header('Content-Type: text/html; charset=utf-8');
+
+$errors = load_json(ERRORS_FILE);
+$sessions = load_json(SESSIONS_FILE);
+
+$totalErrors = count($errors);
+$totalSessions = count($sessions);
+$byType = [];
+$bySeverity = [];
+$byCategory = [];
+$byModule = [];
+$byDay = [];
+$topErrors = [];
+
+foreach ($errors as $e) {
+ $t = $e['type'] ?? 'unknown';
+ $byType[$t] = ($byType[$t] ?? 0) + ($e['count'] ?? 1);
+ $s = $e['severity'] ?? 'error';
+ $bySeverity[$s] = ($bySeverity[$s] ?? 0) + ($e['count'] ?? 1);
+ $m = $e['module'] ?? 'unknown';
+ $byModule[$m] = ($byModule[$m] ?? 0) + ($e['count'] ?? 1);
+ $c = $e['category'] ?? 'runtime';
+ $byCategory[$c] = ($byCategory[$c] ?? 0) + ($e['count'] ?? 1);
+ $day = substr($e['ingested_at'] ?? $e['last_seen'], 0, 10);
+ $byDay[$day] = ($byDay[$day] ?? 0) + ($e['count'] ?? 1);
+
+ $hash = $e['hash'] ?? '';
+ if ($hash) {
+ if (!isset($topErrors[$hash])) {
+ $topErrors[$hash] = ['hash' => $hash, 'message' => $e['message'] ?? '', 'count' => 0, 'type' => $t];
+ }
+ $topErrors[$hash]['count'] += $e['count'] ?? 1;
+ }
+}
+usort($topErrors, fn($a, $b) => $b['count'] - $a['count']);
+$topErrors = array_slice($topErrors, 0, 10);
+ksort($byDay);
+
+$recentSessions = array_slice($sessions, -10, 10, true);
+?>
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="UTF-8">
+<meta name="viewport" content="width=device-width, initial-scale=1.0">
+<title>newOS4 Debug Dashboard</title>
+<style>
+ :root { --bg: #0d0d12; --surface: #16161e; --border: #1e1e2a; --text: #e8e8f0; --muted: #888; --accent: #00e5c8; --error: #ff5555; --warn: #ffaa00; --info: #00aaff; }
+ * { box-sizing: border-box; margin: 0; padding: 0; }
+ body { background: var(--bg); color: var(--text); font-family: 'Courier New', monospace; padding: 20px; }
+ h1 { color: var(--accent); font-size: 1.4em; margin-bottom: 4px; }
+ .subtitle { color: var(--muted); font-size: 0.85em; margin-bottom: 20px; }
+ .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; margin-bottom: 20px; }
+ .card { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 14px; }
+ .card h3 { font-size: 0.8em; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }
+ .stat-value { font-size: 2em; font-weight: bold; }
+ .stat-value.error { color: var(--error); }
+ .stat-value.warn { color: var(--warn); }
+ .stat-value.ok { color: var(--accent); }
+ table { width: 100%; border-collapse: collapse; font-size: 0.85em; }
+ th { text-align: left; color: var(--muted); padding: 6px 8px; border-bottom: 1px solid var(--border); font-size: 0.8em; text-transform: uppercase; }
+ td { padding: 6px 8px; border-bottom: 1px solid var(--border); }
+ .bar { display: inline-block; height: 6px; border-radius: 3px; background: var(--accent); margin-right: 6px; vertical-align: middle; }
+ .bar.error { background: var(--error); }
+ .bar.warn { background: var(--warn); }
+ .tag { display: inline-block; padding: 1px 6px; border-radius: 3px; font-size: 0.75em; background: var(--border); }
+ .tag.error { background: #3a1010; color: var(--error); }
+ .tag.warn { background: #3a2a00; color: var(--warn); }
+ .tag.ok { background: #002a20; color: var(--accent); }
+ .refresh { color: var(--muted); font-size: 0.8em; margin-top: 20px; text-align: center; }
+ a { color: var(--accent); text-decoration: none; }
+ a:hover { text-decoration: underline; }
+ .max-w-300 { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+</style>
+</head>
+<body>
+<h1>🔍 newOS4 Debug Dashboard</h1>
+<div class="subtitle">Server-side error aggregation &middot; <?= date('Y-m-d H:i:s') ?> &middot; <a href="stats.php">JSON Stats</a> &middot; <a href="clusters.php">Clusters</a> &middot; <a href="errors.php">Raw Errors</a> &middot; <a href="health.php">Health</a></div>
+
+<?php
+// Find last sync time
+$lastSync = null;
+if (!empty($errors)) {
+ $lastSync = end($errors)['ingested_at'] ?? null;
+}
+if ($lastSync): ?>
+<div style="color:var(--muted);font-size:0.8em;margin-bottom:16px;">Last sync: <?= htmlspecialchars(substr($lastSync, 0, 19)) ?> UTC</div>
+<?php endif; ?>
+
+<div class="grid">
+ <div class="card">
+ <h3>Total Errors</h3>
+ <div class="stat-value <?= $totalErrors > 0 ? 'error' : 'ok' ?>"><?= $totalErrors ?></div>
+ </div>
+ <div class="card">
+ <h3>Sessions</h3>
+ <div class="stat-value"><?= $totalSessions ?></div>
+ </div>
+ <div class="card">
+ <h3>Error Types</h3>
+ <div class="stat-value"><?= count($byType) ?></div>
+ </div>
+ <div class="card">
+ <h3>Modules Affected</h3>
+ <div class="stat-value"><?= count($byModule) ?></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="card">
+ <h3>By Severity</h3>
+ <table>
+ <?php foreach (['fatal' => 'error', 'error' => 'error', 'warning' => 'warn'] as $sev => $cls): ?>
+ <tr><td><span class="tag <?= $cls ?>"><?= $sev ?></span></td><td><?= $bySeverity[$sev] ?? 0 ?></td></tr>
+ <?php endforeach; ?>
+ </table>
+ </div>
+ <div class="card">
+ <h3>By Category</h3>
+ <table>
+ <?php foreach (['network', 'module', 'runtime', 'vfs', 'security', 'tool'] as $cat): ?>
+ <?php if (($byCategory[$cat] ?? 0) > 0): ?>
+ <tr><td><?= $cat ?></td><td><?= $byCategory[$cat] ?? 0 ?></td></tr>
+ <?php endif; ?>
+ <?php endforeach; ?>
+ </table>
+ </div>
+ <div class="card">
+ <h3>By Module</h3>
+ <table>
+ <?php
+ arsort($byModule);
+ $i = 0;
+ foreach (array_slice($byModule, 0, 8) as $mod => $count):
+ if ($i++ > 5) break;
+ ?>
+ <tr><td class="max-w-300"><?= htmlspecialchars($mod) ?></td><td><?= $count ?></td></tr>
+ <?php endforeach; ?>
+ </table>
+ </div>
+</div>
+
+<div class="card" style="margin-bottom:20px;">
+ <h3>Top Errors</h3>
+ <table>
+ <tr><th>#</th><th>Message</th><th>Type</th><th>Count</th></tr>
+ <?php $i = 1; foreach ($topErrors as $e): ?>
+ <tr>
+ <td><?= $i++ ?></td>
+ <td class="max-w-300"><a href="error.php?hash=<?= urlencode($e['hash'] ?? '') ?>" style="color:var(--text);text-decoration:none;" title="View details"><?= htmlspecialchars(substr($e['message'] ?? '', 0, 80)) ?></a></td>
+ <td><span class="tag"><?= htmlspecialchars($e['type'] ?? '?') ?></span></td>
+ <td><?= $e['count'] ?></td>
+ </tr>
+ <?php endforeach; ?>
+ <?php if (empty($topErrors)): ?>
+ <tr><td colspan="4" style="color:var(--muted);text-align:center;">No errors recorded</td></tr>
+ <?php endif; ?>
+ </table>
+</div>
+
+<div class="card" style="margin-bottom:20px;">
+ <h3>Daily Trend (last 14 days)</h3>
+ <table>
+ <tr><th>Day</th><th>Errors</th><th></th></tr>
+ <?php
+ $maxDay = max($byDay) ?: 1;
+ $days = array_slice($byDay, -14, 14, true);
+ foreach ($days as $day => $count):
+ $pct = round($count / $maxDay * 100, 0);
+ ?>
+ <tr>
+ <td><?= $day ?></td>
+ <td><?= $count ?></td>
+ <td><div class="bar" style="width:<?= $pct ?>%;"></div></td>
+ </tr>
+ <?php endforeach; ?>
+ </table>
+</div>
+
+<div class="card">
+ <h3>Recent Sessions</h3>
+ <table>
+ <tr><th>Session</th><th>Version</th><th>Errors</th><th>Last Seen</th></tr>
+ <?php foreach ($recentSessions as $sid => $s): ?>
+ <tr>
+ <td class="max-w-300"><a href="errors.php?session=<?= urlencode($sid) ?>" style="color:var(--text);text-decoration:none;" title="View this session's errors"><?= htmlspecialchars($sid) ?></a></td>
+ <td><?= htmlspecialchars($s['version'] ?? '?') ?></td>
+ <td><?= $s['error_count'] ?? 0 ?></td>
+ <td><?= htmlspecialchars(substr($s['last_seen'] ?? '', 0, 16)) ?></td>
+ </tr>
+ <?php endforeach; ?>
+ </table>
+</div>
+
+<div class="refresh">Auto-refresh every 30s</div>
+<script>setTimeout(() => location.reload(), 30000);</script>
+</body>
+</html>
diff --git a/api/debug/error.php b/api/debug/error.php
new file mode 100644
index 0000000..242dfca
--- /dev/null
+++ b/api/debug/error.php
@@ -0,0 +1,36 @@
+<?php
+require_once __DIR__ . '/config.php';
+
+$hash = $_GET['hash'] ?? '';
+if (!$hash) error_response('Missing hash parameter');
+
+$errors = load_json(ERRORS_FILE);
+$sessions = load_json(SESSIONS_FILE);
+
+$matches = array_filter($errors, fn($e) => ($e['hash'] ?? '') === $hash);
+$matches = array_values($matches);
+
+if (empty($matches)) {
+ error_response('Error not found', 404);
+}
+
+$totalCount = array_sum(array_column($matches, 'count'));
+$uniqueSessions = array_unique(array_column($matches, 'session_id'));
+$firstSeen = min(array_column($matches, 'first_seen'));
+$lastSeen = max(array_column($matches, 'last_seen'));
+
+json_response([
+ 'success' => true,
+ 'hash' => $hash,
+ 'total_occurrences' => count($matches),
+ 'total_count' => $totalCount,
+ 'unique_sessions' => count($uniqueSessions),
+ 'first_seen' => $firstSeen,
+ 'last_seen' => $lastSeen,
+ 'type' => $matches[0]['type'] ?? 'unknown',
+ 'severity' => $matches[0]['severity'] ?? 'error',
+ 'category' => $matches[0]['category'] ?? 'runtime',
+ 'sample_message' => $matches[0]['message'] ?? '',
+ 'sample_stack' => $matches[0]['stack'] ?? null,
+ 'occurrences' => array_slice($matches, 0, 20),
+]);
diff --git a/api/debug/errors.php b/api/debug/errors.php
new file mode 100644
index 0000000..1ed9a2f
--- /dev/null
+++ b/api/debug/errors.php
@@ -0,0 +1,43 @@
+<?php
+require_once __DIR__ . '/config.php';
+
+$errors = load_json(ERRORS_FILE);
+
+// Filters
+$type = $_GET['type'] ?? null;
+$severity = $_GET['severity'] ?? null;
+$module = $_GET['module'] ?? null;
+$category = $_GET['category'] ?? null;
+$search = $_GET['search'] ?? null;
+$from = $_GET['from'] ?? null;
+$to = $_GET['to'] ?? null;
+$session = $_GET['session'] ?? null;
+$page = max(1, (int)($_GET['page'] ?? 1));
+$perPage = min(100, max(1, (int)($_GET['per_page'] ?? 50)));
+
+$filtered = array_filter($errors, function($e) use ($type, $severity, $module, $category, $search, $from, $to, $session) {
+ if ($type && ($e['type'] ?? '') !== $type) return false;
+ if ($severity && ($e['severity'] ?? '') !== $severity) return false;
+ if ($module && ($e['module'] ?? '') !== $module) return false;
+ if ($category && ($e['category'] ?? '') !== $category) return false;
+ if ($session && ($e['session_id'] ?? '') !== $session) return false;
+ if ($search && stripos($e['message'] ?? '', $search) === false) return false;
+ if ($from && ($e['ingested_at'] ?? '') < $from) return false;
+ if ($to && ($e['ingested_at'] ?? '') > $to) return false;
+ return true;
+});
+
+$filtered = array_values($filtered);
+$total = count($filtered);
+$totalPages = max(1, ceil($total / $perPage));
+$offset = ($page - 1) * $perPage;
+$pageErrors = array_slice($filtered, $offset, $perPage);
+
+json_response([
+ 'success' => true,
+ 'total' => $total,
+ 'page' => $page,
+ 'per_page' => $perPage,
+ 'total_pages' => $totalPages,
+ 'errors' => $pageErrors,
+]);
diff --git a/api/debug/health.php b/api/debug/health.php
new file mode 100644
index 0000000..5a0048f
--- /dev/null
+++ b/api/debug/health.php
@@ -0,0 +1,20 @@
+<?php
+require_once __DIR__ . '/config.php';
+
+$errors = load_json(ERRORS_FILE);
+$sessions = load_json(SESSIONS_FILE);
+
+$lastError = !empty($errors) ? end($errors) : null;
+
+json_response([
+ 'success' => true,
+ 'status' => 'ok',
+ 'uptime' => filemtime(__FILE__) ? (time() - filemtime(__FILE__)) : 0,
+ 'total_errors' => count($errors),
+ 'total_sessions' => count($sessions),
+ 'errors_file_size_kb' => file_exists(ERRORS_FILE) ? round(filesize(ERRORS_FILE) / 1024, 1) : 0,
+ 'sessions_file_size_kb' => file_exists(SESSIONS_FILE) ? round(filesize(SESSIONS_FILE) / 1024, 1) : 0,
+ 'last_ingest' => $lastError ? ($lastError['ingested_at'] ?? null) : null,
+ 'last_ingest_message' => $lastError ? substr($lastError['message'] ?? '', 0, 100) : null,
+ 'generated_at' => date('c'),
+]);
diff --git a/api/debug/ingest.php b/api/debug/ingest.php
new file mode 100644
index 0000000..363dd09
--- /dev/null
+++ b/api/debug/ingest.php
@@ -0,0 +1,117 @@
+<?php
+require_once __DIR__ . '/config.php';
+
+if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
+ error_response('Method not allowed', 405);
+}
+
+$raw = file_get_contents('php://input');
+if (!$raw) error_response('Empty request body');
+
+$payload = json_decode($raw, true);
+if (!$payload || !isset($payload['errors']) || !is_array($payload['errors'])) {
+ error_response('Invalid payload: expected { errors: [...], session: {...} }');
+}
+
+$errors = $payload['errors'];
+$session = $payload['session'] ?? [];
+$clientVersion = $payload['version'] ?? 'unknown';
+
+// Validate batch size
+if (count($errors) > MAX_ERRORS_PER_BATCH) {
+ $errors = array_slice($errors, 0, MAX_ERRORS_PER_BATCH);
+}
+if (strlen($raw) > MAX_BATCH_SIZE_KB * 1024) {
+ error_response('Payload too large (max ' . MAX_BATCH_SIZE_KB . 'KB)', 413);
+}
+
+// Load existing data
+$allErrors = load_json(ERRORS_FILE);
+$sessions = load_json(SESSIONS_FILE);
+
+$sessionId = $session['id'] ?? ('anon_' . bin2hex(random_bytes(4)));
+$ingested = 0;
+$now = date('c');
+
+// Update session metadata
+if (!isset($sessions[$sessionId])) {
+ $sessions[$sessionId] = [
+ 'id' => $sessionId,
+ 'first_seen' => $now,
+ 'version' => $clientVersion,
+ 'modules' => $session['modules'] ?? [],
+ 'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
+ 'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
+ 'error_count' => 0,
+ ];
+}
+$sessions[$sessionId]['last_seen'] = $now;
+$sessions[$sessionId]['error_count'] += count($errors);
+$sessions[$sessionId]['version'] = $clientVersion;
+
+// Build a lookup of existing hashes for dedup
+$existingHashes = [];
+foreach ($allErrors as $idx => $existing) {
+ $h = $existing['hash'] ?? '';
+ if ($h) {
+ $existingHashes[$h] = $idx;
+ }
+}
+
+// Ingest each error (dedup by hash)
+foreach ($errors as $e) {
+ $hash = $e['hash'] ?? md5(($e['message'] ?? '') . '|' . ($e['source'] ?? '') . '|' . ($e['type'] ?? ''));
+
+ if (isset($existingHashes[$hash])) {
+ // Update existing entry
+ $idx = $existingHashes[$hash];
+ $allErrors[$idx]['count'] += $e['count'] ?? 1;
+ $allErrors[$idx]['last_seen'] = $e['lastSeen'] ?? $now;
+ $allErrors[$idx]['ingested_at'] = $now;
+ if (!empty($e['stack'])) {
+ $allErrors[$idx]['stack'] = substr($e['stack'], 0, 2000);
+ }
+ $ingested++;
+ continue;
+ }
+
+ $entry = [
+ 'hash' => $hash,
+ 'message' => substr($e['message'] ?? '', 0, 500),
+ 'type' => $e['type'] ?? 'unknown',
+ 'severity' => $e['severity'] ?? 'error',
+ 'category' => $e['category'] ?? 'runtime',
+ 'source' => $e['source'] ?? '',
+ 'module' => $e['module'] ?? null,
+ 'stack' => isset($e['stack']) ? substr($e['stack'], 0, 2000) : null,
+ 'session_id' => $sessionId,
+ 'count' => $e['count'] ?? 1,
+ 'first_seen' => $e['firstSeen'] ?? $now,
+ 'last_seen' => $e['lastSeen'] ?? $now,
+ 'ingested_at' => $now,
+ ];
+
+ $allErrors[] = $entry;
+ $existingHashes[$hash] = count($allErrors) - 1;
+ $ingested++;
+}
+
+// Trim to last 10,000 errors (ring buffer)
+if (count($allErrors) > 10000) {
+ $allErrors = array_slice($allErrors, -10000);
+}
+
+save_json(ERRORS_FILE, $allErrors);
+save_json(SESSIONS_FILE, $sessions);
+
+// Rebuild clusters asynchronously (flag file)
+$rebuildFlag = DEBUG_DATA_DIR . '/.rebuild_clusters';
+file_put_contents($rebuildFlag, time());
+
+json_response([
+ 'success' => true,
+ 'ingested' => $ingested,
+ 'session_id' => $sessionId,
+ 'total_errors' => count($allErrors),
+ 'total_sessions' => count($sessions),
+]);
diff --git a/api/debug/stats.php b/api/debug/stats.php
new file mode 100644
index 0000000..dd0b0db
--- /dev/null
+++ b/api/debug/stats.php
@@ -0,0 +1,55 @@
+<?php
+require_once __DIR__ . '/config.php';
+
+$errors = load_json(ERRORS_FILE);
+$sessions = load_json(SESSIONS_FILE);
+
+$total = count($errors);
+$byType = [];
+$bySeverity = [];
+$byCategory = [];
+$byModule = [];
+$byDay = [];
+$topErrors = [];
+
+foreach ($errors as $e) {
+ $t = $e['type'] ?? 'unknown';
+ $byType[$t] = ($byType[$t] ?? 0) + ($e['count'] ?? 1);
+
+ $s = $e['severity'] ?? 'error';
+ $bySeverity[$s] = ($bySeverity[$s] ?? 0) + ($e['count'] ?? 1);
+
+ $c = $e['category'] ?? 'runtime';
+ $byCategory[$c] = ($byCategory[$c] ?? 0) + ($e['count'] ?? 1);
+
+ $m = $e['module'] ?? 'unknown';
+ $byModule[$m] = ($byModule[$m] ?? 0) + ($e['count'] ?? 1);
+
+ $day = substr($e['ingested_at'] ?? $e['last_seen'], 0, 10);
+ $byDay[$day] = ($byDay[$day] ?? 0) + ($e['count'] ?? 1);
+
+ $hash = $e['hash'] ?? '';
+ if ($hash) {
+ if (!isset($topErrors[$hash])) {
+ $topErrors[$hash] = ['hash' => $hash, 'message' => $e['message'] ?? '', 'count' => 0, 'type' => $t];
+ }
+ $topErrors[$hash]['count'] += $e['count'] ?? 1;
+ }
+}
+
+usort($topErrors, fn($a, $b) => $b['count'] - $a['count']);
+$topErrors = array_slice($topErrors, 0, 20);
+ksort($byDay);
+
+json_response([
+ 'success' => true,
+ 'total_errors' => $total,
+ 'total_sessions' => count($sessions),
+ 'by_type' => $byType,
+ 'by_severity' => $bySeverity,
+ 'by_category' => $byCategory,
+ 'by_module' => $byModule,
+ 'by_day' => $byDay,
+ 'top_errors' => $topErrors,
+ 'generated_at' => date('c'),
+]);