Files
AstrBot/tests/unit/test_provider_stats.py
Soulter 2b435e0c89 feat: add brand-new statistics dashboarde overview with model usage and platform usage (#7152)
* feat: add new StatsPage for enhanced statistics overview

- Introduced StatsPage.vue to provide a comprehensive overview of statistics with various metrics and charts.
- Implemented fetching and displaying of base and model provider statistics.
- Added unit tests for provider statistics persistence in the database.

* style: refine card styles and remove unnecessary shadow for improved aesthetics
2026-03-29 22:50:15 +08:00

66 lines
2.0 KiB
Python

from types import SimpleNamespace
import pytest
from sqlmodel import select
from astrbot.core.agent.response import AgentStats
from astrbot.core.db.po import ProviderStat
from astrbot.core.pipeline.process_stage.method.agent_sub_stages import internal
from astrbot.core.provider.entities import ProviderRequest, TokenUsage
@pytest.mark.asyncio
async def test_record_internal_agent_stats_persists_provider_stat(
temp_db,
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setattr(internal, "db_helper", temp_db)
event = SimpleNamespace(unified_msg_origin="webchat:FriendMessage:session-42")
req = ProviderRequest(
conversation=SimpleNamespace(cid="conv-123"),
)
stats = AgentStats(
token_usage=TokenUsage(input_other=11, input_cached=3, output=7),
start_time=100.0,
end_time=108.5,
time_to_first_token=0.6,
)
provider = SimpleNamespace(
provider_config={"id": "provider-1"},
meta=lambda: SimpleNamespace(id="provider-1", type="openai"),
get_model=lambda: "gpt-4.1",
)
agent_runner = SimpleNamespace(
provider=provider,
stats=stats,
was_aborted=lambda: False,
)
final_resp = SimpleNamespace(role="assistant")
await internal._record_internal_agent_stats(
event,
req,
agent_runner,
final_resp,
)
async with temp_db.get_db() as session:
result = await session.execute(select(ProviderStat))
records = result.scalars().all()
assert len(records) == 1
record = records[0]
assert record.agent_type == "internal"
assert record.status == "completed"
assert record.umo == "webchat:FriendMessage:session-42"
assert record.conversation_id == "conv-123"
assert record.provider_id == "provider-1"
assert record.provider_model == "gpt-4.1"
assert record.token_input_other == 11
assert record.token_input_cached == 3
assert record.token_output == 7
assert record.start_time == 100.0
assert record.end_time == 108.5
assert record.time_to_first_token == 0.6