83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel
|
|
from typing import List, Optional, Dict, Any
|
|
from sqlalchemy.orm import Session
|
|
from ..services.postgresql_manager import PostgreSQLManager
|
|
from ..utils.config import config
|
|
from ..models.activity import Activity
|
|
from ..models.sync_log import SyncLog
|
|
from datetime import datetime
|
|
|
|
import json
|
|
|
|
router = APIRouter()
|
|
|
|
def get_db():
|
|
db_manager = PostgreSQLManager(config.DATABASE_URL)
|
|
with db_manager.get_db_session() as session:
|
|
yield session
|
|
|
|
class SyncLogResponse(BaseModel):
|
|
id: int
|
|
operation: str
|
|
status: str
|
|
message: Optional[str] = None
|
|
start_time: datetime
|
|
end_time: Optional[datetime] = None
|
|
records_processed: int
|
|
records_failed: int
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
class StatusResponse(BaseModel):
|
|
total_activities: int
|
|
downloaded_activities: int
|
|
recent_logs: List[SyncLogResponse]
|
|
last_sync_stats: Optional[List[Dict[str, Any]]] = None
|
|
|
|
@router.get("/status", response_model=StatusResponse)
|
|
def get_status(db: Session = Depends(get_db)):
|
|
"""Returns the current sync status and recent logs."""
|
|
total_activities = db.query(Activity).count()
|
|
downloaded_activities = db.query(Activity).filter(Activity.download_status == 'downloaded').count()
|
|
|
|
db_logs = db.query(SyncLog).order_by(SyncLog.start_time.desc()).limit(10).all()
|
|
# Pydantic v2 requires explicit conversion or correct config propagation
|
|
recent_logs = [SyncLogResponse.model_validate(log) for log in db_logs]
|
|
|
|
# Get last sync stats
|
|
last_sync_stats = []
|
|
|
|
# Activity
|
|
last_activity_log = db.query(SyncLog).filter(
|
|
SyncLog.operation == 'activity_sync'
|
|
).order_by(SyncLog.start_time.desc()).first()
|
|
|
|
if last_activity_log and last_activity_log.message:
|
|
try:
|
|
data = json.loads(last_activity_log.message)
|
|
if isinstance(data, dict) and "summary" in data:
|
|
last_sync_stats.extend(data["summary"])
|
|
except json.JSONDecodeError:
|
|
pass
|
|
|
|
# Health Metrics
|
|
last_metrics_log = db.query(SyncLog).filter(
|
|
SyncLog.operation == 'health_metric_sync'
|
|
).order_by(SyncLog.start_time.desc()).first()
|
|
|
|
if last_metrics_log and last_metrics_log.message:
|
|
try:
|
|
data = json.loads(last_metrics_log.message)
|
|
if isinstance(data, dict) and "summary" in data:
|
|
last_sync_stats.extend(data["summary"])
|
|
except json.JSONDecodeError:
|
|
pass
|
|
|
|
return StatusResponse(
|
|
total_activities=total_activities,
|
|
downloaded_activities=downloaded_activities,
|
|
recent_logs=recent_logs,
|
|
last_sync_stats=last_sync_stats if last_sync_stats else []
|
|
) |