Skip to content

fix(counter): count plays from every source - #564

Merged
LargeModGames merged 1 commit into
mainfrom
fix/song-counter-all-sources
Sep 23, 2026
Merged

LargeModGames merged 1 commit into
mainfrom
fix/song-counter-all-sources

Conversation

@LargeModGames

@LargeModGames LargeModGames commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

The global song counter lost momentum because of a regression in v0.40.3. The only count was sent from the me/player poll, when the API's track id differed from last_track_id. Native streaming wrote librespot's full spotify:track:<id> URI there, so the first confirming poll always saw a mismatch and counted each track once, by accident. bbc4668 (#385) normalized those ids to base62, and since then a song played on the spotatui device itself sends no count. The chart's daily rate went from about 3,700 in July to about 1,450 in September, starting the week v0.40.3 shipped.

This PR moves the counter off the Spotify poll into the driver tick, where it reads the same source-neutral playback snapshot as lyrics and cover art, so every source counts:

  • src/core/driver/play_count.rs (new): a song counts after 30 s of heard audio (half its length when shorter), for native Spotify, a remote Connect device, both queue slots, Local, Subsonic, Qobuz, YouTube and episodes. Internet radio counts one song per 150 s of heard audio.
  • Only forward movement of the playback position counts, past the furthest point of the current play. A pause, a stall, a load, a seek forward over 3 s or a stale "playing" state adds nothing. A replay from the top counts again; a track resumed where it stopped (after up to 32 other plays) does not.
  • App::decoded_change_pending holds the counter while a decoded source changes tracks, when the snapshot already names the next track but the sink still plays the last one.
  • The old dispatch in the me/player handler is removed, so a remote-device track does not count twice.
  • The native Stopped arm and the unavailable-track halt now set native_is_playing = Some(false). Before, the next poll copied the stale Some(true) back into the context, so the playbar showed a stopped player as playing.
  • The first-run prompt, the Settings description, the README and the CHANGELOG describe the new rule.

Testing

  • cargo fmt --all -- --check: clean
  • cargo clippy -- -D warnings for the default, all-sources, slim, headless (telemetry) and headless-streaming (telemetry,streaming) legs, plus radio-only and local-only builds: all pass
  • cargo test --no-default-features --features telemetry,tui: 1130 passed, including ratchet_counters_match_the_checked_in_baselines
  • cargo test: 1469 passed
  • 20 new tests in play_count.rs cover the rules above, including a native track through current_playback_snapshot

Not verified: live playback on any source, the macOS leg, and the all-sources test leg locally. The events.rs changes and decoded_change_pending have no unit test, because both need a real player or output device.

Additional notes

  • test_attribute_total moves from 2063 to 2083 (the adoption direction).
  • Counts missed since v0.40.3 cannot be recovered; the chart will step up as users update.
  • Known small limits: a forward seek of 3 s or less counts as play, and a replay of a track shorter than about 5 s can fail to count again.
  • Related follow-up: A failed token refresh stops the playback poll for the rest of the session #563.

💬 Questions or want to chat with other contributors? Join the spotatui Discord.

Summary by CodeRabbit

  • Bug Fixes
    • The global song counter now increments after 30 seconds of credited playback rather than when a track starts. Internet radio counts one song per 2.5 minutes.
    • Improved playback state reporting when a player stops or becomes unavailable.

Since v0.40.3 a song played through native streaming sent no count:
bbc4668 made the native track id equal the poll's id, and the poll
counted only on a mismatch. Count in the driver tick from the
playback snapshot instead, for every source, after 30 s of heard
play. Radio counts one song per 150 s.
@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: LargeModGames/spotatui/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 18f176f6-40fe-49e7-9caf-71b74127498b

📥 Commits

Reviewing files that changed from the base of the PR and between e956d3d and 8e30efd.

📒 Files selected for processing (10)
  • CHANGELOG.md
  • README.md
  • src/core/app/playback_routing.rs
  • src/core/app/settings_schema.rs
  • src/core/driver/mod.rs
  • src/core/driver/play_count.rs
  • src/infra/network/playback.rs
  • src/infra/player/events.rs
  • src/runtime/bootstrap.rs
  • tools/gates.count
💤 Files with no reviewable changes (1)
  • src/infra/network/playback.rs

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

Global song counting now uses playback snapshots instead of new-track detection in the Spotify playback poll. It counts qualifying track playback and radio listening intervals across supported sources.

Changes

Global Song Counting

Layer / File(s) Summary
Playback state inputs
src/core/app/playback_routing.rs, src/infra/player/events.rs
The app reports pending decoded playback changes. Stopped and repeatedly unavailable native playback events set the native playing state to false.
Snapshot-based play counting
src/core/driver/*, src/infra/network/playback.rs, src/core/app/settings_schema.rs, src/runtime/bootstrap.rs, README.md, CHANGELOG.md, tools/gates.count
The driver passes playback snapshots to PlayCounter. The counter credits qualifying forward playback, tracks resumed and repeated plays, and dispatches increment events when counts are due. New-track detection in the Spotify playback poll no longer increments the counter. Tests cover counting rules and event dispatch. Counter descriptions and the test attribute baseline are updated.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Driver
  participant App
  participant PlayCounter
  participant IoEvent
  Driver->>PlayCounter: Pass app and current playback snapshot to tick
  PlayCounter->>App: Check whether a decoded change is pending
  PlayCounter->>IoEvent: Dispatch increment when a play count is due
Loading

Merge Risk: ⚪ Minimal · up to 8e30e

Global song counting now counts plays from playback progress across all sources instead of counting track changes in the Spotify poll. The only effect is on anonymous counter telemetry; playback itself is unaffected, and the change is gated by the existing opt-in setting. No blocking issues were found, so this is ready to merge.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title uses the permitted fix prefix with the counter scope. “Count plays from every source” is concise, imperative, and describes the main change.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
✨ Simplify code
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Sep 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.94681% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/core/driver/play_count.rs 97.7% 8 Missing ⚠️
src/core/app/playback_routing.rs 71.4% 6 Missing ⚠️
src/infra/player/events.rs 0.0% 3 Missing ⚠️
src/core/driver/mod.rs 0.0% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@LargeModGames
LargeModGames merged commit b854e5c into main Sep 23, 2026
31 checks passed
@LargeModGames
LargeModGames deleted the fix/song-counter-all-sources branch September 23, 2026 19:56
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