- Add complete ErrorMonitoring module with controllers, models, services - Include error dashboard, error detail views, and error import functionality - Add error monitoring API endpoints and error collection services - Fix missing error monitoring system for critical error tracking
320 lines
11 KiB
PHP
320 lines
11 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\ErrorMonitoring\Models;
|
|
|
|
use App\Core\DatabaseManager;
|
|
|
|
/**
|
|
* ErrorLog Model - Modelo para a tabela error_logs
|
|
*/
|
|
class ErrorLog
|
|
{
|
|
private DatabaseManager $db;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->db = DatabaseManager::getInstance();
|
|
}
|
|
|
|
/**
|
|
* Buscar erro por ID
|
|
*/
|
|
public function findById(int $id): ?array
|
|
{
|
|
$sql = "SELECT * FROM error_logs WHERE id = ?";
|
|
$result = $this->db->fetch($sql, [$id]);
|
|
|
|
return $result ?: null;
|
|
}
|
|
|
|
/**
|
|
* Buscar erro por unique_id
|
|
*/
|
|
public function findByUniqueId(string $uniqueId): ?array
|
|
{
|
|
$sql = "SELECT * FROM error_logs WHERE unique_id = ?";
|
|
$result = $this->db->fetch($sql, [$uniqueId]);
|
|
|
|
return $result ?: null;
|
|
}
|
|
|
|
/**
|
|
* Listar todos os erros com paginação
|
|
*/
|
|
public function getAll(int $limit = 50, int $offset = 0, array $filters = []): array
|
|
{
|
|
$sql = "SELECT * FROM error_logs";
|
|
$params = [];
|
|
|
|
// Aplicar filtros
|
|
if (!empty($filters)) {
|
|
$conditions = [];
|
|
|
|
if (isset($filters['severity'])) {
|
|
$conditions[] = "severity = ?";
|
|
$params[] = $filters['severity'];
|
|
}
|
|
|
|
if (isset($filters['status'])) {
|
|
$conditions[] = "status = ?";
|
|
$params[] = $filters['status'];
|
|
}
|
|
|
|
if (isset($filters['project_name'])) {
|
|
$conditions[] = "project_name = ?";
|
|
$params[] = $filters['project_name'];
|
|
}
|
|
|
|
if (isset($filters['environment'])) {
|
|
$conditions[] = "environment = ?";
|
|
$params[] = $filters['environment'];
|
|
}
|
|
|
|
if (!empty($conditions)) {
|
|
$sql .= " WHERE " . implode(" AND ", $conditions);
|
|
}
|
|
}
|
|
|
|
// Sanitize limit and offset with (string)(int) for maximum MariaDB compatibility
|
|
$limitInt = (string)(int)$limit;
|
|
$offsetInt = (string)(int)$offset;
|
|
|
|
$sql .= " ORDER BY created_at DESC LIMIT $limitInt OFFSET $offsetInt";
|
|
|
|
return $this->db->fetchAll($sql, $params) ?: [];
|
|
}
|
|
|
|
/**
|
|
* Contar total de erros
|
|
*/
|
|
public function count(array $filters = []): int
|
|
{
|
|
$sql = "SELECT COUNT(*) as total FROM error_logs";
|
|
$params = [];
|
|
|
|
// Aplicar filtros
|
|
if (!empty($filters)) {
|
|
$conditions = [];
|
|
|
|
if (isset($filters['severity'])) {
|
|
$conditions[] = "severity = ?";
|
|
$params[] = $filters['severity'];
|
|
}
|
|
|
|
if (isset($filters['status'])) {
|
|
$conditions[] = "status = ?";
|
|
$params[] = $filters['status'];
|
|
}
|
|
|
|
if (isset($filters['project_name'])) {
|
|
$conditions[] = "project_name = ?";
|
|
$params[] = $filters['project_name'];
|
|
}
|
|
|
|
if (isset($filters['environment'])) {
|
|
$conditions[] = "environment = ?";
|
|
$params[] = $filters['environment'];
|
|
}
|
|
|
|
if (!empty($conditions)) {
|
|
$sql .= " WHERE " . implode(" AND ", $conditions);
|
|
}
|
|
}
|
|
|
|
$result = $this->db->fetchAll($sql, $params);
|
|
return $result ? (int)$result[0]['total'] : 0;
|
|
}
|
|
|
|
/**
|
|
* Criar novo erro
|
|
*/
|
|
public function create(array $data): bool
|
|
{
|
|
$sql = "INSERT INTO error_logs (
|
|
unique_id, project_name, environment, error_type, severity,
|
|
message, file_path, line_number, stack_trace, context_data,
|
|
user_id, session_id, request_id, ip_address, user_agent,
|
|
url, http_method, request_data, server_data, occurrences,
|
|
first_seen, last_seen, status, created_at, updated_at
|
|
) VALUES (
|
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW()
|
|
)";
|
|
|
|
$params = [
|
|
$data['unique_id'] ?? null,
|
|
$data['project_name'] ?? 'default',
|
|
$data['environment'] ?? 'production',
|
|
$data['error_type'] ?? 'Exception',
|
|
$data['severity'] ?? 'error',
|
|
$data['message'] ?? '',
|
|
$data['file_path'] ?? '',
|
|
$data['line_number'] ?? 0,
|
|
$data['stack_trace'] ?? null,
|
|
$data['context_data'] ?? null,
|
|
$data['user_id'] ?? null,
|
|
$data['session_id'] ?? null,
|
|
$data['request_id'] ?? null,
|
|
$data['ip_address'] ?? null,
|
|
$data['user_agent'] ?? null,
|
|
$data['url'] ?? null,
|
|
$data['http_method'] ?? null,
|
|
$data['request_data'] ?? null,
|
|
$data['server_data'] ?? null,
|
|
$data['occurrences'] ?? 1,
|
|
$data['first_seen'] ?? date('Y-m-d H:i:s'),
|
|
$data['last_seen'] ?? date('Y-m-d H:i:s'),
|
|
$data['status'] ?? 'open'
|
|
];
|
|
|
|
return $this->db->query($sql, $params) > 0;
|
|
}
|
|
|
|
/**
|
|
* Atualizar erro
|
|
*/
|
|
public function update(int $id, array $data): bool
|
|
{
|
|
$sql = "UPDATE error_logs SET
|
|
status = ?,
|
|
assigned_to = ?,
|
|
resolution_notes = ?,
|
|
resolved_at = ?,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = ?";
|
|
|
|
$params = [
|
|
$data['status'] ?? 'open',
|
|
$data['assigned_to'] ?? null,
|
|
$data['resolution_notes'] ?? null,
|
|
$data['resolved_at'] ?? null,
|
|
$id
|
|
];
|
|
|
|
return $this->db->query($sql, $params) > 0;
|
|
}
|
|
|
|
/**
|
|
* Marcar erro como resolvido
|
|
*/
|
|
public function markAsResolved(string $uniqueId, ?string $notes = null): bool
|
|
{
|
|
$sql = "UPDATE error_logs SET
|
|
status = 'resolved',
|
|
resolved_at = CURRENT_TIMESTAMP,
|
|
resolution_notes = ?,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE unique_id = ?";
|
|
|
|
return $this->db->query($sql, [$notes, $uniqueId]) > 0;
|
|
}
|
|
|
|
/**
|
|
* Obter estatísticas
|
|
*/
|
|
public function getStats(): array
|
|
{
|
|
$stats = [];
|
|
|
|
// Total de erros
|
|
$result = $this->db->fetchAll("SELECT COUNT(*) as total FROM error_logs");
|
|
$stats['total_errors'] = $result ? (int)$result[0]['total'] : 0;
|
|
|
|
// Erros por severidade
|
|
$result = $this->db->fetchAll("SELECT severity, COUNT(*) as count FROM error_logs GROUP BY severity");
|
|
$stats['by_severity'] = $result ?: [];
|
|
|
|
// Erros por status
|
|
$result = $this->db->fetchAll("SELECT status, COUNT(*) as count FROM error_logs GROUP BY status");
|
|
$stats['by_status'] = $result ?: [];
|
|
|
|
// Erros por projeto
|
|
$result = $this->db->fetchAll("SELECT project_name, COUNT(*) as count FROM error_logs GROUP BY project_name");
|
|
$stats['by_project'] = $result ?: [];
|
|
|
|
// Erros recentes (últimas 24h)
|
|
$result = $this->db->fetchAll("SELECT COUNT(*) as count FROM error_logs WHERE created_at >= DATE_SUB(NOW(), INTERVAL 24 HOUR)");
|
|
$stats['recent_24h'] = $result ? (int)$result[0]['count'] : 0;
|
|
|
|
return $stats;
|
|
}
|
|
|
|
/**
|
|
* Inserir dados de teste
|
|
*/
|
|
public function insertTestData(): void
|
|
{
|
|
$testErrors = [
|
|
[
|
|
'unique_id' => 'TEST-001',
|
|
'project_name' => 'CRM Project',
|
|
'environment' => 'development',
|
|
'error_type' => 'Exception',
|
|
'severity' => 'error',
|
|
'message' => 'Division by zero in calculation module',
|
|
'file_path' => '/app/Modules/Crm/Controllers/DealController.php',
|
|
'line_number' => 156,
|
|
'stack_trace' => "Stack trace:\n#0 /app/Modules/Crm/Controllers/DealController.php(156): calculateCommission()\n#1 /public/index.php(45): main()",
|
|
'context_data' => json_encode(['deal_id' => 123, 'user_id' => 456]),
|
|
'user_id' => 'user_456',
|
|
'session_id' => 'sess_789',
|
|
'request_id' => 'req_001',
|
|
'ip_address' => '192.168.1.100',
|
|
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
|
'url' => 'http://localhost:8888/apps/crm/deals/123',
|
|
'http_method' => 'POST',
|
|
'occurrences' => 3,
|
|
'first_seen' => '2024-01-15 10:30:00',
|
|
'last_seen' => '2024-01-15 14:45:00'
|
|
],
|
|
[
|
|
'unique_id' => 'TEST-002',
|
|
'project_name' => 'CRM Project',
|
|
'environment' => 'development',
|
|
'error_type' => 'Error',
|
|
'severity' => 'warning',
|
|
'message' => 'Database connection timeout',
|
|
'file_path' => '/app/Core/DatabaseManager.php',
|
|
'line_number' => 89,
|
|
'stack_trace' => "Stack trace:\n#0 /app/Core/DatabaseManager.php(89): connect()\n#1 /app/Modules/Auth/Controllers/AuthController.php(23): login()",
|
|
'context_data' => json_encode(['attempt' => 3, 'timeout' => 30]),
|
|
'user_id' => 'user_123',
|
|
'session_id' => 'sess_456',
|
|
'request_id' => 'req_002',
|
|
'ip_address' => '192.168.1.101',
|
|
'user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
|
|
'url' => 'http://localhost:8888/auth/login',
|
|
'http_method' => 'POST',
|
|
'occurrences' => 1,
|
|
'first_seen' => '2024-01-15 11:15:00',
|
|
'last_seen' => '2024-01-15 11:15:00'
|
|
],
|
|
[
|
|
'unique_id' => 'TEST-003',
|
|
'project_name' => 'CRM Project',
|
|
'environment' => 'development',
|
|
'error_type' => 'Exception',
|
|
'severity' => 'critical',
|
|
'message' => 'Memory limit exceeded in report generation',
|
|
'file_path' => '/app/Modules/Analytics/Controllers/ReportController.php',
|
|
'line_number' => 234,
|
|
'stack_trace' => "Stack trace:\n#0 /app/Modules/Analytics/Controllers/ReportController.php(234): generateReport()\n#1 /app/Modules/Analytics/Controllers/ReportController.php(189): processData()",
|
|
'context_data' => json_encode(['report_type' => 'monthly', 'data_size' => '2GB']),
|
|
'user_id' => 'user_789',
|
|
'session_id' => 'sess_012',
|
|
'request_id' => 'req_003',
|
|
'ip_address' => '192.168.1.102',
|
|
'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36',
|
|
'url' => 'http://localhost:8888/apps/analytics/reports/monthly',
|
|
'http_method' => 'GET',
|
|
'occurrences' => 5,
|
|
'first_seen' => '2024-01-14 09:00:00',
|
|
'last_seen' => '2024-01-15 16:30:00'
|
|
]
|
|
];
|
|
|
|
foreach ($testErrors as $error) {
|
|
$this->create($error);
|
|
}
|
|
}
|
|
}
|