Files
html/api/ethica-enrich-api.php
2026-04-12 22:57:03 +02:00

102 lines
4.0 KiB
PHP

<?php
/**
* ETHICA ENRICHMENT API — S204
* Routes: status, run_searxng, run_richscraper, run_enrich_v4, run_all, gap_detail
* Called by WEVIA Master intents
*/
header('Content-Type: application/json');
opcache_invalidate(__FILE__, true);
function sentinel($cmd) {
$r = @file_get_contents("http://10.1.0.3:5890/api/sentinel-brain.php?action=exec&cmd=".urlencode($cmd),
false, stream_context_create(['http'=>['timeout'=>25]]));
return @json_decode($r, true)['output'] ?? trim($r);
}
function ok($d) { echo json_encode(array_merge(['ok'=>true], $d)); exit; }
function fail($m) { echo json_encode(['ok'=>false,'error'=>$m]); exit; }
$action = $_GET['action'] ?? $_POST['action'] ?? '';
if (!$action) fail('action required');
switch ($action) {
case 'status':
$scripts = ['ethica-enrich-v4.py','ethica-enrich-searxng.py','ethica-richscraper.py','ethica-cron-scraper.py','tabibi-scraper.py'];
$result = [];
foreach ($scripts as $s) {
$log = '/var/log/' . str_replace('.py', '.log', $s);
$running = trim(sentinel("pgrep -f '$s' 2>/dev/null && echo RUNNING || echo IDLE"));
$tail = sentinel("tail -3 $log 2>/dev/null");
$last = sentinel("stat -c %Y $log 2>/dev/null");
$result[$s] = [
'running' => strpos($running, 'RUNNING') !== false,
'last_run' => $last ? date('Y-m-d H:i', (int)$last) : 'never',
'tail' => $tail
];
}
$c = @pg_connect("host=10.1.0.3 port=5432 dbname=adx_system user=admin password=admin123");
$gap = 0; $total = 0; $emails = 0;
if ($c) {
$total = (int)pg_fetch_result(pg_query($c, "SELECT COUNT(*) FROM ethica.medecins_validated"), 0, 0);
$emails = (int)pg_fetch_result(pg_query($c, "SELECT COUNT(*) FROM ethica.medecins_validated WHERE email IS NOT NULL AND email != ''"), 0, 0);
$gap = $total - $emails;
pg_close($c);
}
$oc = @fsockopen('151.80.235.110', 3210, $e, $m, 3);
ok(['scripts'=>$result, 'total_hcp'=>$total, 'emails'=>$emails, 'gap'=>$gap, 'openclaw_s151'=> $oc ? 'UP' : 'DOWN']);
break;
case 'run_searxng':
$batch = (int)($_GET['batch'] ?? 200);
$check = sentinel("pgrep -f ethica-enrich-searxng.py 2>/dev/null");
if (trim($check)) fail('already running');
sentinel("nohup python3 /opt/ethica-enrich-searxng.py $batch >> /var/log/ethica-enrich-searxng.log 2>&1 &");
ok(['msg'=>"searxng started batch=$batch"]);
break;
case 'run_richscraper':
$check = sentinel("pgrep -f ethica-richscraper.py 2>/dev/null");
if (trim($check)) fail('already running');
sentinel("nohup python3 /opt/ethica-richscraper.py all >> /var/log/ethica-richscraper.log 2>&1 &");
ok(['msg'=>'richscraper started']);
break;
case 'run_enrich_v4':
$check = sentinel("pgrep -f ethica-enrich-v4.py 2>/dev/null");
if (trim($check)) fail('already running');
sentinel("nohup python3 /opt/ethica-enrich-v4.py all >> /var/log/ethica-enrich-v4.log 2>&1 &");
ok(['msg'=>'enrich-v4 started']);
break;
case 'run_all':
$launched = [];
foreach (['ethica-enrich-searxng.py','ethica-richscraper.py','ethica-enrich-v4.py'] as $s) {
$check = sentinel("pgrep -f $s 2>/dev/null");
if (!trim($check)) {
$log = '/var/log/' . str_replace('.py', '.log', $s);
sentinel("nohup python3 /opt/$s all >> $log 2>&1 &");
$launched[] = $s;
sleep(1);
}
}
ok(['launched'=>$launched, 'count'=>count($launched)]);
break;
case 'gap_detail':
$c = @pg_connect("host=10.1.0.3 port=5432 dbname=adx_system user=admin password=admin123");
if (!$c) fail('DB connect failed');
$r = pg_query($c, "SELECT pays, COUNT(*) as total, COUNT(CASE WHEN email IS NOT NULL AND email != '' THEN 1 END) as with_email FROM ethica.medecins_validated GROUP BY pays ORDER BY total DESC");
$rows = [];
while ($row = pg_fetch_assoc($r)) {
$row['gap'] = $row['total'] - $row['with_email'];
$rows[] = $row;
}
pg_close($c);
ok(['countries'=>$rows]);
break;
default:
fail("unknown action: $action");
}