mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-04 16:37:46 +08:00
Add admin operations dashboard and usage state fixes
This commit is contained in:
@@ -7,6 +7,9 @@ use super::replay::{
|
||||
admin_usage_resolve_request_capture_body_for_item, build_admin_usage_curl_response,
|
||||
build_admin_usage_detail_payload, build_admin_usage_replay_response,
|
||||
};
|
||||
use super::summary_routes::{
|
||||
admin_usage_terminal_candidate_state_override, apply_admin_usage_state_override,
|
||||
};
|
||||
use crate::handlers::admin::request::{AdminAppState, AdminRequestContext};
|
||||
use crate::handlers::admin::shared::{attach_admin_audit_response, query_param_bool};
|
||||
use crate::GatewayError;
|
||||
@@ -233,6 +236,19 @@ pub(super) async fn maybe_build_local_admin_usage_detail_response(
|
||||
let provider_key_name = admin_usage_provider_key_name(&item, &provider_key_names);
|
||||
|
||||
let mut detail_item = item.clone();
|
||||
if matches!(detail_item.status.as_str(), "pending" | "streaming")
|
||||
&& state.has_request_candidate_data_reader()
|
||||
{
|
||||
let candidates = state
|
||||
.app()
|
||||
.read_request_candidates_by_request_id(&detail_item.request_id)
|
||||
.await?;
|
||||
if let Some(override_payload) =
|
||||
admin_usage_terminal_candidate_state_override(&candidates)
|
||||
{
|
||||
apply_admin_usage_state_override(&mut detail_item, &override_payload);
|
||||
}
|
||||
}
|
||||
let mut body_load_errors = serde_json::Map::new();
|
||||
let request_body = if include_bodies {
|
||||
let (request_body, provider_request_body, response_body, client_response_body) = tokio::join!(
|
||||
|
||||
@@ -301,7 +301,7 @@ fn admin_usage_unix_millis_to_rfc3339(unix_ms: u64) -> Option<String> {
|
||||
.map(|timestamp| timestamp.to_rfc3339())
|
||||
}
|
||||
|
||||
fn admin_usage_terminal_candidate_state_override(
|
||||
pub(super) fn admin_usage_terminal_candidate_state_override(
|
||||
candidates: &[StoredRequestCandidate],
|
||||
) -> Option<serde_json::Value> {
|
||||
let candidate = admin_usage_current_candidate(candidates)?;
|
||||
@@ -343,6 +343,49 @@ fn admin_usage_terminal_candidate_state_override(
|
||||
Some(payload)
|
||||
}
|
||||
|
||||
pub(super) fn apply_admin_usage_state_override(
|
||||
item: &mut StoredRequestUsageAudit,
|
||||
override_payload: &serde_json::Value,
|
||||
) {
|
||||
if !matches!(item.status.as_str(), "pending" | "streaming") {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(object) = override_payload.as_object() else {
|
||||
return;
|
||||
};
|
||||
|
||||
if let Some(status) = object
|
||||
.get("status")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| matches!(*value, "completed" | "failed" | "cancelled"))
|
||||
{
|
||||
item.status = status.to_string();
|
||||
}
|
||||
if let Some(status_code) = object
|
||||
.get("status_code")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
.and_then(|value| u16::try_from(value).ok())
|
||||
{
|
||||
item.status_code = Some(status_code);
|
||||
}
|
||||
if let Some(response_time_ms) = object
|
||||
.get("response_time_ms")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
{
|
||||
item.response_time_ms = Some(response_time_ms);
|
||||
}
|
||||
if let Some(error_message) = object
|
||||
.get("error_message")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
item.error_message = Some(error_message.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
fn admin_usage_matches_attempt_status(
|
||||
item: &StoredRequestUsageAudit,
|
||||
status: &str,
|
||||
@@ -901,6 +944,21 @@ pub(super) async fn maybe_build_local_admin_usage_summary_response(
|
||||
}
|
||||
};
|
||||
|
||||
let active_candidate_state =
|
||||
resolve_admin_usage_active_candidate_state(state, &usage).await?;
|
||||
let usage = usage
|
||||
.into_iter()
|
||||
.map(|mut item| {
|
||||
if let Some(override_payload) = active_candidate_state
|
||||
.state_overrides_by_request_id
|
||||
.get(&item.request_id)
|
||||
{
|
||||
apply_admin_usage_state_override(&mut item, override_payload);
|
||||
}
|
||||
item
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let user_ids: Vec<String> = usage
|
||||
.iter()
|
||||
.filter_map(|item| item.user_id.clone())
|
||||
|
||||
Reference in New Issue
Block a user