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

102 lines
4.2 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
try {
$db = new PDO("pgsql:host=localhost;dbname=adx_system", "postgres", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$action = $_GET['action'] ?? 'list';
if ($action === 'save') {
$data = json_decode(file_get_contents('php://input'), true);
// Backup avant modification
$backup = $db->query("SELECT jsonb_object_agg(config_key, config_value) FROM admin.brain_config")->fetchColumn();
$db->query("INSERT INTO admin.brain_config_history (action, backup_data, modified_by) VALUES ('save', '$backup'::jsonb, 'admin')");
// Update config
foreach ($data as $key => $value) {
$stmt = $db->prepare("
INSERT INTO admin.brain_config (config_key, config_value)
VALUES (:key, :value)
ON CONFLICT (config_key) DO UPDATE SET
config_value = :value,
updated_at = NOW()
");
$stmt->execute(['key' => $key, 'value' => $value]);
}
// Apply cron changes
if (isset($data['cron_auto_debug']) || isset($data['cron_proactive_monitor']) || isset($data['cron_system_health'])) {
$cron = "# WEVADS Brain Routines\n";
$cron .= ($data['cron_auto_debug'] ?? '*/5 * * * *') . " /opt/wevads/scripts/auto_debug_routines.sh\n";
$cron .= ($data['cron_proactive_monitor'] ?? '*/10 * * * *') . " /opt/wevads/scripts/proactive_monitor.sh\n";
$cron .= ($data['cron_system_health'] ?? '0 * * * *') . " /opt/wevads/scripts/auto_system_health.sh\n";
file_put_contents('/tmp/wevads_cron', $cron);
exec('crontab /tmp/wevads_cron');
}
echo json_encode(['success' => true, 'message' => 'Configuration sauvegardée']);
}
else if ($action === 'backup') {
$backup = $db->query("SELECT jsonb_object_agg(config_key, config_value) FROM admin.brain_config")->fetchColumn();
$stmt = $db->prepare("INSERT INTO admin.brain_config_history (action, backup_data, modified_by) VALUES ('manual_backup', :backup::jsonb, 'admin') RETURNING id");
$stmt->execute(['backup' => $backup]);
$id = $stmt->fetchColumn();
echo json_encode(['success' => true, 'backup_id' => $id]);
}
else if ($action === 'reset') {
$defaults = [
'system_prompt' => 'Vous êtes le Brain IA de WEVADS...',
'disk_threshold' => '80',
'memory_threshold' => '85',
'quota_threshold' => '75',
'log_retention_days' => '30',
'error_timeout_hours' => '1',
'cron_auto_debug' => '*/5 * * * *',
'cron_proactive_monitor' => '*/10 * * * *',
'cron_system_health' => '0 * * * *'
];
foreach ($defaults as $key => $value) {
$stmt = $db->prepare("
INSERT INTO admin.brain_config (config_key, config_value)
VALUES (:key, :value)
ON CONFLICT (config_key) DO UPDATE SET config_value = :value
");
$stmt->execute(['key' => $key, 'value' => $value]);
}
echo json_encode(['success' => true]);
}
else if ($action === 'rollback') {
$data = json_decode(file_get_contents('php://input'), true);
$historyId = $data['history_id'];
$backup = $db->query("SELECT backup_data FROM admin.brain_config_history WHERE id = $historyId")->fetchColumn();
if ($backup) {
$backupData = json_decode($backup, true);
foreach ($backupData as $key => $value) {
$stmt = $db->prepare("UPDATE admin.brain_config SET config_value = :value WHERE config_key = :key");
$stmt->execute(['key' => $key, 'value' => $value]);
}
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'Backup non trouvé']);
}
}
} catch(Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}