Coverage for app / router / monitoring / router.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1from sqlalchemy.orm import Session 

2from sqlalchemy import text 

3from fastapi import APIRouter, Depends 

4from app.dependency.base import get_db 

5from app.router.monitoring.response import ( 

6 Response, 

7 ScheduledJobStatus, 

8 ServiceStatus, 

9 ServiceStatuses, 

10) 

11from app.util.time import uptime, aware_utcnow 

12import logging 

13 

14logger = logging.getLogger("app") 

15 

16monitoring_router = APIRouter(prefix="/monitoring", tags=["Monitoring"]) 

17 

18 

19@monitoring_router.get("/ping") 

20async def ping(): 

21 logger.info("Pinging...") 

22 return Response.Ping(status=ServiceStatus.OK) 

23 

24 

25@monitoring_router.get("/health") 

26async def healthcheck(db: Session = Depends(get_db)): 

27 api_status = ServiceStatus.OK 

28 db_status = ServiceStatus.OK 

29 

30 try: 

31 db.execute(text("SELECT 1")) 

32 except Exception: 

33 api_status = ServiceStatus.DEGRADED 

34 db_status = ServiceStatus.DOWN 

35 

36 return Response.Health( 

37 status=api_status, 

38 uptime=uptime(), 

39 timestamp=aware_utcnow(), 

40 services=ServiceStatuses(database=db_status), 

41 ) 

42 # return {"Status": "Healthy", "DB": "connected", "db_res": response} 

43 

44 

45@monitoring_router.get("/scheduler") 

46async def scheduler_status(db: Session = Depends(get_db)): 

47 """What the background jobs are doing, and when each last ran. 

48 

49 "Has the retention purge been running?" is an audit question, and a 

50 scheduler observable only through container logs is one nobody can answer 

51 it about. Every registered job is listed whether or not it has ever run, 

52 so a job that has *never* fired is visible as such rather than absent. 

53 

54 ``leader`` says whether this replica is the one scheduling. Only one is, 

55 by design, so a false here is normal on every replica but one — and a 

56 false on *all* of them is the thing worth alerting on. 

57 """ 

58 from app.db.postgres.models import ScheduledJob 

59 from app.services.scheduler import jobs as job_registry 

60 from app.services.scheduler.runner import get_scheduler 

61 

62 scheduler = get_scheduler() 

63 try: 

64 rows = {row.key: row for row in db.query(ScheduledJob).all()} 

65 except Exception: 

66 logger.exception("Could not read scheduled job state") 

67 rows = {} 

68 

69 return Response.Scheduler( 

70 enabled=scheduler is not None, 

71 running=bool(scheduler and scheduler.running), 

72 leader=bool(scheduler and scheduler.leadership.held), 

73 tick_seconds=scheduler.tick_seconds if scheduler else None, 

74 jobs=[ 

75 ScheduledJobStatus( 

76 key=job.key, 

77 label=job.label, 

78 description=job.description, 

79 interval_seconds=job.interval_seconds, 

80 # Reported rather than silently rounded: a job asking for a 

81 # finer interval than the tick cannot have it, and a status 

82 # page that hid that would be stating something false. 

83 effective_interval_seconds=( 

84 max(job.interval_seconds, scheduler.tick_seconds) 

85 if scheduler 

86 else job.interval_seconds 

87 ), 

88 last_started_at=getattr(rows.get(job.key), "last_started_at", None), 

89 last_finished_at=getattr(rows.get(job.key), "last_finished_at", None), 

90 last_duration_ms=getattr(rows.get(job.key), "last_duration_ms", None), 

91 last_status=getattr(rows.get(job.key), "last_status", None), 

92 last_detail=getattr(rows.get(job.key), "last_detail", None), 

93 last_error=getattr(rows.get(job.key), "last_error", None), 

94 last_error_at=getattr(rows.get(job.key), "last_error_at", None), 

95 run_count=int(getattr(rows.get(job.key), "run_count", 0) or 0), 

96 failure_count=int(getattr(rows.get(job.key), "failure_count", 0) or 0), 

97 ) 

98 for job in job_registry.all_jobs() 

99 ], 

100 )