Skip to content

feat: add usage statistics tracking - #2568

Merged
AucaCoyan merged 2 commits into
espanso:devfrom
mefengl:feature/usage-statistics
Nov 19, 2025
Merged

AucaCoyan merged 2 commits into
espanso:devfrom
mefengl:feature/usage-statistics

Conversation

@mefengl

@mefengl mefengl commented Nov 8, 2025 •

Copy link
Copy Markdown
Contributor

Adds usage statistics tracking for match expansions.

Usage:

espanso stats                    # View all statistics
espanso stats --period today     # Today's stats
espanso stats --count 20         # Top 20 triggers
espanso stats --grep ':td%'      # Filter by pattern
espanso stats --json             # JSON output
espanso stats clear              # Clear database
espanso stats prune --days 180   # Remove old records

Configuration:

# Stats are disable by default. To enable:
stats:
  enabled: true

I'm using this locally. If you're interested, feel free to test it. Will convert to a regular PR in one week.

Closes #2563

@AucaCoyan

Copy link
Copy Markdown
Collaborator

Thank you! This is great! My personal preference is to put this off by default, as I believe is some kind of tracking.
If enough users asks, we can set it on by default, but at first I would be inclined to turn it off (dunno how many users we need)

@mefengl

mefengl commented Nov 8, 2025

Copy link
Copy Markdown
Contributor Author

Thank you! This is great! My personal preference is to put this off by default, as I believe is some kind of tracking. If enough users asks, we can set it on by default, but at first I would be inclined to turn it off (dunno how many users we need)

agreed, makes sense

@mefengl
mefengl marked this pull request as ready for review November 15, 2025 00:59
@mefengl
mefengl requested a review from AucaCoyan as a code owner November 15, 2025 00:59
Adds .direnv directory to gitignore as it's generated by direnv
and should not be tracked in version control.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds usage statistics tracking functionality to espanso, allowing users to view and manage statistics about their text expansion usage. The feature is opt-in (disabled by default) and stores expansion data in a local SQLite database.

Key Changes:

  • Adds a new espanso stats CLI command with subcommands for viewing, clearing, and pruning statistics
  • Implements a background thread-based statistics recorder using SQLite for persistence
  • Integrates stats recording into the engine's event processing pipeline via middleware

Reviewed Changes

Copilot reviewed 17 out of 19 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
espanso/src/cli/stats.rs New CLI module implementing stats viewing, database management, and background recording thread
espanso/src/cli/worker/stats_recorder.rs Adapter implementing the StatsRecorder trait for the worker process
espanso/src/cli/worker/mod.rs Initializes stats database and recorder when stats are enabled
espanso-engine/src/process/middleware/stats.rs New middleware tracking match expansions and recording statistics
espanso-engine/src/process/default.rs Adds stats middleware to the processing pipeline
espanso-engine/src/event/internal.rs Adds trigger field to ImageRequestedEvent for stats tracking
espanso/src/cli/worker/engine/process/middleware/multiplex.rs Populates trigger field when creating ImageRequestedEvent
espanso-config/src/config/mod.rs Adds stats_enabled() method to Config trait
espanso-config/src/config/resolve.rs Implements stats configuration resolution with default=false
espanso-config/src/config/parse/yaml.rs Adds YAML parsing support for stats configuration
espanso/src/main.rs Adds stats subcommand with argument definitions
espanso/Cargo.toml Adds rusqlite dependency
Cargo.lock Updates lock file with rusqlite and transitive dependencies
.gitignore Adds .direnv/ to ignored paths

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread espanso/src/cli/stats.rs Resolved
Comment thread espanso/src/cli/stats.rs Resolved
Comment thread espanso/src/cli/stats.rs
Comment on lines +115 to +116
let display_trigger: String = trigger.chars().take(19).collect();
println!("│ {:<19} │ {:>5} │", display_trigger, cnt);

Copilot AI Nov 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using chars().take(19) to truncate triggers can produce incorrect display widths for characters that occupy more than one terminal column (e.g., emojis, CJK characters). This will cause table misalignment. Consider using a crate like unicode-width to properly calculate display width, or use graphemes for proper character boundary handling.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice comment, but nah, not worth to add a crate (even if it's already used multiple times in Cargo lock). It would be rare that a trigger use an emoji.
Non ascii characters may be a problem in the future, once #2123 is solved. Even then 19 chars is more than enough to recognize a trigger

Comment thread espanso/src/cli/stats.rs Resolved
Comment thread espanso/src/cli/worker/mod.rs Outdated
let icon_paths =
crate::icon::load_icon_paths(&paths.runtime).expect("unable to initialize icons");

// Initialize stats database and set recorder (configurable, default ON)

Copilot AI Nov 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment states "default ON" but the code in espanso-config/src/config/resolve.rs line 309 shows self.parsed.stats_enabled.unwrap_or(false), meaning stats are disabled by default. The comment should say "default OFF" to match the actual default behavior.

Suggested change
// Initialize stats database and set recorder (configurable, default ON)
// Initialize stats database and set recorder (configurable, default OFF)

Copilot uses AI. Check for mistakes.
Comment thread espanso/src/cli/stats.rs Resolved
Comment thread espanso/src/cli/stats.rs Resolved
Comment thread espanso/src/cli/stats.rs
Comment on lines +115 to +116
let display_trigger: String = trigger.chars().take(19).collect();
println!("│ {:<19} │ {:>5} │", display_trigger, cnt);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice comment, but nah, not worth to add a crate (even if it's already used multiple times in Cargo lock). It would be rare that a trigger use an emoji.
Non ascii characters may be a problem in the future, once #2123 is solved. Even then 19 chars is more than enough to recognize a trigger

Comment thread espanso/src/cli/stats.rs Resolved
@AucaCoyan

AucaCoyan commented Nov 18, 2025 •

Copy link
Copy Markdown
Collaborator

Reviewing the pr:

  • Good job with abstracting the (bad) CLI exit codes from the rust Result(), I intend to use Result for all the commands, but it's impossible to do until we upgrade to clap Derive API (significant rework)
  • I like that we save the data in a sqlite database. Should be really small, and it's very popular.
  • it's good that you picked the synchronous API, don't want to deal with async rust code for such a simple feature
  • I didn't test it, but i doubt that sqlite for triggers, even if you have thousands could cause an impact on performance. We don't have measure of performance (with an exception of a CLI script), so this just a sentiment, not a proof.

Some comments, that don't stop this PR from merging:

  • this is a kind of telemetry (not dangerous, or networking, or anything), it would be great if we update the documentation about it informing that
    • the default is false, so no need to worry if you are not interested
    • in case it's on, what value saves: (trigger name, trigger id (internal espanso number) and datetime of each expansion).

Copilot did some nice suggestions, some of them aren't useful, but some do! Do this changes and I think we are good to go, but in general it's a very nice implementation.

Thank you very much for the submission!

@mefengl

mefengl commented Nov 19, 2025

Copy link
Copy Markdown
Contributor Author

All suggestions addressed.

this is a kind of telemetry (not dangerous, or networking, or anything), it would be great if we update the documentation about it informing that

Should the documentation be added to https://github.com/espanso/website?

@AucaCoyan

Copy link
Copy Markdown
Collaborator

Yes! I meant that documentation, not to document here 😅

@AucaCoyan
AucaCoyan merged commit 383f4c3 into espanso:dev Nov 19, 2025
@mefengl
mefengl deleted the feature/usage-statistics branch November 19, 2025 17:45
@smeech smeech mentioned this pull request Nov 21, 2025
53 of 56 tasks
sboldt1978 pushed a commit to sboldt1978/espanso that referenced this pull request Feb 11, 2026
* chore: add .direnv to .gitignore

Adds .direnv directory to gitignore as it's generated by direnv
and should not be tracked in version control.

* feat: add usage statistics tracking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Usage Statistics Feature

3 participants