Files
html/api/ethica-feed-api.php

70 lines
3.6 KiB
PHP

<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
require_once __DIR__ . '/_secrets.php';
$pg = @pg_connect("host=10.1.0.3 dbname=adx_system user=admin password=" . weval_secret('WEVAL_PG_ADMIN_PASS'));
if (!$pg) { echo json_encode(["feed"=>[],"error"=>"DB"]); exit; }
$feed = [];
// LEVEL 1: Consent events (most important!)
$r = @pg_query($pg, "SELECT to_char(created_at,'YYYY-MM-DD HH24:MI') as ts, email, action, method FROM ethica.consent_log ORDER BY created_at DESC LIMIT 10");
if ($r) while ($row = pg_fetch_assoc($r)) {
$feed[] = ["ts"=>$row["ts"], "type"=>"consent", "color"=>"#34d399",
"icon"=>"", "msg"=>"Consent " . strtoupper($row["action"]) . "" . ($row["email"] ?? "HCP") . " via " . ($row["method"] ?? "web")];
}
// LEVEL 2: Scraper events
$r = @pg_query($pg, "SELECT to_char(created_at,'YYYY-MM-DD HH24:MI') as ts, source, country, found FROM ethica.scraper_v2_log ORDER BY created_at DESC LIMIT 5");
if ($r) while ($row = pg_fetch_assoc($r)) {
$feed[] = ["ts"=>$row["ts"], "type"=>"scraper", "color"=>"#60a5fa",
"icon"=>"🔍", "msg"=>"Scraper " . $row["source"] . " " . $row["country"] . "" . $row["found"] . " HCPs"];
}
// LEVEL 3: Email enrichment
$r = @pg_query($pg, "SELECT to_char(updated_at,'YYYY-MM-DD HH24:MI') as ts, pays, count(*) as c FROM ethica.medecins_real WHERE email IS NOT NULL AND updated_at > NOW() - INTERVAL '7 days' GROUP BY 1,2 ORDER BY 1 DESC LIMIT 5");
if ($r) while ($row = pg_fetch_assoc($r)) {
$feed[] = ["ts"=>$row["ts"], "type"=>"enrichment", "color"=>"#a78bfa",
"icon"=>"📧", "msg"=>"Email enrichi — " . $row["c"] . " HCPs " . $row["pays"]];
}
// LEVEL 4: Campaign events (from send log)
$r = @pg_query($pg, "SELECT to_char(created_at,'YYYY-MM-DD HH24:MI') as ts, status, count(*) as c FROM admin.graph_send_log WHERE created_at > NOW() - INTERVAL '7 days' GROUP BY 1,2 ORDER BY 1 DESC LIMIT 5");
if ($r) while ($row = pg_fetch_assoc($r)) {
$feed[] = ["ts"=>$row["ts"], "type"=>"campaign", "color"=>"#fbbf24",
"icon"=>"📤", "msg"=>"Envoi " . $row["status"] . "" . $row["c"] . " emails"];
}
// LEVEL 5: Warmup events
$r = @pg_query($pg, "SELECT to_char(NOW(),'YYYY-MM-DD HH24:MI') as ts, count(*) as c FROM admin.office_accounts WHERE status='Active'");
if ($r && ($row = pg_fetch_assoc($r))) {
$feed[] = ["ts"=>$row["ts"], "type"=>"warmup", "color"=>"#22d3ee",
"icon"=>"🔥", "msg"=>"Office warmup — " . $row["c"] . " comptes actifs"];
}
// LEVEL 6: System health
$feed[] = ["ts"=>date("Y-m-d H:i"), "type"=>"system", "color"=>"#34d399",
"icon"=>"💚", "msg"=>"Système opérationnel — PMTA UP, SPF+DKIM+DMARC OK"];
// Sort by timestamp desc
usort($feed, function($a, $b) { return strcmp($b["ts"], $a["ts"]); });
// Stats
$total = (int)@pg_fetch_result(@pg_query($pg, "SELECT count(*) FROM ethica.medecins_real"), 0, 0);
$email = (int)@pg_fetch_result(@pg_query($pg, "SELECT count(*) FROM ethica.medecins_real WHERE email IS NOT NULL AND email!=''"), 0, 0);
$tel = (int)@pg_fetch_result(@pg_query($pg, "SELECT count(*) FROM ethica.medecins_real WHERE telephone IS NOT NULL"), 0, 0);
$week = (int)@pg_fetch_result(@pg_query($pg, "SELECT count(*) FROM ethica.medecins_real WHERE created_at > NOW() - INTERVAL '7 days'"), 0, 0);
$consent = (int)@pg_fetch_result(@pg_query($pg, "SELECT count(*) FROM ethica.consent_log WHERE action='optin'"), 0, 0);
echo json_encode([
"feed" => array_slice($feed, 0, 20),
"total" => $total,
"with_email" => $email,
"with_tel" => $tel,
"week_new" => $week,
"consent_optin" => $consent,
"generated" => date("c")
]);
@pg_close($pg);