[codex] Lazy proxy traffic refresh#47
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the ability to manually refresh and persist traffic snapshots for LLM gateway proxy configurations, spanning frontend UI components, database migrations, and backend API endpoints. The review feedback highlights several performance improvements, such as returning early on empty lists, removing redundant database queries, and reusing a shared reqwest::Client to prevent socket exhaustion. Additionally, the feedback recommends translating several Chinese UI strings, feedback messages, and test assertions to English to maintain UI consistency.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| async fn apply_proxy_traffic_snapshots_to_configs( | ||
| &self, | ||
| proxies: &mut [AdminProxyConfig], | ||
| ) -> anyhow::Result<()> { | ||
| let snapshots = self.list_proxy_traffic_snapshots().await?; | ||
| for proxy in proxies { | ||
| proxy.traffic_snapshot = snapshots.get(&proxy.id).cloned(); | ||
| } | ||
| Ok(()) | ||
| } |
There was a problem hiding this comment.
To prevent redundant database round-trips, we should return early if the input proxies list is empty. This avoids executing the list_proxy_traffic_snapshots query when there are no proxies to update.
async fn apply_proxy_traffic_snapshots_to_configs(
&self,
proxies: &mut [AdminProxyConfig],
) -> anyhow::Result<()> {
if proxies.is_empty() {
return Ok(());
}
let snapshots = self.list_proxy_traffic_snapshots().await?;
for proxy in proxies {
proxy.traffic_snapshot = snapshots.get(&proxy.id).cloned();
}
Ok(())
}References
- For functions performing database queries or cache invalidations based on a list of IDs, return early if the input list is empty to prevent redundant database round-trips and cache operations.
| match state | ||
| .admin_proxy_store | ||
| .get_admin_proxy_config(&proxy_id) | ||
| .await | ||
| { | ||
| Ok(Some(_)) => {}, | ||
| Ok(None) => return not_found("LLM gateway proxy config not found").into_response(), | ||
| Err(_) => return internal_error("Failed to load llm gateway proxy config").into_response(), | ||
| } |
There was a problem hiding this comment.
The existence check get_admin_proxy_config is redundant here because the subsequent function record_admin_proxy_traffic_snapshot already performs the same existence check internally and returns None if the proxy config is not found. Removing this pre-check avoids an unnecessary database query.
References
- Avoid redundant database queries by ensuring that a subsequent function (such as a patch or update function) does not already perform the same existence check internally before calling it.
| let response = reqwest::Client::new() | ||
| .get(&url) | ||
| .timeout(USAGE_WORKER_QUERY_TIMEOUT) | ||
| .send() | ||
| .await |
There was a problem hiding this comment.
| proxy_traffic_window_label(snapshot.retention_days), | ||
| format_optional_bytes(Some(snapshot.totals.total_bytes)) | ||
| ), | ||
| None => "流量未计算".to_string(), |
There was a problem hiding this comment.
| feedback.set(Some("流量已刷新".to_string())); | ||
| on_flash.emit(("已刷新代理流量".to_string(), false)); | ||
| }, | ||
| Err(err) => { | ||
| feedback.set(Some(err.clone())); | ||
| on_flash.emit((format!("刷新代理流量失败\n{err}"), true)); |
There was a problem hiding this comment.
The feedback and flash messages are in Chinese, which is inconsistent with the rest of the English admin UI. We should translate them to English to maintain UI consistency.
feedback.set(Some("Traffic refreshed".to_string()));
on_flash.emit(("Refreshed proxy traffic".to_string(), false));
},
Err(err) => {
feedback.set(Some(err.clone()));
on_flash.emit((format!("Failed to refresh proxy traffic\n{err}"), true));| onclick={on_refresh_traffic} | ||
| disabled={*refreshing_traffic} | ||
| > | ||
| { if *refreshing_traffic { "计算中..." } else { "刷新流量" } } |
| assert_eq!(proxy_traffic_snapshot_badge(None), "流量未计算"); | ||
| assert_eq!(proxy_traffic_snapshot_meta(None), "traffic not calculated"); |
Summary
/admin/llm-gateway.llm_proxy_config_traffic_snapshotsPostgres table and return the last snapshot with proxy configs.POST /admin/llm-gateway/proxy-configs/:proxy_id/traffic-refreshendpoint that computes a retained-window snapshot and saves it.Why
The settings page was doing an expensive all-proxy traffic calculation during normal page reloads/tab entry. That made
/admin/llm-gatewayslower and could amplify usage-worker load. The new behavior makes the expensive calculation opt-in per proxy while preserving the last calculated value for display.Validation
CARGO_TARGET_DIR=/mnt/wsl/data4tb/static-flow-data/cargo-target/static_flow cargo test -p static-flow-frontend proxy_traffic_snapshot_helpers_show_uncalculated_and_persisted_badge_state --jobs 4CARGO_TARGET_DIR=/mnt/wsl/data4tb/static-flow-data/cargo-target/static_flow cargo test -p static-flow-frontend proxy_traffic_window_label_matches_retained_analytics_window --jobs 4CARGO_TARGET_DIR=/mnt/wsl/data4tb/static-flow-data/cargo-target/static_flow cargo test -p llm-access-core -p llm-access-migrations -p llm-access-store -p llm-access traffic --jobs 4CARGO_TARGET_DIR=/mnt/wsl/data4tb/static-flow-data/cargo-target/static_flow cargo clippy -p llm-access-core -p llm-access-store -p llm-access-migrations -p llm-access -p static-flow-frontend --jobs 4 -- -D warningsgit diff --checkRollout Note
Run the new Postgres migration before relying on proxy config listing in production, because
traffic_snapshotis loaded from the new snapshot table.