warm cache for analysis

This commit is contained in:
2026-07-13 19:57:28 +03:00
parent 173aef51c6
commit 1bb9928b24
2 changed files with 44 additions and 7 deletions
+24 -7
View File
@@ -16,9 +16,11 @@ try {
exit; exit;
} }
$api = VisualLimsApiClient::getInstance(); $api = VisualLimsApiClient::getInstance(); // also loads dotenv as a side-effect
$webOnly = isset($_GET['web_only']) ? (int)$_GET['web_only'] : 1; $webOnly = isset($_GET['web_only']) ? (int)$_GET['web_only'] : 1;
$refresh = isset($_GET['refresh']) ? (int)$_GET['refresh'] : 0;
$simulate = ($_ENV['SIMULATE_EXPORT_LIMS'] ?? '') === 'true';
$filterString = "Matrice/IdMatrice eq $idMatrice"; $filterString = "Matrice/IdMatrice eq $idMatrice";
@@ -32,19 +34,34 @@ try {
$base_url = 'https://93.43.5.102/limsapi/api/odata/'; $base_url = 'https://93.43.5.102/limsapi/api/odata/';
$full_url = $base_url . $endpoint; $full_url = $base_url . $endpoint;
file_put_contents(__DIR__ . '/last_url_analisi.txt', $full_url . PHP_EOL, FILE_APPEND); // Cache file matches the one warm_cache.php writes (web_only=1 is what the analysis modal asks for).
// Caching skipped in simulate mode to avoid polluting real-data cache files.
$cache_file = __DIR__ . '/cache/analisi_matrice_' . $idMatrice . ($webOnly === 1 ? '' : '_all') . '.json';
$data = null;
$fromCache = false;
$data = $api->get($endpoint, []); if (!$simulate && !$refresh && file_exists($cache_file) && (time() - filemtime($cache_file) < 3600)) { // 1 ora
$data = json_decode(file_get_contents($cache_file), true);
$fromCache = is_array($data);
}
file_put_contents( if (!$fromCache) {
__DIR__ . '/analisi_by_matrice_response.json', $data = $api->get($endpoint, []);
json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) if (!$simulate) {
); file_put_contents($cache_file, json_encode(['value' => $data['value'] ?? []], JSON_UNESCAPED_UNICODE));
}
}
if ($debug === 1) { if ($debug === 1) {
file_put_contents(__DIR__ . '/last_url_analisi.txt', $full_url . PHP_EOL, FILE_APPEND);
file_put_contents(
__DIR__ . '/analisi_by_matrice_response.json',
json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
);
echo json_encode([ echo json_encode([
'endpoint' => $endpoint, 'endpoint' => $endpoint,
'full_url' => $full_url, 'full_url' => $full_url,
'from_cache' => $fromCache,
'raw_response' => $data 'raw_response' => $data
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit; exit;
+20
View File
@@ -113,6 +113,26 @@ try {
file_put_contents($cacheDir . '/matrici.json', json_encode(['success' => true, 'value' => $matrici])); file_put_contents($cacheDir . '/matrici.json', json_encode(['success' => true, 'value' => $matrici]));
warmLog("[matrici] Cached " . count($matrici) . " items", $isCli); warmLog("[matrici] Cached " . count($matrici) . " items", $isCli);
// 6. Analisi — only for matrici actually used by parts (warming all ~2500 matrici
// would mean one API call each; the used set is a few dozen).
$stmt = $pdo->query("SELECT DISTINCT idmatrice FROM identification_parts WHERE idmatrice IS NOT NULL AND idmatrice > 0 ORDER BY idmatrice ASC");
$matriceIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
warmLog("[analisi] Fetching for " . count($matriceIds) . " matrici in use...", $isCli);
foreach ($matriceIds as $mid) {
$mid = (int)$mid;
// Same query as get_analisi_matrice_filter.php with web_only=1 (what the analysis modal requests)
$filter = rawurlencode("Matrice/IdMatrice eq $mid and SelezionabileSuWeb eq true");
try {
$data = $api->get("Analisi?\$filter={$filter}");
$values = $data['value'] ?? [];
file_put_contents($cacheDir . '/analisi_matrice_' . $mid . '.json', json_encode(['value' => $values]));
warmLog("[analisi] Matrice $mid: " . count($values) . " analisi", $isCli);
} catch (Exception $e) {
warmLog("[analisi] Matrice $mid: ERROR " . $e->getMessage(), $isCli);
}
}
$elapsed = round(microtime(true) - $startTime, 1); $elapsed = round(microtime(true) - $startTime, 1);
warmLog("Done in {$elapsed}s", $isCli); warmLog("Done in {$elapsed}s", $isCli);
} catch (Exception $e) { } catch (Exception $e) {