Repository navigation
Port Mac Usage & Spend settings pane #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7e4039d
eae2b09
ce2bbc3
be585be
68e77d3
f5b6ff0
fcad881
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,6 +209,18 @@ fn build_provider_chart_data_with_cancel( | |
| cancel: Option<Arc<AtomicBool>>, | ||
| weekly_window: Option<RateWindowSnapshot>, | ||
| ) -> ProviderChartData { | ||
| if !cost_tracking_enabled() { | ||
| // Track costs off: no local log scan, so no cost, token, local-usage or | ||
| // quota-window history (all read local logs). OpenAI dashboard data is | ||
| // fetched from the web and stays. | ||
| let (credits_history, usage_breakdown) = | ||
| load_openai_dashboard_chart_data(&provider_id, account_email.as_deref()); | ||
| return ProviderChartData { | ||
| credits_history, | ||
| usage_breakdown, | ||
| ..ProviderChartData::empty(provider_id) | ||
| }; | ||
| } | ||
| let live_window = weekly_window.as_ref().map(rate_window_from_snapshot); | ||
| let build_snapshot = || { | ||
| codexbar::providers::chart::build_chart_snapshot( | ||
|
|
@@ -401,6 +413,11 @@ fn map_quota_window_history( | |
| } | ||
| } | ||
|
|
||
| /// "Track costs" (Usage & Spend). Off means no local cost-log scan at all. | ||
| fn cost_tracking_enabled() -> bool { | ||
| codexbar::settings::Settings::load().cost_usage_enabled | ||
| } | ||
|
|
||
| /// The History window the desktop is configured to report. | ||
| fn current_reporting_period() -> CostReportingPeriod { | ||
| codexbar::settings::Settings::load().cost_reporting_period | ||
|
|
@@ -489,6 +506,9 @@ fn load_local_usage_summary_with_unknown_models( | |
| provider_id: &str, | ||
| cancel: Option<&AtomicBool>, | ||
| ) -> (Option<ProviderLocalUsageSummary>, HashSet<String>) { | ||
| if !cost_tracking_enabled() { | ||
| return (None, HashSet::new()); | ||
| } | ||
| let period = current_reporting_period(); | ||
| if provider_id == "muse" { | ||
| let summary = if cancel.is_some_and(|flag| flag.load(Ordering::Relaxed)) { | ||
|
|
@@ -688,7 +708,7 @@ pub(crate) async fn refresh_provider_local_usage_cache(provider_ids: Vec<String> | |
| .await | ||
| .unwrap_or(summary); | ||
| } | ||
| store_local_usage_summary(&provider_id, summary); | ||
| store_local_usage_summary_if_tracking(&provider_id, summary); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -704,6 +724,11 @@ fn load_local_usage_summary_cached( | |
| provider_id: &str, | ||
| cancel: Option<&AtomicBool>, | ||
| ) -> Option<ProviderLocalUsageSummary> { | ||
| // Checked before the cache so an entry from before "Track costs" was | ||
| // turned off is never served. | ||
| if !cost_tracking_enabled() { | ||
| return None; | ||
| } | ||
| let cache = local_usage_cache(); | ||
| let period_identity = local_usage_period_identity(); | ||
| if let Ok(guard) = cache.lock() | ||
|
|
@@ -723,10 +748,31 @@ fn load_local_usage_summary_cached( | |
| return None; | ||
| } | ||
|
|
||
| store_local_usage_summary(provider_id, summary.clone()); | ||
| store_local_usage_summary_if_tracking(provider_id, summary.clone()); | ||
| summary | ||
| } | ||
|
|
||
| /// A scan can outlive a "Track costs" off switch, which clears the cache; drop | ||
| /// its result rather than refill the cache that the PowerToys pipe reads. | ||
| fn store_local_usage_summary_if_tracking( | ||
| provider_id: &str, | ||
| summary: Option<ProviderLocalUsageSummary>, | ||
| ) { | ||
| let tracking = summary.is_none() || cost_tracking_enabled(); | ||
| store_scanned_local_usage_summary(provider_id, summary, tracking); | ||
|
Comment on lines
+761
to
+762
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
# Locate the off-switch cache clear and compare its synchronization with insertion.
rg -n -C 8 'cost_usage_enabled|local_usage_cache|clear.*local.*usage|cached_provider_local_usage_summary' \
apps/desktop-tauri/src-tauri/src/commands/settings.rs \
apps/desktop-tauri/src-tauri/src/commands/chart.rsRepository: nesszer/Win-CodexBar Length of output: 23112 🏁 Script executed: set -o pipefail
printf '%s\n' '--- chart scan/cache block ---'
nl -ba apps/desktop-tauri/src-tauri/src/commands/chart.rs | sed -n '666,806p'
printf '%s\n' '--- cache reader call sites ---'
rg -n -F --glob '*.rs' -- 'cached_provider_local_usage_summary' apps/desktop-tauri/src-tauri/src rust/ || test "$?" -eq 1
printf '%s\n' '--- settings update ordering ---'
nl -ba apps/desktop-tauri/src-tauri/src/commands/settings.rs | sed -n '635,680p'Repository: nesszer/Win-CodexBar Length of output: 9087 Make the tracking check atomic with the cache write. If a scan checks tracking while it is enabled, the settings path can save tracking as disabled and clear the cache before the scan acquires the cache mutex. The scan can then insert its old summary. Perform the tracking check and summary insertion under the same cache lock used by 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| fn store_scanned_local_usage_summary( | ||
| provider_id: &str, | ||
| summary: Option<ProviderLocalUsageSummary>, | ||
| cost_tracking_enabled: bool, | ||
| ) { | ||
| if summary.is_some() && !cost_tracking_enabled { | ||
| return; | ||
| } | ||
| store_local_usage_summary(provider_id, summary); | ||
| } | ||
|
|
||
| fn store_local_usage_summary(provider_id: &str, summary: Option<ProviderLocalUsageSummary>) { | ||
| let period_identity = match summary.as_ref() { | ||
| Some(summary) => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: nesszer/Win-CodexBar
Length of output: 11994
Discard local scan results when tracking turns off.
If tracking turns off during
load_local_usage_summary_cached, the guarded cache write can rejectsummary, but the function still returns it.build_provider_chart_data_with_cancelassigns that result tolocal_usage, so the chart can expose local estimates after tracking is disabled.Recheck tracking before returning:
🐛 Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents