31 lines
1.7 KiB
PHP
31 lines
1.7 KiB
PHP
<?php
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
$pg = @pg_connect("host=127.0.0.1 port=5432 dbname=paperclip user=admin password=admin123");
|
|
if (!$pg) { echo json_encode(["error"=>"DB_DOWN"]); exit; }
|
|
$action = $_GET["action"] ?? "summary";
|
|
if ($action === "summary") {
|
|
$r = pg_query($pg, "SELECT role, COUNT(DISTINCT name) as cnt FROM agents GROUP BY role ORDER BY cnt DESC");
|
|
$roles = [];
|
|
while ($row = pg_fetch_assoc($r)) $roles[] = $row;
|
|
$total = pg_fetch_result(pg_query($pg, "SELECT COUNT(DISTINCT name) FROM agents"), 0, 0);
|
|
echo json_encode(["ok"=>true, "total_unique"=>(int)$total, "roles"=>$roles], JSON_PRETTY_PRINT);
|
|
} elseif ($action === "role") {
|
|
$role = pg_escape_string($pg, $_GET["r"] ?? "");
|
|
$r = pg_query($pg, "SELECT DISTINCT name, title, capabilities, status FROM agents WHERE role='$role' ORDER BY name");
|
|
$agents = [];
|
|
while ($row = pg_fetch_assoc($r)) $agents[] = $row;
|
|
echo json_encode(["ok"=>true, "role"=>$role, "count"=>count($agents), "agents"=>$agents], JSON_PRETTY_PRINT);
|
|
} elseif ($action === "capable") {
|
|
$r = pg_query($pg, "SELECT DISTINCT name, role, capabilities FROM agents WHERE capabilities IS NOT NULL AND LENGTH(capabilities) > 3 ORDER BY role, name");
|
|
$agents = [];
|
|
while ($row = pg_fetch_assoc($r)) $agents[] = $row;
|
|
echo json_encode(["ok"=>true, "count"=>count($agents), "agents"=>$agents], JSON_PRETTY_PRINT);
|
|
} elseif ($action === "all") {
|
|
$r = pg_query($pg, "SELECT DISTINCT name, role, title, capabilities FROM agents ORDER BY role, name");
|
|
$agents = [];
|
|
while ($row = pg_fetch_assoc($r)) $agents[] = $row;
|
|
echo json_encode(["ok"=>true, "count"=>count($agents), "agents"=>$agents]);
|
|
}
|
|
pg_close($pg);
|