111 lines
4.5 KiB
PHP
111 lines
4.5 KiB
PHP
<?php
|
|
// Cache warm-up script.
|
|
// Run via CLI: php warm_cache.php
|
|
// Run via cron: every 30 min: php /path/to/warm_cache.php
|
|
// Run via HTTP: http://localhost:8000/public/userarea/warm_cache.php (requires auth)
|
|
|
|
$startTime = microtime(true);
|
|
$isCli = (php_sapi_name() === 'cli');
|
|
|
|
if (!$isCli) {
|
|
// When called via HTTP, require auth
|
|
include('include/headscript.php');
|
|
if (!Auth::check()) {
|
|
http_response_code(403);
|
|
echo json_encode(['error' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
header('Content-Type: application/json');
|
|
}
|
|
|
|
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
|
require_once __DIR__ . '/class/VisualLimsApiClient.class.php';
|
|
require_once __DIR__ . '/class/db-functions.php';
|
|
|
|
$cacheDir = __DIR__ . '/cache';
|
|
if (!is_dir($cacheDir)) mkdir($cacheDir, 0777, true);
|
|
|
|
$log = [];
|
|
function warmLog(string $msg, bool $isCli) {
|
|
global $log;
|
|
$line = date('H:i:s') . " $msg";
|
|
$log[] = $line;
|
|
if ($isCli) echo $line . PHP_EOL;
|
|
}
|
|
|
|
try {
|
|
$api = VisualLimsApiClient::getInstance();
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
// 1. Clients
|
|
warmLog('[clients] Fetching...', $isCli);
|
|
$params = ['$select' => 'IdCliente,Nominativo,CodiceCliente', '$orderby' => 'Nominativo asc'];
|
|
$clientData = $api->get("Cliente?" . http_build_query($params));
|
|
file_put_contents($cacheDir . '/clienti.json', json_encode($clientData));
|
|
$clientCount = count($clientData['value'] ?? []);
|
|
warmLog("[clients] Cached $clientCount clients", $isCli);
|
|
|
|
// 2. Fixed fields: MoltiplicatorePrezzo, AnagraficaCertestObject, AnagraficaCertestService
|
|
$fixedFields = [
|
|
'MoltiplicatorePrezzo' => ['endpoint' => 'MoltiplicatorePrezzi', 'file' => 'moltiplicatori_prezzo.json'],
|
|
'AnagraficaCertestObject' => ['endpoint' => 'AnagraficaCertestObject', 'file' => 'anagrafica_certest_object.json'],
|
|
'AnagraficaCertestService' => ['endpoint' => 'AnagraficaCertestService', 'file' => 'anagrafica_certest_service.json'],
|
|
];
|
|
|
|
foreach ($fixedFields as $name => $cfg) {
|
|
warmLog("[$name] Fetching...", $isCli);
|
|
$data = $api->get($cfg['endpoint']);
|
|
file_put_contents($cacheDir . '/' . $cfg['file'], json_encode($data, JSON_PRETTY_PRINT));
|
|
$count = count($data['value'] ?? $data ?? []);
|
|
warmLog("[$name] Cached $count items", $isCli);
|
|
}
|
|
|
|
// 3. ClienteResponsabile — for each unique client in datadb
|
|
$stmt = $pdo->query("SELECT DISTINCT idclient FROM datadb WHERE idclient IS NOT NULL AND idclient > 0 ORDER BY idclient ASC");
|
|
$clientIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
warmLog("[ClienteResponsabile] Fetching for " . count($clientIds) . " clients...", $isCli);
|
|
|
|
foreach ($clientIds as $cid) {
|
|
$cid = (int)$cid;
|
|
$endpoint = "Cliente($cid)?\$expand=Responsabili";
|
|
$cacheFile = $cacheDir . '/cliente_responsabili_' . $cid . '.json';
|
|
try {
|
|
$data = $api->get($endpoint);
|
|
file_put_contents($cacheFile, json_encode($data, JSON_PRETTY_PRINT));
|
|
$count = count($data['Responsabili'] ?? []);
|
|
warmLog("[ClienteResponsabile] Client $cid: $count responsabili", $isCli);
|
|
} catch (Exception $e) {
|
|
warmLog("[ClienteResponsabile] Client $cid: ERROR " . $e->getMessage(), $isCli);
|
|
}
|
|
}
|
|
|
|
// 4. CustomField values — all SceltaMultipla field_ids from template_mapping
|
|
$stmt = $pdo->query("SELECT DISTINCT field_id FROM template_mapping WHERE data_type = 'SceltaMultipla' AND is_visible_import = 1 AND field_id IS NOT NULL ORDER BY field_id");
|
|
$fieldIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
warmLog("[CustomFields] Fetching " . count($fieldIds) . " fields...", $isCli);
|
|
|
|
foreach ($fieldIds as $fid) {
|
|
$fid = (int)$fid;
|
|
$cacheFile = $cacheDir . '/customfield_' . $fid . '.json';
|
|
try {
|
|
$endpoint = "CustomField($fid)?\$expand=CustomFieldsValues";
|
|
$data = $api->get($endpoint);
|
|
$values = $data['CustomFieldsValues'] ?? [];
|
|
file_put_contents($cacheFile, json_encode($values));
|
|
warmLog("[CustomField $fid] Cached " . count($values) . " values", $isCli);
|
|
} catch (Exception $e) {
|
|
warmLog("[CustomField $fid] ERROR " . $e->getMessage(), $isCli);
|
|
}
|
|
}
|
|
|
|
$elapsed = round(microtime(true) - $startTime, 1);
|
|
warmLog("Done in {$elapsed}s", $isCli);
|
|
|
|
} catch (Exception $e) {
|
|
warmLog("FATAL: " . $e->getMessage(), $isCli);
|
|
}
|
|
|
|
if (!$isCli) {
|
|
echo json_encode(['success' => true, 'log' => $log]);
|
|
}
|