docs: trace_profile UPSERT fixes + Studio face group API gap analysis
- profile.rs: single/group/merge_groups/undo all use upsert_trace_profile - trace_agent_api.rs: hard delete/merge snapshot + delete trace_profiles - cluster_agent.rs, job_worker.rs: background name writes use UPSERT - schema.rs: upsert_trace_profile helper (backfill from tkg_nodes) - Studio 3 API gaps documented (cluster-results/identity-matches/similar)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use sqlx::PgPool;
|
||||
|
||||
/// Schema-qualified table name helper
|
||||
/// Returns `name` for public schema, `schema.name` for others
|
||||
pub fn t(name: &str) -> String {
|
||||
@@ -13,3 +15,67 @@ pub fn table_name(name: &str) -> String {
|
||||
format!("{}.{}", schema, name)
|
||||
}
|
||||
}
|
||||
|
||||
/// UPSERT a trace profile row (insert on missing, update name on existing).
|
||||
/// When inserting a new row, backfills start_frame/end_frame/frame_count/
|
||||
/// key_frame/key_face/avg_confidence/properties from tkg_nodes face_track node
|
||||
/// so the created profile is complete.
|
||||
///
|
||||
/// `properties`: when provided, also overwrites the properties column on conflict;
|
||||
/// when `None`, only `name` is updated on conflict and properties are preserved.
|
||||
pub async fn upsert_trace_profile(
|
||||
pool: &PgPool,
|
||||
file_uuid: &str,
|
||||
trace_id: i32,
|
||||
name: &str,
|
||||
properties: Option<&serde_json::Value>,
|
||||
) -> anyhow::Result<u64> {
|
||||
let tp_table = table_name("trace_profiles");
|
||||
let tkg_table = table_name("tkg_nodes");
|
||||
|
||||
let tkg_sub = |col: &str| {
|
||||
format!(
|
||||
"(SELECT {col} FROM {tkg_table} tn WHERE tn.file_uuid = $1 \
|
||||
AND tn.node_type = 'face_track' AND (tn.properties->>'trace_id')::int = $2)"
|
||||
)
|
||||
};
|
||||
|
||||
let update_props = if properties.is_some() {
|
||||
", properties = EXCLUDED.properties"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let sql = format!(
|
||||
"INSERT INTO {tp_table} (file_uuid, trace_id, name, start_frame, end_frame, \
|
||||
frame_count, key_frame, key_face, status, avg_confidence, properties) \
|
||||
VALUES ($1, $2, $3, {sf}, {ef}, {fc}, {kf}, {kface}, 'pending', {avgc}, \
|
||||
COALESCE($4::jsonb, {props_sub}, '{{}}'::jsonb)) \
|
||||
ON CONFLICT (file_uuid, trace_id) \
|
||||
DO UPDATE SET name = EXCLUDED.name{update_props}, updated_at = NOW()",
|
||||
sf = tkg_sub("(tn.properties->>'start_frame')::bigint"),
|
||||
ef = tkg_sub("(tn.properties->>'end_frame')::bigint"),
|
||||
fc = tkg_sub("(tn.properties->>'frame_count')::int"),
|
||||
kf = tkg_sub("tn.properties->>'key_frame'"),
|
||||
kface = tkg_sub("tn.properties->>'key_face'"),
|
||||
avgc = tkg_sub("(tn.properties->>'avg_confidence')::double precision"),
|
||||
props_sub = tkg_sub("tn.properties"),
|
||||
);
|
||||
|
||||
let mut query = sqlx::query(&sql)
|
||||
.bind(file_uuid)
|
||||
.bind(trace_id)
|
||||
.bind(name);
|
||||
match properties {
|
||||
Some(p) => {
|
||||
let json = serde_json::to_string(p).unwrap_or_else(|_| "{}".to_string());
|
||||
query = query.bind(json);
|
||||
}
|
||||
None => {
|
||||
query = query.bind(Option::<String>::None);
|
||||
}
|
||||
}
|
||||
|
||||
let res = query.execute(pool).await?;
|
||||
Ok(res.rows_affected())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user