Files
html/api/ambre-count.php
2026-04-21 14:15:02 +02:00

123 lines
4.8 KiB
PHP

<?php
/**
* ambre-count.php — AMBRE session · aggregator chiffres unifiés
* Lit depuis truth-registry (canonical) + tool-registry + filesystem + ethica API
* Returns flat JSON scalars pour consumption par __ambre_truth_snapshot et UI.
* Doctrine #4 : zéro hardcode, toutes les valeurs depuis source live.
*/
header("Content-Type: application/json");
$out = [
'ok' => true,
'ts' => date('c'),
'sources' => [],
];
// === 1. Truth Registry (primary source of truth) ===
$tr_path = '/var/www/html/api/wevia-truth-registry.json';
if (file_exists($tr_path)) {
$tr = @json_decode(@file_get_contents($tr_path), true);
if (is_array($tr)) {
$out['sources'][] = 'truth-registry';
$out['built_at'] = $tr['built_at'] ?? null;
$out['agents'] = $tr['agents']['count_unique'] ?? null;
$out['agents_overlaps'] = $tr['agents']['count_with_overlaps'] ?? null;
$out['intents'] = $tr['intents']['count'] ?? null;
$out['skills'] = $tr['skills']['TOTAL'] ?? $tr['skills']['count'] ?? null;
$out['brains'] = $tr['brains']['count'] ?? null;
$out['doctrines'] = $tr['doctrines']['count'] ?? null;
$out['dashboards']= $tr['dashboards']['count'] ?? null;
$out['providers'] = count($tr['providers']['list'] ?? []) ?: ($tr['providers']['declared_total'] ?? null);
$out['nonreg_score'] = $tr['nonreg']['score'] ?? null;
$out['autonomy'] = $tr['autonomy_level'] ?? null;
$out['apis_php'] = $tr['apis_php_count'] ?? null;
}
}
// === 2. Tool Registry (for tools count) ===
$reg_path = '/var/www/html/api/wevia-tool-registry.json';
if (file_exists($reg_path)) {
$reg = @json_decode(@file_get_contents($reg_path), true);
if (is_array($reg)) {
$out['sources'][] = 'tool-registry';
$out['tools'] = count($reg['tools'] ?? []);
}
}
// === 3. Filesystem counts (pages, apis) ===
$html = @glob('/var/www/html/*.html') ?: [];
$out['pages'] = count($html);
$apis = @glob('/var/www/html/api/*.php') ?: [];
$out['apis_files'] = count($apis);
$stubs = @glob('/var/www/html/api/wired-pending/*.php') ?: [];
$out['wired_stubs'] = count($stubs);
$out['sources'][] = 'filesystem';
// === 4. Ethica HCPs (live API) ===
$ethica_sources = [
'http://127.0.0.1/api/ethica-country-api.php',
'/var/www/html/api/ethica-cache.json',
];
foreach ($ethica_sources as $src) {
if (strpos($src, 'http') === 0) {
$ch = @curl_init($src);
@curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>true, CURLOPT_TIMEOUT=>3, CURLOPT_CONNECTTIMEOUT=>2]);
$raw = @curl_exec($ch); @curl_close($ch);
} else {
$raw = file_exists($src) ? @file_get_contents($src) : null;
}
if ($raw) {
$d = @json_decode($raw, true);
if (is_array($d)) {
$candidates = [
$d['total'] ?? null,
$d['hcps_total'] ?? null,
$d['ethica_hcps'] ?? null,
$d['count'] ?? null,
];
foreach ($candidates as $c) {
if (is_numeric($c) && $c > 1000) { $out['hcps'] = (int)$c; $out['sources'][] = 'ethica-api'; break 2; }
}
}
}
}
// === 5. Qdrant collections count ===
$qh = @curl_init('http://127.0.0.1:6333/collections');
if ($qh) {
@curl_setopt_array($qh, [CURLOPT_RETURNTRANSFER=>true, CURLOPT_TIMEOUT=>2]);
$qraw = @curl_exec($qh); @curl_close($qh);
if ($qraw) {
$qd = @json_decode($qraw, true);
if (isset($qd['result']['collections']) && is_array($qd['result']['collections'])) {
$out['qdrant_collections'] = count($qd['result']['collections']);
$out['sources'][] = 'qdrant';
}
}
}
// === 6. Docker count ===
$docker = @trim(@shell_exec('docker ps -q 2>/dev/null | wc -l'));
if (is_numeric($docker)) { $out['docker'] = (int)$docker; $out['sources'][] = 'docker'; }
// === 7. Crons ===
$crons = @trim(@shell_exec('crontab -l 2>/dev/null | grep -cv "^#"'));
if (is_numeric($crons)) $out['crons'] = (int)$crons;
// === 8. NonReg live ===
$nr_path = '/var/www/html/api/nonreg-latest.json';
if (file_exists($nr_path)) {
$nr = @json_decode(@file_get_contents($nr_path), true);
if (is_array($nr)) {
$out['nonreg_pass'] = $nr['pass'] ?? null;
$out['nonreg_total'] = $nr['total'] ?? null;
}
}
// === Fill any remaining nulls with '?' for display clarity ===
foreach (['agents','intents','skills','brains','doctrines','dashboards','providers','tools','hcps','pages','qdrant_collections','apis_php','nonreg_score','autonomy','built_at','docker','crons'] as $k) {
if (!isset($out[$k]) || $out[$k] === null) $out[$k] = '?';
}
echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);