Files
html/api/opus5-pending-runner.php
2026-04-17 02:50:02 +02:00

91 lines
4.5 KiB
PHP

<?php
// OPUS5 PENDING RUNNER - exec les pendings P1/P2 en autonomie
// Called via WEVIA chat or direct HTTP. Logs + reports to JSON.
header('Content-Type: application/json');
$R = ['ts'=>date('c'), 'actions'=>[], 'done'=>0, 'pending'=>0];
$LOG = '/tmp/opus5-pending.log';
function logp($msg) { global $LOG; @file_put_contents($LOG, date('c')." $msg\n", FILE_APPEND); }
logp("START opus5-pending-runner");
// === P1-1: ETHICA_COUNT DIAGNOSTIC (auto-check + report) ===
// Le vrai fix router nécessite root chattr. Ici on rapporte l'état
try {
$pg_count = @shell_exec("timeout 5 env PGPASSWORD=admin123 psql -h 10.1.0.3 -U admin -d adx_system -tAc 'SELECT COUNT(*) FROM ethica.medecins_real' 2>&1");
$pg_count = trim($pg_count);
$router_file = '/opt/wevia-brain/wevia-master-router.php';
$router_line = @shell_exec("grep -A 2 'MASTER-WIRED INTENT: ethica_count' $router_file 2>/dev/null | head -4");
$has_hostname_bug = strpos($router_line, '/etc/hostname') !== false;
$R['actions'][] = [
'id' => 'ethica_count_diag',
'status' => $has_hostname_bug ? 'BUG_CONFIRMED (router stub hostname)' : 'OK',
'db_count_live' => is_numeric($pg_count) ? (int)$pg_count : $pg_count,
'action_needed' => $has_hostname_bug ? 'sudo chattr -i + php patch-ethica + chattr +i' : 'NONE'
];
$has_hostname_bug ? $R['pending']++ : $R['done']++;
} catch (Exception $e) { $R['actions'][] = ['id'=>'ethica_count_diag','err'=>$e->getMessage()]; }
// === P1-2: WEVADS ACCOUNTS WITHOUT TENANT (scan + report) ===
try {
$sql = "SELECT COUNT(*) FROM office_accounts WHERE tenant_id IS NULL OR tenant_id = '' OR tenant_id = '0'";
$no_tenant = @shell_exec("timeout 5 env PGPASSWORD=admin123 psql -h 10.1.0.3 -U admin -d adx_system -tAc \"$sql\" 2>&1");
$no_tenant = trim($no_tenant);
$total = @shell_exec("timeout 5 env PGPASSWORD=admin123 psql -h 10.1.0.3 -U admin -d adx_system -tAc 'SELECT COUNT(*) FROM office_accounts' 2>&1");
$total = trim($total);
$R['actions'][] = [
'id' => 'wevads_no_tenant',
'accounts_without_tenant' => is_numeric($no_tenant) ? (int)$no_tenant : $no_tenant,
'accounts_total' => is_numeric($total) ? (int)$total : $total,
'action_needed' => (is_numeric($no_tenant) && (int)$no_tenant > 0) ? 'tenant assignment script to write (P1 action, needs tenant list)' : 'NONE'
];
if (is_numeric($no_tenant) && (int)$no_tenant > 0) $R['pending']++; else $R['done']++;
} catch (Exception $e) { $R['actions'][] = ['id'=>'wevads_no_tenant','err'=>$e->getMessage()]; }
// === P1-4: TIMEOUTS CHECK ===
$timeout_apis = ['api-key-hub.php', 'fixall.php', 'l99-chatbot-deep.php'];
$timeout_report = [];
foreach ($timeout_apis as $api) {
$start = microtime(true);
$ch = curl_init("http://127.0.0.1/api/$api");
curl_setopt_array($ch, [CURLOPT_TIMEOUT=>8, CURLOPT_RETURNTRANSFER=>true, CURLOPT_NOBODY=>true]);
curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$time = round((microtime(true) - $start) * 1000);
curl_close($ch);
$timeout_report[$api] = ['http'=>$http, 'ms'=>$time];
}
$R['actions'][] = ['id'=>'timeouts_check', 'apis'=>$timeout_report];
$R['done']++;
// === STUBS opus4 PENDING count ===
$stubs = glob('/var/www/html/api/wired-pending/intent-opus4-*.php') ?: [];
$stub_sum = [];
foreach ($stubs as $s) {
$info = @include $s;
if (is_array($info)) $stub_sum[] = ['name'=>$info['name'], 'status'=>$info['status']];
}
$R['actions'][] = ['id'=>'opus4_stubs_pending', 'count'=>count($stubs), 'list'=>$stub_sum];
// === P2 SCREENS 404 count ===
$screens_health = @json_decode(@file_get_contents('/var/www/html/api/screens-health.json'), true) ?: [];
$n_404 = 0; $n_total = 0;
if (isset($screens_health['screens'])) {
foreach ($screens_health['screens'] as $sc) {
$n_total++;
if (($sc['status'] ?? '') === 'TRULY_404' || ($sc['http'] ?? 0) == 404) $n_404++;
}
}
$R['actions'][] = ['id'=>'p2_screens_404', 'truly_404'=>$n_404, 'total'=>$n_total];
// === BLADE QUEUE STATUS ===
$blade_q = @json_decode(@file_get_contents('/var/www/html/api/blade-queue.json'), true) ?: [];
$R['actions'][] = ['id'=>'blade_queue', 'count'=>count($blade_q)];
// === NonReg + L99 ===
$nr = @json_decode(@file_get_contents('http://127.0.0.1/api/nonreg-api.php?cat=all'), true) ?: [];
$R['actions'][] = ['id'=>'nonreg', 'pass'=>$nr['pass'] ?? '?', 'total'=>$nr['total'] ?? '?'];
logp("END done=".$R['done']." pending=".$R['pending']);
echo json_encode($R, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);