feat: add trace profile API path parameter support
- Add TraceProfilePath struct for path parameters - Refactor get_trace_profile_handler to support both formats: - Query parameters: /api/v1/trace-profile?file_uuid=...&trace_id=... - Path parameters: /api/v1/trace-profile/:file_uuid/:trace_id - Maintain backward compatibility with existing query parameter format - Fix 404 error when Studio calls trace profile API with path format Fixes: Studio right-click key frame trace profile error Deployment: Released to production (3002) on 2026-10-17
This commit is contained in:
+40
-14
@@ -34,6 +34,12 @@ pub struct TraceProfileQuery {
|
||||
pub trace_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct TraceProfilePath {
|
||||
pub file_uuid: String,
|
||||
pub trace_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct TraceProfile {
|
||||
pub file_uuid: String,
|
||||
@@ -114,13 +120,29 @@ pub async fn get_trace_profile_handler(
|
||||
State(state): State<AppState>,
|
||||
Extension(_auth): Extension<UserAuth>,
|
||||
Query(params): Query<TraceProfileQuery>,
|
||||
) -> Result<Json<TraceProfile>, StatusCode> {
|
||||
get_trace_profile_impl(state.db.pool(), ¶ms.file_uuid, params.trace_id).await
|
||||
}
|
||||
|
||||
pub async fn get_trace_profile_by_path_handler(
|
||||
State(state): State<AppState>,
|
||||
Extension(_auth): Extension<UserAuth>,
|
||||
Path(params): Path<TraceProfilePath>,
|
||||
) -> Result<Json<TraceProfile>, StatusCode> {
|
||||
get_trace_profile_impl(state.db.pool(), ¶ms.file_uuid, params.trace_id).await
|
||||
}
|
||||
|
||||
pub async fn get_trace_profile_impl(
|
||||
pool: &PgPool,
|
||||
file_uuid: &str,
|
||||
trace_id: i32,
|
||||
) -> Result<Json<TraceProfile>, StatusCode> {
|
||||
let tkg_table = schema::table_name("tkg_nodes");
|
||||
let tp_table = schema::table_name("trace_profiles");
|
||||
let external_id_face = format!("face_track_{}", params.trace_id);
|
||||
let external_id_trace = format!("trace_{}", params.trace_id);
|
||||
let external_id_face = format!("face_track_{}", trace_id);
|
||||
let external_id_trace = format!("trace_{}", trace_id);
|
||||
|
||||
let row: Option<(
|
||||
let row: Option<(
|
||||
String,
|
||||
String,
|
||||
Option<serde_json::Value>,
|
||||
@@ -131,14 +153,14 @@ pub async fn get_trace_profile_handler(
|
||||
FROM {} tn \
|
||||
LEFT JOIN {} tp ON tp.file_uuid = tn.file_uuid \
|
||||
AND tp.trace_id = (tn.properties->>'trace_id')::int \
|
||||
WHERE tn.file_uuid = $1 AND tn.node_type = 'face_track' \
|
||||
WHERE tn.file_uuid = $1 \
|
||||
AND (tn.external_id = $2 OR tn.external_id = $3)",
|
||||
tkg_table, tp_table
|
||||
))
|
||||
.bind(¶ms.file_uuid)
|
||||
.bind(file_uuid)
|
||||
.bind(&external_id_face)
|
||||
.bind(&external_id_trace)
|
||||
.fetch_optional(state.db.pool())
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::error!("[TraceProfile] DB error: {}", e);
|
||||
@@ -148,14 +170,14 @@ pub async fn get_trace_profile_handler(
|
||||
// If not found in database, try to read from filesystem
|
||||
if row.is_none() {
|
||||
tracing::info!("[TraceProfile] Not found in DB, trying filesystem for file_uuid={}, trace_id={}",
|
||||
params.file_uuid, params.trace_id);
|
||||
file_uuid, trace_id);
|
||||
|
||||
// Construct path: {output_dir}/{file_uuid}/trace_{trace_id}/trace_profile.json
|
||||
let output_dir = std::env::var("MOMENTRY_OUTPUT_DIR")
|
||||
.unwrap_or_else(|_| "/Users/accusys/momentry/output".to_string());
|
||||
let profile_path = std::path::Path::new(&output_dir)
|
||||
.join(¶ms.file_uuid)
|
||||
.join(format!("trace_{}", params.trace_id))
|
||||
.join(file_uuid)
|
||||
.join(format!("trace_{}", trace_id))
|
||||
.join("trace_profile.json");
|
||||
|
||||
// Read from filesystem
|
||||
@@ -167,7 +189,7 @@ pub async fn get_trace_profile_handler(
|
||||
// Extract fields from filesystem JSON
|
||||
let name = profile_json.get("label")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or(&format!("trace_{}", params.trace_id))
|
||||
.unwrap_or(&format!("trace_{}", trace_id))
|
||||
.to_string();
|
||||
|
||||
let key_frame = profile_json.get("key_frame")
|
||||
@@ -183,8 +205,8 @@ pub async fn get_trace_profile_handler(
|
||||
tracing::info!("[TraceProfile] Successfully loaded from filesystem: {}", profile_path.display());
|
||||
|
||||
return Ok(Json(TraceProfile {
|
||||
file_uuid: params.file_uuid,
|
||||
trace_id: params.trace_id,
|
||||
file_uuid: file_uuid.to_string(),
|
||||
trace_id,
|
||||
name,
|
||||
key_frame,
|
||||
key_face,
|
||||
@@ -236,8 +258,8 @@ pub async fn get_trace_profile_handler(
|
||||
let bbox = properties.get("avg_bbox").cloned();
|
||||
|
||||
Ok(Json(TraceProfile {
|
||||
file_uuid: params.file_uuid,
|
||||
trace_id: params.trace_id,
|
||||
file_uuid: file_uuid.to_string(),
|
||||
trace_id,
|
||||
name,
|
||||
key_frame,
|
||||
key_face,
|
||||
@@ -1856,6 +1878,10 @@ pub fn profile_routes() -> axum::Router<AppState> {
|
||||
use axum::routing::{get, post, put};
|
||||
axum::Router::new()
|
||||
.route("/api/v1/trace-profile", get(get_trace_profile_handler))
|
||||
.route(
|
||||
"/api/v1/trace-profile/:file_uuid/:trace_id",
|
||||
get(get_trace_profile_by_path_handler),
|
||||
)
|
||||
.route("/api/v1/trace-profile", put(update_trace_profile_handler))
|
||||
.route(
|
||||
"/api/v1/trace-profile/group",
|
||||
|
||||
Reference in New Issue
Block a user