pub struct Tracker { /* private fields */ }Expand description
Main tracking interface for recording and querying command history.
Manages SQLite database connection and provides methods for:
- Recording command executions with token counts and timing
- Querying aggregated statistics (summary, daily, weekly, monthly)
- Retrieving recent command history
§Database Location
- Linux:
~/.local/share/rtk/tracking.db - macOS:
~/Library/Application Support/rtk/tracking.db - Windows:
%APPDATA%\rtk\tracking.db
§Examples
use rtk::tracking::Tracker;
let tracker = Tracker::new()?;
tracker.record("ls -la", "rtk ls", 1000, 200, 50)?;
let summary = tracker.get_summary()?;
println!("Total saved: {} tokens", summary.total_saved);Implementations§
Source§impl Tracker
impl Tracker
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new tracker instance.
Opens or creates the SQLite database at the platform-specific location.
Automatically creates the commands table if it doesn’t exist and runs
any necessary schema migrations.
§Errors
Returns error if:
- Cannot determine database path
- Cannot create parent directories
- Cannot open/create SQLite database
- Schema creation/migration fails
§Examples
use rtk::tracking::Tracker;
let tracker = Tracker::new()?;Sourcepub fn record(
&self,
original_cmd: &str,
rtk_cmd: &str,
input_tokens: usize,
output_tokens: usize,
exec_time_ms: u64,
) -> Result<()>
pub fn record( &self, original_cmd: &str, rtk_cmd: &str, input_tokens: usize, output_tokens: usize, exec_time_ms: u64, ) -> Result<()>
Record a command execution with token counts and timing.
Calculates savings metrics and stores the record in the database. Automatically cleans up records older than 90 days after insertion.
§Arguments
original_cmd: The standard command (e.g., “ls -la”)rtk_cmd: The RTK command used (e.g., “rtk ls”)input_tokens: Estimated tokens from standard command outputoutput_tokens: Actual tokens from RTK outputexec_time_ms: Execution time in milliseconds
§Examples
use rtk::tracking::Tracker;
let tracker = Tracker::new()?;
tracker.record("ls -la", "rtk ls", 1000, 200, 50)?;Sourcepub fn reset_all(&self) -> Result<()>
pub fn reset_all(&self) -> Result<()>
Delete all tracked data (commands + parse_failures), resetting all stats to zero.
Sourcepub fn record_parse_failure(
&self,
raw_command: &str,
error_message: &str,
fallback_succeeded: bool,
) -> Result<()>
pub fn record_parse_failure( &self, raw_command: &str, error_message: &str, fallback_succeeded: bool, ) -> Result<()>
Record a parse failure for analytics.
Sourcepub fn get_parse_failure_summary(&self) -> Result<ParseFailureSummary>
pub fn get_parse_failure_summary(&self) -> Result<ParseFailureSummary>
Get parse failure summary for rtk gain --failures.
Sourcepub fn get_summary(&self) -> Result<GainSummary>
pub fn get_summary(&self) -> Result<GainSummary>
Get overall summary statistics across all recorded commands.
Returns aggregated metrics including:
- Total commands, tokens (input/output/saved)
- Average savings percentage and execution time
- Top 10 commands by tokens saved
- Last 30 days of activity
§Examples
use rtk::tracking::Tracker;
let tracker = Tracker::new()?;
let summary = tracker.get_summary()?;
println!("Saved {} tokens ({:.1}%)",
summary.total_saved, summary.avg_savings_pct);Sourcepub fn get_summary_filtered(
&self,
project_path: Option<&str>,
) -> Result<GainSummary>
pub fn get_summary_filtered( &self, project_path: Option<&str>, ) -> Result<GainSummary>
Get summary statistics filtered by project path. // added
When project_path is Some, matches the exact working directory
or any subdirectory (prefix match with path separator).
Sourcepub fn get_all_days(&self) -> Result<Vec<DayStats>>
pub fn get_all_days(&self) -> Result<Vec<DayStats>>
Get daily statistics for all recorded days.
Returns one DayStats per day with commands executed, tokens saved,
and execution time metrics. Results are ordered chronologically (oldest first).
§Examples
use rtk::tracking::Tracker;
let tracker = Tracker::new()?;
let days = tracker.get_all_days()?;
for day in days.iter().take(7) {
println!("{}: {} commands, {} tokens saved",
day.date, day.commands, day.saved_tokens);
}Sourcepub fn get_all_days_filtered(
&self,
project_path: Option<&str>,
) -> Result<Vec<DayStats>>
pub fn get_all_days_filtered( &self, project_path: Option<&str>, ) -> Result<Vec<DayStats>>
Get daily statistics filtered by project path. // added
Sourcepub fn get_by_week(&self) -> Result<Vec<WeekStats>>
pub fn get_by_week(&self) -> Result<Vec<WeekStats>>
Get weekly statistics grouped by week.
Returns one WeekStats per week with aggregated metrics.
Weeks start on Sunday (SQLite default). Results ordered chronologically.
§Examples
use rtk::tracking::Tracker;
let tracker = Tracker::new()?;
let weeks = tracker.get_by_week()?;
for week in weeks {
println!("{} to {}: {} tokens saved",
week.week_start, week.week_end, week.saved_tokens);
}Sourcepub fn get_by_week_filtered(
&self,
project_path: Option<&str>,
) -> Result<Vec<WeekStats>>
pub fn get_by_week_filtered( &self, project_path: Option<&str>, ) -> Result<Vec<WeekStats>>
Get weekly statistics filtered by project path. // added
Sourcepub fn get_by_month(&self) -> Result<Vec<MonthStats>>
pub fn get_by_month(&self) -> Result<Vec<MonthStats>>
Get monthly statistics grouped by month.
Returns one MonthStats per month (YYYY-MM format) with aggregated metrics.
Results ordered chronologically.
§Examples
use rtk::tracking::Tracker;
let tracker = Tracker::new()?;
let months = tracker.get_by_month()?;
for month in months {
println!("{}: {} tokens saved ({:.1}%)",
month.month, month.saved_tokens, month.savings_pct);
}Sourcepub fn get_by_month_filtered(
&self,
project_path: Option<&str>,
) -> Result<Vec<MonthStats>>
pub fn get_by_month_filtered( &self, project_path: Option<&str>, ) -> Result<Vec<MonthStats>>
Get monthly statistics filtered by project path. // added
Sourcepub fn get_recent(&self, limit: usize) -> Result<Vec<CommandRecord>>
pub fn get_recent(&self, limit: usize) -> Result<Vec<CommandRecord>>
Get recent command history.
Returns up to limit most recent command records, ordered by timestamp (newest first).
§Arguments
limit: Maximum number of records to return
§Examples
use rtk::tracking::Tracker;
let tracker = Tracker::new()?;
let recent = tracker.get_recent(10)?;
for cmd in recent {
println!("{}: {} saved {:.1}%",
cmd.timestamp, cmd.rtk_cmd, cmd.savings_pct);
}Sourcepub fn get_recent_filtered(
&self,
limit: usize,
project_path: Option<&str>,
) -> Result<Vec<CommandRecord>>
pub fn get_recent_filtered( &self, limit: usize, project_path: Option<&str>, ) -> Result<Vec<CommandRecord>>
Get recent command history filtered by project path. // added
Sourcepub fn count_commands_since(&self, since: DateTime<Utc>) -> Result<i64>
pub fn count_commands_since(&self, since: DateTime<Utc>) -> Result<i64>
Count commands since a given timestamp (for telemetry).
Sourcepub fn top_commands(&self, limit: usize) -> Result<Vec<String>>
pub fn top_commands(&self, limit: usize) -> Result<Vec<String>>
Get top N commands by frequency (for telemetry).
Sourcepub fn overall_savings_pct(&self) -> Result<f64>
pub fn overall_savings_pct(&self) -> Result<f64>
Get overall savings percentage (for telemetry).
Sourcepub fn total_tokens_saved(&self) -> Result<i64>
pub fn total_tokens_saved(&self) -> Result<i64>
Get total tokens saved across all tracked commands (for telemetry).
Sourcepub fn tokens_saved_24h(&self, since: DateTime<Utc>) -> Result<i64>
pub fn tokens_saved_24h(&self, since: DateTime<Utc>) -> Result<i64>
Get tokens saved in the last 24 hours (for telemetry).
Sourcepub fn top_passthrough(&self, limit: usize) -> Result<Vec<(String, i64)>>
pub fn top_passthrough(&self, limit: usize) -> Result<Vec<(String, i64)>>
Top N passthrough commands (0% savings) — commands missing a filter. Groups by first word only to avoid leaking arguments into telemetry.
Sourcepub fn parse_failures_since(&self, since: DateTime<Utc>) -> Result<i64>
pub fn parse_failures_since(&self, since: DateTime<Utc>) -> Result<i64>
Count parse failures in the last 24 hours.
Sourcepub fn low_savings_commands(&self, limit: usize) -> Result<Vec<(String, f64)>>
pub fn low_savings_commands(&self, limit: usize) -> Result<Vec<(String, f64)>>
Count commands with low savings (<30%) — filters that need improvement.
Sourcepub fn avg_savings_per_command(&self) -> Result<f64>
pub fn avg_savings_per_command(&self) -> Result<f64>
Average savings percentage per command (unweighted — each command name counts once).
Sourcepub fn count_meta_command(&self, name: &str) -> Result<i64>
pub fn count_meta_command(&self, name: &str) -> Result<i64>
Count invocations of a specific meta-command (by rtk_cmd suffix).
Sourcepub fn first_seen_days(&self) -> Result<i64>
pub fn first_seen_days(&self) -> Result<i64>
Days since first recorded command (installation age).
Sourcepub fn active_days_30d(&self) -> Result<i64>
pub fn active_days_30d(&self) -> Result<i64>
Number of distinct active days in the last 30 days.
Sourcepub fn commands_total(&self) -> Result<i64>
pub fn commands_total(&self) -> Result<i64>
Total number of recorded commands.
Sourcepub fn ecosystem_mix(&self) -> Result<Vec<(String, f64)>>
pub fn ecosystem_mix(&self) -> Result<Vec<(String, f64)>>
Ecosystem distribution as percentages (top categories by command prefix).
Sourcepub fn tokens_saved_30d(&self) -> Result<i64>
pub fn tokens_saved_30d(&self) -> Result<i64>
Tokens saved in the last 30 days.
Sourcepub fn projects_count(&self) -> Result<i64>
pub fn projects_count(&self) -> Result<i64>
Number of distinct project paths.