mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 01:40:21 +08:00
feat(gateway): 重构 usage 数据层、迁移系统与系统导入
数据库迁移: - 引入 baseline v2 bootstrap,空库首次启动自动初始化 - 服务启动不再自动执行迁移,需显式 `--migrate` 运行 - 新增 pending migration 检测,schema 落后时拒绝启动 Usage 数据层: - usage body 存储外部化为独立 blob 表 - 新增 HTTP audit 表拆分存储请求/响应头与 body ref - 后台清理任务支持 legacy body ref 元数据迁移 - usage runtime 写入迁移到专用 tokio runtime(独立线程池, 8MB 栈) 系统导入/导出: - 支持用户、API Keys、钱包数据的完整导入 - 兼容 legacy 与 v1.3+ 两种导出格式 其他改进: - executor outcome 增加 runtime miss 诊断上下文 - 主 tokio runtime 栈大小调整为 8MB - 前端 provider 管理支持 base URL 配置 - dev.sh 支持 --migrate 参数
This commit is contained in:
@@ -2156,6 +2156,37 @@ fn sample_auth_session(
|
||||
.expect("auth session should build")
|
||||
}
|
||||
|
||||
fn sample_usage_auth_snapshot(
|
||||
api_key_id: &str,
|
||||
user_id: &str,
|
||||
api_key_name: &str,
|
||||
) -> StoredAuthApiKeySnapshot {
|
||||
StoredAuthApiKeySnapshot::new(
|
||||
user_id.to_string(),
|
||||
"alice".to_string(),
|
||||
Some("alice@example.com".to_string()),
|
||||
"user".to_string(),
|
||||
"local".to_string(),
|
||||
true,
|
||||
false,
|
||||
Some(json!(["openai"])),
|
||||
Some(json!(["openai:chat"])),
|
||||
Some(json!(["gpt-5"])),
|
||||
api_key_id.to_string(),
|
||||
Some(api_key_name.to_string()),
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
Some(60),
|
||||
Some(5),
|
||||
None,
|
||||
Some(json!(["openai"])),
|
||||
Some(json!(["openai:chat"])),
|
||||
Some(json!(["gpt-5"])),
|
||||
)
|
||||
.expect("auth api key snapshot should build")
|
||||
}
|
||||
|
||||
async fn start_auth_gateway_with_state(
|
||||
user: StoredUserAuthRecord,
|
||||
wallet: StoredWalletSnapshot,
|
||||
@@ -4671,6 +4702,22 @@ async fn gateway_handles_users_me_usage_locally_without_proxying_upstream() {
|
||||
]),
|
||||
now + chrono::Duration::hours(1),
|
||||
);
|
||||
let mut streaming_usage = sample_user_usage_audit(
|
||||
"usage-users-me-streaming-1",
|
||||
"req-users-me-streaming-1",
|
||||
"user-auth-1",
|
||||
"gpt-4.1-mini",
|
||||
"OpenAI",
|
||||
"streaming",
|
||||
now - chrono::Duration::minutes(5),
|
||||
);
|
||||
streaming_usage.request_metadata = Some(json!({
|
||||
"rate_multiplier": 0.5,
|
||||
"input_price_per_1m": 3.0,
|
||||
"output_price_per_1m": 9.0,
|
||||
"cache_creation_price_per_1m": 3.75,
|
||||
"cache_read_price_per_1m": 0.3,
|
||||
}));
|
||||
let usage_repository = Arc::new(InMemoryUsageReadRepository::seed(vec![
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-completed-1",
|
||||
@@ -4690,34 +4737,41 @@ async fn gateway_handles_users_me_usage_locally_without_proxying_upstream() {
|
||||
"failed",
|
||||
now - chrono::Duration::minutes(10),
|
||||
),
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-streaming-1",
|
||||
"req-users-me-streaming-1",
|
||||
"user-auth-1",
|
||||
"gpt-4.1-mini",
|
||||
"OpenAI",
|
||||
"streaming",
|
||||
now - chrono::Duration::minutes(5),
|
||||
),
|
||||
streaming_usage,
|
||||
]));
|
||||
let auth_repository = Arc::new(InMemoryAuthApiKeySnapshotRepository::seed(vec![(
|
||||
Some("hash-api-key-user-1".to_string()),
|
||||
sample_usage_auth_snapshot("api-key-user-1", "user-auth-1", "renamed-key"),
|
||||
)]));
|
||||
let (gateway_url, upstream_hits, gateway_handle, upstream_handle) =
|
||||
start_auth_gateway_with_usage_state(
|
||||
user,
|
||||
sample_auth_wallet("user-auth-1", now),
|
||||
[sample_auth_session(
|
||||
"user-auth-1",
|
||||
"session-users-me-usage-1",
|
||||
"device-users-me-usage-1",
|
||||
"refresh-token-placeholder",
|
||||
now,
|
||||
)],
|
||||
usage_repository,
|
||||
)
|
||||
start_auth_gateway_with_builder(|| {
|
||||
let data_state = crate::data::GatewayDataState::with_user_wallet_and_usage_for_tests(
|
||||
Arc::new(InMemoryUserReadRepository::seed_auth_users(vec![
|
||||
user.clone()
|
||||
])),
|
||||
Arc::new(InMemoryWalletRepository::seed(vec![sample_auth_wallet(
|
||||
"user-auth-1",
|
||||
now,
|
||||
)])),
|
||||
Arc::clone(&usage_repository),
|
||||
)
|
||||
.with_auth_api_key_reader(auth_repository);
|
||||
AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(data_state)
|
||||
.with_auth_sessions_for_tests([sample_auth_session(
|
||||
"user-auth-1",
|
||||
"session-users-me-usage-1",
|
||||
"device-users-me-usage-1",
|
||||
"refresh-token-placeholder",
|
||||
now,
|
||||
)])
|
||||
})
|
||||
.await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!(
|
||||
"{gateway_url}/api/users/me/usage?limit=10&offset=0"
|
||||
"{gateway_url}/api/users/me/usage?limit=10&offset=0&search=renamed-key"
|
||||
))
|
||||
.header("authorization", format!("Bearer {access_token}"))
|
||||
.header("x-client-device-id", "device-users-me-usage-1")
|
||||
@@ -4744,6 +4798,12 @@ async fn gateway_handles_users_me_usage_locally_without_proxying_upstream() {
|
||||
payload["records"][0]["cache_creation_ephemeral_1h_input_tokens"],
|
||||
6
|
||||
);
|
||||
assert_eq!(payload["records"][0]["input_price_per_1m"], 3.0);
|
||||
assert_eq!(payload["records"][0]["output_price_per_1m"], 9.0);
|
||||
assert_eq!(payload["records"][0]["cache_creation_price_per_1m"], 3.75);
|
||||
assert_eq!(payload["records"][0]["cache_read_price_per_1m"], 0.3);
|
||||
assert_eq!(payload["records"][0]["api_key"]["name"], "renamed-key");
|
||||
assert_eq!(payload["records"][0]["api_key"]["display"], "renamed-key");
|
||||
assert_eq!(
|
||||
payload["summary_by_model"]
|
||||
.as_array()
|
||||
@@ -4771,6 +4831,91 @@ async fn gateway_handles_users_me_usage_locally_without_proxying_upstream() {
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_users_me_usage_without_legacy_api_key_name_fallback_when_auth_reader_exists(
|
||||
) {
|
||||
let now = Utc::now();
|
||||
let user = sample_auth_user(now);
|
||||
let access_token = build_test_auth_token(
|
||||
"access",
|
||||
serde_json::Map::from_iter([
|
||||
("user_id".to_string(), json!(user.id)),
|
||||
("role".to_string(), json!(user.role)),
|
||||
(
|
||||
"created_at".to_string(),
|
||||
json!(user.created_at.map(|value| value.to_rfc3339())),
|
||||
),
|
||||
(
|
||||
"session_id".to_string(),
|
||||
json!("session-users-me-usage-no-legacy-fallback"),
|
||||
),
|
||||
]),
|
||||
now + chrono::Duration::hours(1),
|
||||
);
|
||||
let usage_repository = Arc::new(InMemoryUsageReadRepository::seed(vec![
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-completed-no-legacy-fallback",
|
||||
"req-users-me-completed-no-legacy-fallback",
|
||||
"user-auth-1",
|
||||
"gpt-4.1",
|
||||
"OpenAI",
|
||||
"completed",
|
||||
now - chrono::Duration::minutes(20),
|
||||
),
|
||||
]));
|
||||
let (gateway_url, upstream_hits, gateway_handle, upstream_handle) =
|
||||
start_auth_gateway_with_builder(|| {
|
||||
let data_state = crate::data::GatewayDataState::with_user_wallet_and_usage_for_tests(
|
||||
Arc::new(InMemoryUserReadRepository::seed_auth_users(vec![
|
||||
user.clone()
|
||||
])),
|
||||
Arc::new(InMemoryWalletRepository::seed(vec![sample_auth_wallet(
|
||||
"user-auth-1",
|
||||
now,
|
||||
)])),
|
||||
Arc::clone(&usage_repository),
|
||||
)
|
||||
.with_auth_api_key_reader(Arc::new(InMemoryAuthApiKeySnapshotRepository::seed(vec![])));
|
||||
AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(data_state)
|
||||
.with_auth_sessions_for_tests([sample_auth_session(
|
||||
"user-auth-1",
|
||||
"session-users-me-usage-no-legacy-fallback",
|
||||
"device-users-me-usage-no-legacy-fallback",
|
||||
"refresh-token-users-me-usage-no-legacy-fallback",
|
||||
now,
|
||||
)])
|
||||
})
|
||||
.await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!(
|
||||
"{gateway_url}/api/users/me/usage?limit=10&offset=0&search=default"
|
||||
))
|
||||
.header("authorization", format!("Bearer {access_token}"))
|
||||
.header(
|
||||
"x-client-device-id",
|
||||
"device-users-me-usage-no-legacy-fallback",
|
||||
)
|
||||
.header("user-agent", "AetherTest/1.0")
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = response.json().await.expect("json body should parse");
|
||||
assert_eq!(payload["pagination"]["total"], 0);
|
||||
assert_eq!(
|
||||
payload["records"].as_array().expect("records array").len(),
|
||||
0
|
||||
);
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_users_me_usage_active_locally_without_proxying_upstream() {
|
||||
let now = Utc::now();
|
||||
@@ -4788,6 +4933,18 @@ async fn gateway_handles_users_me_usage_active_locally_without_proxying_upstream
|
||||
]),
|
||||
now + chrono::Duration::hours(1),
|
||||
);
|
||||
let mut streaming_usage = sample_user_usage_audit(
|
||||
"usage-users-me-streaming-1",
|
||||
"req-users-me-streaming-1",
|
||||
"user-auth-1",
|
||||
"gpt-4.1-mini",
|
||||
"OpenAI",
|
||||
"streaming",
|
||||
now - chrono::Duration::minutes(2),
|
||||
);
|
||||
streaming_usage.request_metadata = Some(json!({
|
||||
"rate_multiplier": 0.5,
|
||||
}));
|
||||
let usage_repository = Arc::new(InMemoryUsageReadRepository::seed(vec![
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-pending-1",
|
||||
@@ -4798,15 +4955,7 @@ async fn gateway_handles_users_me_usage_active_locally_without_proxying_upstream
|
||||
"pending",
|
||||
now - chrono::Duration::minutes(4),
|
||||
),
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-streaming-1",
|
||||
"req-users-me-streaming-1",
|
||||
"user-auth-1",
|
||||
"gpt-4.1-mini",
|
||||
"OpenAI",
|
||||
"streaming",
|
||||
now - chrono::Duration::minutes(2),
|
||||
),
|
||||
streaming_usage,
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-completed-1",
|
||||
"req-users-me-completed-1",
|
||||
@@ -4846,6 +4995,7 @@ async fn gateway_handles_users_me_usage_active_locally_without_proxying_upstream
|
||||
let requests = payload["requests"].as_array().expect("requests array");
|
||||
assert_eq!(requests.len(), 2);
|
||||
assert_eq!(requests[0]["status"], "streaming");
|
||||
assert_eq!(requests[0]["rate_multiplier"], 0.5);
|
||||
assert_eq!(requests[0]["cache_creation_ephemeral_5m_input_tokens"], 4);
|
||||
assert_eq!(requests[0]["cache_creation_ephemeral_1h_input_tokens"], 6);
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
@@ -641,6 +641,94 @@ async fn gateway_handles_dashboard_recent_requests_locally_without_proxying_upst
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_dashboard_recent_requests_with_auth_user_fallback() {
|
||||
let now = stable_dashboard_now();
|
||||
let user = sample_auth_user(now);
|
||||
let access_token = build_test_auth_token(
|
||||
"access",
|
||||
serde_json::Map::from_iter([
|
||||
("user_id".to_string(), json!(user.id)),
|
||||
("role".to_string(), json!(user.role)),
|
||||
(
|
||||
"created_at".to_string(),
|
||||
json!(user.created_at.map(|value| value.to_rfc3339())),
|
||||
),
|
||||
(
|
||||
"session_id".to_string(),
|
||||
json!("session-dashboard-recent-auth-fallback"),
|
||||
),
|
||||
]),
|
||||
chrono::Utc::now() + chrono::Duration::hours(1),
|
||||
);
|
||||
let session = sample_auth_session(
|
||||
"user-auth-1",
|
||||
"session-dashboard-recent-auth-fallback",
|
||||
"device-dashboard-recent-auth-fallback",
|
||||
"refresh-dashboard-recent-auth-fallback",
|
||||
now,
|
||||
);
|
||||
let mut usage = sample_user_usage_audit(
|
||||
"usage-dashboard-auth-fallback-1",
|
||||
"req-dashboard-auth-fallback-1",
|
||||
"user-auth-1",
|
||||
"gpt-5",
|
||||
"OpenAI",
|
||||
"completed",
|
||||
now - chrono::Duration::minutes(5),
|
||||
);
|
||||
usage.username = Some("stale-alice".to_string());
|
||||
let usage_repository = Arc::new(InMemoryUsageReadRepository::seed(vec![usage]));
|
||||
|
||||
let (gateway_url, upstream_hits, gateway_handle, upstream_handle) =
|
||||
start_auth_gateway_with_builder(|| {
|
||||
let wallet_repository =
|
||||
Arc::new(InMemoryWalletRepository::seed(vec![sample_auth_wallet(
|
||||
"user-auth-1",
|
||||
now,
|
||||
)]));
|
||||
let data_state = GatewayDataState::with_user_wallet_and_usage_for_tests(
|
||||
Arc::new(InMemoryUserReadRepository::seed_auth_users(Vec::<
|
||||
StoredUserAuthRecord,
|
||||
>::new(
|
||||
))),
|
||||
wallet_repository,
|
||||
usage_repository,
|
||||
);
|
||||
AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(data_state)
|
||||
.with_auth_users_for_tests([user.clone()])
|
||||
.with_auth_sessions_for_tests([session])
|
||||
})
|
||||
.await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!(
|
||||
"{gateway_url}/api/dashboard/recent-requests?limit=5"
|
||||
))
|
||||
.header("authorization", format!("Bearer {access_token}"))
|
||||
.header(
|
||||
"x-client-device-id",
|
||||
"device-dashboard-recent-auth-fallback",
|
||||
)
|
||||
.header("user-agent", "AetherTest/1.0")
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = response.json().await.expect("json body should parse");
|
||||
let requests = payload["requests"].as_array().expect("array");
|
||||
assert_eq!(requests.len(), 1);
|
||||
assert_eq!(requests[0]["id"], "usage-dashboard-auth-fallback-1");
|
||||
assert_eq!(requests[0]["user"], "alice");
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_dashboard_provider_status_locally_without_proxying_upstream() {
|
||||
let now = Utc::now();
|
||||
|
||||
Reference in New Issue
Block a user