Files
whatsapp/api/get_rate_limit_stats.php
2026-02-20 09:07:37 -05:00

95 lines
2.7 KiB
PHP

<?php
/**
* API - Obtener estadísticas de Rate Limiting de Facebook
* Fecha: 6 de febrero de 2026
*/
// Configurar manejo de errores
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
// Cargar config primero (ya incluye el autoloader)
require_once __DIR__ . '/../config/config.php';
// Verificar que la clase exista antes de usarla
if (!class_exists('RateLimitMonitor')) {
$rateLimitMonitorPath = __DIR__ . '/../services/RateLimitMonitor.php';
if (file_exists($rateLimitMonitorPath)) {
require_once $rateLimitMonitorPath;
} else {
header('Content-Type: application/json; charset=utf-8');
http_response_code(500);
echo json_encode([
'success' => false,
'error' => 'RateLimitMonitor service not found',
'path_checked' => $rateLimitMonitorPath
]);
exit;
}
}
// Requerir autenticación
requireAuthentication();
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');
try {
$monitor = new RateLimitMonitor();
$action = $_GET['action'] ?? 'current';
switch ($action) {
case 'current':
// Obtener estadísticas actuales
$stats = $monitor->getCurrentStats();
echo json_encode([
'success' => true,
'data' => $stats,
'timestamp' => time(),
'datetime' => date('Y-m-d H:i:s')
]);
break;
case 'history':
// Obtener historial
$type = $_GET['type'] ?? 'app';
$limit = min(100, intval($_GET['limit'] ?? 20));
$history = $monitor->getUsageHistory($type, $limit);
echo json_encode([
'success' => true,
'data' => $history,
'type' => $type,
'count' => count($history)
]);
break;
case 'cleanup':
// Ejecutar limpieza (solo admin)
$monitor->cleanup();
echo json_encode([
'success' => true,
'message' => 'Limpieza completada'
]);
break;
default:
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Acción no válida. Use: current, history, cleanup'
]);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}