diff --git a/public/userarea/get_analisi_matrice_filter.php b/public/userarea/get_analisi_matrice_filter.php index ce5d038..13a937a 100644 --- a/public/userarea/get_analisi_matrice_filter.php +++ b/public/userarea/get_analisi_matrice_filter.php @@ -16,9 +16,11 @@ try { exit; } - $api = VisualLimsApiClient::getInstance(); + $api = VisualLimsApiClient::getInstance(); // also loads dotenv as a side-effect $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"; @@ -32,19 +34,34 @@ try { $base_url = 'https://93.43.5.102/limsapi/api/odata/'; $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( - __DIR__ . '/analisi_by_matrice_response.json', - json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) - ); + if (!$fromCache) { + $data = $api->get($endpoint, []); + if (!$simulate) { + file_put_contents($cache_file, json_encode(['value' => $data['value'] ?? []], JSON_UNESCAPED_UNICODE)); + } + } 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([ 'endpoint' => $endpoint, 'full_url' => $full_url, + 'from_cache' => $fromCache, 'raw_response' => $data ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); exit; diff --git a/public/userarea/warm_cache.php b/public/userarea/warm_cache.php index 2936590..85152d9 100644 --- a/public/userarea/warm_cache.php +++ b/public/userarea/warm_cache.php @@ -113,6 +113,26 @@ try { file_put_contents($cacheDir . '/matrici.json', json_encode(['success' => true, 'value' => $matrici])); 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); warmLog("Done in {$elapsed}s", $isCli); } catch (Exception $e) {