sync update from LAN repo (source dhk-1 @ 3b1ec5b4)
This commit is contained in:
1 parent
da6521b6cb
commit
264e2200d6
995 files changed
+148105
-13399
No files matched your search
@@ -11,13 +11,15 @@ import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import func, select, update
|
||||
from sqlalchemy import delete, func, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.run.model import RunRow
|
||||
from deerflow.runtime.runs.store.base import RunStore
|
||||
from deerflow.runtime.user_context import AUTO, _AutoSentinel, resolve_user_id
|
||||
|
||||
_TERMINAL_RUN_STATUSES = ("success", "error", "timeout", "interrupted")
|
||||
|
||||
|
||||
class RunRepository(RunStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
@@ -137,6 +139,14 @@ class RunRepository(RunStore):
|
||||
await session.execute(update(RunRow).where(RunRow.run_id == run_id).values(**values))
|
||||
await session.commit()
|
||||
|
||||
async def update_snapshot(self, run_id, *, metadata, kwargs):
|
||||
# Internal RunManager retention path; the run identity comes from its
|
||||
# live registry, never a client-supplied mutation endpoint.
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(update(RunRow).where(RunRow.run_id == run_id).values(metadata_json=self._safe_json(metadata), kwargs_json=self._safe_json(kwargs)))
|
||||
await session.commit()
|
||||
return bool(result.rowcount)
|
||||
|
||||
async def delete(
|
||||
self,
|
||||
run_id,
|
||||
@@ -205,21 +215,70 @@ class RunRepository(RunStore):
|
||||
await session.execute(update(RunRow).where(RunRow.run_id == run_id).values(**values))
|
||||
await session.commit()
|
||||
|
||||
async def count_by_user(self) -> dict[str, int]:
|
||||
"""Return ``{user_id: run_count}`` via a single GROUP BY query.
|
||||
async def count_by_user(self, *, since: datetime | None = None) -> dict[str, int]:
|
||||
"""Return ``{user_id: run_count}`` via one bounded GROUP BY query.
|
||||
|
||||
Each run is one user question, so this is the source for the admin
|
||||
user center's 问答次数 column. Runs without a ``user_id`` are skipped.
|
||||
"""
|
||||
filters = [RunRow.user_id.is_not(None)]
|
||||
if since is not None:
|
||||
since_dt = since if since.tzinfo else since.replace(tzinfo=UTC)
|
||||
filters.append(RunRow.created_at >= since_dt)
|
||||
stmt = (
|
||||
select(RunRow.user_id, func.count().label("cnt"))
|
||||
.where(RunRow.user_id.is_not(None))
|
||||
.where(*filters)
|
||||
.group_by(RunRow.user_id)
|
||||
)
|
||||
async with self._sf() as session:
|
||||
rows = (await session.execute(stmt)).all()
|
||||
return {r.user_id: r.cnt for r in rows}
|
||||
|
||||
async def purge_terminal_before(
|
||||
self,
|
||||
cutoff: datetime,
|
||||
*,
|
||||
batch_size: int = 500,
|
||||
) -> int:
|
||||
"""Delete one portable, primary-key-bounded batch of old run audits.
|
||||
|
||||
Pending and running records are intentionally excluded, so retention
|
||||
never removes work that could still be resumed or observed. A run must
|
||||
also have been *updated* before the cutoff: an unusually long-running
|
||||
job created months ago but completed today must remain visible. The
|
||||
compact run row is cleaned, while the conversation checkpoint stays
|
||||
under its separate user-history lifecycle.
|
||||
"""
|
||||
size = max(1, min(int(batch_size), 500))
|
||||
cutoff_dt = cutoff if cutoff.tzinfo else cutoff.replace(tzinfo=UTC)
|
||||
async with self._sf() as session:
|
||||
ids = list(
|
||||
(
|
||||
await session.scalars(
|
||||
select(RunRow.run_id)
|
||||
.where(
|
||||
RunRow.created_at < cutoff_dt,
|
||||
RunRow.updated_at < cutoff_dt,
|
||||
RunRow.status.in_(_TERMINAL_RUN_STATUSES),
|
||||
)
|
||||
.order_by(RunRow.created_at.asc(), RunRow.run_id.asc())
|
||||
.limit(size)
|
||||
)
|
||||
).all()
|
||||
)
|
||||
if not ids:
|
||||
return 0
|
||||
result = await session.execute(
|
||||
delete(RunRow).where(
|
||||
RunRow.run_id.in_(ids),
|
||||
RunRow.created_at < cutoff_dt,
|
||||
RunRow.updated_at < cutoff_dt,
|
||||
RunRow.status.in_(_TERMINAL_RUN_STATUSES),
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
return int(result.rowcount or 0)
|
||||
|
||||
async def aggregate_tokens_by_thread(self, thread_id: str) -> dict[str, Any]:
|
||||
"""Aggregate token usage via a single SQL GROUP BY query."""
|
||||
_completed = RunRow.status.in_(("success", "error"))
|
||||
|
||||
Reference in new issue
Block a user