Files
wevads-platform/scripts/api_bpms-flows.php
2026-02-26 04:53:11 +01:00

180 lines
6.2 KiB
PHP
Executable File

<?php
require_once("/opt/wevads/config/credentials.php");
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$db_config = [
'host' => 'localhost',
'dbname' => 'wevads',
'user' => 'admin',
'password' => WEVADS_DB_PASS
];
try {
$pdo = new PDO(
"pgsql:host={$db_config['host']};dbname={$db_config['dbname']}",
$db_config['user'],
$db_config['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$pdo->exec("SET search_path TO admin, public;");
$action = $_GET['action'] ?? 'overview';
switch ($action) {
case 'overview':
// Données de performance
$throughput = $pdo->query("
SELECT
flow_type,
SUM(throughput_hour) as total_throughput,
COUNT(*) as flow_count
FROM admin.bpms_flows
GROUP BY flow_type
")->fetchAll(PDO::FETCH_ASSOC);
// Méthodes d'envoi
$methods = $pdo->query("
SELECT
COUNT(*) as config_count,
ROUND(AVG(inbox_rate), 2) as avg_inbox_rate
FROM admin.brain_send_configs
ORDER BY avg_inbox_rate DESC
LIMIT 10
")->fetchAll(PDO::FETCH_ASSOC);
// Performance par ISP
$isp_performance = $pdo->query("
SELECT
isp,
COUNT(*) as send_count,
ROUND(AVG(CASE WHEN status = 'inbox' THEN 1 ELSE 0 END) * 100, 2) as inbox_rate
FROM admin.unified_send_log
WHERE send_date >= NOW() - INTERVAL '7 days'
GROUP BY isp
ORDER BY inbox_rate DESC
LIMIT 10
")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'status' => 'success',
'overview' => [
'throughput' => $throughput,
'methods' => $methods,
'isp_performance' => $isp_performance,
'summary' => [
'total_flows' => count($throughput),
'avg_inbox_rate' => round(array_sum(array_column($isp_performance, 'inbox_rate')) / max(1, count($isp_performance)), 2)
]
]
]);
break;
case 'flows':
$stmt = $pdo->query("SELECT * FROM admin.bpms_flows ORDER BY created_at DESC");
$flows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'status' => 'success',
'flows' => $flows,
'count' => count($flows)
]);
break;
case 'methods':
$stmt = $pdo->query("
FROM admin.brain_send_configs
");
$methods = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
echo json_encode([
'status' => 'success',
'methods' => $methods,
'count' => count($methods)
]);
break;
case 'throughput':
$stmt = $pdo->query("
SELECT
EXTRACT(HOUR FROM send_date) as hour,
COUNT(*) as send_count
FROM admin.unified_send_log
WHERE send_date >= NOW() - INTERVAL '24 hours'
GROUP BY EXTRACT(HOUR FROM send_date)
ORDER BY hour
");
$throughput = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'status' => 'success',
'timeframe' => '24 hours',
'throughput' => $throughput,
'total_sends' => array_sum(array_column($throughput, 'send_count'))
]);
break;
case 'create_flow':
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
$stmt = $pdo->prepare("
INSERT INTO admin.bpms_flows (name, flow_type, steps, throughput_hour)
VALUES (:name, :flow_type, :steps, :throughput)
RETURNING id
");
$stmt->execute([
':name' => $input['name'] ?? 'New Flow',
':flow_type' => $input['flow_type'] ?? 'email',
':steps' => json_encode($input['steps'] ?? []),
':throughput' => $input['throughput_hour'] ?? 0
]);
$flowId = $stmt->fetchColumn();
echo json_encode([
'status' => 'success',
'message' => 'Flow created successfully',
'flow_id' => $flowId
]);
}
break;
case 'stats':
$stats = $pdo->query("
SELECT
COUNT(*) as total_flows,
SUM(throughput_hour) as total_throughput,
AVG(throughput_hour) as avg_throughput,
SUM(error_count) as total_errors
FROM admin.bpms_flows
")->fetch(PDO::FETCH_ASSOC);
echo json_encode([
'status' => 'success',
'stats' => $stats,
'api' => 'bpms-flows',
'endpoints' => ['overview', 'flows', 'methods', 'throughput', 'create_flow']
]);
break;
default:
echo json_encode([
'status' => 'success',
'api' => 'bpms-flows',
'endpoints_available' => ['overview', 'flows', 'methods', 'throughput', 'create_flow'],
'message' => 'API bpms-flows opérationnelle'
]);
}
} catch (PDOException $e) {
echo json_encode([
'status' => 'error',
'message' => 'Erreur base de données: ' . $e->getMessage(),
'api' => 'bpms-flows'
]);
}
?>