Skip to main content

Tracker

Struct Tracker 

Source
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

Source

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()?;
Source

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 output
  • output_tokens: Actual tokens from RTK output
  • exec_time_ms: Execution time in milliseconds
§Examples
use rtk::tracking::Tracker;

let tracker = Tracker::new()?;
tracker.record("ls -la", "rtk ls", 1000, 200, 50)?;
Source

pub fn reset_all(&self) -> Result<()>

Delete all tracked data (commands + parse_failures), resetting all stats to zero.

Source

pub fn record_parse_failure( &self, raw_command: &str, error_message: &str, fallback_succeeded: bool, ) -> Result<()>

Record a parse failure for analytics.

Source

pub fn get_parse_failure_summary(&self) -> Result<ParseFailureSummary>

Get parse failure summary for rtk gain --failures.

Source

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);
Source

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).

Source

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);
}
Source

pub fn get_all_days_filtered( &self, project_path: Option<&str>, ) -> Result<Vec<DayStats>>

Get daily statistics filtered by project path. // added

Source

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);
}
Source

pub fn get_by_week_filtered( &self, project_path: Option<&str>, ) -> Result<Vec<WeekStats>>

Get weekly statistics filtered by project path. // added

Source

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);
}
Source

pub fn get_by_month_filtered( &self, project_path: Option<&str>, ) -> Result<Vec<MonthStats>>

Get monthly statistics filtered by project path. // added

Source

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);
}
Source

pub fn get_recent_filtered( &self, limit: usize, project_path: Option<&str>, ) -> Result<Vec<CommandRecord>>

Get recent command history filtered by project path. // added

Source

pub fn count_commands_since(&self, since: DateTime<Utc>) -> Result<i64>

Count commands since a given timestamp (for telemetry).

Source

pub fn top_commands(&self, limit: usize) -> Result<Vec<String>>

Get top N commands by frequency (for telemetry).

Source

pub fn overall_savings_pct(&self) -> Result<f64>

Get overall savings percentage (for telemetry).

Source

pub fn total_tokens_saved(&self) -> Result<i64>

Get total tokens saved across all tracked commands (for telemetry).

Source

pub fn tokens_saved_24h(&self, since: DateTime<Utc>) -> Result<i64>

Get tokens saved in the last 24 hours (for telemetry).

Source

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.

Source

pub fn parse_failures_since(&self, since: DateTime<Utc>) -> Result<i64>

Count parse failures in the last 24 hours.

Source

pub fn low_savings_commands(&self, limit: usize) -> Result<Vec<(String, f64)>>

Count commands with low savings (<30%) — filters that need improvement.

Source

pub fn avg_savings_per_command(&self) -> Result<f64>

Average savings percentage per command (unweighted — each command name counts once).

Source

pub fn count_meta_command(&self, name: &str) -> Result<i64>

Count invocations of a specific meta-command (by rtk_cmd suffix).

Source

pub fn first_seen_days(&self) -> Result<i64>

Days since first recorded command (installation age).

Source

pub fn active_days_30d(&self) -> Result<i64>

Number of distinct active days in the last 30 days.

Source

pub fn commands_total(&self) -> Result<i64>

Total number of recorded commands.

Source

pub fn ecosystem_mix(&self) -> Result<Vec<(String, f64)>>

Ecosystem distribution as percentages (top categories by command prefix).

Source

pub fn tokens_saved_30d(&self) -> Result<i64>

Tokens saved in the last 30 days.

Source

pub fn projects_count(&self) -> Result<i64>

Number of distinct project paths.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.