mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
feat(gateway/data): 新增 usage 关键词搜索、缓存命中摘要、结算成本摘要及 dashboard 聚合查询能力
- 新增 UsageAuditKeywordSearchQuery,支持多关键词、用户名、API Key 交叉过滤 - 新增 UsageCacheHitSummaryQuery/StoredUsageCacheHitSummary,统计请求缓存命中率 - 新增 UsageSettledCostSummaryQuery/StoredUsageSettledCostSummary,汇总结算成本 - 新增 UsageDashboardSummaryQuery、UsageBreakdownSummaryQuery 等 dashboard 聚合类型 - SQL 层实现对应查询方法,含 list_by_ids、list_usage_audits_by_keyword_search、count_usage_audits_by_keyword_search - 将上述能力通过 GatewayDataState / AdminAppState 暴露给 handler 层 - user_me_usage 及 dashboard_filters 切换为新查询接口,移除旧的内存过滤逻辑 - 同步更新 memory 层及测试
This commit is contained in:
@@ -29,7 +29,6 @@ fn non_admin_handlers_do_not_depend_on_admin_stats_module() {
|
||||
let shared_mod = read_workspace_file("apps/aether-gateway/src/handlers/shared/mod.rs");
|
||||
for pattern in [
|
||||
"admin_stats_bad_request_response",
|
||||
"list_usage_for_optional_range",
|
||||
"parse_bounded_u32",
|
||||
"round_to",
|
||||
"AdminStatsTimeRange",
|
||||
@@ -40,6 +39,10 @@ fn non_admin_handlers_do_not_depend_on_admin_stats_module() {
|
||||
"handlers/shared/mod.rs should expose shared usage stats helper {pattern}"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
!shared_mod.contains("list_usage_for_optional_range"),
|
||||
"handlers/shared/mod.rs should not expose unbounded usage range helpers"
|
||||
);
|
||||
|
||||
let admin_observability_mod =
|
||||
read_workspace_file("apps/aether-gateway/src/handlers/admin/observability/mod.rs");
|
||||
@@ -57,7 +60,6 @@ fn non_admin_handlers_do_not_depend_on_admin_stats_module() {
|
||||
}
|
||||
for pattern in [
|
||||
"admin_stats_bad_request_response",
|
||||
"list_usage_for_optional_range",
|
||||
"parse_bounded_u32",
|
||||
"round_to",
|
||||
"AdminStatsTimeRange",
|
||||
@@ -68,6 +70,12 @@ fn non_admin_handlers_do_not_depend_on_admin_stats_module() {
|
||||
"handlers/admin/observability/mod.rs should expose admin stats facade helper {pattern}"
|
||||
);
|
||||
}
|
||||
for pattern in ["list_usage_for_optional_range", "list_usage_for_range"] {
|
||||
assert!(
|
||||
!admin_observability_mod.contains(pattern),
|
||||
"handlers/admin/observability/mod.rs should not re-export deprecated usage helper {pattern}"
|
||||
);
|
||||
}
|
||||
|
||||
let shared_usage_stats =
|
||||
read_workspace_file("apps/aether-gateway/src/handlers/shared/usage_stats.rs");
|
||||
@@ -193,14 +201,15 @@ fn admin_stats_root_stays_thin() {
|
||||
stats_mod.contains("pub(crate) use self::range::{"),
|
||||
"handlers/admin/observability/stats/mod.rs should re-export the split range seam"
|
||||
);
|
||||
for pattern in [
|
||||
"list_usage_for_optional_range",
|
||||
"list_usage_for_range",
|
||||
"parse_bounded_u32",
|
||||
] {
|
||||
let pattern = "parse_bounded_u32";
|
||||
assert!(
|
||||
stats_mod.contains(pattern),
|
||||
"handlers/admin/observability/stats/mod.rs should keep range re-export {pattern}"
|
||||
);
|
||||
for pattern in ["list_usage_for_optional_range", "list_usage_for_range"] {
|
||||
assert!(
|
||||
stats_mod.contains(pattern),
|
||||
"handlers/admin/observability/stats/mod.rs should keep range re-export {pattern}"
|
||||
!stats_mod.contains(pattern),
|
||||
"handlers/admin/observability/stats/mod.rs should not re-export deprecated range helper {pattern}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -471,7 +480,7 @@ fn admin_usage_root_stays_thin() {
|
||||
"mod cache_affinity;",
|
||||
"mod filters;",
|
||||
"pub(super) use aggregations::admin_usage_aggregation_by_user_json;",
|
||||
"pub(super) use cache_affinity::list_recent_completed_usage_for_cache_affinity;",
|
||||
"pub(super) use cache_affinity::list_usage_cache_affinity_intervals;",
|
||||
"pub(super) use filters::admin_usage_provider_key_names;",
|
||||
] {
|
||||
assert!(
|
||||
@@ -574,9 +583,8 @@ fn admin_usage_root_stays_thin() {
|
||||
"apps/aether-gateway/src/handlers/admin/observability/usage/analytics/cache_affinity.rs",
|
||||
);
|
||||
assert!(
|
||||
analytics_cache_affinity.contains(
|
||||
"pub(in super::super) async fn list_recent_completed_usage_for_cache_affinity("
|
||||
),
|
||||
analytics_cache_affinity
|
||||
.contains("pub(in super::super) async fn list_usage_cache_affinity_intervals("),
|
||||
"usage/analytics/cache_affinity.rs should own cache-affinity analytics helpers"
|
||||
);
|
||||
let analytics_filters = read_workspace_file(
|
||||
|
||||
@@ -161,8 +161,6 @@ fn admin_wrapped_state_owns_observability_capabilities() {
|
||||
"pub(crate) fn has_auth_api_key_data_reader(&self) -> bool",
|
||||
"pub(crate) fn has_user_data_reader(&self) -> bool",
|
||||
"pub(crate) async fn list_provider_catalog_providers(",
|
||||
"pub(crate) async fn list_admin_usage_for_range(",
|
||||
"pub(crate) async fn list_admin_usage_for_optional_range(",
|
||||
"pub(crate) async fn aggregate_finalized_request_candidate_timeline_by_endpoint_ids_since(",
|
||||
"pub(crate) async fn read_recent_request_candidates(",
|
||||
"pub(crate) fn provider_key_rpm_reset_at(",
|
||||
@@ -175,6 +173,15 @@ fn admin_wrapped_state_owns_observability_capabilities() {
|
||||
"handlers/admin/request/mod.rs should expose observability capability {pattern}"
|
||||
);
|
||||
}
|
||||
for pattern in [
|
||||
"pub(crate) async fn list_admin_usage_for_range(",
|
||||
"pub(crate) async fn list_admin_usage_for_optional_range(",
|
||||
] {
|
||||
assert!(
|
||||
!admin_request.contains(pattern),
|
||||
"handlers/admin/request/mod.rs should not expose deprecated unbounded usage helper {pattern}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -196,6 +196,15 @@ impl AuthApiKeyReadRepository for PartialListAuthApiKeyRepository {
|
||||
self.lookup.list_export_api_keys_by_ids(api_key_ids).await
|
||||
}
|
||||
|
||||
async fn list_export_api_keys_by_name_search(
|
||||
&self,
|
||||
name_search: &str,
|
||||
) -> Result<Vec<StoredAuthApiKeyExportRecord>, aether_data::DataLayerError> {
|
||||
self.lookup
|
||||
.list_export_api_keys_by_name_search(name_search)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn list_export_standalone_api_keys_page(
|
||||
&self,
|
||||
query: &StandaloneApiKeyExportListQuery,
|
||||
|
||||
@@ -4916,6 +4916,114 @@ async fn gateway_handles_users_me_usage_without_legacy_api_key_name_fallback_whe
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_users_me_usage_search_with_model_and_api_key_keywords() {
|
||||
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-multi-keyword-search"),
|
||||
),
|
||||
]),
|
||||
now + chrono::Duration::hours(1),
|
||||
);
|
||||
let usage_repository = Arc::new(InMemoryUsageReadRepository::seed(vec![
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-completed-multi-keyword-search",
|
||||
"req-users-me-completed-multi-keyword-search",
|
||||
"user-auth-1",
|
||||
"gpt-4.1",
|
||||
"OpenAI",
|
||||
"completed",
|
||||
now - chrono::Duration::minutes(20),
|
||||
),
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-failed-multi-keyword-search",
|
||||
"req-users-me-failed-multi-keyword-search",
|
||||
"user-auth-1",
|
||||
"claude-3.5-sonnet",
|
||||
"Anthropic",
|
||||
"failed",
|
||||
now - chrono::Duration::minutes(10),
|
||||
),
|
||||
sample_user_usage_audit(
|
||||
"usage-users-me-streaming-multi-keyword-search",
|
||||
"req-users-me-streaming-multi-keyword-search",
|
||||
"user-auth-1",
|
||||
"gpt-4.1-mini",
|
||||
"OpenAI",
|
||||
"streaming",
|
||||
now - chrono::Duration::minutes(5),
|
||||
),
|
||||
]));
|
||||
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_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-multi-keyword-search",
|
||||
"device-users-me-usage-multi-keyword-search",
|
||||
"refresh-token-users-me-usage-multi-keyword-search",
|
||||
now,
|
||||
)])
|
||||
})
|
||||
.await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!(
|
||||
"{gateway_url}/api/users/me/usage?limit=10&offset=0&search=gpt%20renamed-key"
|
||||
))
|
||||
.header("authorization", format!("Bearer {access_token}"))
|
||||
.header(
|
||||
"x-client-device-id",
|
||||
"device-users-me-usage-multi-keyword-search",
|
||||
)
|
||||
.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"], 2);
|
||||
assert_eq!(
|
||||
payload["records"].as_array().expect("records array").len(),
|
||||
2
|
||||
);
|
||||
assert_eq!(payload["records"][0]["model"], "gpt-4.1-mini");
|
||||
assert_eq!(payload["records"][1]["model"], "gpt-4.1");
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user