Skip to content

feat: add usage statistics and global --attr flag - #64

Merged
zx06 merged 7 commits into
mainfrom
feat/usage-stats
Aug 3, 2026
Merged

feat: add usage statistics and global --attr flag#64
zx06 merged 7 commits into
mainfrom
feat/usage-stats

Conversation

@zx06

@zx06 zx06 commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Summary

Add usage statistics tracking to xsql, allowing users to monitor command execution patterns across profiles and custom dimensions.

Changes

Core Module (internal/stats/)

  • types.go: Record, StatsConfig, AggregatedRecord type definitions
  • attrs.go: Attribute parsing (CLI --attr + ENV XSQL_ATTR)
  • store.go: JSONL file storage with atomic append (O_APPEND + Mutex for long-running processes)
  • query.go: Aggregation by cmd × profile × attrs

CLI Commands

  • xsql stats: Aggregate report with table/JSON output
  • xsql stats log: Detailed log with filtering
  • xsql stats reset: Clear statistics
  • --attr key=value: Global flag for custom dimensions (repeatable)

Integration

  • CLI query/schema commands record stats
  • Web API handler records stats
  • MCP server records stats

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

  • XSQL_STATS_ENABLED=true
  • XSQL_STATS_LOG_SQL=true
  • XSQL_ATTR=env=prod,team=ai

Example Usage

Enable stats in config, run commands with attributes, view aggregated stats, JSON output for AI consumption, view detailed log, reset statistics.

Testing

  • 29 unit tests for stats module (all passing)
  • E2E tests for stats command
  • All existing tests continue to pass

- 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

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.71698% with 86 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.39%. Comparing base (799dc46) to head (10af5a3).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
internal/stats/store.go 68.13% 17 Missing and 12 partials ⚠️
internal/mcp/tools.go 19.04% 17 Missing ⚠️
internal/web/handler.go 5.55% 17 Missing ⚠️
cmd/xsql/stats.go 86.81% 7 Missing and 5 partials ⚠️
cmd/xsql/root.go 81.25% 3 Missing and 3 partials ⚠️
internal/stats/query.go 95.45% 2 Missing and 1 partial ⚠️
internal/stats/attrs.go 94.73% 1 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
e2e 47.02% <ø> (ø)
integration 47.02% <ø> (ø)
unittests 72.20% <79.71%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

zx06 added 6 commits August 3, 2026 11:35
- 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%
@zx06
zx06 force-pushed the feat/usage-stats branch from b0fa169 to 10af5a3 Compare August 3, 2026 04:12
@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
6.1% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

❌ The last analysis has failed.

See analysis details on SonarQube Cloud

@zx06
zx06 merged commit 8fc0e79 into main Aug 3, 2026
13 of 14 checks passed
@zx06
zx06 deleted the feat/usage-stats branch August 3, 2026 04:43
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.

1 participant