- FastMCP server with 12 tools (pullzone, cache, statistics, shield) - Dockerfile with multi-stage build (python:3.11-slim + uv) - K8s manifests (Deployment + Service) - Gitea Actions CI/CD pipeline (build → push → deploy)
48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
"""Statistics and analytics tools."""
|
|
|
|
import json
|
|
from typing import Annotated
|
|
|
|
from pydantic import Field
|
|
|
|
from ..client import client
|
|
from ..config import logger
|
|
|
|
|
|
def register_statistics_tools(mcp):
|
|
|
|
@mcp.tool()
|
|
async def bunny_get_statistics(
|
|
pullzone_id: Annotated[int | None, Field(default=None, description="Pull Zone ID (optional, omit for account-wide stats)")] = None,
|
|
date_from: Annotated[str | None, Field(default=None, description="Start date in YYYY-MM-DD format (optional)")] = None,
|
|
date_to: Annotated[str | None, Field(default=None, description="End date in YYYY-MM-DD format (optional)")] = None,
|
|
) -> str:
|
|
"""Get bandwidth, cache hit rate, and request statistics."""
|
|
try:
|
|
params = {}
|
|
if pullzone_id is not None:
|
|
params["pullZone"] = pullzone_id
|
|
if date_from:
|
|
params["dateFrom"] = date_from
|
|
if date_to:
|
|
params["dateTo"] = date_to
|
|
|
|
data = await client.get("/statistics", params=params or None)
|
|
|
|
summary = {
|
|
"TotalBandwidthUsed": data.get("TotalBandwidthUsed"),
|
|
"TotalRequestsServed": data.get("TotalRequestsServed"),
|
|
"CacheHitRate": data.get("CacheHitRate"),
|
|
"AverageOriginResponseTime": data.get("AverageOriginResponseTime"),
|
|
"BandwidthUsedChart": data.get("BandwidthUsedChart"),
|
|
"RequestsServedChart": data.get("RequestsServedChart"),
|
|
"CacheHitRateChart": data.get("CacheHitRateChart"),
|
|
"Error3xxChart": data.get("Error3xxChart"),
|
|
"Error4xxChart": data.get("Error4xxChart"),
|
|
"Error5xxChart": data.get("Error5xxChart"),
|
|
}
|
|
return json.dumps(summary, indent=2)
|
|
except Exception as e:
|
|
logger.error("bunny_get_statistics failed: %s", e)
|
|
return f"Error: {e}"
|