diff --git a/backend/crates/gateway-admin/src/model/observability.rs b/backend/crates/gateway-admin/src/model/observability.rs index 96ef18ac3..16fe61398 100644 --- a/backend/crates/gateway-admin/src/model/observability.rs +++ b/backend/crates/gateway-admin/src/model/observability.rs @@ -158,7 +158,7 @@ impl OtherRequestOutcome { #[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] pub struct UsageFilter { pub client_api_key_name: Option, - pub provider_account_name: Option, + pub provider_account_search: Option, pub client_api_key_ref: Option, pub request_id: Option, pub provider_account_ref: Option, diff --git a/backend/crates/gateway-api/src/admin/observability/query.rs b/backend/crates/gateway-api/src/admin/observability/query.rs index aafab5dc4..fed31794f 100644 --- a/backend/crates/gateway-api/src/admin/observability/query.rs +++ b/backend/crates/gateway-api/src/admin/observability/query.rs @@ -32,7 +32,7 @@ impl DashboardQuery { #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct UsageQuery { pub client_api_key_name: Option, - pub account_name: Option, + pub account_search: Option, pub status: Option, pub current_page: Option, pub page_size: Option, @@ -339,7 +339,7 @@ pub(crate) fn dashboard_today_range( pub(crate) fn usage_filter(query: &UsageQuery) -> domain::UsageFilter { domain::UsageFilter { client_api_key_name: non_empty(query.client_api_key_name.clone()), - provider_account_name: non_empty(query.account_name.clone()), + provider_account_search: non_empty(query.account_search.clone()), provider_kind: non_empty(query.provider.clone()), model_contains: non_empty(query.model.clone()), ..Default::default() diff --git a/backend/crates/gateway-api/tests/admin/observability/response.rs b/backend/crates/gateway-api/tests/admin/observability/response.rs index f7821b09c..8ffe3af34 100644 --- a/backend/crates/gateway-api/tests/admin/observability/response.rs +++ b/backend/crates/gateway-api/tests/admin/observability/response.rs @@ -917,7 +917,7 @@ pub(super) fn usage_record_with_account( } #[tokio::test] -async fn usage_route_should_forward_name_and_model_contains_filters() { +async fn usage_route_should_forward_record_text_filters() { use axum::{ body::Body, http::{Request, StatusCode, header}, @@ -933,7 +933,7 @@ async fn usage_route_should_forward_name_and_model_contains_filters() { .with_state(fixture.state()) .oneshot( Request::builder() - .uri("/api/admin/usage/records?model=%20coding%20&clientApiKeyName=%20Key%20%25%20&accountName=%20Account%20Name%20") + .uri("/api/admin/usage/records?model=%20coding%20&clientApiKeyName=%20Key%20%25%20&accountSearch=%20alice%40%20") .header(header::COOKIE, "cpr_session=valid-session") .header("x-request-id", "req_usage_other_outcome") .body(Body::empty()) @@ -947,10 +947,7 @@ async fn usage_route_should_forward_name_and_model_contains_filters() { let filter = &observations.records.last().expect("usage query").filter; assert_eq!(filter.client_api_key_name.as_deref(), Some("Key %")); assert_eq!(filter.model_contains.as_deref(), Some("coding")); - assert_eq!( - filter.provider_account_name.as_deref(), - Some("Account Name") - ); + assert_eq!(filter.provider_account_search.as_deref(), Some("alice@")); assert!(filter.client_api_key_ref.is_none()); assert!(filter.provider_account_ref.is_none()); } diff --git a/backend/crates/gateway-store/src/postgres/observability/analytics/contributions.rs b/backend/crates/gateway-store/src/postgres/observability/analytics/contributions.rs index 1f9486520..000ee9698 100644 --- a/backend/crates/gateway-store/src/postgres/observability/analytics/contributions.rs +++ b/backend/crates/gateway-store/src/postgres/observability/analytics/contributions.rs @@ -36,10 +36,8 @@ impl AnalyticsSnapshot<'_> { || filter.attempt_index.is_some() || filter.status_code.is_some() || filter.outcome.is_some() - || filter.search.is_some() || filter.model_contains.is_some() - || filter.client_api_key_name.is_some() - || filter.provider_account_name.is_some() + || filter.needs_observation_text() || matches!( dimension, Some(DiagnosticDimension::Failure | DiagnosticDimension::Status) diff --git a/backend/crates/gateway-store/src/postgres/observability/analytics/query.rs b/backend/crates/gateway-store/src/postgres/observability/analytics/query.rs index f32487c34..7915a0ae6 100644 --- a/backend/crates/gateway-store/src/postgres/observability/analytics/query.rs +++ b/backend/crates/gateway-store/src/postgres/observability/analytics/query.rs @@ -1232,10 +1232,8 @@ pub(super) fn collection_for( || filter.attempt_index.is_some() || filter.response_id.is_some() || filter.upstream_request_id.is_some() - || filter.search.is_some() || filter.model_contains.is_some() - || filter.client_api_key_name.is_some() - || filter.provider_account_name.is_some() + || filter.needs_observation_text() { return None; } @@ -1286,21 +1284,15 @@ pub(crate) fn push_analytics_filter( alias: &str, ) { let mut typed = filter.clone(); - typed.search = None; - typed.client_api_key_name = None; - typed.provider_account_name = None; + let text = UsageRecordFilter { + client_api_key_name: typed.client_api_key_name.take(), + provider_account_search: typed.provider_account_search.take(), + search: typed.search.take(), + ..Default::default() + }; push_usage_filter(query, &typed, alias); - if filter.search.is_some() - || filter.client_api_key_name.is_some() - || filter.provider_account_name.is_some() - { + if text.needs_observation_text() { query.push(format!(" and exists(select 1 from model_request_observations searched where searched.id={alias}.id")); - let text = UsageRecordFilter { - client_api_key_name: filter.client_api_key_name.clone(), - provider_account_name: filter.provider_account_name.clone(), - search: filter.search.clone(), - ..Default::default() - }; push_usage_filter(query, &text, "searched"); query.push(")"); } diff --git a/backend/crates/gateway-store/src/postgres/observability/mapping.rs b/backend/crates/gateway-store/src/postgres/observability/mapping.rs index 811dfdee5..640bf6879 100644 --- a/backend/crates/gateway-store/src/postgres/observability/mapping.rs +++ b/backend/crates/gateway-store/src/postgres/observability/mapping.rs @@ -19,7 +19,7 @@ pub(crate) fn store_usage_filter(filter: admin_observability::UsageFilter) -> Us UsageRecordFilter { model_contains: filter.model_contains, client_api_key_name: filter.client_api_key_name, - provider_account_name: filter.provider_account_name, + provider_account_search: filter.provider_account_search, client_api_key_ref: filter.client_api_key_ref, request_id: filter.request_id, provider_account_ref: filter.provider_account_ref, diff --git a/backend/crates/gateway-store/src/postgres/observability/model.rs b/backend/crates/gateway-store/src/postgres/observability/model.rs index 25dc2a109..2d1ad4206 100644 --- a/backend/crates/gateway-store/src/postgres/observability/model.rs +++ b/backend/crates/gateway-store/src/postgres/observability/model.rs @@ -40,7 +40,7 @@ pub(crate) fn observability_page_offset( #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct UsageRecordFilter { pub client_api_key_name: Option, - pub provider_account_name: Option, + pub provider_account_search: Option, pub client_api_key_ref: Option, pub request_id: Option, pub provider_account_ref: Option, @@ -58,6 +58,12 @@ pub struct UsageRecordFilter { } impl UsageRecordFilter { + pub(super) fn needs_observation_text(&self) -> bool { + self.search.is_some() + || self.client_api_key_name.is_some() + || self.provider_account_search.is_some() + } + pub fn validate(&self) -> StoreResult<()> { for (value, field) in [ ( @@ -65,8 +71,8 @@ impl UsageRecordFilter { "client API key name filter", ), ( - self.provider_account_name.as_deref(), - "provider account name filter", + self.provider_account_search.as_deref(), + "provider account search filter", ), (self.client_api_key_ref.as_deref(), "client API key filter"), (self.request_id.as_deref(), "request ID filter"), diff --git a/backend/crates/gateway-store/src/postgres/observability/queries/usage.rs b/backend/crates/gateway-store/src/postgres/observability/queries/usage.rs index 6811a9675..c45c502ad 100644 --- a/backend/crates/gateway-store/src/postgres/observability/queries/usage.rs +++ b/backend/crates/gateway-store/src/postgres/observability/queries/usage.rs @@ -11,18 +11,10 @@ pub(crate) fn push_usage_filter( filter: &UsageRecordFilter, alias: &str, ) { - for (column, value) in [ - ("client_api_key_name_snapshot", &filter.client_api_key_name), - ( - "provider_account_name_snapshot", - &filter.provider_account_name, - ), - ] { - if let Some(value) = value { - query.push(format!(" and {alias}.{column} like ")); - query.push_bind(literal_contains_pattern(value)); - query.push(" escape '\\'"); - } + if let Some(value) = &filter.client_api_key_name { + query.push(format!(" and {alias}.client_api_key_name_snapshot like ")); + query.push_bind(literal_contains_pattern(value)); + query.push(" escape '\\'"); } if let Some(value) = &filter.client_api_key_ref { query.push(format!(" and {alias}.client_api_key_ref = ")); @@ -51,11 +43,23 @@ pub(crate) fn push_usage_filter( query.push_bind(value.clone()); query.push(")"); } - if let Some(value) = &filter.model_contains { + for (value, first, second) in [ + ( + &filter.provider_account_search, + "provider_account_name_snapshot", + "provider_account_email_snapshot", + ), + ( + &filter.model_contains, + "requested_model_id", + "upstream_model_id", + ), + ] { + let Some(value) = value else { continue }; let pattern = literal_contains_pattern(value); - query.push(format!(" and ({alias}.requested_model_id like ")); + query.push(format!(" and ({alias}.{first} like ")); query.push_bind(pattern.clone()); - query.push(format!(" escape '\\' or {alias}.upstream_model_id like ")); + query.push(format!(" escape '\\' or {alias}.{second} like ")); query.push_bind(pattern); query.push(" escape '\\')"); } diff --git a/backend/crates/gateway-store/tests/postgres/observability/mod.rs b/backend/crates/gateway-store/tests/postgres/observability/mod.rs index 5dedec23a..d8b838e3f 100644 --- a/backend/crates/gateway-store/tests/postgres/observability/mod.rs +++ b/backend/crates/gateway-store/tests/postgres/observability/mod.rs @@ -961,6 +961,69 @@ async fn usage_search_should_ignore_request_ids_and_preserve_exact_filters() { database.close().await; } +#[tokio::test] +async fn usage_account_filter_matches_only_historical_names_and_emails() { + use admin_observability::UsageRecordStatus::{All, Error, Success}; + + let Some(database) = TestDatabase::create("usage_account_filter").await else { + return; + }; + let now = Utc::now(); + seed_observability_facts(&database.pool, now) + .await + .expect("seed observability facts"); + seed_calculated_billing_facts(&database.pool, now) + .await + .expect("seed unrelated request"); + sqlx::query( + "update model_requests set request_observation_json = jsonb_set( + request_observation_json, '{account}', case when id = 'req_observe_success' + then jsonb_build_object('email', $1::text) + else jsonb_build_object('name', $2::text) end) + where id in ('req_observe_success', 'req_observe_failed')", + ) + .bind(r"alice_%\ops@example.invalid") + .bind("alice team") + .execute(&database.pool) + .await + .expect("set email-only and name-only snapshots"); + super::drain_analytics(&database.pool).await; + + let range = ObservabilityRange::new(now - TimeDelta::hours(1), now).expect("range"); + let repo = observability_repository(&database.pool); + for (status, search, page, total, expected_id) in [ + (Success, r"_%\", 1, 1, Some("req_observe_success")), + (Error, "team", 1, 1, Some("req_observe_failed")), + (All, "alice", 1, 2, Some("req_observe_failed")), + (All, "alice", 2, 2, Some("req_observe_success")), + (Error, "@example", 1, 0, None), + (All, "ALICE", 1, 0, None), + (All, "alice%ops", 1, 0, None), + (All, "public-model", 1, 0, None), + ] { + let result = repo + .list_usage_records(UsageRecordQuery { + status, + range, + filter: UsageRecordFilter { + provider_account_search: Some(search.to_owned()), + ..Default::default() + }, + current_page: page, + page_size: ObservabilityPageSize::new(1).expect("page size"), + }) + .await + .expect("filter usage by account snapshot"); + assert_eq!(result.total, total, "{status:?} {search} page {page}"); + assert_eq!( + result.items.first().map(|item| item.id.as_str()), + expected_id, + "{status:?} {search} page {page}" + ); + } + database.close().await; +} + #[tokio::test] async fn usage_search_should_match_account_email_and_name_substrings() { let Some(database) = TestDatabase::create("usage_account_contains").await else { @@ -2207,7 +2270,7 @@ async fn admin_observability_adapter_preserves_utc_queries_metrics_costs_and_det filter: admin_observability::UsageFilter { model_contains: None, client_api_key_name: None, - provider_account_name: None, + provider_account_search: Some("@example.invalid".to_owned()), client_api_key_ref: Some("key_observe".to_owned()), request_id: Some("req_observe_success".to_owned()), provider_account_ref: Some("acct_observe".to_owned()), diff --git a/backend/crates/gateway-store/tests/postgres/snapshots.rs b/backend/crates/gateway-store/tests/postgres/snapshots.rs index 19516108e..6cd652366 100644 --- a/backend/crates/gateway-store/tests/postgres/snapshots.rs +++ b/backend/crates/gateway-store/tests/postgres/snapshots.rs @@ -1452,13 +1452,14 @@ async fn queued_history_keeps_frozen_identity_after_configuration_deletion() { if published { super::drain_analytics(&database.pool).await; } - for (key_name, account_name, expected) in [ + for (key_name, account_search, expected) in [ (Some("Original Key"), None, 1), (None, Some("Original Account"), 1), (Some("Original Key"), Some("Original Account"), 1), (Some("Original"), None, 1), (Some("ginal K"), None, 1), (None, Some("ginal Acc"), 1), + (None, Some("old@"), 1), (Some("queued_key"), None, 0), (None, Some("queued_account"), 0), (Some("Original Account"), None, 0), @@ -1473,7 +1474,7 @@ async fn queued_history_keeps_frozen_identity_after_configuration_deletion() { started_at, UsageRecordFilter { client_api_key_name: key_name.map(str::to_owned), - provider_account_name: account_name.map(str::to_owned), + provider_account_search: account_search.map(str::to_owned), ..Default::default() }, ); @@ -1481,7 +1482,7 @@ async fn queued_history_keeps_frozen_identity_after_configuration_deletion() { let page = repository.list_usage_records(query).await.unwrap(); assert_eq!( page.total, expected, - "published={published}, key={key_name:?}, account={account_name:?}, status={status:?}" + "published={published}, key={key_name:?}, account={account_search:?}, status={status:?}" ); assert_eq!(page.items.len() as u64, expected); if expected == 1 { diff --git a/docs/api.md b/docs/api.md index d9e58dca3..fa7e5b874 100644 --- a/docs/api.md +++ b/docs/api.md @@ -2790,7 +2790,7 @@ Dashboard 的 `accountUsage[]` 返回稳定指标 `metric=requests|tokens`、可 Dashboard 默认 `period=today`,用量、诊断和错误查询默认 `period=7d`,范围参数遵循[页面时间合同](#页面时间合同)。 Dashboard 返回原始 `asOf` 及 `asOfDisplay`,作为当前数据的查询锚点 -请求明细列表接受 `currentPage`、`pageSize`、时间范围、`provider`、`clientApiKeyName`、`accountName`、`model` 和 `status`。 +请求明细列表接受 `currentPage`、`pageSize`、时间范围、`provider`、`clientApiKeyName`、`accountSearch`、`model` 和 `status`。 汇总和洞察接受时间范围与 `provider`;性能和成本另要求 `metric`,诊断另接受 `dimension`、`model` 和 `statusCode`。 各接口拒绝未知参数,明细筛选不改变顶部统计范围 @@ -2860,8 +2860,8 @@ HTTP SSE 请求可能在返回 HTTP 200 后流内失败;WebSocket 已升级时 管理端请求列表与详情保留 `requestedModel`(客户端请求)与 `upstreamModel`(网关发送) -请求明细的 `clientApiKeyName`、`accountName` 按字面量包含匹配请求保存的历史名称快照,区分大小写, -不解析为 ID,也不依赖当前配置是否存在。`model` 按字面量包含匹配请求模型或上游模型,`%`、`_` 和 `\` 按普通字符处理 +请求明细的 `clientApiKeyName` 匹配 Key 名称,`accountSearch` 匹配账号名称或邮箱。 +这些筛选仅使用历史快照,可与时间、状态等条件组合。`model` 匹配请求模型或上游模型。以上文本筛选均按区分大小写的字面量包含匹配,`%`、`_` 和 `\` 按普通字符处理 运维错误列表的 `search` 使用字面量包含匹配,`%`、`_` 和 `\` 按普通字符处理,不支持密钥值或可见前缀搜索。 请求详情通过 `/api/admin/usage/records/detail?id=...` 查询 diff --git a/frontend/src/api/modules/usage.ts b/frontend/src/api/modules/usage.ts index 7b24c1437..4b86c7db4 100644 --- a/frontend/src/api/modules/usage.ts +++ b/frontend/src/api/modules/usage.ts @@ -425,7 +425,7 @@ interface PageQuery { type UsagePageQuery = UsageRangeQuery & PageQuery & { clientApiKeyName?: string - accountName?: string + accountSearch?: string model?: string status?: UsageRecordStatus } diff --git a/frontend/src/views/usage/composables/useUsageRecordsTable.ts b/frontend/src/views/usage/composables/useUsageRecordsTable.ts index fb963ed6b..402b4b86e 100644 --- a/frontend/src/views/usage/composables/useUsageRecordsTable.ts +++ b/frontend/src/views/usage/composables/useUsageRecordsTable.ts @@ -30,7 +30,7 @@ export function useUsageRecordsTable(options: UseUsageRecordsTableOptions) { clientApiKeyName: filterField.value === 'key' ? value : undefined, - accountName: filterField.value === 'account' + accountSearch: filterField.value === 'account' ? value : undefined, model: filterField.value === 'model' @@ -74,7 +74,7 @@ export function useUsageRecordsTable(options: UseUsageRecordsTableOptions) { function filterChanged() { const filter = filterParams.value return tableParams.clientApiKeyName !== filter.clientApiKeyName - || tableParams.accountName !== filter.accountName + || tableParams.accountSearch !== filter.accountSearch || tableParams.model !== filter.model }