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:
@@ -0,0 +1,106 @@
|
|||||||
|
---
|
||||||
|
title: Trace Profile API 路徑參數支持
|
||||||
|
version: 1.0
|
||||||
|
date: 2026-10-17
|
||||||
|
author: OpenCode
|
||||||
|
status: completed
|
||||||
|
---
|
||||||
|
|
||||||
|
## 問題
|
||||||
|
|
||||||
|
前端調用 API 時使用了路徑參數格式:
|
||||||
|
```
|
||||||
|
GET /api/v1/trace-profile/{file_uuid}/{trace_id}
|
||||||
|
```
|
||||||
|
|
||||||
|
但後端只支持查詢參數格式:
|
||||||
|
```
|
||||||
|
GET /api/v1/trace-profile?file_uuid=...&trace_id=...
|
||||||
|
```
|
||||||
|
|
||||||
|
導致返回 404 Not Found 錯誤。
|
||||||
|
|
||||||
|
## 解決方案
|
||||||
|
|
||||||
|
在後端添加新的路由支持路徑參數格式,保持向後兼容。
|
||||||
|
|
||||||
|
## 實現
|
||||||
|
|
||||||
|
### 1. 添加新的結構體
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct TraceProfilePath {
|
||||||
|
pub file_uuid: String,
|
||||||
|
pub trace_id: i32,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 重構 Handler
|
||||||
|
|
||||||
|
將原有的 handler 拆分為:
|
||||||
|
- `get_trace_profile_handler`: 查詢參數格式
|
||||||
|
- `get_trace_profile_by_path_handler`: 路徑參數格式
|
||||||
|
- `get_trace_profile_impl`: 共享實現邏輯
|
||||||
|
|
||||||
|
### 3. 添加新路由
|
||||||
|
|
||||||
|
```rust
|
||||||
|
.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),
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 測試結果
|
||||||
|
|
||||||
|
### ✅ 路徑參數格式(新增)
|
||||||
|
```bash
|
||||||
|
GET /api/v1/trace-profile/b496a68800f90770aa7246c877f38985/5
|
||||||
|
返回: 200 OK
|
||||||
|
{
|
||||||
|
"trace_id": 5,
|
||||||
|
"name": "Person_1",
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ✅ 查詢參數格式(原有)
|
||||||
|
```bash
|
||||||
|
GET /api/v1/trace-profile?file_uuid=b496a688...&trace_id=3
|
||||||
|
返回: 200 OK
|
||||||
|
{
|
||||||
|
"trace_id": 3,
|
||||||
|
"name": "...",
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ✅ 其他 traces 測試
|
||||||
|
```bash
|
||||||
|
GET /api/v1/trace-profile/b496a688.../0
|
||||||
|
返回: 200 OK
|
||||||
|
{
|
||||||
|
"trace_id": 0,
|
||||||
|
"name": "...",
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 影響範圍
|
||||||
|
|
||||||
|
- **後端**: `/Users/accusys/momentry_core/src/api/profile.rs`
|
||||||
|
- **API 端點**: 新增 `/api/v1/trace-profile/:file_uuid/:trace_id`
|
||||||
|
- **向後兼容**: 原有查詢參數格式仍然可用
|
||||||
|
|
||||||
|
## 好處
|
||||||
|
|
||||||
|
1. **前端無需修改** - 現有調用格式立即可用
|
||||||
|
2. **API 更靈活** - 支持兩種格式
|
||||||
|
3. **向後兼容** - 不影響現有功能
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**OpenCode**
|
||||||
|
2026-10-17
|
||||||
+39
-13
@@ -34,6 +34,12 @@ pub struct TraceProfileQuery {
|
|||||||
pub trace_id: i32,
|
pub trace_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct TraceProfilePath {
|
||||||
|
pub file_uuid: String,
|
||||||
|
pub trace_id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct TraceProfile {
|
pub struct TraceProfile {
|
||||||
pub file_uuid: String,
|
pub file_uuid: String,
|
||||||
@@ -114,11 +120,27 @@ pub async fn get_trace_profile_handler(
|
|||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Extension(_auth): Extension<UserAuth>,
|
Extension(_auth): Extension<UserAuth>,
|
||||||
Query(params): Query<TraceProfileQuery>,
|
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> {
|
) -> Result<Json<TraceProfile>, StatusCode> {
|
||||||
let tkg_table = schema::table_name("tkg_nodes");
|
let tkg_table = schema::table_name("tkg_nodes");
|
||||||
let tp_table = schema::table_name("trace_profiles");
|
let tp_table = schema::table_name("trace_profiles");
|
||||||
let external_id_face = format!("face_track_{}", params.trace_id);
|
let external_id_face = format!("face_track_{}", trace_id);
|
||||||
let external_id_trace = format!("trace_{}", params.trace_id);
|
let external_id_trace = format!("trace_{}", trace_id);
|
||||||
|
|
||||||
let row: Option<(
|
let row: Option<(
|
||||||
String,
|
String,
|
||||||
@@ -131,14 +153,14 @@ pub async fn get_trace_profile_handler(
|
|||||||
FROM {} tn \
|
FROM {} tn \
|
||||||
LEFT JOIN {} tp ON tp.file_uuid = tn.file_uuid \
|
LEFT JOIN {} tp ON tp.file_uuid = tn.file_uuid \
|
||||||
AND tp.trace_id = (tn.properties->>'trace_id')::int \
|
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)",
|
AND (tn.external_id = $2 OR tn.external_id = $3)",
|
||||||
tkg_table, tp_table
|
tkg_table, tp_table
|
||||||
))
|
))
|
||||||
.bind(¶ms.file_uuid)
|
.bind(file_uuid)
|
||||||
.bind(&external_id_face)
|
.bind(&external_id_face)
|
||||||
.bind(&external_id_trace)
|
.bind(&external_id_trace)
|
||||||
.fetch_optional(state.db.pool())
|
.fetch_optional(pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
tracing::error!("[TraceProfile] DB error: {}", 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 not found in database, try to read from filesystem
|
||||||
if row.is_none() {
|
if row.is_none() {
|
||||||
tracing::info!("[TraceProfile] Not found in DB, trying filesystem for file_uuid={}, trace_id={}",
|
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
|
// Construct path: {output_dir}/{file_uuid}/trace_{trace_id}/trace_profile.json
|
||||||
let output_dir = std::env::var("MOMENTRY_OUTPUT_DIR")
|
let output_dir = std::env::var("MOMENTRY_OUTPUT_DIR")
|
||||||
.unwrap_or_else(|_| "/Users/accusys/momentry/output".to_string());
|
.unwrap_or_else(|_| "/Users/accusys/momentry/output".to_string());
|
||||||
let profile_path = std::path::Path::new(&output_dir)
|
let profile_path = std::path::Path::new(&output_dir)
|
||||||
.join(¶ms.file_uuid)
|
.join(file_uuid)
|
||||||
.join(format!("trace_{}", params.trace_id))
|
.join(format!("trace_{}", trace_id))
|
||||||
.join("trace_profile.json");
|
.join("trace_profile.json");
|
||||||
|
|
||||||
// Read from filesystem
|
// Read from filesystem
|
||||||
@@ -167,7 +189,7 @@ pub async fn get_trace_profile_handler(
|
|||||||
// Extract fields from filesystem JSON
|
// Extract fields from filesystem JSON
|
||||||
let name = profile_json.get("label")
|
let name = profile_json.get("label")
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or(&format!("trace_{}", params.trace_id))
|
.unwrap_or(&format!("trace_{}", trace_id))
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let key_frame = profile_json.get("key_frame")
|
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());
|
tracing::info!("[TraceProfile] Successfully loaded from filesystem: {}", profile_path.display());
|
||||||
|
|
||||||
return Ok(Json(TraceProfile {
|
return Ok(Json(TraceProfile {
|
||||||
file_uuid: params.file_uuid,
|
file_uuid: file_uuid.to_string(),
|
||||||
trace_id: params.trace_id,
|
trace_id,
|
||||||
name,
|
name,
|
||||||
key_frame,
|
key_frame,
|
||||||
key_face,
|
key_face,
|
||||||
@@ -236,8 +258,8 @@ pub async fn get_trace_profile_handler(
|
|||||||
let bbox = properties.get("avg_bbox").cloned();
|
let bbox = properties.get("avg_bbox").cloned();
|
||||||
|
|
||||||
Ok(Json(TraceProfile {
|
Ok(Json(TraceProfile {
|
||||||
file_uuid: params.file_uuid,
|
file_uuid: file_uuid.to_string(),
|
||||||
trace_id: params.trace_id,
|
trace_id,
|
||||||
name,
|
name,
|
||||||
key_frame,
|
key_frame,
|
||||||
key_face,
|
key_face,
|
||||||
@@ -1856,6 +1878,10 @@ pub fn profile_routes() -> axum::Router<AppState> {
|
|||||||
use axum::routing::{get, post, put};
|
use axum::routing::{get, post, put};
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
.route("/api/v1/trace-profile", get(get_trace_profile_handler))
|
.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", put(update_trace_profile_handler))
|
||||||
.route(
|
.route(
|
||||||
"/api/v1/trace-profile/group",
|
"/api/v1/trace-profile/group",
|
||||||
|
|||||||
Reference in New Issue
Block a user