Add an account Summary tab to Analytics (#45769)

## What changed

- Make Summary the initial Analytics tab, showing profile identity, token totals, streaks, activity insights, and most-used plugins and skills.
- Move daily, weekly, and cumulative token activity charts into Summary. Replace the usage menu's separate views with “View analytics”; `/usage weekly` and the other explicit modes open Summary with the requested view, while reopening from the menu retains navigation.
- Load account profiles independently of other reports, preserve missing values separately from zero, and reject responses when the active account changes. Refresh retries profile failures.
- Adapt Summary to narrow terminals and compact large chart totals and axis labels. Show the account email without its ID in the Analytics header.
- Flush queued transcript history before opening an overlay so startup output stays in the transcript.

## Testing

Add profile decoding and account identity tests, Summary layout and navigation snapshots, refresh and cancellation coverage, and regression tests for retained navigation and transcript history when opening Analytics.

GitOrigin-RevId: 6dbe6c4c98209000d8b4bc18a4e01b98fab5991a
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-09-15 19:13:08 +00:00
1 parent 8f0d2459ac
commit 9bd49c9dcc
122 files changed
+3685 -1980

No files matched your search

@@ -0,0 +1,78 @@
---
source: tui/src/app/tests/transcript_composer.rs
expression: snapshot
---
Analytics · Live account
[7d] 30d · [date range]
1 Summary 2 Total usage history 3 Messages [4 Plugins called] 5 Skills
used 6 Top chats
▎ Plugins called
────────────────────────────────────────────────────────────────────────────
No data reported for this range.
tab/1–6 section · z dashboard · ←/→ day
r 7/30d · R refresh · pgup/pgdn scroll · esc back · q close
Analytics · Live account
[7d] 30d · [date range]
1 Summary 2 Total usage history 3 Messages [4 Plugins called] 5 Skills
used 6 Top chats
▎ Plugins called
────────────────────────────────────────────────────────────────────────────
No data reported for this range.
tab/1–6 section · z dashboard · ←/→ day
r 7/30d · R refresh · pgup/pgdn scroll · esc back · q close
Analytics · Live account
[1 Summary] 2 Total usage history 3 Messages 4 Plugins called 5 Skills
used 6 Top chats
▎ Summary
────────────────────────────────────────────────────────────────────────────
Plus
12.3K — —
Lifetime tokens Peak tokens Longest chat
— —
Current streak Longest streak
Token activity Daily [Weekly] Cumulative
Last 12 months
Token activity history unavailable
Activity insights Most used plugins and skills
tab/1–6 section · z dashboard · ↑/↓ scroll
g view · R refresh · pgup/pgdn scroll · esc back · q close
@@ -47,8 +47,12 @@ async fn analytics_preserves_the_draft_and_retains_the_view_on_reopen() -> Resul
let mut tui = crate::tui::test_support::make_test_tui()?;
app.chat_widget
.apply_external_edit("preserved draft".into());
app.handle_event(&mut tui, &mut app_server, AppEvent::OpenAnalytics)
.await?;
app.handle_event(
&mut tui,
&mut app_server,
AppEvent::OpenAnalytics { view: None },
)
.await?;
for code in [
KeyCode::Char('4'),
KeyCode::Enter,
@@ -62,8 +66,12 @@ async fn analytics_preserves_the_draft_and_retains_the_view_on_reopen() -> Resul
press_key(&mut app, &mut tui, &mut app_server, KeyCode::Char('q')).await?;
assert!(app.overlay.is_none());
assert!(app.retained_analytics.is_some());
app.handle_event(&mut tui, &mut app_server, AppEvent::OpenAnalytics)
.await?;
app.handle_event(
&mut tui,
&mut app_server,
AppEvent::OpenAnalytics { view: None },
)
.await?;
assert!(matches!(app.overlay, Some(Overlay::Analytics(_))));
assert!(app.retained_analytics.is_none());
press_key(&mut app, &mut tui, &mut app_server, KeyCode::Char('q')).await?;
@@ -74,6 +82,138 @@ async fn analytics_preserves_the_draft_and_retains_the_view_on_reopen() -> Resul
Ok(())
}
#[tokio::test]
async fn analytics_menu_reopen_preserves_navigation_and_explicit_view_selects_summary() -> Result<()>
{
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
let http = wiremock::MockServer::start().await;
wiremock::Mock::given(wiremock::matchers::method("GET"))
.and(wiremock::matchers::path("/backend-api/wham/accounts/check"))
.and(wiremock::matchers::header(
"chatgpt-account-id",
"test-account",
))
.respond_with(wiremock::ResponseTemplate::new(/*s*/ 200).set_body_json(
serde_json::json!({"accounts": [{"id": "test-account", "plan_type": "plus"}]}),
))
.expect(/*r*/ 3)
.mount(&http)
.await;
wiremock::Mock::given(wiremock::matchers::method("GET"))
.respond_with(
wiremock::ResponseTemplate::new(/*s*/ 200)
.set_body_json(serde_json::json!({"stats":{"lifetime_tokens":12345},"data":[]})),
)
.mount(&http)
.await;
app.config.chatgpt_base_url = format!("{}/backend-api", http.uri());
app.config.cli_auth_credentials_store_mode = codex_login::AuthCredentialsStoreMode::File;
app_test_support::write_chatgpt_auth(
&app.config.codex_home,
app_test_support::ChatGptAuthFixture::new("test-access-token")
.account_id("test-account")
.chatgpt_account_id("test-account")
.chatgpt_user_id("test-user")
.plan_type("plus"),
codex_login::AuthCredentialsStoreMode::File,
)
.unwrap();
let mut app_server = start_config_write_test_app_server(&app).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
let mut screens = Vec::new();
for explicit in [
None,
None,
Some(crate::analytics::TokenActivityView::Weekly),
] {
app.handle_event(
&mut tui,
&mut app_server,
AppEvent::OpenAnalytics { view: explicit },
)
.await?;
tokio::time::timeout(std::time::Duration::from_secs(/*secs*/ 10), async {
loop {
app.handle_tui_event(&mut tui, &mut app_server, TuiEvent::Draw)
.await?;
let text = buffer_text(crate::custom_terminal::test_support::last_rendered_buffer(
&tui.terminal,
));
if screens.is_empty() && text.contains("Lifetime tokens") {
press_key(&mut app, &mut tui, &mut app_server, KeyCode::Char('4')).await?;
continue;
}
if text.contains("Lifetime tokens")
|| text.contains("No activity")
|| text.contains("No data reported")
{
return Ok::<(), color_eyre::Report>(());
}
tokio::time::sleep(std::time::Duration::from_millis(/*millis*/ 5)).await;
}
})
.await??;
let screen = buffer_text(crate::custom_terminal::test_support::last_rendered_buffer(
&tui.terminal,
));
screens.push(screen);
press_key(&mut app, &mut tui, &mut app_server, KeyCode::Char('q')).await?;
}
assert_eq!(screens[0], screens[1]);
assert!(screens[1].contains("[4 Plugins called]"));
assert!(screens[2].contains("[1 Summary]"));
assert!(screens[2].contains("[Weekly]"));
let snapshot = screens.join("\n\n");
let snapshot = snapshot
.lines()
.map(|line| {
if line.starts_with(" [7d] 30d · ") {
" [7d] 30d · [date range]"
} else {
line
}
})
.collect::<Vec<_>>()
.join("\n");
insta::assert_snapshot!(snapshot);
Ok(())
}
#[tokio::test]
async fn analytics_keeps_queued_and_late_startup_history_out_of_the_overlay() -> Result<()> {
for alternate_screen in [true, false] {
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
let mut app_server = start_config_write_test_app_server(&app).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
tui.set_alt_screen_enabled(alternate_screen);
app.insert_history_cell(
&mut tui,
Box::new(PlainHistoryCell::new(vec!["Startup header".into()])),
);
assert!(!tui.pending_history_lines_for_test().is_empty());
app.handle_event(
&mut tui,
&mut app_server,
AppEvent::OpenAnalytics { view: None },
)
.await?;
assert!(tui.pending_history_lines_for_test().is_empty());
app.insert_history_cell(
&mut tui,
Box::new(PlainHistoryCell::new(vec![
"Late startup announcement".into(),
])),
);
assert!(tui.pending_history_lines_for_test().is_empty());
let deferred = app.deferred_history_lines.clone();
assert!(!deferred.is_empty());
press_key(&mut app, &mut tui, &mut app_server, KeyCode::Char('q')).await?;
assert_eq!(tui.pending_history_lines_for_test(), deferred);
assert_eq!(app.transcript_cells.len(), 2);
}
Ok(())
}
#[tokio::test]
async fn transcript_flag_off_preserves_viewer_and_backtracking() -> Result<()> {
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;