add caching of logs
This commit is contained in:
parent
8217f68a0f
commit
28c045fee7
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
.claude/
|
.claude/
|
||||||
debug/
|
debug/
|
||||||
|
cached_logs/
|
||||||
fflogs-schema.json
|
fflogs-schema.json
|
||||||
config.php
|
config.php
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
ini_set('display_errors', '0');
|
ini_set('display_errors', '0');
|
||||||
require_once __DIR__ . '/../config.php';
|
require_once __DIR__ . '/../config.php';
|
||||||
|
require_once __DIR__ . '/cache.php';
|
||||||
session_start_safe();
|
session_start_safe();
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['error' => 'Method not allowed']); exit; }
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['error' => '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'] ?? '');
|
$reportCode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['report_code'] ?? '');
|
||||||
$fightId = (int)($_POST['fight_id'] ?? 0);
|
$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; }
|
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'];
|
$token = $_SESSION['access_token'];
|
||||||
|
|
||||||
function localized_graphql_uri(string $language): string {
|
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']));
|
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;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
ini_set('display_errors', '0');
|
ini_set('display_errors', '0');
|
||||||
require_once __DIR__ . '/../config.php';
|
require_once __DIR__ . '/../config.php';
|
||||||
|
require_once __DIR__ . '/cache.php';
|
||||||
session_start_safe();
|
session_start_safe();
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
@ -11,15 +12,6 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|||||||
exit;
|
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'] ?? '');
|
$reportCode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['report_code'] ?? '');
|
||||||
$fightId = (int)($_POST['fight_id'] ?? 0);
|
$fightId = (int)($_POST['fight_id'] ?? 0);
|
||||||
$startTime = (float)($_POST['start_time'] ?? 0);
|
$startTime = (float)($_POST['start_time'] ?? 0);
|
||||||
@ -34,6 +26,22 @@ if (!$reportCode || !$fightId || !$endTime) {
|
|||||||
exit;
|
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'];
|
$token = $_SESSION['access_token'];
|
||||||
|
|
||||||
function localized_graphql_uri(string $language): string {
|
function localized_graphql_uri(string $language): string {
|
||||||
@ -514,9 +522,17 @@ foreach ($clusters as $group) {
|
|||||||
}
|
}
|
||||||
usort($aoeEvents, fn($a, $b) => $a['timestamp'] <=> $b['timestamp']);
|
usort($aoeEvents, fn($a, $b) => $a['timestamp'] <=> $b['timestamp']);
|
||||||
|
|
||||||
echo json_encode([
|
$response = json_encode([
|
||||||
'players' => array_values($players),
|
'players' => array_values($players),
|
||||||
'aoe_events' => $aoeEvents,
|
'aoe_events' => $aoeEvents,
|
||||||
'fight_start' => (int)$startTime,
|
'fight_start' => (int)$startTime,
|
||||||
'mitigation_names' => $mitigationNames,
|
'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;
|
||||||
|
|||||||
51
api/cache.php
Normal file
51
api/cache.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
const CACHED_LOG_DIR = __DIR__ . '/../cached_logs';
|
||||||
|
|
||||||
|
function cache_language(string $language): string {
|
||||||
|
$language = strtolower(trim($language));
|
||||||
|
return in_array($language, ['en', 'de', 'fr', 'jp'], true) ? $language : 'en';
|
||||||
|
}
|
||||||
|
|
||||||
|
function cache_report_code(string $reportCode): string {
|
||||||
|
return preg_replace('/[^a-zA-Z0-9]/', '', $reportCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cache_key_part(string|int|float|null $value): string {
|
||||||
|
$value = (string)($value ?? '');
|
||||||
|
$value = preg_replace('/[^a-zA-Z0-9._-]/', '_', $value);
|
||||||
|
return trim($value, '_') ?: 'all';
|
||||||
|
}
|
||||||
|
|
||||||
|
function cached_log_path(string $kind, string $reportCode, string $language, array $parts = []): string {
|
||||||
|
$reportCode = cache_report_code($reportCode);
|
||||||
|
$language = cache_language($language);
|
||||||
|
$kind = cache_key_part($kind);
|
||||||
|
$safeParts = array_map('cache_key_part', $parts);
|
||||||
|
$suffix = $safeParts ? '_' . implode('_', $safeParts) : '';
|
||||||
|
|
||||||
|
return CACHED_LOG_DIR . '/' . $reportCode . '/' . $language . '/' . $kind . $suffix . '.json';
|
||||||
|
}
|
||||||
|
|
||||||
|
function read_cached_log(string $kind, string $reportCode, string $language, array $parts = []): ?string {
|
||||||
|
$path = cached_log_path($kind, $reportCode, $language, $parts);
|
||||||
|
if (!is_file($path)) return null;
|
||||||
|
|
||||||
|
$body = file_get_contents($path);
|
||||||
|
if ($body === false || trim($body) === '') return null;
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
function write_cached_log(string $kind, string $reportCode, string $language, array $parts, string $body): void {
|
||||||
|
$decoded = json_decode($body, true);
|
||||||
|
if (!is_array($decoded) || isset($decoded['error'], $decoded['errors'], $decoded['reauth'])) return;
|
||||||
|
|
||||||
|
$path = cached_log_path($kind, $reportCode, $language, $parts);
|
||||||
|
$dir = dirname($path);
|
||||||
|
if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) return;
|
||||||
|
|
||||||
|
$tmp = $path . '.' . bin2hex(random_bytes(4)) . '.tmp';
|
||||||
|
if (file_put_contents($tmp, $body, LOCK_EX) === false) return;
|
||||||
|
rename($tmp, $path);
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
ini_set('display_errors', '0');
|
ini_set('display_errors', '0');
|
||||||
require_once __DIR__ . '/../config.php';
|
require_once __DIR__ . '/../config.php';
|
||||||
|
require_once __DIR__ . '/cache.php';
|
||||||
session_start_safe();
|
session_start_safe();
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
@ -21,6 +22,12 @@ if (strlen($reportCode) < 1) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$cached = read_cached_log('fight', $reportCode, $language);
|
||||||
|
if ($cached !== null) {
|
||||||
|
echo $cached;
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($_SESSION['access_token'])) {
|
if (empty($_SESSION['access_token'])) {
|
||||||
http_response_code(401);
|
http_response_code(401);
|
||||||
echo json_encode(['error' => 'Not authenticated', 'reauth' => true]);
|
echo json_encode(['error' => 'Not authenticated', 'reauth' => true]);
|
||||||
@ -91,6 +98,7 @@ curl_setopt_array($ch, [
|
|||||||
$body = curl_exec($ch);
|
$body = curl_exec($ch);
|
||||||
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
$curlError = curl_error($ch);
|
$curlError = curl_error($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
if ($curlError) {
|
if ($curlError) {
|
||||||
http_response_code(502);
|
http_response_code(502);
|
||||||
@ -112,5 +120,8 @@ if ($httpStatus === 401) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
http_response_code($httpStatus === 200 ? 200 : $httpStatus);
|
http_response_code($httpStatus === 200 ? 200 : $httpStatus);
|
||||||
|
if ($httpStatus === 200) {
|
||||||
|
write_cached_log('fight', $reportCode, $language, [], $body);
|
||||||
|
}
|
||||||
echo $body;
|
echo $body;
|
||||||
exit;
|
exit;
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
ini_set('display_errors', '0');
|
ini_set('display_errors', '0');
|
||||||
require_once __DIR__ . '/../config.php';
|
require_once __DIR__ . '/../config.php';
|
||||||
|
require_once __DIR__ . '/cache.php';
|
||||||
session_start_safe();
|
session_start_safe();
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['error' => 'Method not allowed']); exit; }
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['error' => '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'] ?? '');
|
$reportCode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['report_code'] ?? '');
|
||||||
$fightId = (int)($_POST['fight_id'] ?? 0);
|
$fightId = (int)($_POST['fight_id'] ?? 0);
|
||||||
@ -20,6 +19,16 @@ if (!$reportCode || !$fightId || !$endTime) {
|
|||||||
exit;
|
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'];
|
$token = $_SESSION['access_token'];
|
||||||
|
|
||||||
function pl_gql(string $query): array {
|
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;
|
||||||
|
|||||||
1
run_Server.bat
Normal file
1
run_Server.bat
Normal file
@ -0,0 +1 @@
|
|||||||
|
php -S localhost:8080
|
||||||
Loading…
x
Reference in New Issue
Block a user