Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.pyc
Binary file added backend/app/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file added backend/app/__pycache__/db.cpython-313.pyc
Binary file not shown.
Binary file added backend/app/__pycache__/main.cpython-313.pyc
Binary file not shown.
Binary file added backend/app/api/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
24 changes: 14 additions & 10 deletions backend/app/api/routes/alerts.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
"""
Alert routes - now using async MongoDB operations
"""
from fastapi import APIRouter, Query
from app.services.alert_service import alert_service
from fastapi import APIRouter, Depends, Query, Response
from typing import Optional, List
from datetime import datetime
import io, csv
from app.services.alert_service import AlertService
from app.db import get_database

router = APIRouter(tags=["Alerts"])

# Hàm service alert
def get_alert_service() -> AlertService:
db = get_database()
return alert_service

# Hàm hiển thị tất cả danh sách alert
@router.get("/alerts")
async def get_alerts(
severity: str = Query(None),
limit: int = Query(10, ge=1, le=10000), # Increased from 100 to 10000 for pagination
limit: int = Query(10, ge=1, le=10000),
offset: int = Query(0, ge=0)
):
"""Get all alerts with optional severity filter"""
if severity:
return await alert_service.get_alerts_by_severity(severity, limit)
return await alert_service.get_all_alerts(limit, offset)

@router.get("/alerts/stats")
async def get_alert_stats():
"""Get alert statistics"""
return await alert_service.get_alerts_statistics()

@router.get("/alerts/{alert_id}")
async def get_alert(alert_id: str):
"""Get specific alert by ID"""
alert = await alert_service.get_alert_by_id(alert_id)
if not alert:
return {"error": "Alert not found"}
return alert

@router.post("/alerts/generate")
async def generate_alert():
"""Generate random alert for testing"""
alert = alert_service.generate_random_alert()
saved_alert = await alert_service.add_alert(alert)
return saved_alert

@router.post("/alerts")
async def create_alert(alert_data: dict):
"""Create new alert"""
return await alert_service.add_alert(alert_data)

@router.patch("/alerts/{alert_id}")
async def update_alert(alert_id: str, update_data: dict):
"""Update alert (e.g., acknowledge, change status)"""
return await alert_service.update_alert(alert_id, update_data)

13 changes: 0 additions & 13 deletions backend/app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,19 @@ class Database:

@classmethod
async def connect_db(cls):
"""Connect to MongoDB"""
mongodb_uri = os.getenv("MONGODB_URI")
db_name = os.getenv("DB_NAME", "ids_ips_db")

if not mongodb_uri:
raise ValueError("MONGODB_URI not found in environment variables")

# Replace <db_password> with actual password
# For now, we assume the URI is complete or password is set in env
cls.client = AsyncIOMotorClient(mongodb_uri)

# Test connection
try:
await cls.client.admin.command('ping')
print(f"[OK] Connected to MongoDB: {db_name}")
except Exception as e:
print(f"[FAIL] Failed to connect to MongoDB: {e}")
raise

# Get database
cls.db = cls.client[db_name]

# Setup indexes
await cls.setup_indexes()

@classmethod
Expand Down Expand Up @@ -73,11 +63,8 @@ async def setup_indexes(cls):
print("[OK] Database indexes created successfully")
except Exception as e:
print(f"[WARN] Could not create indexes: {e}")

# Create global database instance
db = Database()

def get_database():
"""Get database instance"""
return db.db

Loading