feat(core): implement SqliteStatsRepository (closes #36)

Merged
MP
7mo ago
+375-12

Summary

  • Replace InMemoryStatsRepository stub with SqliteStatsRepo backed by the existing statistics table
  • Persistent download statistics survive app restarts
  • Real success_rate, daily_volumes, and top_hosts computed via SQL aggregation

Changes

  • New: src-tauri/src/adapters/driven/sqlite/stats_repo.rs — SqliteStatsRepo implementing StatsRepository trait
    • record_completed: UPSERT daily stats row with running average speed and peak speed tracking
    • get_stats: 4 raw SQL queries aggregating totals, daily volumes, success rate (terminal states only), and top hosts by download volume
  • Modified: src-tauri/src/lib.rs — wire SqliteStatsRepo replacing InMemoryStatsRepository
  • Modified: src-tauri/src/adapters/driven/mod.rs — gate stats module behind #[cfg(test)]
  • Modified: src-tauri/tests/app_state_wiring.rs — use SqliteStatsRepo in integration test

Test plan

  • 6 integration tests with in-memory SQLite (setup_test_db())
  • Empty DB returns zero-state
  • Single record inserts correctly
  • Multiple records accumulate (bytes, files, avg_speed, peak_speed)
  • Daily volumes returned in DESC order
  • Success rate computed from terminal states (Completed/Failed/Cancelled)
  • Top hosts ordered by total bytes DESC
  • cargo clippy --workspace -- -D warnings clean
  • 443 tests pass, 0 failures

Summary by cubic

Replaced the in-memory stats stub with SQLite-backed SqliteStatsRepo to persist stats and compute aggregates (totals, daily volumes, success rate, top hosts). Wired into the app and tests; the in-memory repo is now test-only; closes #36.

  • New Features

    • Upserts daily stats with a running average speed and peak speed.
  • Bug Fixes

    • Use localtime for daily bucketing.
    • Compute success rate from terminal states only (Completed, Error).
    • Limit top hosts to completed downloads.
    • Fix CHANGELOG to reference SqliteStatsRepo.

Summary by CodeRabbit

  • New Features

    • Statistics are now persisted in SQLite by default, with daily aggregates, rolling averages, peak speeds, and per-host summaries retained across sessions.
  • Bug Fixes

    • In-memory stats implementation is now only used for unit tests; persistent storage is the default.
  • Tests

    • Updated tests to validate persistent statistics, daily aggregation ordering, success-rate computation, and top-host ranking.