Repository navigation
fix(dao): count VoD views per stream and hide the misattributed ones - #2155
Merged
Merged
Conversation
In production the lookup took ~5s per view (2,566 slow queries in 24h).
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.
Motivation and Context
AddVodViewlooked up the current hour's VoD stat row without filtering by stream: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
statstable: in production it took ~5s per view, with 2,566 slow-query warnings in 24h. It read the row outside its transaction and savedviewers+1, so concurrent views were lost.GetLectureNumLiveViewshad nolive = 1filter either, so the inflated VoD row could show up as a lecture's live peak.Description
AddVodViewincrements 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.(stream_id, live, time)onstats.AutoMigratebuilds it on the next boot, which takes a while on a large table.GetLectureNumLiveViewsfilterslive = 1.202611150soft-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.statsfiltersdeleted_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 oldAddVodView, the per-stream test gives stream 1 all 4 views and stream 2 none.Manual check, with prerequisites:
202611150runs on boot.Screenshots
No UI changes.