From 28c045fee7ff43e0d1aa754b5ef482f49089d92a Mon Sep 17 00:00:00 2001 From: Akurosia Kamo Date: Sun, 24 May 2026 08:22:27 +0200 Subject: [PATCH] add caching of logs --- .gitignore | 1 + api/abilities.php | 23 ++++++++++++++++++--- api/analysis.php | 36 +++++++++++++++++++++++---------- api/cache.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++ api/fight.php | 11 ++++++++++ api/players.php | 23 ++++++++++++++++++--- run_Server.bat | 1 + 7 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 api/cache.php create mode 100644 run_Server.bat diff --git a/.gitignore b/.gitignore index c51124f..6eb1281 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .claude/ debug/ +cached_logs/ fflogs-schema.json config.php diff --git a/api/abilities.php b/api/abilities.php index 5e29d8b..7cdf50e 100644 --- a/api/abilities.php +++ b/api/abilities.php @@ -1,13 +1,12 @@ 'Method not allowed']); exit; } -if (empty($_SESSION['access_token'])) { echo json_encode(['reauth' => true]); exit; } -if (($_SESSION['token_expires'] ?? 0) <= time()) { echo json_encode(['reauth' => true]); exit; } $reportCode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['report_code'] ?? ''); $fightId = (int)($_POST['fight_id'] ?? 0); @@ -19,6 +18,16 @@ $translate = 'true'; if (!$reportCode || !$fightId || !$endTime) { http_response_code(400); echo json_encode(['error' => 'Missing params']); exit; } +$cacheParts = [$fightId, (int)$startTime, (int)$endTime]; +$cached = read_cached_log('abilities', $reportCode, $language, $cacheParts); +if ($cached !== null) { + echo $cached; + exit; +} + +if (empty($_SESSION['access_token'])) { echo json_encode(['reauth' => true]); exit; } +if (($_SESSION['token_expires'] ?? 0) <= time()) { echo json_encode(['reauth' => true]); exit; } + $token = $_SESSION['access_token']; function localized_graphql_uri(string $language): string { @@ -123,4 +132,12 @@ foreach (array_keys($seenIds) as $id) { } usort($abilities, fn($a, $b) => strcmp($a['name'], $b['name'])); -echo json_encode(['abilities' => $abilities, 'players' => $players]); +$response = json_encode(['abilities' => $abilities, 'players' => $players]); +if ($response === false) { + http_response_code(500); + echo json_encode(['error' => 'Could not encode abilities response']); + exit; +} + +write_cached_log('abilities', $reportCode, $language, $cacheParts, $response); +echo $response; diff --git a/api/analysis.php b/api/analysis.php index 60b37b3..b609620 100644 --- a/api/analysis.php +++ b/api/analysis.php @@ -1,6 +1,7 @@ true]); - exit; -} -if (($_SESSION['token_expires'] ?? 0) <= time()) { - echo json_encode(['reauth' => true]); - exit; -} - $reportCode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['report_code'] ?? ''); $fightId = (int)($_POST['fight_id'] ?? 0); $startTime = (float)($_POST['start_time'] ?? 0); @@ -34,6 +26,22 @@ if (!$reportCode || !$fightId || !$endTime) { exit; } +$cacheParts = [$fightId, (int)$startTime, (int)$endTime]; +$cached = read_cached_log('analysis', $reportCode, $language, $cacheParts); +if ($cached !== null) { + echo $cached; + exit; +} + +if (empty($_SESSION['access_token'])) { + echo json_encode(['reauth' => true]); + exit; +} +if (($_SESSION['token_expires'] ?? 0) <= time()) { + echo json_encode(['reauth' => true]); + exit; +} + $token = $_SESSION['access_token']; function localized_graphql_uri(string $language): string { @@ -514,9 +522,17 @@ foreach ($clusters as $group) { } usort($aoeEvents, fn($a, $b) => $a['timestamp'] <=> $b['timestamp']); -echo json_encode([ +$response = json_encode([ 'players' => array_values($players), 'aoe_events' => $aoeEvents, 'fight_start' => (int)$startTime, 'mitigation_names' => $mitigationNames, ]); +if ($response === false) { + http_response_code(500); + echo json_encode(['error' => 'Could not encode analysis response']); + exit; +} + +write_cached_log('analysis', $reportCode, $language, $cacheParts, $response); +echo $response; diff --git a/api/cache.php b/api/cache.php new file mode 100644 index 0000000..65bc873 --- /dev/null +++ b/api/cache.php @@ -0,0 +1,51 @@ + 'Not authenticated', 'reauth' => true]); @@ -91,6 +98,7 @@ curl_setopt_array($ch, [ $body = curl_exec($ch); $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); +curl_close($ch); if ($curlError) { http_response_code(502); @@ -112,5 +120,8 @@ if ($httpStatus === 401) { } http_response_code($httpStatus === 200 ? 200 : $httpStatus); +if ($httpStatus === 200) { + write_cached_log('fight', $reportCode, $language, [], $body); +} echo $body; exit; diff --git a/api/players.php b/api/players.php index 263ce13..0b150e1 100644 --- a/api/players.php +++ b/api/players.php @@ -1,13 +1,12 @@ 'Method not allowed']); exit; } -if (empty($_SESSION['access_token'])) { echo json_encode(['reauth' => true]); exit; } -if (($_SESSION['token_expires'] ?? 0) <= time()) { echo json_encode(['reauth' => true]); exit; } $reportCode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['report_code'] ?? ''); $fightId = (int)($_POST['fight_id'] ?? 0); @@ -20,6 +19,16 @@ if (!$reportCode || !$fightId || !$endTime) { exit; } +$cacheParts = [$fightId, (int)$startTime, (int)$endTime]; +$cached = read_cached_log('players', $reportCode, 'en', $cacheParts); +if ($cached !== null) { + echo $cached; + exit; +} + +if (empty($_SESSION['access_token'])) { echo json_encode(['reauth' => true]); exit; } +if (($_SESSION['token_expires'] ?? 0) <= time()) { echo json_encode(['reauth' => true]); exit; } + $token = $_SESSION['access_token']; function pl_gql(string $query): array { @@ -92,4 +101,12 @@ foreach ($result['data']['reportData']['report']['events']['data'] ?? [] as $ev) } } -echo json_encode(['players' => array_values($players)]); +$response = json_encode(['players' => array_values($players)]); +if ($response === false) { + http_response_code(500); + echo json_encode(['error' => 'Could not encode players response']); + exit; +} + +write_cached_log('players', $reportCode, 'en', $cacheParts, $response); +echo $response; diff --git a/run_Server.bat b/run_Server.bat new file mode 100644 index 0000000..7ca8b0f --- /dev/null +++ b/run_Server.bat @@ -0,0 +1 @@ +php -S localhost:8080