fix codex window usage stats

This commit is contained in:
fawney19
2026-05-07 13:35:25 +08:00
parent 64af7b1d8a
commit 0c03c188f1
5 changed files with 98 additions and 7 deletions

View File

@@ -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> {
value.and_then(|value| {
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 {
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;
};
let Some(window_seconds) = window_minutes.checked_mul(60) else {
@@ -890,8 +902,7 @@ mod tests {
},
{
"code": "weekly",
"reset_at": 700_000u64,
"window_minutes": 10_080u64
"reset_at": 700_000u64
}
]
}

View File

@@ -45,7 +45,11 @@ parsed_windows AS (
)
)
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,
CASE
WHEN text_values.usage_reset_at_text ~ '^[0-9]+$'

View File

@@ -43,7 +43,11 @@ parsed_windows AS (
)
)
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,
CASE
WHEN text_values.usage_reset_at_text ~ '^[0-9]+$'

View File

@@ -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}'")
);
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!(
!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}'")
);
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]