Instrument API timing and reduce detail fallbacks
This commit is contained in:
+10
-3
@@ -2,7 +2,7 @@
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Query, Request
|
||||
from fastapi import APIRouter, BackgroundTasks, Query, Request, Response
|
||||
|
||||
from web.services.city_api import (
|
||||
get_city_detail_batch_payload,
|
||||
@@ -12,6 +12,7 @@ from web.services.city_api import (
|
||||
list_cities_payload,
|
||||
)
|
||||
from web.services.city_realtime_stream import get_realtime_stream_payload
|
||||
from web.services.request_timing import attach_server_timing_header
|
||||
|
||||
router = APIRouter(tags=["city"])
|
||||
|
||||
@@ -108,6 +109,7 @@ async def cities_model_range(
|
||||
@router.get("/api/cities/detail-batch")
|
||||
async def city_detail_batch(
|
||||
request: Request,
|
||||
response: Response,
|
||||
cities: str = "",
|
||||
force_refresh: bool = False,
|
||||
market_slug: Optional[str] = None,
|
||||
@@ -115,7 +117,7 @@ async def city_detail_batch(
|
||||
resolution: Optional[str] = "10m",
|
||||
limit: int = 12,
|
||||
):
|
||||
return await get_city_detail_batch_payload(
|
||||
payload = await get_city_detail_batch_payload(
|
||||
request,
|
||||
cities=cities,
|
||||
force_refresh=force_refresh,
|
||||
@@ -124,6 +126,8 @@ async def city_detail_batch(
|
||||
resolution=resolution,
|
||||
limit=limit,
|
||||
)
|
||||
attach_server_timing_header(response, request, "city_detail_batch_server_timing")
|
||||
return payload
|
||||
|
||||
|
||||
@router.get("/api/city/{name}")
|
||||
@@ -159,13 +163,14 @@ async def city_summary(
|
||||
@router.get("/api/city/{name}/detail")
|
||||
async def city_detail_aggregate(
|
||||
request: Request,
|
||||
response: Response,
|
||||
name: str,
|
||||
force_refresh: bool = False,
|
||||
market_slug: Optional[str] = None,
|
||||
target_date: Optional[str] = None,
|
||||
resolution: Optional[str] = "10m",
|
||||
):
|
||||
return await get_city_detail_aggregate_payload(
|
||||
payload = await get_city_detail_aggregate_payload(
|
||||
request,
|
||||
name,
|
||||
force_refresh=force_refresh,
|
||||
@@ -173,6 +178,8 @@ async def city_detail_aggregate(
|
||||
target_date=target_date,
|
||||
resolution=resolution,
|
||||
)
|
||||
attach_server_timing_header(response, request, "city_detail_server_timing")
|
||||
return payload
|
||||
|
||||
|
||||
@router.get("/api/city/{name}/realtime-stream")
|
||||
|
||||
+25
-4
@@ -1,6 +1,6 @@
|
||||
"""Operations/admin API routes."""
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi import APIRouter, Request, Response
|
||||
|
||||
from web.core import GrantPointsRequest
|
||||
from web.services.ops_api import (
|
||||
@@ -30,14 +30,35 @@ from web.services.ops_api import (
|
||||
get_ops_training_accuracy,
|
||||
get_ops_telegram_audit,
|
||||
)
|
||||
from web.services.request_timing import ServerTimingRecorder, attach_server_timing_header
|
||||
|
||||
router = APIRouter(tags=["ops"])
|
||||
|
||||
|
||||
@router.get("/api/ops/online-users")
|
||||
async def ops_online_users():
|
||||
from src.utils.online_tracker import online_count
|
||||
return {"online": online_count()}
|
||||
async def ops_online_users(request: Request, response: Response):
|
||||
timer = ServerTimingRecorder(
|
||||
request,
|
||||
log_name="ops_online_users_timing",
|
||||
prefix="ops_online_users",
|
||||
state_attr="ops_online_users_server_timing",
|
||||
)
|
||||
outcome = "ok"
|
||||
status_code = 200
|
||||
try:
|
||||
from src.utils.online_tracker import online_count
|
||||
return {"online": timer.measure("online_count", online_count)}
|
||||
except Exception:
|
||||
outcome = "exception"
|
||||
status_code = 500
|
||||
raise
|
||||
finally:
|
||||
timer.finish(outcome=outcome, status_code=status_code)
|
||||
attach_server_timing_header(
|
||||
response,
|
||||
request,
|
||||
"ops_online_users_server_timing",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/ops/users")
|
||||
|
||||
+4
-1
@@ -9,6 +9,7 @@ from web.services.scan_api import (
|
||||
get_scan_terminal_overview_payload,
|
||||
get_scan_terminal_payload,
|
||||
)
|
||||
from web.services.request_timing import attach_server_timing_header
|
||||
|
||||
router = APIRouter(tags=["scan"])
|
||||
|
||||
@@ -45,12 +46,14 @@ async def scan_terminal(
|
||||
region=region or trading_region or None,
|
||||
timezone_offset_seconds=timezone_offset_seconds,
|
||||
)
|
||||
return JSONResponse(
|
||||
response = JSONResponse(
|
||||
content=payload,
|
||||
headers={
|
||||
"Cache-Control": "public, s-maxage=30, stale-while-revalidate=120",
|
||||
},
|
||||
)
|
||||
attach_server_timing_header(response, request, "scan_terminal_server_timing")
|
||||
return response
|
||||
|
||||
|
||||
@router.post("/api/scan/terminal/overview")
|
||||
|
||||
Reference in New Issue
Block a user