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; } /** * Marcar erro como resolvido */ public function resolveError(int $id, string $resolutionNotes = null): bool { try { $sql = "UPDATE error_logs SET status = 'resolved', resolved_at = NOW(), resolution_notes = ? WHERE id = ?"; $result = $this->db->execute($sql, [$resolutionNotes, $id]); return $result > 0; } catch (\Exception $e) { return false; } } /** * Marcar todos os erros como resolvidos */ public function resolveAllErrors(string $resolutionNotes = null): bool { try { $sql = "UPDATE error_logs SET status = 'resolved', resolved_at = NOW(), resolution_notes = ? WHERE status = 'open'"; $result = $this->db->execute($sql, [$resolutionNotes]); return $result > 0; } catch (\Exception $e) { return false; } } /** * Marcar erro como ignorado */ public function ignoreError(int $id, string $resolutionNotes = null): bool { try { $sql = "UPDATE error_logs SET status = 'ignored', resolved_at = NOW(), resolution_notes = ? WHERE id = ?"; $result = $this->db->execute($sql, [$resolutionNotes, $id]); return $result > 0; } catch (\Exception $e) { return false; } } /** * 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; } }