perf(usage): 将 status 过滤下推到 SQL 查询层

为 UsageAuditListQuery 新增 statuses 字段,支持在数据库端按状态
筛选用量记录。admin usage summary 路由不再全量拉取后内存过滤,
改为直接查询 pending/streaming 状态的记录。
This commit is contained in:
fawney19
2026-04-15 09:30:42 +08:00
parent adde9ff237
commit 8827c46c33
8 changed files with 34 additions and 4 deletions

View File

@@ -1195,10 +1195,22 @@ impl SqlxUsageReadRepository {
}
if let Some(model) = query.model.as_deref() {
builder.push(if has_where { " AND " } else { " WHERE " });
has_where = true;
builder
.push("\"usage\".model = ")
.push_bind(model.to_string());
}
if let Some(statuses) = query.statuses.as_deref() {
if !statuses.is_empty() {
builder.push(if has_where { " AND " } else { " WHERE " });
builder.push("\"usage\".status IN (");
let mut separated = builder.separated(", ");
for status in statuses {
separated.push_bind(status.to_string());
}
separated.push_unseparated(")");
}
}
builder.push(" ORDER BY \"usage\".created_at ASC, \"usage\".request_id ASC");
let query = builder.build();