netgescon-day0/scripts/smoke_unita.php

104 lines
3.8 KiB
PHP
Executable File

<?php
// Smoke test Unità: invoca LegacyArchiveService::unitaYear
// Uso: php scripts/smoke_unita.php [legacy_code] [anno] [limit]
$root = dirname(__DIR__);
chdir($root);
require $root . '/vendor/autoload.php';
$app = require_once $root . '/bootstrap/app.php';
$kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
use App\Models\UserSetting;
use App\Services\GesconImport\LegacyArchiveService;
$legacy = $argv[1] ?? null;
$rawAnnoOrFolder = $argv[2] ?? null;
$anno = null;
if ($rawAnnoOrFolder !== null) {
if (ctype_digit($rawAnnoOrFolder) && strlen($rawAnnoOrFolder) === 4 && (int) $rawAnnoOrFolder >= 1900) {
$anno = (int) $rawAnnoOrFolder;
} else {
// keep folder string like '0001'
$anno = $rawAnnoOrFolder; // will be resolved by service
}
}
$limit = isset($argv[3]) ? (int) $argv[3] : 20;
$hasMdbExport = trim((string) @shell_exec('command -v mdb-export')) !== '';
$hasMdbTables = trim((string) @shell_exec('command -v mdb-tables')) !== '';
$base = UserSetting::get('gescon.source_path', '/mnt/gescon-archives/gescon');
fwrite(STDERR, "[diag] mdb-export=" . ($hasMdbExport ? 'yes' : 'no') . " mdb-tables=" . ($hasMdbTables ? 'yes' : 'no') . " base={$base}\n");
if (! $legacy) {
fwrite(STDERR, "legacy_code obbligatorio. Uso: php scripts/smoke_unita.php <legacy_code> [anno] [limit]\n");
exit(2);
}
$svc = new LegacyArchiveService();
if (! $anno) {
$years = $svc->scanLegacyYears($legacy);
$anno = ! empty($years) ? (int) end($years) : null;
}
fwrite(STDERR, "[diag] tentative legacy={$legacy} anno=" . ($anno ?: 'null') . " limit={$limit}\n");
// Probe gestioni for resolution and diagnostics
$gest = $svc->gestioniYear($legacy, $anno ?? $rawAnnoOrFolder);
if (($gest['ok'] ?? false)) {
$meta = $gest['meta'] ?? [];
$resolvedAnno = $gest['anno'] ?? null;
$resolvedFolder = $meta['folder'] ?? null;
$period = $meta['period'] ?? null;
fwrite(STDERR, "[diag] resolved folder=" . ($resolvedFolder ?? 'null') . " anno=" . ($resolvedAnno ?? 'null') . ($period ? ' periodo=' . (json_encode($period)) : '') . "\n");
$o = $gest['ordinaria'] ?? null;
$r = $gest['riscaldamento'] ?? null;
if ($o || $r) {
fwrite(STDERR, "[diag] gestioni: ");
if ($o) {
fwrite(STDERR, "ORD(stato=" . ($o['stato'] ?? '') . " rate=" . count(($o['rate'] ?? [])) . ") ");
}
if ($r) {
fwrite(STDERR, "RISC(stato=" . ($r['stato'] ?? '') . " rate=" . count(($r['rate'] ?? [])) . ") ");
}
fwrite(STDERR, "\n");
}
if ($resolvedAnno) {
$anno = $resolvedAnno;
}
}
$result = $svc->unitaYear($legacy, $anno ?? $rawAnnoOrFolder, $limit);
$ok = $result['ok'] ?? false;
if (! $ok) {
fwrite(STDERR, "Errore: " . (($result['error'] ?? 'sconosciuto')) . "\n");
// prova fallback: anno nullo per staging
$res2 = $svc->unitaYear($legacy, null, $limit);
if ($res2['ok'] ?? false) {
$result = $res2;
$ok = true;
fwrite(STDERR, "Fallback staging riuscito.\n");
}
}
if ($ok) {
echo "OK legacy={$legacy} anno=" . ($result['anno'] ?? 'null') . " limit={$limit}\n";
$headers = $result['headers'] ?? [];
$rows = $result['rows'] ?? [];
echo "Headers (" . count($headers) . "): " . implode(', ', $headers) . "\n";
echo "Rows: " . count($rows) . "\n";
foreach (array_slice($rows, 0, min(3, count($rows))) as $i => $r) {
echo "Row " . ($i + 1) . ": " . json_encode($r, JSON_UNESCAPED_UNICODE) . "\n";
}
if (isset($result['staging_connection'])) {
echo "Staging: conn=" . $result['staging_connection'] . " table=" . ($result['staging_table'] ?? '') . "\n";
}
exit(0);
} else {
exit(1);
}