Skip to content

fix(dao): count VoD views per stream and hide the misattributed ones - #2155

Merged
joschahenningsen merged 1 commit into
devfrom
fix/vod-view-stats
Nov 19, 2026
Merged

joschahenningsen merged 1 commit into
devfrom
fix/vod-view-stats

Conversation

@joschahenningsen

Copy link
Copy Markdown
Member

Motivation and Context

AddVodView looked up the current hour's VoD stat row without filtering by stream:

DB.First(&stat, "live = 0 AND time BETWEEN ? and ?", tFrom, tUntil)

Each hour, every VoD view was added to whichever stream was watched first that hour. Per-course and per-lecture VoD statistics have been wrong since #394. The views can't be reattributed, because the stream was never recorded.

The query also scanned the whole stats table: in production it took ~5s per view, with 2,566 slow-query warnings in 24h. It read the row outside its transaction and saved viewers+1, so concurrent views were lost.

GetLectureNumLiveViews had no live = 1 filter either, so the inflated VoD row could show up as a lecture's live peak.

Description

  • AddVodView increments the stream's own row for the hour in SQL and creates the row if there is none. Two racing first views leave two rows for the hour; every reader sums VoD rows.
  • Index (stream_id, live, time) on stats. AutoMigrate builds it on the next boot, which takes a while on a large table.
  • GetLectureNumLiveViews filters live = 1.
  • Migration 202611150 soft-deletes all existing VoD stat rows, so admins and lecturers no longer see the broken numbers. Live stat rows are untouched. Rolling back the migration restores the rows.
  • Every raw query on stats filters deleted_at IS NULL; GORM doesn't add its soft-delete filter to raw SQL.

After deploy, VoD views start from zero and are counted per stream.

Steps for Testing

go test -race ./dao/... covers per-stream counting, concurrent views, the live-peak filter, hiding soft-deleted rows, and the migration with its rollback. Against the old AddVodView, the per-stream test gives stream 1 all 4 views and stream 2 none.

Manual check, with prerequisites:

  • 1 Admin
  • 2 Students
  • 2 lectures with a VoD
  1. Before the migration, check that the course's VoD stats show numbers.
  2. Start the server; migration 202611150 runs on boot.
  3. Check that the course and lecture VoD stats are empty and the live stats are unchanged.
  4. As each student, watch a different VoD for more than the VoD-view threshold, then close the tab.
  5. Check that each lecture's VoD stats show exactly 1 view.

Screenshots

No UI changes.

In production the lookup took ~5s per view (2,566 slow queries in 24h).
@joschahenningsen
joschahenningsen merged commit 451182e into dev Nov 19, 2026
8 checks passed
@joschahenningsen
joschahenningsen deleted the fix/vod-view-stats branch November 19, 2026 10:39
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