Add consumer Top chats usage analytics (#45768)

## Why

Consumer analytics lacked a per-chat view of allowance usage and balance credit debits.

## What changed

- Add a Top chats panel for up to 100 local chats active in the past 30 days, including discovered descendants and excluding archived roots.
- Show weekly and five-hour usage as percentages of current full limits, plus balance credits including adjustments. Preserve exact credit decimals and distinguish missing values from zero.
- Support cycling sort metrics with `s`, a top-five dashboard summary, and expandable model, reasoning effort, and speed breakdowns.
- Query task usage through `query_v2` with bounded, disjoint descendant groups. Retain partial and unavailable rows, hide titles when usage is unavailable, and report ranking coverage and freshness.

## Testing

Add backend and TUI tests covering decimal precision, request and response validation, paginated archived descendants, unavailable responses, metric selection, title hiding, and top-five rendering.

GitOrigin-RevId: b8f233166576389c8212bb97eceb7c6be7f5f51d
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-09-15 19:12:44 +00:00
1 parent 0d0979f457
commit 8f0d2459ac
41 files changed
+1825 -194

No files matched your search

+17 -1
View File
@@ -81,6 +81,7 @@ impl AnalyticsView {
section.history.poll();
}
self.chats.poll();
self.tasks.poll();
self.plan.poll();
self.account.poll();
self.start_reports();
@@ -96,6 +97,11 @@ impl AnalyticsView {
{
self.group_picker = None;
}
if let Some(chats) = self.tasks.ready() {
self.sections[Section::Chats].cursor = self.sections[Section::Chats]
.cursor
.min(chats.rows.len().saturating_sub(/*rhs*/ 1));
}
if let Some(chats) = self.chats.ready() {
self.sections[Section::Chats].cursor = self.sections[Section::Chats]
.cursor
@@ -145,12 +151,14 @@ impl AnalyticsView {
Section::Activity,
Section::Plugins,
Section::Skills,
Section::Chats,
],
Some(AccountKind::Consumer) => &[
Section::Usage,
Section::Activity,
Section::Plugins,
Section::Skills,
Section::Chats,
],
Some(AccountKind::Business | AccountKind::Enterprise)
if super::models::thread_usage_supported(self.account.ready().copied()) =>
@@ -216,7 +224,15 @@ impl AnalyticsView {
let live = std::sync::Arc::clone(live);
self.plan.report = Load::start(async move { live.plan_history().await }, frame.clone());
}
if visible.contains(&Section::Chats) {
if visible.contains(&Section::Chats) && !self.business() {
if let (Some((_, handle, frame)), Some(live)) = (&self.connection, &self.live) {
self.tasks = Load::start_with_timeout(
super::tasks::read(handle.clone(), std::sync::Arc::clone(live)),
frame.clone(),
std::time::Duration::from_secs(/*secs*/ 120),
);
}
} else if visible.contains(&Section::Chats) {
self.chats =
if let (Some((_, handle, frame)), Some(live)) = (&self.connection, &self.live) {
Load::start_with_timeout(