gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf52862a2f52862a2log: HM62b startedmref52862a2/lib/watches.hl

6.5 KB

  1. // lib/watches.hl — PER-USER WATCH STATE (episode/season checks, tracker.worldapi.org#4), reused as-is
  2. // (not copied) by /unwatched and /schedule (tickets #5/#6: "same check icon and watch logic as the
  3. // show page — reuse, don't copy"). storage/mpackdb/watches.db, migrated by tools/migrate.hl
  4. // (tracker.worldapi.org#2): { targetType ('episode'|'season'|'movie'), target, user, at, oldId }. 'movie' (mission 058,
  5. // tracker#24): target = the movie's shows.db id. A season's CHECK on the page is derived from its episodes (components/show.hl).
  6. import { MPackDB } from 'hl:mpackdb'
  7. import { now } from 'hl:time'
  8. import { randomBytes } from 'hl:crypto'
  9. import { storageDir } from './util.hl'
  10. static watchesTable = new MPackDB(file = storageDir + '/watches.db', primaryKey = '@id', indexes = ['!oldId'])
  11. // every watch of this user — a linear scan (11692 rows total across every user, migrated data; cheap
  12. // once per page load) — no declared index on `user`
  13. // mission 062: KEPT per user until that user's next write (setWatched) — one watch click asked for it 3 times (the face, then
  14. // the page's re-mount for the session sync: rows + watchedEpisodes), each a scan + decode of every watch (~25 ms on the real data).
  15. // One process per storage (hybriel#21): every write goes through setWatched below. Flat statics, written key by key (plain data
  16. // deep-copies on assignment)
  17. static watchCache = {}
  18. static watchCacheOk = {}
  19. static watchesOfUser = (&userId) => {
  20. if (userId == null) { return [] }
  21. if (watchCacheOk[userId] == true) { return watchCache[userId] }
  22. out = []
  23. for (w of watchesTable.iterate(null, null)) { if (w.user == userId) { out.push(w) } }
  24. watchCache[userId] = out
  25. watchCacheOk[userId] = true
  26. return out
  27. }
  28. // '<targetType>:<target>' → true for every watch in `rows`: built ONCE per page load so a list asks
  29. // `watched['episode:' + e.id] == true` instead of `isWatched` scanning all the user's watches per episode
  30. // (/my/unwatched, tracker.worldapi.org#8: 831 rows × 11,692 watches took ~15 s)
  31. static watchedSet = (&rows) => {
  32. m = {}
  33. for (w of rows) { if (w.target != null) { m[w.targetType + ':' + w.target] = true } }
  34. return m
  35. }
  36. // tracker#30 (mission 025): `watchedSet` of a user's watches, KEPT like watchesOfUser until that user's next setWatched — the
  37. // /my/ lists ask for it on every load (11.7k rows → a fresh 11.7k-key map each time). Returned, never `let`-copied (shows.hl
  38. // cardOf).
  39. static watchedSetCache = {}
  40. static watchedSetOk = {}
  41. static watchedSetOf = (&userId) => {
  42. if (userId == null) { return {} }
  43. if (watchedSetOk[userId] == true && watchCacheOk[userId] == true) { return watchedSetCache[userId] }
  44. watchedSetCache[userId] = watchedSet(watchesOfUser(&userId))
  45. watchedSetOk[userId] = true
  46. return watchedSetCache[userId]
  47. }
  48. // GC1 measurement: users whose watch rows / set are kept
  49. static watchCacheSizes = () => { return { rows = watchCache.keys().length sets = watchedSetCache.keys().length } }
  50. // merge.hl (tracker#31) moved watches of this user to another target: the kept rows/set are read again
  51. static forgetUserWatches = (&userId) => {
  52. if (userId == null) { return null }
  53. watchCacheOk[userId] = false
  54. watchedSetOk[userId] = false
  55. return null
  56. }
  57. // every episode id anyone has watched (project.hl warms shows.hl episodeKeyOf with them at boot, tracker#30)
  58. static allWatchedEpisodeIds = () => {
  59. out = []
  60. for (w of watchesTable.iterate(null, null)) { if (w.targetType == 'episode' && w.target != null) { out.push(w.target) } }
  61. return out
  62. }
  63. static findWatch = (&rows, &targetType, &targetId) => {
  64. for (w of rows) { if (w.targetType == targetType && w.target == targetId) { return w } }
  65. return null
  66. }
  67. static isWatched = (&rows, &targetType, &targetId) => { return findWatch(&rows, &targetType, &targetId) != null }
  68. // the check's class — "a disc with a check in it", solid once watched, regular while not (the
  69. // SAME icon and class the show page uses, tracker.worldapi.org#4/#5 — /unwatched and /schedule
  70. // reuse this instead of each declaring their own)
  71. static watchClassOf = (&watched) => { return watched ? 'checkicon solid' : 'checkicon regular' }
  72. // a NEW watch's oldId can't be null: the table's `!oldId` UNIQUE index would refuse a second one
  73. // (the migrated rows all carry a real Mongo hex id; a fresh watch gets its own unique marker instead)
  74. static setWatched = (&userId, targetType, &targetId, &rows, &watched) => {
  75. existing = findWatch(&rows, &targetType, &targetId)
  76. watchCacheOk[userId] = false
  77. watchedSetOk[userId] = false
  78. if (watched) {
  79. if (existing != null) { return true }
  80. id = watchesTable.put({ id = randomBytes(8, 'hex') targetType = targetType target = targetId user = userId at = now() oldId = 'app:' + randomBytes(8, 'hex') })
  81. return id != null
  82. }
  83. if (existing == null) { return true }
  84. return watchesTable.delete(existing.id) > 0
  85. }
  86. // a season check toggles the season's OWN watch record AND every one of its episodes (creator: "if
  87. // the season is checked or unchecked all its episodes get checked or unchecked") — `rows` is the
  88. // snapshot from BEFORE this batch; every target below is distinct, so the staleness across the loop
  89. // never matters (no two calls touch the same target)
  90. static setSeasonWatched = (&userId, &seasonId, &episodeIds, &rows, &watched) => {
  91. let ok = setWatched(&userId, 'season', &seasonId, &rows, &watched)
  92. for (eid of episodeIds) { ok = setWatched(&userId, 'episode', &eid, &rows, &watched) && ok }
  93. return ok
  94. }
  95. // ---- the checks clicked (the show page's faces, components/show.hl — mission 028: out of the faces) ------------------------
  96. // an episode's or a movie's check (#4, #24): watched → not, not → watched; the new state, or null when it could not be saved
  97. static toggleWatched = (&userId, targetType, &targetId) => {
  98. rows = watchesOfUser(&userId)
  99. watched = isWatched(&rows, &targetType, &targetId) == false
  100. return setWatched(&userId, targetType, &targetId, &rows, &watched) ? watched : null
  101. }
  102. // #23: a season is watched when ALL its episodes are (by the ids it lists); one without episodes keeps its own season watch
  103. static seasonAllWatched = (&s, &watched) => {
  104. if (s.episodes.length == 0) { return watched['season:' + s.id] == true }
  105. for (eid of s.episodes) { if (watched['episode:' + eid] != true) { return false } }
  106. return true
  107. }
  108. // #23: a season's check: not all its episodes watched → watch them all; all watched → unwatch them all. The new state or null
  109. static toggleSeasonWatched = (&userId, &season) => {
  110. rows = watchesOfUser(&userId)
  111. current = watchedSetOf(&userId)
  112. watched = !seasonAllWatched(&season, &current)
  113. return setSeasonWatched(&userId, &season.id, &season.episodes, &rows, &watched) ? watched : null
  114. }

Branches

Latest commits

  • f52862a2log: HM62b startedmre
  • 88fde2c4mission HM62bmre
  • a722e526log: #62 blocked by hybriel#317mre
  • 074a7418log: #62 assigned to Tracker; pending rulemre
  • 06f6ae8fHM62 report: mpackdb index gapmre
  • 86fe5430log: HM62 startedmre
  • 755307bbmission HM62mre
  • 4b2f2995HM61 report: tracker#61 packed people ordermre
  • 48faa84flog: #60 deploy startedmre
  • 90734c6cMerge branch 'hm60-shortids'mre
  • e0db0d1bHM60 report: tracker#60 packed short-ID ownersmre
  • 3c79b576Pack short-ID ownership into byte buffersmre
  • e822524bstatus wordingmre
  • 1c7f0480log: HM60/HM61 startedmre
  • 2841d215missions HM60 HM61mre
  • 289c6ce4HM1 report: heap object breakdownmre
  • 2e7d6257log: HM1 startedmre
  • 2f790465mission HM1: heap object breakdownmre
  • a18957falog: evening re-measure after CBmre
  • aac9a98dlog: evening re-measure startmre