sync update from LAN repo (source dhk-1 @ c21a2a4a)

This commit is contained in:
gozeros committed 2026-11-10 14:04:00 +08:00
1 parent 264e2200d6
commit a1fa13a238
122 files changed
+8881 -623

No files matched your search

@@ -30,7 +30,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Request
from fastapi.responses import StreamingResponse
from langchain_core.messages import HumanMessage, SystemMessage
from pydantic import BaseModel, Field
from sqlalchemy import and_, case, desc, func, not_, or_, select, text
from sqlalchemy import and_, case, desc, func, literal, not_, or_, select, text
from app.gateway.admin_query_window import (
ADMIN_QUERY_WINDOW_DAYS,
@@ -279,6 +279,41 @@ class AdminLeaderboardDimension(BaseModel):
last_seen_at: str = ""
class AdminLeaderboardDetailItem(BaseModel):
"""One row in the expanded skill, tool, or agent analytics drawer."""
name: str
count: int = 0
user_count: int = 0
total_tokens: int = 0
avg_duration_ms: int = 0
class AdminLeaderboardDetailSummary(BaseModel):
count: int = 0
item_count: int = 0
user_count: int = 0
total_tokens: int = 0
avg_duration_ms: int = 0
class AdminLeaderboardDetailResponse(BaseModel):
"""Fresh, drill-down analytics for a leaderboard dimension.
Unlike the dashboard's compact daily snapshots, this response is queried
directly. It lets an administrator inspect the entire matching dimension
(up to the explicit limit) and does not lose long-tail rows because they
were outside a daily top-N snapshot.
"""
kind: str
range_days: int
since: str
until: str
summary: AdminLeaderboardDetailSummary
items: list[AdminLeaderboardDetailItem] = Field(default_factory=list)
class AdminLeaderboardTrendPoint(BaseModel):
date: str
runs: int = 0
@@ -961,6 +996,125 @@ async def _query_run_dimensions(
]
async def _query_leaderboard_dimension_details(
session,
since: datetime,
until: datetime,
*,
kind: str,
limit: int,
exclude_user_ids: set[str],
include_scheduled: bool,
include_admins: bool,
include_failed: bool,
) -> tuple[
AdminLeaderboardDetailSummary,
list[AdminLeaderboardDetailItem],
]:
"""Load complete, fresh detail statistics for one leaderboard dimension.
The main leaderboard deliberately reads compact daily snapshots so it can
remain quick under load. The drawer is an intentional admin drill-down,
therefore it runs a bounded aggregate against the source metric tables and
returns exact unique-user counts for the selected interval.
"""
if kind in {"skill", "tool"}:
name_col = ToolCallMetricRow.skill_name if kind == "skill" else ToolCallMetricRow.tool_name
base_filters = [
_tool_time_filter(since, until),
name_col.is_not(None),
name_col != "",
]
def apply_scope(stmt):
return _apply_tool_scope(
stmt,
exclude_user_ids=exclude_user_ids,
include_admins=include_admins,
include_scheduled=include_scheduled,
)
count_col = func.count(ToolCallMetricRow.id)
user_count_col = func.count(func.distinct(ToolCallMetricRow.user_id))
total_tokens_col = literal(0)
avg_duration_col = func.coalesce(func.avg(ToolCallMetricRow.duration_ms), 0)
elif kind == "agent":
name_col = RunRow.assistant_id
base_filters = [
_run_time_filter(since, until),
name_col.is_not(None),
name_col != "",
]
def apply_scope(stmt):
return _apply_run_scope(
stmt,
user_column=RunRow.user_id,
run_id_column=RunRow.run_id,
exclude_user_ids=exclude_user_ids,
include_admins=include_admins,
include_failed=include_failed,
include_scheduled=include_scheduled,
)
count_col = func.count(RunRow.run_id)
user_count_col = func.count(func.distinct(RunRow.user_id))
total_tokens_col = func.coalesce(func.sum(RunRow.total_tokens), 0)
# Run rows have no reliable wall-clock duration. Keep this explicit at
# zero instead of inferring elapsed time from an asynchronously updated
# journal timestamp.
avg_duration_col = literal(0)
else:
raise ValueError(f"Unknown leaderboard detail kind: {kind}")
item_stmt = (
select(
name_col.label("name"),
count_col.label("count"),
user_count_col.label("user_count"),
total_tokens_col.label("total_tokens"),
avg_duration_col.label("avg_duration_ms"),
)
.where(*base_filters)
.group_by(name_col)
.order_by(count_col.desc(), name_col.asc())
.limit(limit)
)
item_stmt = apply_scope(item_stmt)
summary_stmt = select(
count_col.label("count"),
func.count(func.distinct(name_col)).label("item_count"),
user_count_col.label("user_count"),
total_tokens_col.label("total_tokens"),
avg_duration_col.label("avg_duration_ms"),
).where(*base_filters)
summary_stmt = apply_scope(summary_stmt)
item_rows = (await _execute_leaderboard_query(session, f"{kind}_detail.items", item_stmt)).all()
summary_row = (await _execute_leaderboard_query(session, f"{kind}_detail.summary", summary_stmt)).one()
summary = AdminLeaderboardDetailSummary(
count=int(summary_row[0] or 0),
item_count=int(summary_row[1] or 0),
user_count=int(summary_row[2] or 0),
total_tokens=int(summary_row[3] or 0),
avg_duration_ms=int(summary_row[4] or 0),
)
items = [
AdminLeaderboardDetailItem(
name=row[0] or "",
count=int(row[1] or 0),
user_count=int(row[2] or 0),
total_tokens=int(row[3] or 0),
avg_duration_ms=int(row[4] or 0),
)
for row in item_rows
]
return summary, items
async def _query_model_rankings(
session,
since: datetime,
@@ -1876,6 +2030,59 @@ async def get_user_leaderboard(
)
@router.get("/leaderboard/details", response_model=AdminLeaderboardDetailResponse)
async def get_user_leaderboard_details(
request: Request,
kind: str = Query(description="One of: skill, tool, agent", pattern="^(skill|tool|agent)$"),
days: int = Query(default=30, ge=1, le=ADMIN_QUERY_WINDOW_DAYS),
since: str | None = Query(default=None, description="Inclusive range start, ISO datetime"),
until: str | None = Query(default=None, description="Inclusive range end, ISO datetime"),
limit: int = Query(default=500, ge=1, le=1000),
) -> AdminLeaderboardDetailResponse:
"""Return fresh drill-down statistics for one leaderboard card.
This endpoint intentionally reads the source metric tables instead of the
asynchronous daily leaderboard snapshots. A drawer represents an explicit
admin investigation, where exact unique-user counts, all matching entries,
and complete detail rows are more useful than a compact cache.
"""
await _require_admin(request)
session_factory = get_session_factory()
if session_factory is None:
raise HTTPException(status_code=503, detail="Analytics requires database persistence")
settings = load_system_settings().leaderboard
since_dt, until_dt, range_days = resolve_admin_query_window(
days=days,
since=since,
until=until,
)
excluded, include_scheduled, include_admins, include_failed, _settings_hash = _resolve_leaderboard_snapshot_scope(settings)
async with session_factory() as session:
await _set_analytics_read_timeout(session)
summary, items = await _query_leaderboard_dimension_details(
session,
since_dt,
until_dt,
kind=kind,
limit=limit,
exclude_user_ids=excluded,
include_scheduled=include_scheduled,
include_admins=include_admins,
include_failed=include_failed,
)
return AdminLeaderboardDetailResponse(
kind=kind,
range_days=range_days,
since=_iso(since_dt),
until=_iso(until_dt),
summary=summary,
items=items,
)
@router.get("/leaderboard/snapshots", response_model=AdminLeaderboardSnapshotStatusResponse)
async def get_leaderboard_snapshot_status(
request: Request,