feat(report): add slow API ranking module for admin
This commit is contained in:
@@ -13,6 +13,7 @@ from app_logger import get_logger
|
||||
from flask import jsonify, session
|
||||
from routes.admin_api import admin_api_bp
|
||||
from routes.decorators import admin_required
|
||||
from services.request_metrics import get_request_metrics_snapshot
|
||||
from services.time_utils import BEIJING_TZ, get_beijing_now
|
||||
|
||||
logger = get_logger("app")
|
||||
@@ -25,6 +26,10 @@ _DOCKER_STATS_CACHE_TTL = max(2.0, float(os.environ.get("ADMIN_DOCKER_STATS_CACH
|
||||
_docker_stats_cache: dict[str, object] = {"expires_at_monotonic": 0.0, "data": None}
|
||||
_docker_stats_cache_lock = threading.Lock()
|
||||
|
||||
_REQUEST_METRICS_CACHE_TTL = max(1.0, float(os.environ.get("ADMIN_REQUEST_METRICS_CACHE_TTL_SECONDS", "3")))
|
||||
_request_metrics_cache: dict[str, object] = {"expires_at_monotonic": 0.0, "data": None}
|
||||
_request_metrics_cache_lock = threading.Lock()
|
||||
|
||||
|
||||
def _get_system_stats_cached() -> dict:
|
||||
now = time.monotonic()
|
||||
@@ -43,6 +48,23 @@ def _get_system_stats_cached() -> dict:
|
||||
return dict(fresh_data)
|
||||
|
||||
|
||||
def _get_request_metrics_cached() -> dict:
|
||||
now = time.monotonic()
|
||||
with _request_metrics_cache_lock:
|
||||
expires_at = float(_request_metrics_cache.get("expires_at_monotonic") or 0.0)
|
||||
cached_data = _request_metrics_cache.get("data")
|
||||
if isinstance(cached_data, dict) and now < expires_at:
|
||||
return dict(cached_data)
|
||||
|
||||
fresh_data = get_request_metrics_snapshot() or {}
|
||||
|
||||
with _request_metrics_cache_lock:
|
||||
_request_metrics_cache["data"] = dict(fresh_data)
|
||||
_request_metrics_cache["expires_at_monotonic"] = now + _REQUEST_METRICS_CACHE_TTL
|
||||
|
||||
return dict(fresh_data)
|
||||
|
||||
|
||||
@admin_api_bp.route("/stats", methods=["GET"])
|
||||
@admin_required
|
||||
def get_system_stats():
|
||||
@@ -52,6 +74,18 @@ def get_system_stats():
|
||||
return jsonify(stats)
|
||||
|
||||
|
||||
@admin_api_bp.route("/request_metrics", methods=["GET"])
|
||||
@admin_required
|
||||
def get_request_metrics():
|
||||
"""获取请求级监控指标"""
|
||||
try:
|
||||
metrics = _get_request_metrics_cached()
|
||||
return jsonify(metrics)
|
||||
except Exception as e:
|
||||
logger.exception(f"获取请求级监控指标失败: {e}")
|
||||
return jsonify({"error": "获取请求级监控指标失败"}), 500
|
||||
|
||||
|
||||
@admin_api_bp.route("/browser_pool/stats", methods=["GET"])
|
||||
@admin_required
|
||||
def get_browser_pool_stats():
|
||||
|
||||
Reference in New Issue
Block a user