Files
codex/codex-rs/tui/src/analytics/models.rs
T
Felipe Coury 1fc46a532b Load analytics reports with server plans and account identity checks (#45762)
## Why

Token plan claims can be stale, and the active account or user can change while a request is in flight. Analytics report selection and account-bound response data need to reflect the verified identity and current server plan.

## What changed

- Fetch the active account's plan once per analytics session and use it to select report endpoints and supported credit breakdowns.
- Add report loading with a fixed end date, account-scoped response caching, and token model filtering. Reuse payloads across grouping changes and evict invalid cached responses so requests can retry.
- Prefer complete attribution for usage breakdowns within the requested range; retain legacy surface/model data when attribution is incomplete and include all features in turn-start breakdowns.
- Add cancellable report-loading state with timeout and interruption errors, and preserve actionable sign-in and retry messages.
- Recheck the active identity after rate-limit reads before exposing account-bound fields.

## Testing

Add regression tests for server plan discovery, report routing and caching, model filtering, attribution fallback, failed-request retries, load cancellation, authentication recovery, and account or user changes during requests.

GitOrigin-RevId: 45c4c09f108c9703893f3ed15613437ebd7c74a8
2026-09-15 19:00:56 +00:00

88 lines
2.5 KiB
Rust

//! Private analytics display types, independent of the app-server wire protocol.
use codex_protocol::account::PlanType;
/// Billing families match the App's consumer, business, and workspace scopes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum AccountKind {
Consumer,
Business,
Enterprise,
Unknown,
}
impl From<Option<PlanType>> for AccountKind {
fn from(plan: Option<PlanType>) -> Self {
match plan {
None | Some(PlanType::Unknown) => Self::Unknown,
Some(plan) if plan.is_team_like() => Self::Business,
Some(plan) if plan.is_workspace_account() => Self::Enterprise,
Some(_) => Self::Consumer,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AccountAnalyticsReport {
Usage,
Credits,
Messages,
Plugins,
Skills,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AccountAnalyticsGrouping {
Feature,
Model,
Surface,
TaskStart,
Speed,
Reasoning,
TokenType,
}
impl AccountAnalyticsGrouping {
/// Credit breakdowns supported by the account's billing report.
pub(crate) fn credit_groupings(plan: Option<PlanType>) -> &'static [Self] {
match AccountKind::from(plan) {
AccountKind::Business => &[Self::Surface, Self::Model, Self::Speed],
AccountKind::Enterprise => &[Self::Surface, Self::Model, Self::Speed, Self::Reasoning],
AccountKind::Consumer => &[Self::Surface],
AccountKind::Unknown => &[],
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AccountAnalyticsUnit {
RelativeUsage,
Credits,
Count,
Tokens,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct AccountAnalyticsHistory {
pub(crate) unit: AccountAnalyticsUnit,
/// Source freshness in Unix seconds, when provided. Not a completeness watermark.
pub(crate) updated_at: Option<i64>,
pub(crate) data: Vec<AccountAnalyticsDay>,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct AccountAnalyticsDay {
/// Source reporting date; display formatting belongs to the UI.
pub(crate) date: chrono::NaiveDate,
/// Full daily denominator, including non-task usage when grouped by task start.
pub(crate) total: f64,
pub(crate) values: Vec<AccountAnalyticsValue>,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct AccountAnalyticsValue {
pub(crate) key: String,
pub(crate) label: String,
pub(crate) value: f64,
}