mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-04 00:17:45 +08:00
refactor: 大规模模块拆分与重组,新增 aether-admin crate
- 新建独立 aether-admin crate 承载 admin 相关共享契约与纯辅助函数 - 拆分 ai_pipeline 下 kiro/private_envelope/conversion/planner 等大文件为子模块目录 - 重组 admin handlers 各业务域(billing/oauth/provider/system/users 等)为目录结构,移除 shared.rs/builders.rs 等反模式 - 移除 ai_pipeline runtime adapters 旧实现(claude/openai/gemini/kiro/vertex/antigravity 等),改由 provider transport 统一承载 - 移除 control_facade/execution_facade/auth_snapshot_facade 等冗余 facade 层 - 拆分 query/billing 与 query/monitoring 模块、state/runtime/payments 与 security 模块 - 扩展架构测试覆盖 admin_billing/admin_model/admin_users 等新模块 - 删除 docs/architecture/refactor-execution-plan.md 已完成的执行计划文档
This commit is contained in:
@@ -1,181 +1,16 @@
|
||||
use super::helpers::{
|
||||
AdminStatsLeaderboardItem, AdminStatsLeaderboardMetric, AdminStatsSortOrder,
|
||||
AdminStatsUserMetadata,
|
||||
};
|
||||
use crate::{AppState, GatewayError};
|
||||
use crate::handlers::admin::request::AdminAppState;
|
||||
use crate::GatewayError;
|
||||
use aether_data_contracts::repository::usage::StoredRequestUsageAudit;
|
||||
|
||||
pub(super) fn build_model_leaderboard_items(
|
||||
items: &[StoredRequestUsageAudit],
|
||||
) -> Vec<AdminStatsLeaderboardItem> {
|
||||
let mut grouped: std::collections::BTreeMap<String, AdminStatsLeaderboardItem> =
|
||||
std::collections::BTreeMap::new();
|
||||
for item in items {
|
||||
if matches!(item.status.as_str(), "pending" | "streaming")
|
||||
|| matches!(item.provider_name.as_str(), "unknown" | "pending")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let entry =
|
||||
grouped
|
||||
.entry(item.model.clone())
|
||||
.or_insert_with(|| AdminStatsLeaderboardItem {
|
||||
id: item.model.clone(),
|
||||
name: item.model.clone(),
|
||||
requests: 0,
|
||||
tokens: 0,
|
||||
cost: 0.0,
|
||||
});
|
||||
entry.requests = entry.requests.saturating_add(1);
|
||||
entry.tokens = entry.tokens.saturating_add(
|
||||
item.input_tokens
|
||||
.saturating_add(item.output_tokens)
|
||||
.saturating_add(item.cache_creation_input_tokens)
|
||||
.saturating_add(item.cache_read_input_tokens),
|
||||
);
|
||||
entry.cost += item.total_cost_usd;
|
||||
}
|
||||
grouped.into_values().collect()
|
||||
}
|
||||
|
||||
pub(super) fn build_api_key_leaderboard_items(
|
||||
items: &[StoredRequestUsageAudit],
|
||||
snapshots: Option<&[aether_data::repository::auth::StoredAuthApiKeySnapshot]>,
|
||||
include_inactive: bool,
|
||||
exclude_admin: bool,
|
||||
) -> Vec<AdminStatsLeaderboardItem> {
|
||||
let snapshot_by_api_key_id: std::collections::BTreeMap<_, _> = snapshots
|
||||
.unwrap_or(&[])
|
||||
.iter()
|
||||
.map(|snapshot| (snapshot.api_key_id.as_str(), snapshot))
|
||||
.collect();
|
||||
let mut grouped: std::collections::BTreeMap<String, AdminStatsLeaderboardItem> =
|
||||
std::collections::BTreeMap::new();
|
||||
let snapshots_available = snapshots.is_some();
|
||||
|
||||
for item in items {
|
||||
if matches!(item.status.as_str(), "pending" | "streaming")
|
||||
|| matches!(item.provider_name.as_str(), "unknown" | "pending")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Some(api_key_id) = item.api_key_id.as_deref() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let entry_name = if let Some(snapshot) = snapshot_by_api_key_id.get(api_key_id) {
|
||||
if snapshot.user_is_deleted {
|
||||
continue;
|
||||
}
|
||||
if !include_inactive && !snapshot.api_key_is_active {
|
||||
continue;
|
||||
}
|
||||
if exclude_admin && snapshot.user_role.eq_ignore_ascii_case("admin") {
|
||||
continue;
|
||||
}
|
||||
snapshot
|
||||
.api_key_name
|
||||
.clone()
|
||||
.or_else(|| item.api_key_name.clone())
|
||||
.unwrap_or_else(|| api_key_id.to_string())
|
||||
} else {
|
||||
if snapshots_available {
|
||||
continue;
|
||||
}
|
||||
item.api_key_name
|
||||
.clone()
|
||||
.unwrap_or_else(|| api_key_id.to_string())
|
||||
};
|
||||
|
||||
let entry =
|
||||
grouped
|
||||
.entry(api_key_id.to_string())
|
||||
.or_insert_with(|| AdminStatsLeaderboardItem {
|
||||
id: api_key_id.to_string(),
|
||||
name: entry_name,
|
||||
requests: 0,
|
||||
tokens: 0,
|
||||
cost: 0.0,
|
||||
});
|
||||
entry.requests = entry.requests.saturating_add(1);
|
||||
entry.tokens = entry.tokens.saturating_add(
|
||||
item.input_tokens
|
||||
.saturating_add(item.output_tokens)
|
||||
.saturating_add(item.cache_creation_input_tokens)
|
||||
.saturating_add(item.cache_read_input_tokens),
|
||||
);
|
||||
entry.cost += item.total_cost_usd;
|
||||
}
|
||||
|
||||
grouped.into_values().collect()
|
||||
}
|
||||
|
||||
pub(super) fn build_user_leaderboard_items(
|
||||
items: &[StoredRequestUsageAudit],
|
||||
users: &std::collections::BTreeMap<String, AdminStatsUserMetadata>,
|
||||
include_inactive: bool,
|
||||
exclude_admin: bool,
|
||||
) -> Vec<AdminStatsLeaderboardItem> {
|
||||
let mut grouped: std::collections::BTreeMap<String, AdminStatsLeaderboardItem> =
|
||||
std::collections::BTreeMap::new();
|
||||
|
||||
for item in items {
|
||||
if matches!(item.status.as_str(), "pending" | "streaming")
|
||||
|| matches!(item.provider_name.as_str(), "unknown" | "pending")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Some(user_id) = item.user_id.as_deref() else {
|
||||
continue;
|
||||
};
|
||||
let entry_name = if let Some(user) = users.get(user_id) {
|
||||
if user.is_deleted {
|
||||
continue;
|
||||
}
|
||||
if !include_inactive && !user.is_active {
|
||||
continue;
|
||||
}
|
||||
if exclude_admin && user.role.eq_ignore_ascii_case("admin") {
|
||||
continue;
|
||||
}
|
||||
user.name.clone()
|
||||
} else {
|
||||
if exclude_admin {
|
||||
continue;
|
||||
}
|
||||
item.username
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(ToOwned::to_owned)
|
||||
.unwrap_or_else(|| user_id.to_string())
|
||||
};
|
||||
|
||||
let entry =
|
||||
grouped
|
||||
.entry(user_id.to_string())
|
||||
.or_insert_with(|| AdminStatsLeaderboardItem {
|
||||
id: user_id.to_string(),
|
||||
name: entry_name,
|
||||
requests: 0,
|
||||
tokens: 0,
|
||||
cost: 0.0,
|
||||
});
|
||||
entry.requests = entry.requests.saturating_add(1);
|
||||
entry.tokens = entry.tokens.saturating_add(
|
||||
item.input_tokens
|
||||
.saturating_add(item.output_tokens)
|
||||
.saturating_add(item.cache_creation_input_tokens)
|
||||
.saturating_add(item.cache_read_input_tokens),
|
||||
);
|
||||
entry.cost += item.total_cost_usd;
|
||||
}
|
||||
|
||||
grouped.into_values().collect()
|
||||
}
|
||||
pub(super) use aether_admin::observability::stats::{
|
||||
build_admin_stats_leaderboard_response, build_api_key_leaderboard_items,
|
||||
build_model_leaderboard_items, build_user_leaderboard_items, compare_leaderboard_items,
|
||||
compute_dense_rank, AdminStatsLeaderboardItem, AdminStatsLeaderboardMetric,
|
||||
AdminStatsLeaderboardNameMode, AdminStatsSortOrder, AdminStatsUserMetadata,
|
||||
};
|
||||
|
||||
pub(super) async fn load_user_leaderboard_metadata(
|
||||
state: &AppState,
|
||||
state: &AdminAppState<'_>,
|
||||
user_ids: &[String],
|
||||
) -> Result<std::collections::BTreeMap<String, AdminStatsUserMetadata>, GatewayError> {
|
||||
let mut metadata = std::collections::BTreeMap::new();
|
||||
@@ -222,57 +57,3 @@ pub(super) async fn load_user_leaderboard_metadata(
|
||||
|
||||
Ok(metadata)
|
||||
}
|
||||
|
||||
pub(super) fn compare_leaderboard_items(
|
||||
metric: AdminStatsLeaderboardMetric,
|
||||
order: AdminStatsSortOrder,
|
||||
left: &AdminStatsLeaderboardItem,
|
||||
right: &AdminStatsLeaderboardItem,
|
||||
) -> std::cmp::Ordering {
|
||||
let metric_order = match metric {
|
||||
AdminStatsLeaderboardMetric::Requests => left.requests.cmp(&right.requests),
|
||||
AdminStatsLeaderboardMetric::Tokens => left.tokens.cmp(&right.tokens),
|
||||
AdminStatsLeaderboardMetric::Cost => left
|
||||
.cost
|
||||
.partial_cmp(&right.cost)
|
||||
.unwrap_or(std::cmp::Ordering::Equal),
|
||||
};
|
||||
let metric_order = match order {
|
||||
AdminStatsSortOrder::Asc => metric_order,
|
||||
AdminStatsSortOrder::Desc => metric_order.reverse(),
|
||||
};
|
||||
if metric_order == std::cmp::Ordering::Equal {
|
||||
left.id.cmp(&right.id)
|
||||
} else {
|
||||
metric_order
|
||||
}
|
||||
}
|
||||
|
||||
fn leaderboard_metric_equal(
|
||||
metric: AdminStatsLeaderboardMetric,
|
||||
left: &AdminStatsLeaderboardItem,
|
||||
right: &AdminStatsLeaderboardItem,
|
||||
) -> bool {
|
||||
match metric {
|
||||
AdminStatsLeaderboardMetric::Requests => left.requests == right.requests,
|
||||
AdminStatsLeaderboardMetric::Tokens => left.tokens == right.tokens,
|
||||
AdminStatsLeaderboardMetric::Cost => (left.cost - right.cost).abs() < 1e-9,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn compute_dense_rank(
|
||||
metric: AdminStatsLeaderboardMetric,
|
||||
items: &[AdminStatsLeaderboardItem],
|
||||
index: usize,
|
||||
) -> usize {
|
||||
if index == 0 {
|
||||
return 1;
|
||||
}
|
||||
let mut rank = 1usize;
|
||||
for current in 1..=index {
|
||||
if !leaderboard_metric_equal(metric, &items[current - 1], &items[current]) {
|
||||
rank = rank.saturating_add(1);
|
||||
}
|
||||
}
|
||||
rank
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user