slot.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from typing import List
  2. from fastapi import APIRouter, Query, Depends
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from app.core.redis import get_redis_client
  5. from app.core.database import get_db
  6. from redis.asyncio import Redis
  7. from app.utils.response import success
  8. from app.schemas.common import ApiResponse
  9. from app.schemas.slot_refresh_status import RefreshBase, RefreshFail, RefreshStatusOut
  10. from app.schemas.slot_snapshot import SlotOverviewOut, SlotSnapshotCreate, SlotSnapshotOut
  11. from app.services.slot_refresh_status_service import SlotRefreshStatusService
  12. from app.services.slot_snapshot_service import SlotSnapshotService
  13. public_router = APIRouter()
  14. protected_router = APIRouter()
  15. admin_required_router = APIRouter()
  16. @public_router.get("/slots/latest", summary="查询最近的slot", tags=["Slot数据"], response_model=ApiResponse[SlotSnapshotOut])
  17. async def slots_latest_get(
  18. country: str = Query("", description="目的国家"),
  19. city: str = Query("", description="递交城市"),
  20. visa_type: str = Query("", description="签证类型"),
  21. db: AsyncSession = Depends(get_db)
  22. ):
  23. res = await SlotSnapshotService.latest_for(db, country, city, visa_type)
  24. return success(data=res)
  25. @public_router.get("/slots/overview", summary="查询最近的slot", tags=["Slot数据"], response_model=ApiResponse[List[SlotOverviewOut]])
  26. async def slots_latest_get(
  27. city: str = Query("", description="递交城市"),
  28. db: AsyncSession = Depends(get_db)
  29. ):
  30. res = await SlotSnapshotService.get_slot_overview(db, city)
  31. return success(data=res)
  32. @admin_required_router.post("/slots/report", summary="上报最近的slot", tags=["Slot数据"], response_model=ApiResponse[SlotSnapshotOut])
  33. async def slots_report(
  34. payload: SlotSnapshotCreate,
  35. db: AsyncSession = Depends(get_db),
  36. redis_client: Redis = Depends(get_redis_client)
  37. ):
  38. res = await SlotSnapshotService.report(db, redis_client, payload)
  39. return success(data=res)
  40. @admin_required_router.post("/slot_refresh/start", summary="刷新slot开始", tags=["Slot Monitor 监控"], response_model=ApiResponse[RefreshStatusOut])
  41. async def slot_refresh_start(data: RefreshBase, db: AsyncSession = Depends(get_db)
  42. ):
  43. data = await SlotRefreshStatusService.refresh_start(db, data)
  44. return success(data=data)
  45. @admin_required_router.post("/slot_refresh/success", summary="刷新slot成功", tags=["Slot Monitor 监控"], response_model=ApiResponse[RefreshStatusOut])
  46. async def slot_refresh_success(data: RefreshBase, db: AsyncSession = Depends(get_db)
  47. ):
  48. data = await SlotRefreshStatusService.refresh_success(db, data)
  49. return success(data=data)
  50. @admin_required_router.post("/slot_refresh/fail", summary="刷新slot失败", tags=["Slot Monitor 监控"], response_model=ApiResponse[RefreshStatusOut])
  51. async def slot_refresh_fail(data: RefreshFail,db: AsyncSession = Depends(get_db)
  52. ):
  53. data = await SlotRefreshStatusService.refresh_fail(db, data)
  54. return success(data=data)
  55. @admin_required_router.get("/slot_refresh/status", summary="查询刷新纪录", tags=["Slot Monitor 监控"], response_model=ApiResponse[List[RefreshStatusOut]])
  56. async def slot_refresh_status(db: AsyncSession = Depends(get_db)):
  57. data = await SlotRefreshStatusService.list_all(db)
  58. return success(data=data)