docs: sync notes + identity_binding.rs traces pagination

This commit is contained in:
Accusys
2026-05-21 16:30:27 +08:00
parent bebaa743ed
commit e7eb90b987
2 changed files with 110 additions and 5 deletions

View File

@@ -375,18 +375,31 @@ pub struct IdentityTracesResponse {
pub success: bool,
pub identity_uuid: String,
pub name: String,
pub total_traces: usize,
pub total: usize,
pub page: usize,
pub page_size: usize,
pub total_faces: i64,
pub traces: Vec<IdentityTraceInfo>,
}
#[derive(Debug, Deserialize)]
pub struct TracesQuery {
pub page: Option<usize>,
pub page_size: Option<usize>,
}
pub async fn get_identity_traces(
State(state): State<crate::api::server::AppState>,
Path(identity_uuid): Path<String>,
Query(params): Query<TracesQuery>,
) -> Result<Json<IdentityTracesResponse>, (StatusCode, String)> {
let id_table = crate::core::db::schema::table_name("identities");
let fd_table = crate::core::db::schema::table_name("face_detections");
let page = params.page.unwrap_or(1);
let page_size = params.page_size.unwrap_or(20);
let offset = ((page - 1) as i64) * (page_size as i64);
// Get identity name
let identity: Option<(i32, String)> = sqlx::query_as(&format!(
"SELECT id, name FROM {} WHERE uuid = $1::uuid",
@@ -400,7 +413,7 @@ pub async fn get_identity_traces(
let (identity_id, name) =
identity.ok_or((StatusCode::NOT_FOUND, "Identity not found".to_string()))?;
// Get all traces for this identity across all files
// Get paginated traces for this identity across all files
let rows: Vec<(String, i32, i64, i32, i32, f64, f64, f64)> = sqlx::query_as(&format!(
r#"SELECT fd.file_uuid::text, fd.trace_id,
COUNT(*)::bigint AS frame_count,
@@ -412,15 +425,27 @@ pub async fn get_identity_traces(
FROM {} fd
WHERE fd.identity_id = $1
GROUP BY fd.file_uuid, fd.trace_id
ORDER BY fd.file_uuid, fd.trace_id"#,
ORDER BY fd.file_uuid, fd.trace_id
LIMIT $2 OFFSET $3"#,
fd_table
))
.bind(identity_id)
.bind(page_size as i64)
.bind(offset)
.fetch_all(state.db.pool())
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let total_traces = rows.len();
// Get total count for pagination
let total: (i64,) = sqlx::query_as(&format!(
"SELECT COUNT(*) FROM (SELECT 1 FROM {} fd WHERE fd.identity_id = $1 GROUP BY fd.file_uuid, fd.trace_id) sub",
fd_table
))
.bind(identity_id)
.fetch_one(state.db.pool())
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let total_traces = total.0 as usize;
let total_faces: i64 = rows.iter().map(|r| r.2).sum();
let traces: Vec<IdentityTraceInfo> = rows
@@ -452,7 +477,9 @@ pub async fn get_identity_traces(
success: true,
identity_uuid,
name,
total_traces,
total: total_traces,
page,
page_size,
total_faces,
traces,
}))