working
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
from typing import List, Optional, Dict, Any
|
||||
from sqlalchemy.orm import Session
|
||||
from ..services.postgresql_manager import PostgreSQLManager
|
||||
from ..utils.config import config
|
||||
@@ -8,6 +8,8 @@ from ..models.activity import Activity
|
||||
from ..models.sync_log import SyncLog
|
||||
from datetime import datetime
|
||||
|
||||
import json
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
def get_db():
|
||||
@@ -26,12 +28,13 @@ class SyncLogResponse(BaseModel):
|
||||
records_failed: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
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)):
|
||||
@@ -39,10 +42,42 @@ def get_status(db: Session = Depends(get_db)):
|
||||
total_activities = db.query(Activity).count()
|
||||
downloaded_activities = db.query(Activity).filter(Activity.download_status == 'downloaded').count()
|
||||
|
||||
recent_logs = db.query(SyncLog).order_by(SyncLog.start_time.desc()).limit(10).all()
|
||||
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
|
||||
recent_logs=recent_logs,
|
||||
last_sync_stats=last_sync_stats if last_sync_stats else []
|
||||
)
|
||||
Reference in New Issue
Block a user