[Phase 0 Reset]
- 清除舊版 app/、alembic/versions/、雜亂測試腳本
- 新 requirements.txt (移除 caldav/redis/keycloak-lib,加入 apscheduler/croniter/docker/paramiko/ping3/dnspython)
[Phase 1 資料庫]
- 9 張資料表 SQLAlchemy Models:tenants / accounts / schedules / schedule_logs /
tenant_schedule_results / account_schedule_results / servers / server_status_logs / system_status_logs
- Alembic migration 001_create_all_tables (已套用到 10.1.0.20:5433/virtual_mis)
- seed.py:schedules 初始 3 筆 / servers 初始 4 筆
[Phase 2 CRUD API]
- GET/POST/PUT/DELETE: /api/v1/tenants / accounts / servers / schedules
- /api/v1/system-status
- 帳號編碼自動產生 (prefix + seq_no 4碼左補0)
- 燈號 (lights) 從最新排程結果取得
[Phase 3 Watchdog]
- APScheduler interval 3分鐘,原子 UPDATE status=Going 防重複執行
- 手動觸發 API: POST /api/v1/schedules/{id}/run
[Phase 4 Service Clients]
- KeycloakClient:vmis-admin realm,REST API (不用 python-keycloak)
- MailClient:Docker Mailserver @ 10.1.0.254:8080,含 MX DNS 驗證
- DockerClient:docker-py 本機 + paramiko SSH 遠端 compose
- NextcloudClient:OCS API user/quota
- SystemChecker:功能驗證 (traefik routers>0 / keycloak token / SMTP EHLO / DB SELECT 1 / ping)
[TDD]
- 37 tests / 37 passed (2.11s)
- SQLite in-memory + StaticPool,無需外部 DB
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
from typing import List
|
||
from fastapi import APIRouter, Depends
|
||
from sqlalchemy.orm import Session
|
||
from pydantic import BaseModel
|
||
from typing import Optional
|
||
from datetime import datetime
|
||
|
||
from app.core.database import get_db
|
||
from app.models.server import SystemStatusLog
|
||
|
||
router = APIRouter(tags=["status"])
|
||
|
||
|
||
class SystemStatusItem(BaseModel):
|
||
id: int
|
||
environment: str
|
||
service_name: str
|
||
service_desc: Optional[str]
|
||
result: bool
|
||
fail_reason: Optional[str]
|
||
recorded_at: datetime
|
||
|
||
class Config:
|
||
from_attributes = True
|
||
|
||
|
||
@router.get("/system-status", response_model=List[SystemStatusItem])
|
||
def get_system_status(db: Session = Depends(get_db)):
|
||
"""最新一次系統狀態 (8 筆: test/prod × traefik/keycloak/mail/db)"""
|
||
# Get latest schedule_log_id for schedule_id=3
|
||
from app.models.schedule import ScheduleLog
|
||
latest_log = (
|
||
db.query(ScheduleLog)
|
||
.filter(ScheduleLog.schedule_id == 3)
|
||
.order_by(ScheduleLog.started_at.desc())
|
||
.first()
|
||
)
|
||
if not latest_log:
|
||
return []
|
||
return (
|
||
db.query(SystemStatusLog)
|
||
.filter(SystemStatusLog.schedule_log_id == latest_log.id)
|
||
.all()
|
||
)
|