use std::{
collections::{BTreeSet, HashMap, HashSet},
hash::Hash,
io,
sync::mpsc::{self, Receiver, Sender},
thread,
time::{Duration, Instant},
};
use anyhow::Result;
use chrono::{DateTime, Local};
use crossterm::{
event::{
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, KeyModifiers,
MouseButton, MouseEvent, MouseEventKind,
},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, layout::Rect, Terminal};
use crate::{
analytics,
config::{self, ColorTheme, Config, Scope, ThemeScope},
db::{self, UsageStats},
index,
sources::{self, SyncMode, SyncProgress, SyncReport, SyncSummary},
time_window::{self, CalendarScale, DailyStart, Mode, PeriodKey, WeekStart},
tui,
};
mod action;
use action::Action;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum View {
Dashboard,
CalendarOverview,
CalendarDetail,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GraphMetric {
Tokens,
Cost,
}
impl GraphMetric {
fn toggle(self) -> Self {
match self {
Self::Tokens => Self::Cost,
Self::Cost => Self::Tokens,
}
}
}
pub struct CalendarState {
pub scale: CalendarScale,
pub selected: PeriodKey,
pub visible_periods: Vec<PeriodKey>,
}
pub struct ScopePickerState {
pub selection: usize,
}
pub struct ProviderPickerState {
pub selection: usize,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SourceSyncStage {
Pending,
Syncing,
Ready { scanned: usize, imported: usize },
Failed,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SourceSyncItem {
pub name: String,
pub stage: SourceSyncStage,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct SourceSyncStatus {
pub sources: Vec<SourceSyncItem>,
}
impl SourceSyncStatus {
pub fn completed(&self) -> usize {
self.sources
.iter()
.filter(|source| {
matches!(
source.stage,
SourceSyncStage::Ready { .. } | SourceSyncStage::Failed
)
})
.count()
}
pub fn active_names(&self) -> Vec<&str> {
self.sources
.iter()
.filter(|source| source.stage == SourceSyncStage::Syncing)
.map(|source| source.name.as_str())
.collect()
}
}
pub struct DeferredLoadState<K> {
pub loading: HashSet<K>,
pub pending: HashSet<K>,
}
impl<K> Default for DeferredLoadState<K> {
fn default() -> Self {
Self {
loading: HashSet::new(),
pending: HashSet::new(),
}
}
}
impl<K: Copy + Eq + Hash> DeferredLoadState<K> {
fn mark_pending(&mut self, key: K) {
self.pending.insert(key);
}
fn clear_pending(&mut self, key: K) {
self.pending.remove(&key);
}
fn begin(&mut self, key: K) -> bool {
if self.loading.contains(&key) || !self.pending.contains(&key) {
false
} else {
self.loading.insert(key);
true
}
}
fn finish(&mut self, key: K) {
self.loading.remove(&key);
}
fn reset(&mut self) {
self.loading.clear();
self.pending.clear();
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ConfigNotice {
pub message: String,
pub is_error: bool,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ConfigEditorItem {
AutoRefresh,
DailyStart,
RefreshSeconds,
WeekStart,
ColorTheme,
ThemeScope,
ShowComparison,
EstimateApiCost,
HiddenProviders,
ProviderAliases,
ModelAliases,
}
impl ConfigEditorItem {
pub const ALL: [Self; 11] = [
Self::AutoRefresh,
Self::DailyStart,
Self::RefreshSeconds,
Self::WeekStart,
Self::ColorTheme,
Self::ThemeScope,
Self::ShowComparison,
Self::EstimateApiCost,
Self::HiddenProviders,
Self::ProviderAliases,
Self::ModelAliases,
];
pub fn label(self) -> &'static str {
match self {
Self::AutoRefresh => "auto_refresh",
Self::DailyStart => "daily_start",
Self::RefreshSeconds => "refresh_seconds",
Self::WeekStart => "week_start",
Self::ColorTheme => "color_theme",
Self::ThemeScope => "theme_scope",
Self::ShowComparison => "show_comparison",
Self::EstimateApiCost => "estimate_api_cost",
Self::HiddenProviders => "hidden_providers",
Self::ProviderAliases => "provider_aliases",
Self::ModelAliases => "model_aliases",
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AliasKind {
Provider,
Model,
}
impl AliasKind {
pub fn title(self) -> &'static str {
match self {
Self::Provider => "Provider aliases",
Self::Model => "Model aliases",
}
}
pub fn source_label(self) -> &'static str {
match self {
Self::Provider => "Provider ID",
Self::Model => "Model ID or provider/model",
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AliasInputField {
Source,
Alias,
}
pub struct AliasInputState {
pub original_source: Option<String>,
pub source: String,
pub alias: String,
pub field: AliasInputField,
}
pub struct AliasEditorState {
pub kind: AliasKind,
pub selection: usize,
pub input: Option<AliasInputState>,
}
pub struct AppState {
pub config: Config,
pub view: View,
pub show_help: bool,
pub scope_picker: Option<ScopePickerState>,
pub provider_picker: Option<ProviderPickerState>,
pub alias_editor: Option<AliasEditorState>,
pub projects: Vec<db::ProjectInfo>,
pub providers: Vec<String>,
pub help_scroll: usize,
pub dashboard_model_scroll: usize,
pub history_model_scroll: usize,
pub dashboard_token_bucket: Option<usize>,
pub history_token_bucket: Option<usize>,
pub token_graph_dragging: bool,
pub graph_metric: GraphMetric,
pub config_selection: usize,
pub config_notice: Option<ConfigNotice>,
pub mode: Mode,
pub stats: HashMap<Mode, UsageStats>,
pub loading: HashSet<Mode>,
pub dashboard_graph_load: DeferredLoadState<Mode>,
pub dashboard_prefetch_attempted: HashSet<Mode>,
pub calendar: CalendarState,
pub calendar_costs: HashMap<PeriodKey, f64>,
pub calendar_loading: bool,
pub history_stats: HashMap<PeriodKey, UsageStats>,
pub history_loading: HashSet<PeriodKey>,
pub history_graph_load: DeferredLoadState<PeriodKey>,
pub error: Option<String>,
pub source_sync: Option<SyncMode>,
pub source_sync_status: Option<SourceSyncStatus>,
pub source_sync_error: Option<String>,
pub first_sync: bool,
pub last_refresh_started: Option<DateTime<Local>>,
pub next_refresh_due: Instant,
pub logical_day: PeriodKey,
pub(crate) pending_source_sync: Option<SyncMode>,
pub(crate) request_generation: u64,
}
impl AppState {
fn new(config: Config) -> Result<Self> {
let next_refresh_due = Instant::now() + config.refresh_interval;
let selected = time_window::current_period(
CalendarScale::Day,
Local::now(),
config.daily_start,
config.week_start,
)?;
let visible_periods =
time_window::visible_periods(selected, config.daily_start, config.week_start)?;
let first_sync = index::UsageIndex::open(&config.index_path)
.and_then(|index| index.diagnostics())
.map(|diagnostics| diagnostics.sources == 0 && diagnostics.artifacts == 0)
.unwrap_or(false);
let projects = index::list_projects(&config.index_path).unwrap_or_default();
let providers = index::list_providers(&config.index_path).unwrap_or_default();
Ok(Self {
config,
view: View::Dashboard,
show_help: false,
scope_picker: None,
provider_picker: None,
alias_editor: None,
projects,
providers,
help_scroll: 0,
dashboard_model_scroll: 0,
history_model_scroll: 0,
dashboard_token_bucket: None,
history_token_bucket: None,
token_graph_dragging: false,
graph_metric: GraphMetric::Tokens,
config_selection: 0,
config_notice: None,
mode: Mode::Daily,
stats: HashMap::new(),
loading: HashSet::new(),
dashboard_graph_load: DeferredLoadState::default(),
dashboard_prefetch_attempted: HashSet::new(),
calendar: CalendarState {
scale: CalendarScale::Day,
selected,
visible_periods,
},
calendar_costs: HashMap::new(),
calendar_loading: false,
history_stats: HashMap::new(),
history_loading: HashSet::new(),
history_graph_load: DeferredLoadState::default(),
error: None,
source_sync: None,
source_sync_status: None,
source_sync_error: None,
first_sync,
last_refresh_started: None,
next_refresh_due,
logical_day: selected,
pending_source_sync: None,
request_generation: 0,
})
}
pub fn current_stats(&self) -> Option<&UsageStats> {
self.stats.get(&self.mode)
}
pub fn is_current_loading(&self) -> bool {
self.loading.contains(&self.mode)
}
pub fn is_current_graph_loading(&self) -> bool {
self.dashboard_graph_load.loading.contains(&self.mode)
}
pub fn selected_history_stats(&self) -> Option<&UsageStats> {
self.history_stats.get(&self.calendar.selected)
}
pub fn is_selected_history_loading(&self) -> bool {
self.history_loading.contains(&self.calendar.selected)
}
pub fn is_selected_history_graph_loading(&self) -> bool {
self.history_graph_load
.loading
.contains(&self.calendar.selected)
}
pub fn calendar_cost(&self, period: PeriodKey) -> Option<f64> {
self.calendar_costs.get(&period).copied()
}
pub fn selected_config_item(&self) -> ConfigEditorItem {
ConfigEditorItem::ALL[self.config_selection.min(ConfigEditorItem::ALL.len() - 1)]
}
fn toggle_help(&mut self) {
self.show_help = !self.show_help;
self.scope_picker = None;
self.provider_picker = None;
self.alias_editor = None;
if self.show_help {
self.help_scroll = 0;
self.config_selection = 0;
}
}
pub fn alias_entries(&self, kind: AliasKind) -> Vec<(String, String)> {
let aliases = match kind {
AliasKind::Provider => &self.config.aliases.providers,
AliasKind::Model => &self.config.aliases.models,
};
aliases
.iter()
.map(|(source, alias)| (source.clone(), alias.clone()))
.collect()
}
fn open_alias_editor(&mut self, kind: AliasKind) {
self.scope_picker = None;
self.provider_picker = None;
self.alias_editor = Some(AliasEditorState {
kind,
selection: 0,
input: None,
});
}
pub fn provider_entries(&self) -> Vec<String> {
self.providers
.iter()
.cloned()
.chain(self.config.hidden_providers.iter().cloned())
.collect::<BTreeSet<_>>()
.into_iter()
.collect()
}
fn open_provider_picker(&mut self) {
if let Ok(providers) = index::list_providers(&self.config.index_path) {
if !providers.is_empty() || self.providers.is_empty() {
self.providers = providers;
}
}
self.scope_picker = None;
self.alias_editor = None;
self.provider_picker = Some(ProviderPickerState { selection: 0 });
}
fn move_provider_selection(&mut self, direction: i32) {
let option_count = self.provider_entries().len();
let Some(picker) = self.provider_picker.as_mut() else {
return;
};
if direction > 0 {
picker.selection = (picker.selection + 1).min(option_count.saturating_sub(1));
} else {
picker.selection = picker.selection.saturating_sub(1);
}
}
fn toggle_selected_provider(&mut self, tx: &Sender<RefreshMessage>) {
let Some(selection) = self.provider_picker.as_ref().map(|picker| picker.selection) else {
return;
};
let Some(provider) = self.provider_entries().get(selection).cloned() else {
return;
};
if !self.config.hidden_providers.remove(&provider) {
self.config.hidden_providers.insert(provider);
}
self.invalidate_usage_config(tx);
self.save_config_notice();
}
fn move_alias_selection(&mut self, direction: i32) {
let Some(editor) = self.alias_editor.as_ref() else {
return;
};
let option_count = self.alias_entries(editor.kind).len().saturating_add(1);
let Some(editor) = self.alias_editor.as_mut() else {
return;
};
if direction > 0 {
editor.selection = (editor.selection + 1).min(option_count.saturating_sub(1));
} else {
editor.selection = editor.selection.saturating_sub(1);
}
}
fn begin_alias_input(&mut self) {
let Some(editor) = self.alias_editor.as_ref() else {
return;
};
let entries = self.alias_entries(editor.kind);
let selected = entries.get(editor.selection).cloned();
let Some(editor) = self.alias_editor.as_mut() else {
return;
};
editor.input = Some(match selected {
Some((source, alias)) => AliasInputState {
original_source: Some(source.clone()),
source,
alias,
field: AliasInputField::Source,
},
None => AliasInputState {
original_source: None,
source: String::new(),
alias: String::new(),
field: AliasInputField::Source,
},
});
}
fn delete_selected_alias(&mut self) {
let Some(editor) = self.alias_editor.as_ref() else {
return;
};
let entries = self.alias_entries(editor.kind);
let Some((source, _)) = entries.get(editor.selection) else {
return;
};
match editor.kind {
AliasKind::Provider => self.config.aliases.providers.remove(source),
AliasKind::Model => self.config.aliases.models.remove(source),
};
let remaining = entries.len().saturating_sub(1);
if let Some(editor) = self.alias_editor.as_mut() {
editor.selection = editor.selection.min(remaining);
}
self.apply_cached_aliases();
self.save_config_notice();
}
fn save_alias_input(&mut self) {
let Some(editor) = self.alias_editor.as_mut() else {
return;
};
let Some(input) = editor.input.take() else {
return;
};
let source = input.source.trim().to_string();
let alias = input.alias.trim().to_string();
if source.is_empty() || alias.is_empty() {
editor.input = Some(input);
self.config_notice = Some(ConfigNotice {
message: "alias source and replacement are required".to_string(),
is_error: true,
});
return;
}
let aliases = match editor.kind {
AliasKind::Provider => &mut self.config.aliases.providers,
AliasKind::Model => &mut self.config.aliases.models,
};
if let Some(original) = input.original_source {
if original != source {
aliases.remove(&original);
}
}
aliases.insert(source.clone(), alias);
editor.selection = aliases.keys().position(|key| key == &source).unwrap_or(0);
self.apply_cached_aliases();
self.save_config_notice();
}
fn apply_cached_aliases(&mut self) {
for stats in self
.stats
.values_mut()
.chain(self.history_stats.values_mut())
{
analytics::apply_model_aliases(&self.config, stats);
}
}
pub fn scope_label(&self) -> String {
match &self.config.scope {
Scope::All => "all projects".to_string(),
Scope::Current => self
.projects
.iter()
.filter(|project| {
std::path::Path::new(&self.config.current_directory)
.starts_with(std::path::Path::new(&project.worktree))
})
.max_by_key(|project| std::path::Path::new(&project.worktree).components().count())
.map(|project| project.name.clone())
.unwrap_or_else(|| "current project".to_string()),
Scope::Project(id) => self
.projects
.iter()
.find(|project| &project.id == id)
.map(|project| project.name.clone())
.unwrap_or_else(|| id.clone()),
}
}
fn open_scope_picker(&mut self) {
if let Ok(projects) = index::list_projects(&self.config.index_path) {
if !projects.is_empty() || self.projects.is_empty() {
self.projects = projects;
}
}
let selection = match &self.config.scope {
Scope::All => 0,
Scope::Current => 1,
Scope::Project(id) => self
.projects
.iter()
.position(|project| &project.id == id)
.map(|index| index + 2)
.unwrap_or(0),
};
self.show_help = false;
self.provider_picker = None;
self.alias_editor = None;
self.scope_picker = Some(ScopePickerState { selection });
}
fn move_scope_selection(&mut self, direction: i32) {
let option_count = self.projects.len().saturating_add(2);
let Some(picker) = self.scope_picker.as_mut() else {
return;
};
if direction > 0 {
picker.selection = (picker.selection + 1).min(option_count.saturating_sub(1));
} else {
picker.selection = picker.selection.saturating_sub(1);
}
}
fn apply_scope_selection(&mut self, tx: &Sender<RefreshMessage>) {
let Some(picker) = self.scope_picker.take() else {
return;
};
let scope = match picker.selection {
0 => Scope::All,
1 => Scope::Current,
index => match self.projects.get(index - 2) {
Some(project) => Scope::Project(project.id.clone()),
None => return,
},
};
if self.config.scope == scope {
return;
}
self.config.scope = scope;
self.invalidate_usage_config(tx);
self.save_config_notice();
}
fn invalidate_usage_config(&mut self, tx: &Sender<RefreshMessage>) {
self.request_generation = self.request_generation.wrapping_add(1);
self.stats.clear();
self.loading.clear();
self.dashboard_graph_load.reset();
self.dashboard_prefetch_attempted.clear();
self.calendar_costs.clear();
self.calendar_loading = false;
self.history_stats.clear();
self.history_loading.clear();
self.history_graph_load.reset();
self.dashboard_token_bucket = None;
self.history_token_bucket = None;
self.error = None;
self.trigger_current_refresh(tx);
}
fn switch_mode(&mut self, mode: Mode, tx: &Sender<RefreshMessage>) {
self.view = View::Dashboard;
if self.mode != mode {
self.dashboard_model_scroll = 0;
self.dashboard_token_bucket = None;
}
self.mode = mode;
if !self.stats.contains_key(&mode) {
self.trigger_dashboard_refresh(tx);
}
}
fn trigger_source_sync(&mut self, mode: SyncMode, tx: &Sender<RefreshMessage>) {
if self.source_sync.is_some() {
self.pending_source_sync = Some(match (self.pending_source_sync, mode) {
(Some(SyncMode::Full), _) | (_, SyncMode::Full) => SyncMode::Full,
_ => SyncMode::Incremental,
});
return;
}
self.source_sync = Some(mode);
self.source_sync_status = Some(SourceSyncStatus::default());
self.source_sync_error = None;
let tx = tx.clone();
let config = self.config.clone();
thread::spawn(move || {
let progress_tx = tx.clone();
let result = sources::sync_configured_with_progress(&config, mode, |progress| {
let _ = progress_tx.send(RefreshMessage::SourceSyncProgress { progress });
})
.map_err(|error| format!("{error:#}"));
let _ = tx.send(RefreshMessage::SourceSync { result });
});
}
fn apply_source_sync_progress(&mut self, progress: SyncProgress, tx: &Sender<RefreshMessage>) {
match progress {
SyncProgress::Planned { sources } => {
self.source_sync_status = Some(SourceSyncStatus {
sources: sources
.into_iter()
.map(|name| SourceSyncItem {
name,
stage: SourceSyncStage::Pending,
})
.collect(),
});
}
SyncProgress::Started { source } => {
self.update_source_stage(&source, SourceSyncStage::Syncing);
}
SyncProgress::Finished {
source,
report,
error,
changed,
} => {
let stage = match (report, error) {
(
Some(SyncReport {
scanned, imported, ..
}),
_,
) => SourceSyncStage::Ready { scanned, imported },
(_, Some(_)) | (None, None) => SourceSyncStage::Failed,
};
self.update_source_stage(&source, stage);
if changed {
if let Ok(projects) = index::list_projects(&self.config.index_path) {
self.projects = projects;
}
if let Ok(providers) = index::list_providers(&self.config.index_path) {
self.providers = providers;
}
self.invalidate_after_source_change(tx);
}
}
}
}
fn update_source_stage(&mut self, source_name: &str, stage: SourceSyncStage) {
let status = self
.source_sync_status
.get_or_insert_with(SourceSyncStatus::default);
if let Some(source) = status
.sources
.iter_mut()
.find(|source| source.name == source_name)
{
source.stage = stage;
} else {
status.sources.push(SourceSyncItem {
name: source_name.to_string(),
stage,
});
}
}
fn apply_source_sync(
&mut self,
result: std::result::Result<SyncSummary, String>,
tx: &Sender<RefreshMessage>,
) {
self.source_sync = None;
self.first_sync = false;
match result {
Ok(summary) => {
self.source_sync_error =
(!summary.errors.is_empty()).then(|| summary.errors.join("; "));
if let Ok(projects) = index::list_projects(&self.config.index_path) {
self.projects = projects;
}
if let Ok(providers) = index::list_providers(&self.config.index_path) {
self.providers = providers;
}
}
Err(error) => self.source_sync_error = Some(error),
}
self.source_sync_status = None;
if let Some(mode) = self.pending_source_sync.take() {
self.trigger_source_sync(mode, tx);
}
}
fn invalidate_after_source_change(&mut self, tx: &Sender<RefreshMessage>) {
self.request_generation = self.request_generation.wrapping_add(1);
self.loading.clear();
self.dashboard_graph_load.reset();
self.dashboard_prefetch_attempted.clear();
self.calendar_costs.clear();
self.calendar_loading = false;
self.history_loading.clear();
self.history_graph_load.reset();
match self.view {
View::Dashboard => {
self.stats.retain(|mode, _| *mode == self.mode);
self.history_stats.clear();
}
View::CalendarOverview => {
self.stats.clear();
self.history_stats.clear();
}
View::CalendarDetail => {
let selected = self.calendar.selected;
self.stats.clear();
self.history_stats.retain(|period, _| *period == selected);
}
}
self.trigger_current_refresh(tx);
}
fn trigger_current_refresh(&mut self, tx: &Sender<RefreshMessage>) {
match self.view {
View::Dashboard => self.trigger_dashboard_refresh(tx),
View::CalendarOverview => self.trigger_calendar_refresh(tx),
View::CalendarDetail => self.trigger_history_refresh(tx),
}
}
fn trigger_dashboard_refresh(&mut self, tx: &Sender<RefreshMessage>) {
self.trigger_dashboard_refresh_for_mode(self.mode, tx, true);
}
fn trigger_dashboard_refresh_for_mode(
&mut self,
mode: Mode,
tx: &Sender<RefreshMessage>,
foreground: bool,
) {
if self.loading.contains(&mode) {
return;
}
self.loading.insert(mode);
if foreground {
self.dashboard_prefetch_attempted.clear();
self.last_refresh_started = Some(Local::now());
self.error = None;
}
let tx = tx.clone();
let config = self.config.clone();
let generation = self.request_generation;
thread::spawn(move || {
let result =
refresh_dashboard_summary(config, mode).map_err(|error| format!("{error:#}"));
let _ = tx.send(RefreshMessage::Dashboard {
generation,
mode,
result,
});
});
}
fn trigger_dashboard_graph_refresh(&mut self, mode: Mode, tx: &Sender<RefreshMessage>) {
if !self.dashboard_graph_load.begin(mode) {
return;
}
let Some(stats) = self.stats.get(&mode) else {
self.dashboard_graph_load.finish(mode);
return;
};
if stats.totals.messages == 0 {
self.dashboard_graph_load.finish(mode);
self.dashboard_graph_load.clear_pending(mode);
return;
}
let cutoff_millis = stats.cutoff_millis;
let end_millis = stats.end_millis;
let snapshot_millis = stats.snapshot_millis;
let tx = tx.clone();
let config = self.config.clone();
let generation = self.request_generation;
thread::spawn(move || {
let result = index::load_usage_token_buckets_at_scoped_with_options(
&config.index_path,
mode,
cutoff_millis,
end_millis,
snapshot_millis,
index::UsageQueryOptions {
scope: &config.scope,
current_directory: &config.current_directory,
hidden_providers: &config.hidden_providers,
estimate_api_cost: config.estimate_api_cost,
},
)
.map_err(|error| format!("{error:#}"));
let _ = tx.send(RefreshMessage::DashboardGraph {
generation,
mode,
cutoff_millis,
snapshot_millis,
result,
});
});
}
fn enter_calendar(&mut self, tx: &Sender<RefreshMessage>) {
self.view = View::CalendarOverview;
self.error = None;
self.ensure_calendar_costs(tx);
}
fn open_calendar_detail(&mut self, tx: &Sender<RefreshMessage>) {
self.view = View::CalendarDetail;
self.history_model_scroll = 0;
self.history_token_bucket = None;
self.error = None;
if !self.history_stats.contains_key(&self.calendar.selected) {
self.trigger_history_refresh(tx);
}
}
fn set_calendar_scale(
&mut self,
scale: CalendarScale,
tx: &Sender<RefreshMessage>,
) -> Result<()> {
let selected_start = local_from_millis(self.calendar.selected.start_millis)?;
self.calendar.scale = scale;
self.calendar.selected = time_window::current_period(
scale,
selected_start,
self.config.daily_start,
self.config.week_start,
)?;
self.history_model_scroll = 0;
self.history_token_bucket = None;
self.sync_visible_periods()?;
match self.view {
View::Dashboard => {}
View::CalendarOverview => self.ensure_calendar_costs(tx),
View::CalendarDetail => {
self.ensure_calendar_costs(tx);
self.trigger_history_refresh(tx);
}
}
Ok(())
}
fn move_calendar_selection(&mut self, steps: i32, tx: &Sender<RefreshMessage>) -> Result<()> {
self.calendar.selected = time_window::shift_period(self.calendar.selected, steps)?;
self.history_model_scroll = 0;
self.history_token_bucket = None;
self.sync_visible_periods()?;
self.ensure_calendar_costs(tx);
if self.view == View::CalendarDetail {
self.trigger_history_refresh(tx);
}
Ok(())
}
fn select_calendar_period(
&mut self,
period: PeriodKey,
tx: &Sender<RefreshMessage>,
) -> Result<()> {
self.calendar.scale = period.scale;
self.calendar.selected = period;
self.history_model_scroll = 0;
self.history_token_bucket = None;
self.sync_visible_periods()?;
self.ensure_calendar_costs(tx);
Ok(())
}
fn sync_visible_periods(&mut self) -> Result<()> {
self.calendar.visible_periods = time_window::visible_periods(
self.calendar.selected,
self.config.daily_start,
self.config.week_start,
)?;
Ok(())
}
fn ensure_calendar_costs(&mut self, tx: &Sender<RefreshMessage>) {
if self
.calendar
.visible_periods
.iter()
.any(|period| !self.calendar_costs.contains_key(period))
{
self.trigger_calendar_refresh(tx);
}
}
fn trigger_calendar_refresh(&mut self, tx: &Sender<RefreshMessage>) {
if self.calendar_loading {
return;
}
let periods = self.calendar.visible_periods.clone();
self.calendar_loading = true;
self.last_refresh_started = Some(Local::now());
self.error = None;
let tx = tx.clone();
let config = self.config.clone();
let generation = self.request_generation;
thread::spawn(move || {
let result = index::load_period_costs_scoped_with_options(
&config.index_path,
&periods,
index::UsageQueryOptions {
scope: &config.scope,
current_directory: &config.current_directory,
hidden_providers: &config.hidden_providers,
estimate_api_cost: config.estimate_api_cost,
},
)
.map_err(|error| format!("{error:#}"));
let _ = tx.send(RefreshMessage::Calendar { generation, result });
});
}
fn trigger_history_refresh(&mut self, tx: &Sender<RefreshMessage>) {
let period = self.calendar.selected;
if self.history_loading.contains(&period) {
return;
}
self.history_loading.insert(period);
self.last_refresh_started = Some(Local::now());
self.error = None;
let tx = tx.clone();
let config = self.config.clone();
let generation = self.request_generation;
thread::spawn(move || {
let result =
refresh_period_summary(config, period).map_err(|error| format!("{error:#}"));
let _ = tx.send(RefreshMessage::History {
generation,
period,
result,
});
});
}
fn trigger_history_graph_refresh(&mut self, period: PeriodKey, tx: &Sender<RefreshMessage>) {
if !self.history_graph_load.begin(period) {
return;
}
let Some(stats) = self.history_stats.get(&period) else {
self.history_graph_load.finish(period);
return;
};
if stats.totals.messages == 0 {
self.history_graph_load.finish(period);
self.history_graph_load.clear_pending(period);
return;
}
let snapshot_millis = stats.snapshot_millis;
let tx = tx.clone();
let config = self.config.clone();
let generation = self.request_generation;
thread::spawn(move || {
let result = index::load_usage_token_buckets_at_scoped_with_options(
&config.index_path,
period.mode(),
Some(period.start_millis),
Some(period.end_millis),
snapshot_millis,
index::UsageQueryOptions {
scope: &config.scope,
current_directory: &config.current_directory,
hidden_providers: &config.hidden_providers,
estimate_api_cost: config.estimate_api_cost,
},
)
.map_err(|error| format!("{error:#}"));
let _ = tx.send(RefreshMessage::HistoryGraph {
generation,
period,
snapshot_millis,
result,
});
});
}
fn apply_dashboard_refresh(
&mut self,
mode: Mode,
result: std::result::Result<UsageStats, String>,
) {
self.loading.remove(&mode);
match result {
Ok(mut stats) => {
let mut retained_graph = false;
if let Some(previous) = self.stats.get(&mode).filter(|previous| {
previous.cutoff_millis == stats.cutoff_millis
&& previous.end_millis == stats.end_millis
&& stats.totals.messages > 0
}) {
stats.token_buckets.clone_from(&previous.token_buckets);
retained_graph = !stats.token_buckets.is_empty();
}
if stats.totals.messages > 0 {
self.dashboard_graph_load.mark_pending(mode);
} else {
self.dashboard_graph_load.clear_pending(mode);
}
self.stats.insert(mode, stats);
if self.view == View::Dashboard && mode == self.mode {
if !retained_graph {
self.dashboard_token_bucket = None;
}
self.error = None;
}
}
Err(error) => {
if self.view == View::Dashboard && mode == self.mode {
self.error = Some(error);
}
}
}
}
fn apply_dashboard_graph_refresh(
&mut self,
mode: Mode,
cutoff_millis: Option<i64>,
snapshot_millis: i64,
result: std::result::Result<Vec<db::TokenBucket>, String>,
) -> bool {
let placeholder_was_visible = self.view == View::Dashboard
&& mode == self.mode
&& self
.stats
.get(&mode)
.map(|stats| stats.token_buckets.is_empty())
.unwrap_or(false);
self.dashboard_graph_load.finish(mode);
let is_current_request = self
.stats
.get(&mode)
.map(|stats| {
stats.cutoff_millis == cutoff_millis && stats.snapshot_millis == snapshot_millis
})
.unwrap_or(false);
if !is_current_request {
return false;
}
self.dashboard_graph_load.clear_pending(mode);
match result {
Ok(token_buckets) => {
let mut graph_changed = false;
if let Some(stats) = self.stats.get_mut(&mode) {
if stats.token_buckets != token_buckets {
graph_changed = true;
stats.token_buckets = token_buckets;
if self
.dashboard_token_bucket
.map(|idx| idx >= stats.token_buckets.len())
.unwrap_or(false)
{
self.dashboard_token_bucket = None;
}
}
}
let mut error_changed = false;
if self.view == View::Dashboard && mode == self.mode {
error_changed = self.error.is_some();
self.error = None;
}
(self.view == View::Dashboard && mode == self.mode)
&& (placeholder_was_visible || graph_changed || error_changed)
}
Err(error) => {
if self.view == View::Dashboard && mode == self.mode {
let changed = self.error.as_deref() != Some(error.as_str());
self.error = Some(error);
changed
} else {
false
}
}
}
}
fn apply_calendar_refresh(
&mut self,
result: std::result::Result<Vec<db::PeriodCost>, String>,
tx: &Sender<RefreshMessage>,
) {
self.calendar_loading = false;
match result {
Ok(costs) => {
for cost in costs {
self.calendar_costs.insert(cost.period, cost.cost);
}
if self.view == View::CalendarOverview {
self.error = None;
}
}
Err(error) => {
if self.view == View::CalendarOverview {
self.error = Some(error);
}
}
}
self.ensure_calendar_costs(tx);
}
fn apply_history_refresh(
&mut self,
period: PeriodKey,
result: std::result::Result<UsageStats, String>,
) {
self.history_loading.remove(&period);
match result {
Ok(mut stats) => {
let mut retained_graph = false;
if let Some(previous) = self.history_stats.get(&period).filter(|previous| {
previous.cutoff_millis == stats.cutoff_millis
&& previous.end_millis == stats.end_millis
&& stats.totals.messages > 0
}) {
stats.token_buckets.clone_from(&previous.token_buckets);
retained_graph = !stats.token_buckets.is_empty();
}
if stats.totals.messages > 0 {
self.history_graph_load.mark_pending(period);
} else {
self.history_graph_load.clear_pending(period);
}
self.history_stats.insert(period, stats);
if self.view == View::CalendarDetail && period == self.calendar.selected {
if !retained_graph {
self.history_token_bucket = None;
}
self.error = None;
}
}
Err(error) => {
if self.view == View::CalendarDetail && period == self.calendar.selected {
self.error = Some(error);
}
}
}
}
fn apply_history_graph_refresh(
&mut self,
period: PeriodKey,
snapshot_millis: i64,
result: std::result::Result<Vec<db::TokenBucket>, String>,
) -> bool {
let placeholder_was_visible = self.view == View::CalendarDetail
&& period == self.calendar.selected
&& self
.history_stats
.get(&period)
.map(|stats| stats.token_buckets.is_empty())
.unwrap_or(false);
self.history_graph_load.finish(period);
let is_current_request = self
.history_stats
.get(&period)
.map(|stats| stats.snapshot_millis == snapshot_millis)
.unwrap_or(false);
if !is_current_request {
return false;
}
self.history_graph_load.clear_pending(period);
match result {
Ok(token_buckets) => {
let mut graph_changed = false;
if let Some(stats) = self.history_stats.get_mut(&period) {
if stats.token_buckets != token_buckets {
graph_changed = true;
stats.token_buckets = token_buckets;
if self
.history_token_bucket
.map(|idx| idx >= stats.token_buckets.len())
.unwrap_or(false)
{
self.history_token_bucket = None;
}
}
}
let mut error_changed = false;
if self.view == View::CalendarDetail && period == self.calendar.selected {
error_changed = self.error.is_some();
self.error = None;
}
(self.view == View::CalendarDetail && period == self.calendar.selected)
&& (placeholder_was_visible || graph_changed || error_changed)
}
Err(error) => {
if self.view == View::CalendarDetail && period == self.calendar.selected {
let changed = self.error.as_deref() != Some(error.as_str());
self.error = Some(error);
changed
} else {
false
}
}
}
}
fn ensure_visible_graph(&mut self, area: Rect, tx: &Sender<RefreshMessage>) -> bool {
if tui::token_graph_capacity_area(area, self).is_none() {
return false;
}
match self.view {
View::Dashboard => {
let was_loading = self.dashboard_graph_load.loading.contains(&self.mode);
self.trigger_dashboard_graph_refresh(self.mode, tx);
!was_loading
&& self.dashboard_graph_load.loading.contains(&self.mode)
&& self
.current_stats()
.map(|stats| stats.token_buckets.is_empty())
.unwrap_or(false)
}
View::CalendarDetail => {
let period = self.calendar.selected;
let was_loading = self.history_graph_load.loading.contains(&period);
self.trigger_history_graph_refresh(period, tx);
!was_loading
&& self.history_graph_load.loading.contains(&period)
&& self
.selected_history_stats()
.map(|stats| stats.token_buckets.is_empty())
.unwrap_or(false)
}
View::CalendarOverview => false,
}
}
fn prefetch_dashboard_summaries(&mut self, tx: &Sender<RefreshMessage>) {
if self.view != View::Dashboard
|| !self.stats.contains_key(&self.mode)
|| !self.loading.is_empty()
|| !self.dashboard_graph_load.loading.is_empty()
|| self.calendar_loading
|| !self.history_loading.is_empty()
|| !self.history_graph_load.loading.is_empty()
{
return;
}
for mode in Mode::ALL {
if mode != self.mode
&& !self.stats.contains_key(&mode)
&& !self.dashboard_prefetch_attempted.contains(&mode)
{
self.dashboard_prefetch_attempted.insert(mode);
self.trigger_dashboard_refresh_for_mode(mode, tx, false);
break;
}
}
}
fn move_help_selection(&mut self, steps: i32, layout: &tui::HelpLayoutState) {
for _ in 0..steps.unsigned_abs() {
if steps > 0 {
self.move_help_down(layout);
} else {
self.move_help_up(layout);
}
}
}
fn move_model_breakdown_scroll(&mut self, steps: i32, area: Rect) {
let max_scroll = match self.view {
View::Dashboard => self
.current_stats()
.map(|stats| tui::model_breakdown_max_scroll(area, stats.models.len())),
View::CalendarDetail => self
.selected_history_stats()
.map(|stats| tui::model_breakdown_max_scroll(area, stats.models.len())),
View::CalendarOverview => None,
}
.unwrap_or(0);
let amount = steps.unsigned_abs() as usize;
match self.view {
View::Dashboard => {
let current = self.dashboard_model_scroll.min(max_scroll);
self.dashboard_model_scroll = if steps > 0 {
current.saturating_add(amount).min(max_scroll)
} else {
current.saturating_sub(amount)
};
}
View::CalendarDetail => {
let current = self.history_model_scroll.min(max_scroll);
self.history_model_scroll = if steps > 0 {
current.saturating_add(amount).min(max_scroll)
} else {
current.saturating_sub(amount)
};
}
View::CalendarOverview => {}
}
}
fn select_token_bucket(&mut self, bucket_idx: usize) {
match self.view {
View::Dashboard => self.dashboard_token_bucket = Some(bucket_idx),
View::CalendarDetail => self.history_token_bucket = Some(bucket_idx),
View::CalendarOverview => {}
}
}
fn move_help_down(&mut self, layout: &tui::HelpLayoutState) {
if self.help_config_visible(layout) {
if self.config_selection + 1 < ConfigEditorItem::ALL.len() {
self.config_selection += 1;
self.ensure_help_selection_visible(layout);
} else {
self.help_scroll = self.help_scroll.saturating_add(1).min(layout.max_scroll);
}
} else {
self.help_scroll = self.help_scroll.saturating_add(1).min(layout.max_scroll);
}
}
fn move_help_up(&mut self, layout: &tui::HelpLayoutState) {
if self.help_config_visible(layout) && self.config_selection > 0 {
self.config_selection -= 1;
self.ensure_help_selection_visible(layout);
} else {
self.help_scroll = self.help_scroll.saturating_sub(1);
}
}
fn help_config_visible(&self, layout: &tui::HelpLayoutState) -> bool {
help_config_visible(self.help_scroll.min(layout.max_scroll), layout)
}
fn ensure_help_selection_visible(&mut self, layout: &tui::HelpLayoutState) {
let Some(&selected_row) = layout.config_item_starts.get(self.config_selection) else {
return;
};
let visible_start = self.help_scroll.min(layout.max_scroll);
let visible_end = visible_start.saturating_add(layout.visible_height);
if selected_row < visible_start {
self.help_scroll = selected_row.min(layout.max_scroll);
} else if selected_row >= visible_end {
self.help_scroll = selected_row
.saturating_add(1)
.saturating_sub(layout.visible_height)
.min(layout.max_scroll);
}
}
fn edit_selected_config(&mut self, direction: i32, tx: &Sender<RefreshMessage>) -> Result<()> {
match self.selected_config_item() {
ConfigEditorItem::AutoRefresh => {
self.config.auto_refresh = !self.config.auto_refresh;
if self.config.auto_refresh {
self.next_refresh_due = Instant::now() + self.config.refresh_interval;
}
}
ConfigEditorItem::DailyStart => {
self.config.daily_start = shift_daily_start(self.config.daily_start, direction);
self.apply_time_window_config_change(tx)?;
}
ConfigEditorItem::RefreshSeconds => {
self.config.refresh_interval =
shift_refresh_interval(self.config.refresh_interval, direction);
self.next_refresh_due = Instant::now() + self.config.refresh_interval;
}
ConfigEditorItem::WeekStart => {
self.config.week_start = cycle_week_start(self.config.week_start, direction);
self.apply_time_window_config_change(tx)?;
}
ConfigEditorItem::ColorTheme => {
self.config.color_theme =
cycle_value(&ColorTheme::ALL, self.config.color_theme, direction);
}
ConfigEditorItem::ThemeScope => {
self.config.theme_scope = cycle_value(
&[ThemeScope::Calendar, ThemeScope::All],
self.config.theme_scope,
direction,
);
}
ConfigEditorItem::ShowComparison => {
self.config.show_comparison = !self.config.show_comparison;
}
ConfigEditorItem::EstimateApiCost => {
self.config.estimate_api_cost = !self.config.estimate_api_cost;
self.invalidate_usage_config(tx);
}
ConfigEditorItem::HiddenProviders => {
self.open_provider_picker();
return Ok(());
}
ConfigEditorItem::ProviderAliases => {
self.open_alias_editor(AliasKind::Provider);
return Ok(());
}
ConfigEditorItem::ModelAliases => {
self.open_alias_editor(AliasKind::Model);
return Ok(());
}
}
self.save_config_notice();
Ok(())
}
fn apply_time_window_config_change(&mut self, tx: &Sender<RefreshMessage>) -> Result<()> {
match self.view {
View::Dashboard => self.trigger_dashboard_refresh(tx),
View::CalendarOverview | View::CalendarDetail => {
self.realign_calendar_for_config(tx)?
}
}
Ok(())
}
fn realign_calendar_for_config(&mut self, tx: &Sender<RefreshMessage>) -> Result<()> {
let selected_start = local_from_millis(self.calendar.selected.start_millis)?;
self.calendar.selected = time_window::current_period(
self.calendar.scale,
selected_start,
self.config.daily_start,
self.config.week_start,
)?;
self.history_model_scroll = 0;
self.history_token_bucket = None;
self.sync_visible_periods()?;
match self.view {
View::Dashboard => {}
View::CalendarOverview => self.ensure_calendar_costs(tx),
View::CalendarDetail => {
self.ensure_calendar_costs(tx);
self.trigger_history_refresh(tx);
}
}
Ok(())
}
fn save_config_notice(&mut self) {
match config::save(&self.config) {
Ok(()) => {
self.config_notice = Some(ConfigNotice {
message: "saved config".to_string(),
is_error: false,
});
}
Err(error) => {
self.config_notice = Some(ConfigNotice {
message: format!("config not saved: {error:#}"),
is_error: true,
});
}
}
}
}
enum RefreshMessage {
Dashboard {
generation: u64,
mode: Mode,
result: std::result::Result<UsageStats, String>,
},
DashboardGraph {
generation: u64,
mode: Mode,
cutoff_millis: Option<i64>,
snapshot_millis: i64,
result: std::result::Result<Vec<db::TokenBucket>, String>,
},
Calendar {
generation: u64,
result: std::result::Result<Vec<db::PeriodCost>, String>,
},
History {
generation: u64,
period: PeriodKey,
result: std::result::Result<UsageStats, String>,
},
HistoryGraph {
generation: u64,
period: PeriodKey,
snapshot_millis: i64,
result: std::result::Result<Vec<db::TokenBucket>, String>,
},
SourceSync {
result: std::result::Result<SyncSummary, String>,
},
SourceSyncProgress {
progress: SyncProgress,
},
}
impl RefreshMessage {
fn generation(&self) -> Option<u64> {
match self {
Self::Dashboard { generation, .. }
| Self::DashboardGraph { generation, .. }
| Self::Calendar { generation, .. }
| Self::History { generation, .. }
| Self::HistoryGraph { generation, .. } => Some(*generation),
Self::SourceSync { .. } | Self::SourceSyncProgress { .. } => None,
}
}
}
pub fn run(config: Config) -> Result<()> {
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let result = run_loop(&mut terminal, config);
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
DisableMouseCapture,
LeaveAlternateScreen
)?;
terminal.show_cursor()?;
result
}
fn run_loop(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, config: Config) -> Result<()> {
let (tx, rx) = mpsc::channel();
let mut app = AppState::new(config)?;
app.trigger_dashboard_refresh(&tx);
app.trigger_source_sync(SyncMode::Incremental, &tx);
let mut needs_draw = true;
let mut last_size = None;
loop {
needs_draw |= drain_refreshes(&rx, &mut app, &tx);
needs_draw |= maybe_day_rollover(&mut app, &tx);
needs_draw |= maybe_auto_refresh(&mut app, &tx);
needs_draw |= app.first_sync && app.source_sync.is_some();
let size = terminal.size()?;
let current_size = (size.width, size.height);
if last_size != Some(current_size) {
last_size = Some(current_size);
needs_draw = true;
}
let area = Rect::new(0, 0, size.width, size.height);
needs_draw |= app.ensure_visible_graph(area, &tx);
app.prefetch_dashboard_summaries(&tx);
if needs_draw {
terminal.draw(|frame| tui::draw(frame, &app))?;
needs_draw = false;
}
if event::poll(Duration::from_millis(200))? {
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => {
let size = terminal.size()?;
let area = Rect::new(0, 0, size.width, size.height);
if handle_key(key.code, key.modifiers, area, &mut app, &tx) {
return Ok(());
}
needs_draw = true;
}
Event::Mouse(mouse) => {
let size = terminal.size()?;
let area = Rect::new(0, 0, size.width, size.height);
needs_draw |= handle_mouse(mouse, area, &mut app, &tx);
}
Event::Resize(_, _) => needs_draw = true,
_ => {}
}
}
}
}
fn handle_key(
code: KeyCode,
modifiers: KeyModifiers,
area: Rect,
app: &mut AppState,
tx: &Sender<RefreshMessage>,
) -> bool {
if app.provider_picker.is_some() {
match code {
KeyCode::Char('q') => return true,
KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => return true,
KeyCode::Esc => app.provider_picker = None,
KeyCode::Up | KeyCode::Char('k') | KeyCode::BackTab => app.move_provider_selection(-1),
KeyCode::Down | KeyCode::Char('j') | KeyCode::Tab => app.move_provider_selection(1),
KeyCode::Enter | KeyCode::Char(' ') => app.toggle_selected_provider(tx),
_ => {}
}
return false;
}
if app.alias_editor.is_some() {
let editing = app
.alias_editor
.as_ref()
.and_then(|editor| editor.input.as_ref())
.is_some();
if editing {
match code {
KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => return true,
KeyCode::Esc => {
if let Some(editor) = app.alias_editor.as_mut() {
editor.input = None;
}
}
KeyCode::Tab | KeyCode::BackTab | KeyCode::Up | KeyCode::Down => {
if let Some(input) = app
.alias_editor
.as_mut()
.and_then(|editor| editor.input.as_mut())
{
input.field = match input.field {
AliasInputField::Source => AliasInputField::Alias,
AliasInputField::Alias => AliasInputField::Source,
};
}
}
KeyCode::Enter => {
let field = app
.alias_editor
.as_ref()
.and_then(|editor| editor.input.as_ref())
.map(|input| input.field);
if field == Some(AliasInputField::Source) {
if let Some(input) = app
.alias_editor
.as_mut()
.and_then(|editor| editor.input.as_mut())
{
input.field = AliasInputField::Alias;
}
} else {
app.save_alias_input();
}
}
KeyCode::Backspace => {
if let Some(input) = app
.alias_editor
.as_mut()
.and_then(|editor| editor.input.as_mut())
{
match input.field {
AliasInputField::Source => {
input.source.pop();
}
AliasInputField::Alias => {
input.alias.pop();
}
}
}
}
KeyCode::Char(character) if !modifiers.contains(KeyModifiers::CONTROL) => {
if let Some(input) = app
.alias_editor
.as_mut()
.and_then(|editor| editor.input.as_mut())
{
match input.field {
AliasInputField::Source => input.source.push(character),
AliasInputField::Alias => input.alias.push(character),
}
}
}
_ => {}
}
} else {
match code {
KeyCode::Char('q') => return true,
KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => return true,
KeyCode::Esc => app.alias_editor = None,
KeyCode::Up | KeyCode::Char('k') | KeyCode::BackTab => app.move_alias_selection(-1),
KeyCode::Down | KeyCode::Char('j') | KeyCode::Tab => app.move_alias_selection(1),
KeyCode::Enter | KeyCode::Char(' ') | KeyCode::Char('a') => app.begin_alias_input(),
KeyCode::Char('d') | KeyCode::Delete => app.delete_selected_alias(),
_ => {}
}
}
return false;
}
if app.scope_picker.is_some() {
match code {
KeyCode::Char('q') => return true,
KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => return true,
KeyCode::Esc | KeyCode::Char('p') => app.scope_picker = None,
KeyCode::Up | KeyCode::Char('k') | KeyCode::BackTab => app.move_scope_selection(-1),
KeyCode::Down | KeyCode::Char('j') | KeyCode::Tab => app.move_scope_selection(1),
KeyCode::Enter | KeyCode::Char(' ') => app.apply_scope_selection(tx),
_ => {}
}
return false;
}
if code == KeyCode::Char('?') {
app.toggle_help();
return false;
}
if app.show_help {
let help_layout = tui::help_layout_state(area, app);
match code {
KeyCode::Char('q') => return true,
KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => return true,
KeyCode::Up | KeyCode::Char('k') => {
app.move_help_selection(-1, &help_layout);
return false;
}
KeyCode::Down | KeyCode::Char('j') | KeyCode::Tab => {
app.move_help_selection(1, &help_layout);
return false;
}
KeyCode::BackTab => {
app.move_help_selection(-1, &help_layout);
return false;
}
KeyCode::Left | KeyCode::Char('h') => {
if app.help_config_visible(&help_layout) {
apply_config_action(app, tx, |app, tx| app.edit_selected_config(-1, tx));
}
return false;
}
KeyCode::Right | KeyCode::Char('l') | KeyCode::Enter | KeyCode::Char(' ') => {
if app.help_config_visible(&help_layout) {
apply_config_action(app, tx, |app, tx| app.edit_selected_config(1, tx));
}
return false;
}
KeyCode::Esc => {
app.show_help = false;
return false;
}
_ => return false,
}
}
let Some(action) = Action::from_key(code, modifiers, app.view, app.calendar.scale) else {
return false;
};
apply_action(action, app, tx)
}
fn apply_action(action: Action, app: &mut AppState, tx: &Sender<RefreshMessage>) -> bool {
match action {
Action::Quit => true,
Action::Back => match app.view {
View::Dashboard => true,
View::CalendarOverview => {
app.view = View::Dashboard;
app.error = None;
false
}
View::CalendarDetail => {
app.view = View::CalendarOverview;
app.error = None;
false
}
},
Action::OpenCalendar => {
app.enter_calendar(tx);
false
}
Action::NextTab => {
match app.view {
View::Dashboard => app.switch_mode(app.mode.next(), tx),
View::CalendarOverview | View::CalendarDetail => {
apply_calendar_action(app, tx, |app, tx| {
app.set_calendar_scale(app.calendar.scale.next(), tx)
});
}
}
false
}
Action::PreviousTab => {
match app.view {
View::Dashboard => app.switch_mode(app.mode.previous(), tx),
View::CalendarOverview | View::CalendarDetail => {
apply_calendar_action(app, tx, |app, tx| {
app.set_calendar_scale(app.calendar.scale.previous(), tx)
});
}
}
false
}
Action::Refresh => {
app.trigger_current_refresh(tx);
app.trigger_source_sync(SyncMode::Incremental, tx);
app.next_refresh_due = Instant::now() + app.config.refresh_interval;
false
}
Action::RebuildIndex => {
app.trigger_current_refresh(tx);
app.trigger_source_sync(SyncMode::Full, tx);
app.next_refresh_due = Instant::now() + app.config.refresh_interval;
false
}
Action::ToggleGraphMetric => {
app.graph_metric = app.graph_metric.toggle();
false
}
Action::OpenScopePicker => {
app.open_scope_picker();
false
}
Action::OpenSelectedPeriod => {
app.open_calendar_detail(tx);
false
}
Action::MoveCalendar(steps) => {
apply_calendar_action(app, tx, |app, tx| app.move_calendar_selection(steps, tx));
false
}
}
}
fn handle_mouse(
mouse: MouseEvent,
area: Rect,
app: &mut AppState,
tx: &Sender<RefreshMessage>,
) -> bool {
if app.scope_picker.is_some() || app.provider_picker.is_some() || app.alias_editor.is_some() {
return false;
}
if app.show_help {
let help_layout = tui::help_layout_state(area, app);
return match mouse.kind {
MouseEventKind::ScrollUp => {
app.move_help_selection(-1, &help_layout);
true
}
MouseEventKind::ScrollDown => {
app.move_help_selection(1, &help_layout);
true
}
_ => false,
};
}
let mut changed = false;
match mouse.kind {
MouseEventKind::Down(MouseButton::Left) => {
changed = app.token_graph_dragging;
app.token_graph_dragging = false;
if let Some(bucket_idx) =
tui::token_bucket_at_position(mouse.column, mouse.row, area, app)
{
app.select_token_bucket(bucket_idx);
app.token_graph_dragging = true;
return true;
}
}
MouseEventKind::Drag(MouseButton::Left) | MouseEventKind::Moved
if app.token_graph_dragging =>
{
if let Some(bucket_idx) =
tui::token_bucket_at_position(mouse.column, mouse.row, area, app)
{
let previous = match app.view {
View::Dashboard => app.dashboard_token_bucket,
View::CalendarDetail => app.history_token_bucket,
View::CalendarOverview => None,
};
app.select_token_bucket(bucket_idx);
return previous != Some(bucket_idx);
}
return false;
}
MouseEventKind::Up(MouseButton::Left) => {
let changed = app.token_graph_dragging;
app.token_graph_dragging = false;
return changed;
}
_ => {}
}
match mouse.kind {
MouseEventKind::ScrollUp | MouseEventKind::ScrollDown
if tui::model_breakdown_at_position(mouse.column, mouse.row, area, app) =>
{
if let Some(model_area) = tui::model_breakdown_area(area, app) {
let previous = (app.dashboard_model_scroll, app.history_model_scroll);
let steps = if matches!(mouse.kind, MouseEventKind::ScrollDown) {
1
} else {
-1
};
app.move_model_breakdown_scroll(steps, model_area);
return previous != (app.dashboard_model_scroll, app.history_model_scroll);
}
return false;
}
_ => {}
}
if !matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) {
return changed;
}
if let Some(target) = tui::tab_at_position(mouse.column, mouse.row, area) {
match target {
tui::TabTarget::Mode(mode) => match app.view {
View::Dashboard => app.switch_mode(mode, tx),
View::CalendarOverview | View::CalendarDetail => {
if let Some(scale) = CalendarScale::from_mode(mode) {
apply_calendar_action(app, tx, |app, tx| app.set_calendar_scale(scale, tx));
} else {
app.switch_mode(mode, tx);
}
}
},
tui::TabTarget::Calendar => {
if app.view == View::Dashboard {
app.enter_calendar(tx);
} else {
app.view = View::Dashboard;
app.error = None;
if !app.stats.contains_key(&app.mode) {
app.trigger_dashboard_refresh(tx);
}
}
}
}
return true;
}
if let Some(period) = tui::calendar_period_at_position(mouse.column, mouse.row, area, app) {
apply_calendar_action(app, tx, |app, tx| {
app.select_calendar_period(period, tx)?;
app.open_calendar_detail(tx);
Ok(())
});
return true;
}
changed
}
fn drain_refreshes(
rx: &Receiver<RefreshMessage>,
app: &mut AppState,
tx: &Sender<RefreshMessage>,
) -> bool {
let mut needs_draw = false;
while let Ok(message) = rx.try_recv() {
if message
.generation()
.is_some_and(|generation| generation != app.request_generation)
{
continue;
}
match message {
RefreshMessage::Dashboard {
generation: _,
mode,
result,
} => {
app.apply_dashboard_refresh(mode, result);
needs_draw = true;
}
RefreshMessage::DashboardGraph {
generation: _,
mode,
cutoff_millis,
snapshot_millis,
result,
} => {
needs_draw |=
app.apply_dashboard_graph_refresh(mode, cutoff_millis, snapshot_millis, result);
}
RefreshMessage::Calendar {
generation: _,
result,
} => {
app.apply_calendar_refresh(result, tx);
needs_draw = true;
}
RefreshMessage::History {
generation: _,
period,
result,
} => {
app.apply_history_refresh(period, result);
needs_draw = true;
}
RefreshMessage::HistoryGraph {
generation: _,
period,
snapshot_millis,
result,
} => {
needs_draw |= app.apply_history_graph_refresh(period, snapshot_millis, result);
}
RefreshMessage::SourceSync { result } => {
app.apply_source_sync(result, tx);
needs_draw = true;
}
RefreshMessage::SourceSyncProgress { progress } => {
app.apply_source_sync_progress(progress, tx);
needs_draw = true;
}
}
}
needs_draw
}
fn maybe_auto_refresh(app: &mut AppState, tx: &Sender<RefreshMessage>) -> bool {
if !app.config.auto_refresh || Instant::now() < app.next_refresh_due {
return false;
}
app.trigger_current_refresh(tx);
app.trigger_source_sync(SyncMode::Incremental, tx);
app.next_refresh_due = Instant::now() + app.config.refresh_interval;
true
}
fn maybe_day_rollover(app: &mut AppState, tx: &Sender<RefreshMessage>) -> bool {
let current_day = match time_window::current_period(
CalendarScale::Day,
Local::now(),
app.config.daily_start,
app.config.week_start,
) {
Ok(period) => period,
Err(error) => {
app.error = Some(format!("{error:#}"));
return true;
}
};
if current_day == app.logical_day {
return false;
}
let previous_day = app.logical_day;
app.logical_day = current_day;
let old_now = local_from_millis(previous_day.end_millis.saturating_sub(1));
let new_now = local_from_millis(current_day.start_millis);
if let (Ok(old_now), Ok(new_now)) = (old_now, new_now) {
let old_current = time_window::current_period(
app.calendar.scale,
old_now,
app.config.daily_start,
app.config.week_start,
);
let new_current = time_window::current_period(
app.calendar.scale,
new_now,
app.config.daily_start,
app.config.week_start,
);
if let (Ok(old_current), Ok(new_current)) = (old_current, new_current) {
if app.calendar.selected == old_current {
app.calendar.selected = new_current;
let _ = app.sync_visible_periods();
}
}
}
app.invalidate_after_source_change(tx);
app.trigger_source_sync(SyncMode::Incremental, tx);
true
}
fn refresh_dashboard_summary(config: Config, mode: Mode) -> Result<UsageStats> {
analytics::load_dashboard(&config, mode)
}
fn refresh_period_summary(config: Config, period: PeriodKey) -> Result<UsageStats> {
analytics::load_period(&config, period)
}
fn apply_calendar_action(
app: &mut AppState,
tx: &Sender<RefreshMessage>,
action: impl FnOnce(&mut AppState, &Sender<RefreshMessage>) -> Result<()>,
) {
if let Err(error) = action(app, tx) {
app.error = Some(format!("{error:#}"));
}
}
fn apply_config_action(
app: &mut AppState,
tx: &Sender<RefreshMessage>,
action: impl FnOnce(&mut AppState, &Sender<RefreshMessage>) -> Result<()>,
) {
if let Err(error) = action(app, tx) {
app.config_notice = Some(ConfigNotice {
message: format!("config not applied: {error:#}"),
is_error: true,
});
}
}
fn help_config_visible(scroll: usize, layout: &tui::HelpLayoutState) -> bool {
layout.visible_height > 0
&& layout.config_end > layout.config_start
&& scroll.saturating_add(layout.visible_height) > layout.config_start
&& scroll < layout.config_end
}
fn cycle_week_start(current: WeekStart, direction: i32) -> WeekStart {
cycle_value(&[WeekStart::Monday, WeekStart::Sunday], current, direction)
}
fn shift_daily_start(current: DailyStart, direction: i32) -> DailyStart {
const DAY_MINUTES: i32 = 24 * 60;
const STEP_MINUTES: i32 = 15;
let current_minutes = (current.hour * 60 + current.minute) as i32;
let shifted = (current_minutes + direction.signum() * STEP_MINUTES).rem_euclid(DAY_MINUTES);
DailyStart {
hour: (shifted / 60) as u32,
minute: (shifted % 60) as u32,
}
}
fn shift_refresh_interval(current: Duration, direction: i32) -> Duration {
const STEP_SECONDS: u64 = 15;
let current = current.as_secs().max(1);
let next = if direction < 0 {
current.saturating_sub(STEP_SECONDS).max(1)
} else {
current.saturating_add(STEP_SECONDS)
};
Duration::from_secs(next)
}
fn cycle_value<T: Copy + Eq>(values: &[T], current: T, direction: i32) -> T {
let current_idx = values
.iter()
.position(|value| *value == current)
.unwrap_or(0) as i32;
let next_idx = (current_idx + direction.signum()).rem_euclid(values.len() as i32) as usize;
values[next_idx]
}
fn local_from_millis(millis: i64) -> Result<DateTime<Local>> {
DateTime::from_timestamp_millis(millis)
.map(|value| value.with_timezone(&Local))
.ok_or_else(|| anyhow::anyhow!("timestamp is outside the supported range"))
}
#[cfg(test)]
mod tests {
use std::{fs, path::PathBuf};
use chrono::TimeZone;
use crate::{
config::Scope,
db::{ModelUsage, TokenBucket, UsageTotals},
time_window::{DailyStart, WeekStart},
};
use super::*;
#[test]
fn help_space_toggles_auto_refresh_and_saves_config() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path.clone())).unwrap();
app.show_help = true;
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 120, 40);
let should_quit = handle_key(KeyCode::Char(' '), KeyModifiers::NONE, area, &mut app, &tx);
assert!(!should_quit);
assert!(!app.config.auto_refresh);
assert_eq!(
app.config_notice.as_ref().map(|notice| notice.is_error),
Some(false)
);
assert!(fs::read_to_string(config_path)
.unwrap()
.contains("auto_refresh = false"));
}
#[test]
fn help_space_enables_comparison_panel_and_saves_config() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path.clone())).unwrap();
app.show_help = true;
app.config_selection = ConfigEditorItem::ALL
.iter()
.position(|item| *item == ConfigEditorItem::ShowComparison)
.unwrap();
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 120, 40);
let should_quit = handle_key(KeyCode::Char(' '), KeyModifiers::NONE, area, &mut app, &tx);
assert!(!should_quit);
assert!(app.config.show_comparison);
assert!(fs::read_to_string(config_path)
.unwrap()
.contains("show_comparison = true"));
}
#[test]
fn help_space_enables_api_cost_estimates_and_invalidates_usage() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut config = test_config(config_path.clone());
config.index_path = tempdir.path().join("usage.sqlite3");
let mut app = AppState::new(config).unwrap();
app.show_help = true;
app.stats
.insert(Mode::Daily, many_model_stats(Mode::Daily, 1));
app.config_selection = ConfigEditorItem::ALL
.iter()
.position(|item| *item == ConfigEditorItem::EstimateApiCost)
.unwrap();
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 120, 40);
handle_key(KeyCode::Char(' '), KeyModifiers::NONE, area, &mut app, &tx);
assert!(app.config.estimate_api_cost);
assert_eq!(app.request_generation, 1);
assert!(app.stats.is_empty());
assert!(fs::read_to_string(config_path)
.unwrap()
.contains("estimate_api_cost = true"));
}
#[test]
fn provider_picker_toggles_visibility_and_saves_config() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut config = test_config(config_path.clone());
config.index_path = tempdir.path().join("usage.sqlite3");
let mut app = AppState::new(config).unwrap();
app.providers = vec!["openai".to_string(), "openrouter".to_string()];
app.open_provider_picker();
let (tx, _rx) = mpsc::channel();
app.move_provider_selection(1);
app.toggle_selected_provider(&tx);
assert!(app.config.hidden_providers.contains("openrouter"));
assert_eq!(app.request_generation, 1);
assert!(fs::read_to_string(config_path)
.unwrap()
.contains(r#"hidden_providers = ["openrouter"]"#));
}
#[test]
fn help_mouse_wheel_moves_config_selection() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path)).unwrap();
app.show_help = true;
let (tx, _rx) = mpsc::channel();
handle_mouse(
MouseEvent {
kind: MouseEventKind::ScrollDown,
column: 0,
row: 0,
modifiers: KeyModifiers::NONE,
},
Rect::new(0, 0, 120, 24),
&mut app,
&tx,
);
assert_eq!(app.selected_config_item(), ConfigEditorItem::DailyStart);
handle_mouse(
MouseEvent {
kind: MouseEventKind::ScrollUp,
column: 0,
row: 0,
modifiers: KeyModifiers::NONE,
},
Rect::new(0, 0, 120, 24),
&mut app,
&tx,
);
assert_eq!(app.selected_config_item(), ConfigEditorItem::AutoRefresh);
}
#[test]
fn idle_mouse_movement_does_not_request_a_redraw() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
let (tx, _rx) = mpsc::channel();
let changed = handle_mouse(
MouseEvent {
kind: MouseEventKind::Moved,
column: 10,
row: 10,
modifiers: KeyModifiers::NONE,
},
Rect::new(0, 0, 120, 24),
&mut app,
&tx,
);
assert!(!changed);
}
#[test]
fn small_help_mouse_wheel_scrolls_before_config_selection() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path)).unwrap();
app.show_help = true;
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 80, 16);
handle_mouse(
MouseEvent {
kind: MouseEventKind::ScrollDown,
column: 0,
row: 0,
modifiers: KeyModifiers::NONE,
},
area,
&mut app,
&tx,
);
assert_eq!(app.help_scroll, 1);
assert_eq!(app.selected_config_item(), ConfigEditorItem::AutoRefresh);
}
#[test]
fn help_edit_keys_do_nothing_before_config_is_visible() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path.clone())).unwrap();
app.show_help = true;
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 80, 16);
handle_key(KeyCode::Char(' '), KeyModifiers::NONE, area, &mut app, &tx);
handle_key(KeyCode::Right, KeyModifiers::NONE, area, &mut app, &tx);
assert!(app.config.auto_refresh);
assert!(app.config_notice.is_none());
assert!(!config_path.exists());
}
#[test]
fn reopening_help_resets_scroll_and_config_selection() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path)).unwrap();
app.show_help = true;
app.help_scroll = 8;
app.config_selection = 4;
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 80, 16);
handle_key(KeyCode::Char('?'), KeyModifiers::NONE, area, &mut app, &tx);
handle_key(KeyCode::Char('?'), KeyModifiers::NONE, area, &mut app, &tx);
assert!(app.show_help);
assert_eq!(app.help_scroll, 0);
assert_eq!(app.selected_config_item(), ConfigEditorItem::AutoRefresh);
}
#[test]
fn help_cycles_selected_option_and_saves_config() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path.clone())).unwrap();
app.show_help = true;
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 120, 40);
for _ in 0..4 {
handle_key(KeyCode::Down, KeyModifiers::NONE, area, &mut app, &tx);
}
handle_key(KeyCode::Right, KeyModifiers::NONE, area, &mut app, &tx);
assert_eq!(app.selected_config_item(), ConfigEditorItem::ColorTheme);
assert_eq!(app.config.color_theme, ColorTheme::Ember);
assert!(fs::read_to_string(config_path)
.unwrap()
.contains(r#"color_theme = "ember""#));
}
#[test]
fn help_edits_timing_config_and_saves() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path.clone())).unwrap();
app.show_help = true;
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 120, 40);
handle_key(KeyCode::Down, KeyModifiers::NONE, area, &mut app, &tx);
handle_key(KeyCode::Right, KeyModifiers::NONE, area, &mut app, &tx);
assert_eq!(app.selected_config_item(), ConfigEditorItem::DailyStart);
assert_eq!(
app.config.daily_start,
DailyStart {
hour: 4,
minute: 15
}
);
handle_key(KeyCode::Down, KeyModifiers::NONE, area, &mut app, &tx);
handle_key(KeyCode::Left, KeyModifiers::NONE, area, &mut app, &tx);
assert_eq!(app.selected_config_item(), ConfigEditorItem::RefreshSeconds);
assert_eq!(app.config.refresh_interval, Duration::from_secs(45));
let content = fs::read_to_string(config_path).unwrap();
assert!(content.contains(r#"daily_start = "04:15""#));
assert!(content.contains("refresh_seconds = 45"));
}
#[test]
fn small_help_scrolls_before_config_selection() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path)).unwrap();
app.show_help = true;
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 80, 16);
handle_key(KeyCode::Down, KeyModifiers::NONE, area, &mut app, &tx);
assert_eq!(app.help_scroll, 1);
assert_eq!(app.selected_config_item(), ConfigEditorItem::AutoRefresh);
loop {
let layout = tui::help_layout_state(area, &app);
if app.help_scroll.saturating_add(layout.visible_height) > layout.config_start {
break;
}
handle_key(KeyCode::Down, KeyModifiers::NONE, area, &mut app, &tx);
}
assert_eq!(app.selected_config_item(), ConfigEditorItem::AutoRefresh);
handle_key(KeyCode::Down, KeyModifiers::NONE, area, &mut app, &tx);
assert_eq!(app.selected_config_item(), ConfigEditorItem::DailyStart);
}
#[test]
fn clicking_calendar_period_selects_and_opens_detail() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path)).unwrap();
let selected = time_window::current_period(
CalendarScale::Day,
Local.with_ymd_and_hms(2026, 6, 15, 10, 0, 0).unwrap(),
DailyStart::default(),
WeekStart::default(),
)
.unwrap();
app.view = View::CalendarOverview;
app.calendar.scale = CalendarScale::Day;
app.calendar.selected = selected;
app.calendar.visible_periods =
time_window::visible_periods(selected, DailyStart::default(), WeekStart::default())
.unwrap();
for period in &app.calendar.visible_periods {
app.calendar_costs.insert(*period, 0.0);
}
let (tx, _rx) = mpsc::channel();
handle_mouse(
MouseEvent {
kind: MouseEventKind::Down(MouseButton::Left),
column: 2,
row: 7,
modifiers: KeyModifiers::NONE,
},
Rect::new(0, 0, 120, 24),
&mut app,
&tx,
);
assert_eq!(app.view, View::CalendarDetail);
assert_eq!(app.calendar.selected, selected);
}
#[test]
fn model_breakdown_mouse_wheel_scrolls_dashboard_pane() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path)).unwrap();
app.stats
.insert(Mode::Daily, many_model_stats(Mode::Daily, 12));
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 100, 16);
let model_area = tui::model_breakdown_area(area, &app).unwrap();
handle_mouse(
MouseEvent {
kind: MouseEventKind::ScrollDown,
column: model_area.x + 1,
row: model_area.y + 1,
modifiers: KeyModifiers::NONE,
},
area,
&mut app,
&tx,
);
assert_eq!(app.dashboard_model_scroll, 1);
handle_mouse(
MouseEvent {
kind: MouseEventKind::ScrollUp,
column: model_area.x + 1,
row: model_area.y + 1,
modifiers: KeyModifiers::NONE,
},
area,
&mut app,
&tx,
);
assert_eq!(app.dashboard_model_scroll, 0);
}
#[test]
fn model_breakdown_mouse_wheel_scrolls_history_pane_separately() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path)).unwrap();
app.view = View::CalendarDetail;
app.history_stats
.insert(app.calendar.selected, many_model_stats(Mode::Daily, 12));
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 100, 16);
let model_area = tui::model_breakdown_area(area, &app).unwrap();
handle_mouse(
MouseEvent {
kind: MouseEventKind::ScrollDown,
column: model_area.x + 1,
row: model_area.y + 1,
modifiers: KeyModifiers::NONE,
},
area,
&mut app,
&tx,
);
assert_eq!(app.dashboard_model_scroll, 0);
assert_eq!(app.history_model_scroll, 1);
}
#[test]
fn token_graph_click_and_drag_selects_dashboard_bucket() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path)).unwrap();
let bucket_count = 8;
app.stats
.insert(Mode::Daily, many_model_stats(Mode::Daily, bucket_count));
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 100, 32);
let graph_area = tui::token_graph_area(area, &app).unwrap();
let inner_width = graph_area.width.saturating_sub(2) as usize;
let bucket_one_column = graph_area.x + 1 + inner_width.div_ceil(bucket_count) as u16;
handle_mouse(
MouseEvent {
kind: MouseEventKind::Down(MouseButton::Left),
column: bucket_one_column,
row: graph_area.y + 2,
modifiers: KeyModifiers::NONE,
},
area,
&mut app,
&tx,
);
assert_eq!(app.dashboard_token_bucket, Some(1));
assert!(app.token_graph_dragging);
handle_mouse(
MouseEvent {
kind: MouseEventKind::Moved,
column: graph_area.x + graph_area.width - 1,
row: graph_area.y + 2,
modifiers: KeyModifiers::NONE,
},
area,
&mut app,
&tx,
);
assert_eq!(app.dashboard_token_bucket, Some(7));
handle_mouse(
MouseEvent {
kind: MouseEventKind::Up(MouseButton::Left),
column: graph_area.x + graph_area.width - 1,
row: graph_area.y + 2,
modifiers: KeyModifiers::NONE,
},
area,
&mut app,
&tx,
);
assert!(!app.token_graph_dragging);
}
#[test]
fn initial_dashboard_summary_defers_graph_loading() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
let mut summary = many_model_stats(Mode::Daily, 4);
summary.token_buckets.clear();
app.apply_dashboard_refresh(Mode::Daily, Ok(summary));
assert!(app.current_stats().unwrap().token_buckets.is_empty());
assert!(app.dashboard_graph_load.pending.contains(&Mode::Daily));
}
#[test]
fn dashboard_refresh_retains_graph_until_replacement_arrives() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
let previous = many_model_stats(Mode::Daily, 4);
let previous_buckets = previous.token_buckets.clone();
let previous_refreshed_at = previous.refreshed_at;
app.stats.insert(Mode::Daily, previous);
app.dashboard_token_bucket = Some(2);
let mut summary = many_model_stats(Mode::Daily, 4);
summary.refreshed_at = previous_refreshed_at + chrono::Duration::minutes(1);
summary.token_buckets.clear();
app.apply_dashboard_refresh(Mode::Daily, Ok(summary));
assert_eq!(app.current_stats().unwrap().token_buckets, previous_buckets);
assert_eq!(app.dashboard_token_bucket, Some(2));
assert!(app.dashboard_graph_load.pending.contains(&Mode::Daily));
}
#[test]
fn stale_dashboard_graph_result_does_not_replace_retained_graph() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
let previous = many_model_stats(Mode::Daily, 4);
let previous_buckets = previous.token_buckets.clone();
let previous_refreshed_at = previous.refreshed_at;
let previous_snapshot_millis = previous.snapshot_millis;
app.stats.insert(Mode::Daily, previous);
let mut summary = many_model_stats(Mode::Daily, 4);
summary.refreshed_at = previous_refreshed_at + chrono::Duration::minutes(1);
summary.snapshot_millis = previous_snapshot_millis + 60_000;
summary.token_buckets.clear();
app.apply_dashboard_refresh(Mode::Daily, Ok(summary));
app.dashboard_graph_load.loading.insert(Mode::Daily);
let replacement = vec![TokenBucket {
start_millis: 0,
end_millis: 1,
tokens: 999,
cost: 9.99,
api_estimated_cost: 0.0,
}];
let changed = app.apply_dashboard_graph_refresh(
Mode::Daily,
None,
previous_snapshot_millis,
Ok(replacement),
);
assert!(!changed);
assert_eq!(app.current_stats().unwrap().token_buckets, previous_buckets);
assert!(app.dashboard_graph_load.pending.contains(&Mode::Daily));
}
#[test]
fn unchanged_dashboard_graph_does_not_request_a_redraw() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
let stats = many_model_stats(Mode::Daily, 4);
let cutoff_millis = stats.cutoff_millis;
let snapshot_millis = stats.snapshot_millis;
let buckets = stats.token_buckets.clone();
app.stats.insert(Mode::Daily, stats);
app.dashboard_graph_load.loading.insert(Mode::Daily);
app.dashboard_graph_load.pending.insert(Mode::Daily);
let changed = app.apply_dashboard_graph_refresh(
Mode::Daily,
cutoff_millis,
snapshot_millis,
Ok(buckets),
);
assert!(!changed);
assert!(!app.dashboard_graph_load.pending.contains(&Mode::Daily));
}
#[test]
fn history_refresh_retains_graph_until_replacement_arrives() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
let period = app.calendar.selected;
let previous = many_model_stats(period.mode(), 4);
let previous_buckets = previous.token_buckets.clone();
let previous_refreshed_at = previous.refreshed_at;
app.view = View::CalendarDetail;
app.history_stats.insert(period, previous);
app.history_token_bucket = Some(2);
let mut summary = many_model_stats(period.mode(), 4);
summary.refreshed_at = previous_refreshed_at + chrono::Duration::minutes(1);
summary.cutoff_millis = Some(period.start_millis);
summary.end_millis = Some(period.end_millis);
if let Some(previous) = app.history_stats.get_mut(&period) {
previous.cutoff_millis = Some(period.start_millis);
previous.end_millis = Some(period.end_millis);
}
summary.token_buckets.clear();
app.apply_history_refresh(period, Ok(summary));
assert_eq!(
app.selected_history_stats().unwrap().token_buckets,
previous_buckets
);
assert_eq!(app.history_token_bucket, Some(2));
assert!(app.history_graph_load.pending.contains(&period));
}
#[test]
fn project_picker_persists_scope_and_invalidates_cached_usage() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path.clone())).unwrap();
app.projects = vec![db::ProjectInfo {
id: "project-a".to_string(),
name: "Project A".to_string(),
worktree: "/tmp/project".to_string(),
}];
app.stats
.insert(Mode::Daily, many_model_stats(Mode::Daily, 1));
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 120, 30);
handle_key(KeyCode::Char('p'), KeyModifiers::NONE, area, &mut app, &tx);
handle_key(KeyCode::Down, KeyModifiers::NONE, area, &mut app, &tx);
handle_key(KeyCode::Down, KeyModifiers::NONE, area, &mut app, &tx);
handle_key(KeyCode::Enter, KeyModifiers::NONE, area, &mut app, &tx);
assert_eq!(app.config.scope, Scope::Project("project-a".to_string()));
assert_eq!(app.request_generation, 1);
assert!(app.stats.is_empty());
assert!(fs::read_to_string(config_path)
.unwrap()
.contains(r#"scope = "project:project-a""#));
}
#[test]
fn ignores_refresh_results_from_an_old_scope_generation() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
app.request_generation = 2;
let (tx, rx) = mpsc::channel();
tx.send(RefreshMessage::Dashboard {
generation: 1,
mode: Mode::Daily,
result: Ok(many_model_stats(Mode::Daily, 1)),
})
.unwrap();
let changed = drain_refreshes(&rx, &mut app, &tx);
assert!(!changed);
assert!(app.stats.is_empty());
}
#[test]
fn coalesces_source_refreshes_while_one_is_running() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
let (tx, _rx) = mpsc::channel();
app.source_sync = Some(SyncMode::Incremental);
app.trigger_source_sync(SyncMode::Incremental, &tx);
assert_eq!(app.pending_source_sync, Some(SyncMode::Incremental));
app.trigger_source_sync(SyncMode::Full, &tx);
assert_eq!(app.pending_source_sync, Some(SyncMode::Full));
app.trigger_source_sync(SyncMode::Incremental, &tx);
assert_eq!(app.pending_source_sync, Some(SyncMode::Full));
}
#[test]
fn tracks_each_source_sync_stage_and_counts() {
let tempdir = tempfile::tempdir().unwrap();
let mut config = test_config(tempdir.path().join("config.toml"));
config.index_path = tempdir.path().join("usage.sqlite3");
let mut app = AppState::new(config).unwrap();
let (tx, _rx) = mpsc::channel();
app.apply_source_sync_progress(
SyncProgress::Planned {
sources: vec!["OpenCode".to_string(), "Pi".to_string()],
},
&tx,
);
app.apply_source_sync_progress(
SyncProgress::Started {
source: "OpenCode".to_string(),
},
&tx,
);
app.apply_source_sync_progress(
SyncProgress::Finished {
source: "OpenCode".to_string(),
report: Some(SyncReport {
scanned: 12,
imported: 8,
..SyncReport::default()
}),
error: None,
changed: false,
},
&tx,
);
app.apply_source_sync_progress(
SyncProgress::Started {
source: "Pi".to_string(),
},
&tx,
);
let status = app.source_sync_status.unwrap();
assert_eq!(status.completed(), 1);
assert_eq!(status.active_names(), vec!["Pi"]);
assert_eq!(
status.sources[0].stage,
SourceSyncStage::Ready {
scanned: 12,
imported: 8
}
);
}
#[test]
fn fresh_index_is_marked_as_first_sync_until_sync_finishes() {
let tempdir = tempfile::tempdir().unwrap();
let mut config = test_config(tempdir.path().join("config.toml"));
config.index_path = tempdir.path().join("usage.sqlite3");
let mut app = AppState::new(config).unwrap();
let (tx, _rx) = mpsc::channel();
assert!(app.first_sync);
app.apply_source_sync(Ok(SyncSummary::default()), &tx);
assert!(!app.first_sync);
}
#[test]
fn tui_alias_editor_saves_and_updates_loaded_model_names() {
let tempdir = tempfile::tempdir().unwrap();
let config_path = tempdir.path().join("config.toml");
let mut app = AppState::new(test_config(config_path.clone())).unwrap();
let mut stats = many_model_stats(Mode::Daily, 1);
stats.models[0].provider = "github-copilot".to_string();
stats.models[0].display_name = "github-copilot/model-0".to_string();
app.stats.insert(Mode::Daily, stats);
app.open_alias_editor(AliasKind::Provider);
app.begin_alias_input();
let (tx, _rx) = mpsc::channel();
let area = Rect::new(0, 0, 100, 24);
for character in "github-copilot".chars() {
handle_key(
KeyCode::Char(character),
KeyModifiers::NONE,
area,
&mut app,
&tx,
);
}
handle_key(KeyCode::Enter, KeyModifiers::NONE, area, &mut app, &tx);
for character in "gc".chars() {
handle_key(
KeyCode::Char(character),
KeyModifiers::NONE,
area,
&mut app,
&tx,
);
}
handle_key(KeyCode::Enter, KeyModifiers::NONE, area, &mut app, &tx);
assert_eq!(app.config.aliases.providers["github-copilot"], "gc");
assert_eq!(
app.current_stats().unwrap().models[0].display_name,
"gc/model-0"
);
let content = fs::read_to_string(config_path).unwrap();
assert!(content.contains("provider_aliases"));
assert!(content.contains("github-copilot = \"gc\""));
}
#[test]
fn graph_metric_toggle_does_not_reload_usage() {
let tempdir = tempfile::tempdir().unwrap();
let mut app = AppState::new(test_config(tempdir.path().join("config.toml"))).unwrap();
app.stats
.insert(Mode::Daily, many_model_stats(Mode::Daily, 1));
let (tx, rx) = mpsc::channel();
handle_key(
KeyCode::Char('g'),
KeyModifiers::NONE,
Rect::new(0, 0, 100, 24),
&mut app,
&tx,
);
assert_eq!(app.graph_metric, GraphMetric::Cost);
assert!(rx.try_recv().is_err());
assert!(app.current_stats().is_some());
}
#[test]
fn deferred_load_state_keeps_pending_work_until_completion() {
let mut state = DeferredLoadState::default();
state.mark_pending(Mode::Daily);
assert!(state.begin(Mode::Daily));
assert!(!state.begin(Mode::Daily));
state.finish(Mode::Daily);
assert!(state.begin(Mode::Daily));
state.finish(Mode::Daily);
state.clear_pending(Mode::Daily);
assert!(!state.begin(Mode::Daily));
}
fn test_config(config_path: PathBuf) -> Config {
Config {
db_path: PathBuf::from("/tmp/opencode.db"),
index_path: PathBuf::from("/tmp/expensive.sqlite3"),
copilot_home: PathBuf::from("/tmp/copilot"),
codex_home: PathBuf::from("/tmp/codex"),
pi_sessions_root: PathBuf::from("/tmp/pi/sessions"),
current_directory: PathBuf::from("/tmp/project"),
config_path: Some(config_path),
daily_start: DailyStart::default(),
week_start: WeekStart::default(),
refresh_interval: Duration::from_secs(60),
auto_refresh: true,
show_comparison: false,
estimate_api_cost: false,
hidden_providers: std::collections::BTreeSet::new(),
scope: Scope::All,
color_theme: ColorTheme::Aurora,
theme_scope: ThemeScope::Calendar,
aliases: config::ModelAliases::default(),
}
}
fn many_model_stats(mode: Mode, count: usize) -> UsageStats {
let models = (0..count)
.map(|idx| ModelUsage {
provider: "provider".to_string(),
model_id: format!("model-{idx}"),
variant: "default".to_string(),
display_name: format!("provider/model-{idx}"),
totals: UsageTotals {
messages: 1,
unpriced_messages: 0,
api_estimated_messages: 0,
cost: (count - idx) as f64,
api_estimated_cost: 0.0,
total: 100,
input: 10,
output: 20,
cache_read: 30,
cache_write: 40,
},
})
.collect::<Vec<_>>();
let totals = UsageTotals {
messages: count as u64,
unpriced_messages: 0,
api_estimated_messages: 0,
cost: models.iter().map(|model| model.totals.cost).sum(),
api_estimated_cost: 0.0,
total: count as u64 * 100,
input: models.iter().map(|model| model.totals.input).sum(),
output: models.iter().map(|model| model.totals.output).sum(),
cache_read: models.iter().map(|model| model.totals.cache_read).sum(),
cache_write: models.iter().map(|model| model.totals.cache_write).sum(),
};
UsageStats {
mode,
refreshed_at: Local.with_ymd_and_hms(2026, 6, 15, 10, 0, 0).unwrap(),
snapshot_millis: 1_750_000_000_000,
cutoff_millis: None,
end_millis: None,
totals,
models,
token_buckets: token_buckets(count),
comparison: None,
projected_cost: None,
}
}
fn token_buckets(count: usize) -> Vec<TokenBucket> {
const HOUR: i64 = 60 * 60 * 1000;
(0..count)
.map(|idx| TokenBucket {
start_millis: idx as i64 * HOUR,
end_millis: (idx as i64 + 1) * HOUR,
tokens: (idx as u64 + 1) * 10,
cost: (idx as f64 + 1.0) / 10.0,
api_estimated_cost: 0.0,
})
.collect()
}
}