2026-03-31 19:19:04 +08:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
|
|
|
|
|
use axum::extract::{Path, Query, State};
|
|
|
|
|
use axum::response::IntoResponse;
|
|
|
|
|
use axum::Json;
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
2026-04-07 02:50:19 +08:00
|
|
|
use crate::{AppState, GatewayError};
|
2026-04-05 20:23:16 +08:00
|
|
|
use aether_data::repository::audit::RequestAuditBundle;
|
2026-04-07 02:50:19 +08:00
|
|
|
use aether_data_contracts::repository::usage::StoredRequestUsageAudit;
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub(crate) struct GetRequestAuditBundleQuery {
|
|
|
|
|
pub(crate) attempted_only: Option<bool>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) async fn get_request_usage_audit(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Path(request_id): Path<String>,
|
2026-04-05 20:23:16 +08:00
|
|
|
) -> Result<Json<StoredRequestUsageAudit>, axum::response::Response> {
|
2026-03-31 19:19:04 +08:00
|
|
|
let usage = state
|
2026-04-07 02:50:19 +08:00
|
|
|
.data
|
2026-03-31 19:19:04 +08:00
|
|
|
.read_request_usage_audit(&request_id)
|
|
|
|
|
.await
|
2026-04-07 02:50:19 +08:00
|
|
|
.map_err(|err| GatewayError::Internal(err.to_string()).into_response())?;
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
match usage {
|
|
|
|
|
Some(usage) => Ok(Json(usage)),
|
|
|
|
|
None => Err((
|
|
|
|
|
axum::http::StatusCode::NOT_FOUND,
|
|
|
|
|
Json(json!({
|
|
|
|
|
"error": {
|
|
|
|
|
"message": "Request usage not found",
|
|
|
|
|
}
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
.into_response()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) async fn get_request_audit_bundle(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Path(request_id): Path<String>,
|
|
|
|
|
Query(query): Query<GetRequestAuditBundleQuery>,
|
2026-04-05 20:23:16 +08:00
|
|
|
) -> Result<Json<RequestAuditBundle>, axum::response::Response> {
|
2026-03-31 19:19:04 +08:00
|
|
|
let attempted_only = query.attempted_only.unwrap_or(false);
|
|
|
|
|
let bundle = state
|
2026-04-07 02:50:19 +08:00
|
|
|
.data
|
2026-03-31 19:19:04 +08:00
|
|
|
.read_request_audit_bundle(&request_id, attempted_only, current_unix_secs())
|
|
|
|
|
.await
|
2026-04-07 02:50:19 +08:00
|
|
|
.map_err(|err| GatewayError::Internal(err.to_string()).into_response())?;
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
match bundle {
|
|
|
|
|
Some(bundle) => Ok(Json(bundle)),
|
|
|
|
|
None => Err((
|
|
|
|
|
axum::http::StatusCode::NOT_FOUND,
|
|
|
|
|
Json(json!({
|
|
|
|
|
"error": {
|
|
|
|
|
"message": "Request audit bundle not found",
|
|
|
|
|
}
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
.into_response()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn current_unix_secs() -> u64 {
|
|
|
|
|
SystemTime::now()
|
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
.as_secs()
|
|
|
|
|
}
|