mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 09:50:21 +08:00
fix codex window usage stats
This commit is contained in:
@@ -609,6 +609,19 @@ fn preserve_quota_window_usage_state(current_status_snapshot: Option<&Value>, qu
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if next_window
|
||||||
|
.get("window_minutes")
|
||||||
|
.and_then(admin_provider_quota_pure::coerce_json_u64)
|
||||||
|
.is_none()
|
||||||
|
{
|
||||||
|
if let Some(window_minutes) = current_window
|
||||||
|
.get("window_minutes")
|
||||||
|
.and_then(admin_provider_quota_pure::coerce_json_u64)
|
||||||
|
.or_else(|| codex_default_window_minutes(code))
|
||||||
|
{
|
||||||
|
next_window.insert("window_minutes".to_string(), json!(window_minutes));
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Some(usage_reset_at) = current_window
|
if let Some(usage_reset_at) = current_window
|
||||||
.get("usage_reset_at")
|
.get("usage_reset_at")
|
||||||
.and_then(admin_provider_quota_pure::coerce_json_u64)
|
.and_then(admin_provider_quota_pure::coerce_json_u64)
|
||||||
@@ -621,6 +634,16 @@ fn preserve_quota_window_usage_state(current_status_snapshot: Option<&Value>, qu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn codex_default_window_minutes(code: &str) -> Option<u64> {
|
||||||
|
if code.eq_ignore_ascii_case("5h") {
|
||||||
|
Some(300)
|
||||||
|
} else if code.eq_ignore_ascii_case("weekly") {
|
||||||
|
Some(10_080)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn codex_quota_window_snapshot(
|
fn codex_quota_window_snapshot(
|
||||||
metadata: &Map<String, Value>,
|
metadata: &Map<String, Value>,
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
@@ -658,18 +681,19 @@ fn codex_quota_window_snapshot(
|
|||||||
.zip(reset_seconds)
|
.zip(reset_seconds)
|
||||||
.map(|(observed_at, reset_seconds)| observed_at.saturating_add(reset_seconds))
|
.map(|(observed_at, reset_seconds)| observed_at.saturating_add(reset_seconds))
|
||||||
});
|
});
|
||||||
let window_minutes = metadata
|
let explicit_window_minutes = metadata
|
||||||
.get(&window_minutes_key)
|
.get(&window_minutes_key)
|
||||||
.and_then(admin_provider_quota_pure::coerce_json_u64);
|
.and_then(admin_provider_quota_pure::coerce_json_u64);
|
||||||
|
|
||||||
if used_percent.is_none()
|
if used_percent.is_none()
|
||||||
&& reset_at.is_none()
|
&& reset_at.is_none()
|
||||||
&& reset_seconds.is_none()
|
&& reset_seconds.is_none()
|
||||||
&& window_minutes.is_none()
|
&& explicit_window_minutes.is_none()
|
||||||
{
|
{
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let window_minutes = explicit_window_minutes.or_else(|| codex_default_window_minutes(code));
|
||||||
let used_ratio = used_percent.map(|value| (value / 100.0).clamp(0.0, 1.0));
|
let used_ratio = used_percent.map(|value| (value / 100.0).clamp(0.0, 1.0));
|
||||||
let remaining_ratio = used_ratio.map(|value| (1.0 - value).max(0.0));
|
let remaining_ratio = used_ratio.map(|value| (1.0 - value).max(0.0));
|
||||||
|
|
||||||
@@ -2240,6 +2264,44 @@ mod tests {
|
|||||||
assert!(weekly.get("usage").is_none());
|
assert!(weekly.get("usage").is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sync_provider_key_quota_status_snapshot_defaults_codex_window_minutes() {
|
||||||
|
let upstream_metadata = json!({
|
||||||
|
"codex": {
|
||||||
|
"updated_at": 1_775_800_000u64,
|
||||||
|
"plan_type": "plus",
|
||||||
|
"primary_used_percent": 5.0,
|
||||||
|
"primary_reset_at": 1_900_000_000u64,
|
||||||
|
"secondary_used_percent": 1.0,
|
||||||
|
"secondary_reset_at": 1_900_500_000u64
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let payload = sync_provider_key_quota_status_snapshot(
|
||||||
|
None,
|
||||||
|
"codex",
|
||||||
|
Some(&upstream_metadata),
|
||||||
|
"response_headers",
|
||||||
|
)
|
||||||
|
.expect("quota snapshot should sync");
|
||||||
|
let windows = payload["quota"]["windows"]
|
||||||
|
.as_array()
|
||||||
|
.expect("quota windows should exist");
|
||||||
|
let weekly = windows
|
||||||
|
.iter()
|
||||||
|
.filter_map(Value::as_object)
|
||||||
|
.find(|window| window.get("code") == Some(&json!("weekly")))
|
||||||
|
.expect("weekly window should exist");
|
||||||
|
let five_h = windows
|
||||||
|
.iter()
|
||||||
|
.filter_map(Value::as_object)
|
||||||
|
.find(|window| window.get("code") == Some(&json!("5h")))
|
||||||
|
.expect("5h window should exist");
|
||||||
|
|
||||||
|
assert_eq!(weekly.get("window_minutes"), Some(&json!(10_080u64)));
|
||||||
|
assert_eq!(five_h.get("window_minutes"), Some(&json!(300u64)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn provider_key_status_snapshot_payload_backfills_thin_ok_snapshot_from_upstream_metadata() {
|
fn provider_key_status_snapshot_payload_backfills_thin_ok_snapshot_from_upstream_metadata() {
|
||||||
let mut key = sample_catalog_key();
|
let mut key = sample_catalog_key();
|
||||||
|
|||||||
@@ -159,6 +159,16 @@ fn apply_f64_delta(current: f64, delta: f64) -> f64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_codex_window_minutes(code: &str) -> Option<u64> {
|
||||||
|
if code.eq_ignore_ascii_case("5h") {
|
||||||
|
Some(300)
|
||||||
|
} else if code.eq_ignore_ascii_case("weekly") {
|
||||||
|
Some(10_080)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn json_u64(value: Option<&Value>) -> Option<u64> {
|
fn json_u64(value: Option<&Value>) -> Option<u64> {
|
||||||
value.and_then(|value| {
|
value.and_then(|value| {
|
||||||
value.as_u64().or_else(|| {
|
value.as_u64().or_else(|| {
|
||||||
@@ -216,7 +226,9 @@ fn codex_window_matches_usage_time(window: &Map<String, Value>, usage_created_at
|
|||||||
let Some(reset_at) = json_u64(window.get("reset_at")) else {
|
let Some(reset_at) = json_u64(window.get("reset_at")) else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
let Some(window_minutes) = json_u64(window.get("window_minutes")) else {
|
let Some(window_minutes) =
|
||||||
|
json_u64(window.get("window_minutes")).or_else(|| default_codex_window_minutes(code))
|
||||||
|
else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
let Some(window_seconds) = window_minutes.checked_mul(60) else {
|
let Some(window_seconds) = window_minutes.checked_mul(60) else {
|
||||||
@@ -890,8 +902,7 @@ mod tests {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"code": "weekly",
|
"code": "weekly",
|
||||||
"reset_at": 700_000u64,
|
"reset_at": 700_000u64
|
||||||
"window_minutes": 10_080u64
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,11 @@ parsed_windows AS (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
THEN text_values.window_minutes_text::BIGINT
|
THEN text_values.window_minutes_text::BIGINT
|
||||||
ELSE NULL
|
ELSE CASE lower(BTRIM(COALESCE(window_items.window_item ->> 'code', '')))
|
||||||
|
WHEN '5h' THEN 300
|
||||||
|
WHEN 'weekly' THEN 10080
|
||||||
|
ELSE NULL
|
||||||
|
END
|
||||||
END AS window_minutes,
|
END AS window_minutes,
|
||||||
CASE
|
CASE
|
||||||
WHEN text_values.usage_reset_at_text ~ '^[0-9]+$'
|
WHEN text_values.usage_reset_at_text ~ '^[0-9]+$'
|
||||||
|
|||||||
@@ -43,7 +43,11 @@ parsed_windows AS (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
THEN text_values.window_minutes_text::BIGINT
|
THEN text_values.window_minutes_text::BIGINT
|
||||||
ELSE NULL
|
ELSE CASE lower(BTRIM(COALESCE(window_items.window_item ->> 'code', '')))
|
||||||
|
WHEN '5h' THEN 300
|
||||||
|
WHEN 'weekly' THEN 10080
|
||||||
|
ELSE NULL
|
||||||
|
END
|
||||||
END AS window_minutes,
|
END AS window_minutes,
|
||||||
CASE
|
CASE
|
||||||
WHEN text_values.usage_reset_at_text ~ '^[0-9]+$'
|
WHEN text_values.usage_reset_at_text ~ '^[0-9]+$'
|
||||||
|
|||||||
@@ -243,6 +243,11 @@ fn usage_sql_materializes_provider_key_window_usage_in_status_snapshot() {
|
|||||||
super::APPLY_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_DELTA_SQL.contains("'{quota,windows}'")
|
super::APPLY_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_DELTA_SQL.contains("'{quota,windows}'")
|
||||||
);
|
);
|
||||||
assert!(super::APPLY_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_DELTA_SQL.contains("'usage'"));
|
assert!(super::APPLY_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_DELTA_SQL.contains("'usage'"));
|
||||||
|
assert!(
|
||||||
|
super::APPLY_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_DELTA_SQL.contains("WHEN '5h' THEN 300")
|
||||||
|
);
|
||||||
|
assert!(super::APPLY_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_DELTA_SQL
|
||||||
|
.contains("WHEN 'weekly' THEN 10080"));
|
||||||
assert!(
|
assert!(
|
||||||
!super::APPLY_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_DELTA_SQL.contains("usage_billing_facts")
|
!super::APPLY_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_DELTA_SQL.contains("usage_billing_facts")
|
||||||
);
|
);
|
||||||
@@ -260,6 +265,11 @@ fn usage_sql_rebuilds_provider_key_window_usage_into_status_snapshot() {
|
|||||||
super::REBUILD_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_STATS_SQL.contains("'{quota,windows}'")
|
super::REBUILD_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_STATS_SQL.contains("'{quota,windows}'")
|
||||||
);
|
);
|
||||||
assert!(super::REBUILD_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_STATS_SQL.contains("'{usage}'"));
|
assert!(super::REBUILD_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_STATS_SQL.contains("'{usage}'"));
|
||||||
|
assert!(
|
||||||
|
super::REBUILD_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_STATS_SQL.contains("WHEN '5h' THEN 300")
|
||||||
|
);
|
||||||
|
assert!(super::REBUILD_PROVIDER_API_KEY_CODEX_WINDOW_USAGE_STATS_SQL
|
||||||
|
.contains("WHEN 'weekly' THEN 10080"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user