feat: add usage statistics and global --attr flag - #64
Merged
Conversation
- Add internal/stats module (JSONL store, aggregation, attribute parsing) - Add xsql stats command (aggregate report, log, reset) - Add global --attr key=value flag for dimension tagging - Integrate stats recording in CLI query/schema, Web API, and MCP server - Support XSQL_ATTR env and XSQL_STATS_* config options - Stats disabled by default, SQL logging optional for audit scenarios - Update docs: cli-spec, config, env - Add E2E tests
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #64 +/- ##
==========================================
- Coverage 83.43% 82.39% -1.04%
==========================================
Files 46 50 +4
Lines 3839 4249 +410
==========================================
+ Hits 3203 3501 +298
- Misses 477 565 +88
- Partials 159 183 +24
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- Handle f.Close() error return values in store.go (errcheck) - Fix goimports formatting in types.go - Skip TestStore_Permissions on Windows (different permission model)
- Add tests for StatsTableFormatter and StatsLogTableFormatter - Add tests for formatAttrs, GetXSQLAttrEnv - Add tests for store error paths (corrupted lines, mkdir errors) - Add tests for cleanup edge cases (no old records, all old)
- Test recordCmdStats with enabled/disabled, log_sql, error_code - Test runStats, runStatsLog, runStatsReset - Test NewStatsCommand structure - cmd/xsql coverage: 69.9% → 76.8% - recordCmdStats: 22.2% → 100%
Add expandPath function to expand ~/ prefix to user home directory. Now file_path: ~/.config/xsql/stats.jsonl works correctly.
- runStats: 75% → 91.7% (test with profile filter and JSON) - runStatsLog: 65% → 95% (test with profile, limit, JSON) - newStatsResetCommand: 50% → 100% (test cobra command execution) - recordCmdStats: 100%
|
|
❌ The last analysis has failed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Add usage statistics tracking to xsql, allowing users to monitor command execution patterns across profiles and custom dimensions.
Changes
Core Module (internal/stats/)
CLI Commands
Integration
Configuration
stats.enabled: true (default: false)
stats.log_sql: false (record SQL content for audit)
stats.file_path: ~/.config/xsql/stats.jsonl
stats.retention_days: 30 (auto-cleanup)
ENV Variables
Example Usage
Enable stats in config, run commands with attributes, view aggregated stats, JSON output for AI consumption, view detailed log, reset statistics.
Testing