128 lines
5.6 KiB
PHP
Executable File
128 lines
5.6 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Brain Creative Optimizer API
|
|
* Endpoints:
|
|
* ?action=performance - Show creative A/B test results
|
|
* ?action=calculate - Recalculate performance scores
|
|
* ?action=recommendations - Show pending offer recommendations
|
|
* ?action=approve&id=X - Approve a recommendation
|
|
* ?action=reject&id=X - Reject a recommendation
|
|
* ?action=translate&creative_id=X&lang=XX - Queue translation
|
|
* ?action=winners - Show current winning creatives
|
|
* ?action=status - Full optimizer status
|
|
*/
|
|
|
|
header('Content-Type: application/json');
|
|
$db = pg_connect("host=localhost dbname=adx_system user=admin password=admin123");
|
|
|
|
$action = $_GET['action'] ?? 'status';
|
|
|
|
switch ($action) {
|
|
case 'performance':
|
|
$offer_id = intval($_GET['offer_id'] ?? 0);
|
|
$where = $offer_id > 0 ? "AND cp.offer_id = $offer_id" : "";
|
|
$res = pg_query($db, "
|
|
SELECT cp.offer_id, cp.creative_id, cp.country, cp.sends, cp.opens, cp.clicks,
|
|
cp.open_rate, cp.click_rate, cp.score, cp.is_winner, cp.is_paused,
|
|
o.name as offer_name
|
|
FROM admin.creative_performance cp
|
|
LEFT JOIN affiliate.offers o ON o.id = cp.offer_id
|
|
WHERE cp.sends > 0 $where
|
|
ORDER BY cp.score DESC, cp.sends DESC
|
|
LIMIT 50
|
|
");
|
|
$data = [];
|
|
while ($row = pg_fetch_assoc($res)) $data[] = $row;
|
|
echo json_encode(['performance' => $data, 'count' => count($data)], JSON_PRETTY_PRINT);
|
|
break;
|
|
|
|
case 'calculate':
|
|
$output = shell_exec('python3 /opt/wevads/scripts/brain-creative-optimizer.py calculate 2>&1');
|
|
echo json_encode(['status' => 'calculated', 'output' => $output]);
|
|
break;
|
|
|
|
case 'recommendations':
|
|
$status = $_GET['status'] ?? 'pending';
|
|
$res = pg_query($db, "
|
|
SELECT id, source, external_offer_id, offer_name, country, vertical,
|
|
brain_score, brain_reason, status, tracking_url, thumbnail_url,
|
|
created_at
|
|
FROM admin.offer_recommendations
|
|
WHERE status = '".pg_escape_string($db, $status)."'
|
|
ORDER BY brain_score DESC
|
|
");
|
|
$data = [];
|
|
while ($row = pg_fetch_assoc($res)) $data[] = $row;
|
|
echo json_encode(['recommendations' => $data, 'count' => count($data)], JSON_PRETTY_PRINT);
|
|
break;
|
|
|
|
case 'approve':
|
|
$id = intval($_GET['id'] ?? 0);
|
|
if ($id <= 0) { echo json_encode(['error' => 'Missing id']); break; }
|
|
pg_query($db, "UPDATE admin.offer_recommendations SET status='approved', approved_by='api', approved_at=NOW() WHERE id=$id AND status='pending'");
|
|
$affected = pg_affected_rows(pg_query($db, "SELECT 1")); // dummy
|
|
echo json_encode(['status' => 'approved', 'id' => $id]);
|
|
break;
|
|
|
|
case 'reject':
|
|
$id = intval($_GET['id'] ?? 0);
|
|
if ($id <= 0) { echo json_encode(['error' => 'Missing id']); break; }
|
|
pg_query($db, "UPDATE admin.offer_recommendations SET status='rejected' WHERE id=$id AND status='pending'");
|
|
echo json_encode(['status' => 'rejected', 'id' => $id]);
|
|
break;
|
|
|
|
case 'winners':
|
|
$res = pg_query($db, "
|
|
SELECT cp.offer_id, cp.creative_id, cp.country, cp.sends, cp.opens, cp.clicks,
|
|
cp.score, o.name as offer_name
|
|
FROM admin.creative_performance cp
|
|
LEFT JOIN affiliate.offers o ON o.id = cp.offer_id
|
|
WHERE cp.is_winner = true
|
|
ORDER BY cp.score DESC
|
|
");
|
|
$data = [];
|
|
while ($row = pg_fetch_assoc($res)) $data[] = $row;
|
|
echo json_encode(['winners' => $data, 'count' => count($data)], JSON_PRETTY_PRINT);
|
|
break;
|
|
|
|
case 'translate':
|
|
$cr_id = intval($_GET['creative_id'] ?? 0);
|
|
$lang = $_GET['lang'] ?? '';
|
|
if ($cr_id <= 0 || empty($lang)) {
|
|
echo json_encode(['error' => 'Missing creative_id or lang']);
|
|
break;
|
|
}
|
|
$output = shell_exec("python3 /opt/wevads/scripts/brain-creative-optimizer.py translate $cr_id $lang 2>&1");
|
|
echo json_encode(['status' => 'queued', 'output' => trim($output)]);
|
|
break;
|
|
|
|
case 'scan':
|
|
$output = shell_exec('python3 /opt/wevads/scripts/brain-creative-optimizer.py scan 2>&1');
|
|
echo json_encode(['status' => 'scanned', 'output' => $output]);
|
|
break;
|
|
|
|
case 'status':
|
|
default:
|
|
$stats = [];
|
|
$queries = [
|
|
'total_creatives_tracked' => "SELECT COUNT(*) FROM admin.creative_performance",
|
|
'total_sends' => "SELECT COALESCE(SUM(sends),0) FROM admin.creative_performance",
|
|
'total_opens' => "SELECT COALESCE(SUM(opens),0) FROM admin.creative_performance",
|
|
'total_clicks' => "SELECT COALESCE(SUM(clicks),0) FROM admin.creative_performance",
|
|
'winners' => "SELECT COUNT(*) FROM admin.creative_performance WHERE is_winner = true",
|
|
'paused_creatives' => "SELECT COUNT(*) FROM admin.creative_performance WHERE is_paused = true",
|
|
'pending_translations' => "SELECT COUNT(*) FROM admin.creative_translations WHERE status = 'pending'",
|
|
'pending_recommendations' => "SELECT COUNT(*) FROM admin.offer_recommendations WHERE status = 'pending'",
|
|
'approved_offers' => "SELECT COUNT(*) FROM admin.brain_offer_config WHERE is_approved = true AND good_creatives > 0",
|
|
];
|
|
foreach ($queries as $key => $sql) {
|
|
$res = pg_query($db, $sql);
|
|
$stats[$key] = pg_fetch_result($res, 0, 0);
|
|
}
|
|
echo json_encode(['optimizer_status' => $stats], JSON_PRETTY_PRINT);
|
|
break;
|
|
}
|
|
|
|
pg_close($db);
|
|
?>
|