fix: prevent duplicate face group cards - deduplicate tkg_nodes and DISTINCT ON in get_face_groups_handler
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#!/bin/bash
|
||||
# Cleanup duplicate tkg_nodes entries
|
||||
# Usage: ./scripts/cleanup_duplicate_tkg_nodes.sh [file_uuid]
|
||||
|
||||
cleanup_duplicates() {
|
||||
local uuid=$1
|
||||
echo "🔄 Cleaning up duplicate tkg_nodes for $uuid..."
|
||||
|
||||
# Find and remove duplicate face_track nodes
|
||||
# Keep trace_N, remove face_track_N for same trace_id
|
||||
cat << 'SQL' | psql -U accusys -d momentry -h localhost 2>/dev/null || echo "Note: Run manually if psql not available"
|
||||
WITH duplicates AS (
|
||||
SELECT tn1.id as delete_id
|
||||
FROM tkg_nodes tn1
|
||||
JOIN tkg_nodes tn2 ON tn1.file_uuid = tn2.file_uuid
|
||||
AND tn1.node_type = tn2.node_type
|
||||
AND tn1.properties->>'trace_id' = tn2.properties->>'trace_id'
|
||||
AND tn1.id < tn2.id
|
||||
WHERE tn1.file_uuid = '$uuid'
|
||||
AND tn1.node_type = 'face_track'
|
||||
AND tn1.external_id LIKE 'face_track_%'
|
||||
AND tn2.external_id LIKE 'trace_%'
|
||||
)
|
||||
DELETE FROM tkg_nodes WHERE id IN (SELECT delete_id FROM duplicates);
|
||||
|
||||
-- Show remaining nodes
|
||||
SELECT external_id, label, properties->>'trace_id' as trace_id
|
||||
FROM tkg_nodes
|
||||
WHERE file_uuid = '$uuid' AND node_type = 'face_track'
|
||||
ORDER BY (properties->>'trace_id')::int;
|
||||
SQL
|
||||
|
||||
echo "✅ Cleanup complete for $uuid"
|
||||
}
|
||||
|
||||
if [ -n "$1" ]; then
|
||||
cleanup_duplicates "$1"
|
||||
else
|
||||
echo "Usage: $0 <file_uuid>"
|
||||
echo "Example: $0 b496a68800f90770aa7246c877f38985"
|
||||
fi
|
||||
+2
-1
@@ -1363,7 +1363,8 @@ pub async fn get_face_groups_handler(
|
||||
.unwrap_or_else(crate::core::file_profile::FaceFilterSettings::defaults);
|
||||
|
||||
let rows: Vec<(String, i32, Option<i32>)> = sqlx::query_as(&format!(
|
||||
"SELECT COALESCE(tp.name, tn.label) as name, \
|
||||
"SELECT DISTINCT ON (COALESCE(tp.trace_id, (tn.properties->>'trace_id')::int)) \
|
||||
COALESCE(tp.name, tn.label) as name, \
|
||||
COALESCE(tp.trace_id, (tn.properties->>'trace_id')::int) as trace_id, \
|
||||
tp.frame_count \
|
||||
FROM {} tn \
|
||||
|
||||
+36
-5
@@ -112,6 +112,9 @@ pub async fn upsert_trace_profile(
|
||||
/// when a group is (re)named but its tkg_nodes row was previously removed,
|
||||
/// re-insert a default node so face-groups/cluster-results queries (which
|
||||
/// read FROM tkg_nodes) keep working. Keeps an existing node untouched.
|
||||
///
|
||||
/// FIX: Prevent duplicate nodes by checking for both external_id formats
|
||||
/// and only creating one node per trace_id.
|
||||
pub async fn ensure_face_track_node(
|
||||
pool: &PgPool,
|
||||
file_uuid: &str,
|
||||
@@ -123,17 +126,45 @@ pub async fn ensure_face_track_node(
|
||||
let ext_id_trace = format!("trace_{}", trace_id);
|
||||
let props = serde_json::json!({ "trace_id": trace_id });
|
||||
|
||||
let existing: Option<(i64,)> = sqlx::query_as(&format!(
|
||||
"SELECT id FROM {tkg_table} WHERE file_uuid = $1 AND node_type = 'face_track' \
|
||||
AND (external_id = $2 OR external_id = $3) LIMIT 1"
|
||||
// Check for existing nodes with either external_id format
|
||||
let existing: Vec<(i64, String)> = sqlx::query_as(&format!(
|
||||
"SELECT id, external_id FROM {tkg_table} WHERE file_uuid = $1 AND node_type = 'face_track' \
|
||||
AND (external_id = $2 OR external_id = $3)",
|
||||
))
|
||||
.bind(file_uuid)
|
||||
.bind(&ext_id_face)
|
||||
.bind(&ext_id_trace)
|
||||
.fetch_optional(pool)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
if let Some((node_id,)) = existing {
|
||||
if existing.len() >= 2 {
|
||||
// Duplicate nodes exist - remove the face_track_* variant, keep trace_*
|
||||
let to_delete: Vec<i64> = existing.iter()
|
||||
.filter(|(_, ext_id)| ext_id.starts_with("face_track_"))
|
||||
.map(|(id, _)| *id)
|
||||
.collect();
|
||||
|
||||
for node_id in to_delete {
|
||||
let _ = sqlx::query(&format!("DELETE FROM {tkg_table} WHERE id = $1"))
|
||||
.bind(node_id)
|
||||
.execute(pool)
|
||||
.await;
|
||||
}
|
||||
|
||||
// Update the remaining node
|
||||
let remaining = existing.iter().find(|(_, ext_id)| ext_id.starts_with("trace_"));
|
||||
if let Some((node_id, _)) = remaining {
|
||||
let res = sqlx::query(&format!(
|
||||
"UPDATE {tkg_table} SET label = $1, properties = $2 WHERE id = $3"
|
||||
))
|
||||
.bind(name)
|
||||
.bind(&props)
|
||||
.bind(node_id)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
return Ok(res.rows_affected());
|
||||
}
|
||||
} else if let Some((node_id, _)) = existing.first() {
|
||||
// Node exists - update the label
|
||||
let res = sqlx::query(&format!(
|
||||
"UPDATE {tkg_table} SET label = $1, properties = $2 WHERE id = $3"
|
||||
|
||||
Reference in New Issue
Block a user