refactor(runtime): 优化管理端摘要查询与维护聚合链路

- 为 provider catalog key 和 video task 列表增加 summary/page 查询与排序能力,减少列表场景读取重字段
- 将多处 SQL 结果读取改为流式收集,降低 `fetch_all` 的内存占用
- 把日/小时统计、钱包日用量等维护任务改为数据库侧 `CTE + upsert` 聚合
- 修复视频任务轮询更新时从本地 snapshot 回填稀疏字段,避免 `prompt` 和请求体信息丢失
This commit is contained in:
AAEE86
2026-04-12 20:45:47 +08:00
committed by fawney19
parent 6aa16ec792
commit b2d85d70ca
55 changed files with 1954 additions and 1382 deletions

View File

@@ -1,7 +1,7 @@
use crate::error::SqlxResultExt;
use crate::postgres::{DatabaseRecordId, PostgresTransactionOptions, PostgresTransactionRunner};
use crate::DataLayerError;
use futures_util::FutureExt;
use futures_util::{FutureExt, TryStreamExt};
use sqlx::query_scalar;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -105,13 +105,15 @@ impl PostgresLeaseRunner {
self.transaction_runner
.run(tx_options, |tx| {
async move {
let rows = query_scalar::<_, String>(&sql)
let mut rows = query_scalar::<_, String>(&sql)
.bind(owner)
.bind(lease_ms)
.fetch_all(&mut **tx)
.await
.map_postgres_err()?;
Ok(rows.into_iter().map(DatabaseRecordId).collect())
.fetch(&mut **tx);
let mut ids = Vec::new();
while let Some(id) = rows.try_next().await.map_postgres_err()? {
ids.push(DatabaseRecordId(id));
}
Ok(ids)
}
.boxed()
})
@@ -140,13 +142,15 @@ impl PostgresLeaseRunner {
self.transaction_runner
.run(tx_options, |tx| {
async move {
let rows = query_scalar::<_, String>(&sql)
let mut rows = query_scalar::<_, String>(&sql)
.bind(ids)
.bind(owner)
.fetch_all(&mut **tx)
.await
.map_postgres_err()?;
Ok(rows.into_iter().map(DatabaseRecordId).collect())
.fetch(&mut **tx);
let mut released = Vec::new();
while let Some(id) = rows.try_next().await.map_postgres_err()? {
released.push(DatabaseRecordId(id));
}
Ok(released)
}
.boxed()
})
@@ -184,14 +188,16 @@ impl PostgresLeaseRunner {
self.transaction_runner
.run(tx_options, |tx| {
async move {
let rows = query_scalar::<_, String>(&sql)
let mut rows = query_scalar::<_, String>(&sql)
.bind(ids)
.bind(owner)
.bind(lease_ms)
.fetch_all(&mut **tx)
.await
.map_postgres_err()?;
Ok(rows.into_iter().map(DatabaseRecordId).collect())
.fetch(&mut **tx);
let mut renewed = Vec::new();
while let Some(id) = rows.try_next().await.map_postgres_err()? {
renewed.push(DatabaseRecordId(id));
}
Ok(renewed)
}
.boxed()
})