Files
AstrBot/astrbot/dashboard/api/stats.py
Weilong Liao 0d8e8682db refactor(core): migrate backend backbone from Quart to FastAPI and introduce more OpenAPI (#8688)
* refactor: migrate to fastapi

* structure refactor

* fix: pyright fix

* refactor: improve error handling and public messages in plugin services

* feat(api): refactor API client integration and enhance request handling

- Updated API client configuration to use a dedicated HTTP client.
- Introduced utility functions for generating options, queries, and form data for API requests.
- Refactored multiple API methods to utilize the new utility functions for improved consistency and readability.
- Renamed types for clarity and updated import statements accordingly.

feat(docs): add script to update OpenAPI JSON from YAML spec

- Created a Python script to convert OpenAPI YAML specification to JSON format.
- The script supports customizable input and output paths.
- Ensured the script handles directory creation for output paths and validates the YAML structure.

* fix

* feat(auth): implement rate limiting for v1 login endpoint and enhance request handling

* Refactor dashboard API routers to use legacy_router for backward compatibility

- Changed all instances of dashboard_router to legacy_router across multiple API modules including platform, plugins, providers, sessions, skills, stats, subagents, t2i, tools, updates, and asgi_runtime.
- Updated route definitions to ensure existing endpoints remain functional under the new router structure.
- Introduced support for Quart request context in asgi_runtime to enhance compatibility with existing Quart-based plugins.
- Added a test case to validate the functionality of the new Quart request context handling in plugin extensions.

* chore: remove cli test

* fix: update dashboard tests for fastapi migration

* chore: satisfy ruff checks

* fix: update openapi api key scopes

* fix: sync config scope chip selection

* fix: restore quart dependency

* docs: clarify quart plugin api compatibility

* docs: update openapi scope documentation

* fix: use singular skill openapi scope

* fix: hide update service exception details

* fix: address fastapi review comments

* fix: address dashboard review findings

* docs: revert unrelated package deployment changes

* docs: update agent api generation guidance

* feat: add plugin page web api helpers

* docs: add plugin page bridge demo

* fix: type plugin upload files

* fix: stabilize plugin page uploads

* fix: type plugin web request proxy

* docs: remove plugin page docs example

* fix: authenticate plugin page SSE bridge
2026-06-14 15:03:26 +08:00

236 lines
6.9 KiB
Python

from __future__ import annotations
from fastapi import APIRouter, Depends, Query, Request
from astrbot.dashboard.async_utils import run_maybe_async
from astrbot.dashboard.responses import ApiError, ok
from astrbot.dashboard.schemas import GhProxyTestRequest, StorageCleanupRequest
from astrbot.dashboard.services.stat_service import StatService, StatServiceError
from .auth import AuthContext, require_dashboard_user, require_scope
router = APIRouter(tags=["System Stats"])
legacy_router = APIRouter(
prefix="/api/stat",
tags=["Dashboard System Stats"],
include_in_schema=False,
)
async def require_system_scope(request: Request) -> AuthContext:
return await require_scope(request, "system")
def get_service(request: Request) -> StatService:
return request.app.state.services.stats
def _raise_stat_error(exc: StatServiceError) -> None:
raise ApiError(str(exc)) from exc
async def _run(operation):
try:
result = await run_maybe_async(operation)
return ok(result)
except StatServiceError as exc:
_raise_stat_error(exc)
def _parse_int(value: object, default: int, name: str) -> int:
if value is None:
return default
if not isinstance(value, int | float | str | bytes | bytearray):
raise ApiError(f"{name} must be an integer")
try:
return int(value)
except (TypeError, ValueError) as exc:
raise ApiError(f"{name} must be an integer") from exc
@router.get("/stats")
async def get_stats(
offset_sec: int = Query(default=86400),
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(service.get_stat(offset_sec))
@router.get("/stats/provider-tokens")
async def get_provider_token_stats(
days: int = Query(default=1),
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(service.get_provider_token_stats(days))
@router.get("/stats/version")
async def get_version(
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(service.get_version())
@router.get("/stats/first-notice")
async def get_first_notice(
locale: str | None = None,
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(lambda: service.get_first_notice(locale))
@router.post("/stats/ghproxy/test")
async def test_ghproxy_connection(
payload: GhProxyTestRequest,
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(service.test_ghproxy_connection(payload.proxy_url))
@router.get("/changelogs")
async def list_changelog_versions(
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(service.list_changelog_versions)
@router.get("/changelogs/{version}")
async def get_changelog(
version: str,
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(lambda: service.get_changelog(version))
@router.get("/stats/start-time")
async def get_start_time(
service: StatService = Depends(get_service),
):
return await _run(service.get_start_time)
@router.get("/stats/storage")
async def get_storage_status(
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(service.get_storage_status())
@router.post("/stats/storage/cleanup")
async def cleanup_storage(
payload: StorageCleanupRequest,
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(service.cleanup_storage(payload.target))
@router.post("/system/restart")
async def restart_system(
_auth: AuthContext = Depends(require_system_scope),
service: StatService = Depends(get_service),
):
return await _run(service.restart_core())
@legacy_router.get("/get")
async def get_dashboard_stats(
offset_sec: int | None = Query(default=86400),
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(service.get_stat(_parse_int(offset_sec, 86400, "offset_sec")))
@legacy_router.get("/provider-tokens")
async def get_dashboard_provider_token_stats(
days: int | None = Query(default=1),
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(service.get_provider_token_stats(_parse_int(days, 1, "days")))
@legacy_router.get("/version")
async def get_dashboard_version(
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(service.get_version())
@legacy_router.get("/start-time")
async def get_dashboard_start_time(
service: StatService = Depends(get_service),
):
return await _run(service.get_start_time)
@legacy_router.post("/restart-core")
async def restart_dashboard_core(
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(service.restart_core())
@legacy_router.post("/test-ghproxy-connection")
async def test_dashboard_ghproxy_connection(
payload: GhProxyTestRequest,
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(service.test_ghproxy_connection(payload.proxy_url))
@legacy_router.get("/changelog")
async def get_dashboard_changelog(
version: str | None = None,
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(lambda: service.get_changelog(version))
@legacy_router.get("/changelog/list")
async def list_dashboard_changelog_versions(
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(service.list_changelog_versions)
@legacy_router.get("/first-notice")
async def get_dashboard_first_notice(
locale: str | None = None,
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(lambda: service.get_first_notice(locale))
@legacy_router.get("/storage")
async def get_dashboard_storage_status(
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
return await _run(service.get_storage_status())
@legacy_router.post("/storage/cleanup")
async def cleanup_dashboard_storage(
payload: StorageCleanupRequest | None = None,
_username: str = Depends(require_dashboard_user),
service: StatService = Depends(get_service),
):
target = payload.target if payload is not None else "all"
return await _run(service.cleanup_storage(target))