gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8751adb88751adb8tracker: report 032mre8751adb8/lib/catalog.hl

12.5 KB

  1. // catalog.hl — THE PUBLIC LISTS (tracker.worldapi.org#13): every movie, newest release first, and every series, the one
  2. // whose newest RELEASED episode is newest first — the homepage's rows (10 each) and /movies, /series, /shows (24 per page).
  3. // Movies and series are both rows of `shows` (`type` 'movie' / 'series', the old tracker's shape).
  4. //
  5. // BUILT ONCE when the app starts — one scan of shows and of episodes (real data: 9,453 shows, 232k episodes, ~1 s) — and
  6. // kept in memory as two sorted lists of { id, key }. A request only slices its page and fetches those 24 shows: no scan.
  7. // Kept up to date without a rebuild:
  8. // - the daily TMDB sync (jobs.hl syncTick) calls `refreshCatalogShow(id)` after each show: its entry is recomputed
  9. // from its own seasons/episodes (new episodes, moved release dates) and moved to its new place;
  10. // - a NEW DAY: an episode upcoming yesterday may be out today — each series keeps the release dates of its upcoming
  11. // episodes (`upcoming`), and the first read of a new day moves the ones that are out into `latest` and re-sorts.
  12. // tools/sync-tmdb.hl runs with the app stopped (hybriel#21): the next start builds the lists afresh.
  13. //
  14. // ADULT TITLES (mission 054): only a title with `adult == false` (shows.hl isPublicTitle) is in the two lists — unknown is
  15. // hidden. The adult backfill (jobs.hl backfillTick) calls `refreshCatalogShow(id)` per title, so the lists fill as the
  16. // flags arrive. The series' release maps (latest/upcoming/release) hold every series, shown or not.
  17. //
  18. // ORDER: a movie by its `release` ('YYYY-MM-DD'; none → last). A series by its newest released episode (release ≤
  19. // today, UTC like the rest of the app); a series with none released yet comes after all that have one, by its own
  20. // `release`. Ties keep the table's order. `key` is one string so one comparison sorts: series '1<date>' / '0<date>'.
  21. //
  22. // SERIES AND SHOWS (tracker.worldapi.org#21, mission 068): the TV titles are two lists by their kind (shows.hl `titleKind`) —
  23. // /series and the homepage's Series row, /shows and its Shows row — both in the order above. `refreshCatalogShow` puts a title
  24. // into the list of its CURRENT kind (the kind backfill, jobs.hl kindTick, calls it when TMDB's type moves a title).
  25. //
  26. // GENRES (mission 058, tracker#25, /genres/<genre>): per genre id one list of its public movies AND series, newest first by
  27. // ONE date for both kinds: a movie's `release`, a series' newest released episode (none → its own `release`). Built in the
  28. // same scans; `refreshCatalogShow` moves the title in the lists of its old genres (`genresOf`) and its current ones.
  29. import { todayStr, dateOr, sortDesc } from './util.hl'
  30. import { genresTable, allShows, allEpisodes, showById, seasonsOfShow, episodesOfSeason, showYear, posterUrlOf, isPublicTitle, titleKind, titlePath, cardOf, posterUrlOfName, refreshTitleCaches } from './shows.hl'
  31. static pageSize = 24
  32. static seriesKey = (&latest, &release) => { return latest != null && latest != '' ? '1' + latest : '0' + release }
  33. // a series in a genre list: the plain date (comparable with a movie's release) — its newest released episode, else its release
  34. static genreKey = (&latest, &release) => { return latest != null && latest != '' ? latest : release }
  35. // ---- the lists' entries (sorted by util.hl sortDesc) -----------------------------------------------------------
  36. // one entry moved to its place: out of the list, then in before the first entry with a smaller key (linear)
  37. static placed = (&list, &entry) => {
  38. out = []
  39. let done = false
  40. for (x of list) {
  41. if (x.id != entry.id) {
  42. if (!done && entry.key > x.key) { out.push(entry) done = true }
  43. out.push(x)
  44. }
  45. }
  46. if (!done) { out.push(entry) }
  47. return out
  48. }
  49. // one entry out of the list (a title that is not public, mission 054)
  50. static without = (&list, &id) => {
  51. out = []
  52. for (x of list) { if (x.id != id) { out.push(x) } }
  53. return out
  54. }
  55. // ---- the index -------------------------------------------------------------------------------------------------
  56. static buildIndex = () => {
  57. today = todayStr()
  58. movies = []
  59. seriesIds = []
  60. showIds = []
  61. release = {}
  62. genreTitles = []
  63. for (s of allShows()) {
  64. if (s.type == 'movie') { if (isPublicTitle(s)) { movies.push({ id = s.id key = dateOr(s.release) }) } }
  65. else if (s.type == 'series') {
  66. if (isPublicTitle(s)) { if (titleKind(s) == 'show') { showIds.push(s.id) } else { seriesIds.push(s.id) } }
  67. release[s.id] = dateOr(s.release)
  68. }
  69. if ((s.type == 'movie' || s.type == 'series') && isPublicTitle(s) && s.genres != null && s.genres.length > 0) {
  70. genreTitles.push({ id = s.id movie = s.type == 'movie' release = dateOr(s.release) genres = s.genres })
  71. }
  72. }
  73. latest = {}
  74. upcoming = {}
  75. for (e of allEpisodes()) {
  76. // tracker#31: a merged episode is its keeper episode's twin (same date) — not counted twice
  77. if (e.show != null && e.mergedInto == null && dateOr(e.release) != '') {
  78. if (e.release <= today) {
  79. let l = latest[e.show]
  80. if (l == null || e.release > l) { latest[e.show] = e.release }
  81. } else {
  82. let u = upcoming[e.show]
  83. if (u == null) { u = [] }
  84. u.push(e.release)
  85. upcoming[e.show] = u
  86. }
  87. }
  88. }
  89. series = []
  90. for (id of seriesIds) { series.push({ id = id key = seriesKey(latest[id], release[id]) }) }
  91. shows = []
  92. for (id of showIds) { shows.push({ id = id key = seriesKey(latest[id], release[id]) }) }
  93. byGenre = {}
  94. genresOf = {}
  95. for (t of genreTitles) {
  96. let key = t.movie ? t.release : genreKey(latest[t.id], t.release)
  97. for (g of t.genres) {
  98. let list = byGenre[g]
  99. if (list == null) { list = [] }
  100. list.push({ id = t.id key = key })
  101. byGenre[g] = list
  102. }
  103. genresOf[t.id] = t.genres
  104. }
  105. genres = {}
  106. for (g of byGenre.keys()) { genres[g] = sortDesc(byGenre[g]) }
  107. return { day = today movies = sortDesc(movies) series = sortDesc(series) shows = sortDesc(shows) latest = latest upcoming = upcoming release = release genres = genres genresOf = genresOf }
  108. }
  109. static index = buildIndex()
  110. // a TV list with every key from the current release maps, sorted again
  111. static rekeyed = (&list) => {
  112. out = []
  113. for (x of list) { out.push({ id = x.id key = seriesKey(index.latest[x.id], index.release[x.id]) }) }
  114. return sortDesc(out)
  115. }
  116. // a new day: upcoming episodes that are out now become their series' newest released one
  117. static rollDay = () => {
  118. today = todayStr()
  119. if (index.day == today) { return null }
  120. let moved = false
  121. for (id of index.upcoming.keys()) {
  122. let rest = []
  123. let l = index.latest[id]
  124. for (d of index.upcoming[id]) {
  125. if (d <= today) {
  126. if (l == null || d > l) { l = d }
  127. moved = true
  128. } else { rest.push(d) }
  129. }
  130. index.upcoming[id] = rest
  131. if (l != null) { index.latest[id] = l }
  132. }
  133. if (moved) {
  134. index.series = rekeyed(index.series)
  135. index.shows = rekeyed(index.shows)
  136. // the genre lists too: a series' key is its newest released episode (movies keep theirs)
  137. for (g of index.genres.keys()) {
  138. let list = []
  139. for (x of index.genres[g]) { list.push({ id = x.id key = index.release[x.id] != null ? genreKey(index.latest[x.id], index.release[x.id]) : x.key }) }
  140. index.genres[g] = sortDesc(list)
  141. }
  142. }
  143. index.day = today
  144. return null
  145. }
  146. // one title out of the lists of the genres it was in, then (when public) into those of its current genres
  147. static refreshGenres = (&show, &shown, key) => {
  148. old = index.genresOf[show.id]
  149. if (old != null) {
  150. for (g of old) { if (index.genres[g] != null) { index.genres[g] = without(index.genres[g], show.id) } }
  151. }
  152. index.genresOf[show.id] = null
  153. if (!shown || show.genres == null || show.genres.length == 0) { return null }
  154. for (g of show.genres) {
  155. let list = index.genres[g]
  156. if (list == null) { list = [] }
  157. index.genres[g] = placed(list, { id = show.id key = key })
  158. }
  159. index.genresOf[show.id] = show.genres
  160. return null
  161. }
  162. // after the sync (or the adult backfill) touched a show: its entry from its own data again (only that show's
  163. // seasons/episodes are read) — or out of the list when it is not public (adult or unknown, mission 054)
  164. static refreshCatalogShow = (&showId) => {
  165. rollDay()
  166. show = showById(showId)
  167. if (show == null) { return null }
  168. // tracker#30: the lists' slim cache of this title (shows.hl cardOf / episodeRowsOf) from the changed record
  169. refreshTitleCaches(show)
  170. shown = isPublicTitle(show)
  171. if (show.type == 'movie') {
  172. index.movies = shown ? placed(index.movies, { id = show.id key = dateOr(show.release) }) : without(index.movies, show.id)
  173. refreshGenres(show, shown, dateOr(show.release))
  174. return null
  175. }
  176. if (show.type != 'series') { return null }
  177. today = index.day
  178. let l = null
  179. up = []
  180. for (s of seasonsOfShow(show)) {
  181. for (e of episodesOfSeason(s)) {
  182. if (dateOr(e.release) != '') {
  183. if (e.release <= today) { if (l == null || e.release > l) { l = e.release } } else { up.push(e.release) }
  184. }
  185. }
  186. }
  187. index.latest[show.id] = l
  188. index.upcoming[show.id] = up
  189. index.release[show.id] = dateOr(show.release)
  190. // in the list of its kind (#21), out of the other one
  191. entry = { id = show.id key = seriesKey(l, index.release[show.id]) }
  192. isShow = titleKind(show) == 'show'
  193. index.series = shown && !isShow ? placed(index.series, entry) : without(index.series, show.id)
  194. index.shows = shown && isShow ? placed(index.shows, entry) : without(index.shows, show.id)
  195. refreshGenres(show, shown, genreKey(l, index.release[show.id]))
  196. return null
  197. }
  198. // ---- what the pages read ---------------------------------------------------------------------------------------
  199. // one poster tile: poster, title, year, the title's page (/movies/, /series/ or /shows/<slug>, shows.hl titlePath — one
  200. // component for all three, with its Follow button). tracker#30 (mission 025): from the slim cached card (shows.hl cardOf),
  201. // not the whole record with its cast and crew
  202. static tilesOf = (&entries) => {
  203. out = []
  204. for (x of entries) {
  205. let c = cardOf(x.id)
  206. if (c != null) { out.push({ id = c.id title = c.title year = c.year href = c.href posterUrl = posterUrlOfName(c.posterName) }) }
  207. }
  208. return out
  209. }
  210. // the homepage: the n latest RELEASED movies (none dated after today) and the n series with the newest released episode
  211. static latestMovies = (&n) => {
  212. rollDay()
  213. want = []
  214. for (x of index.movies) {
  215. if (want.length < n && x.key != '' && x.key <= index.day) { want.push(x) }
  216. if (want.length >= n) { return tilesOf(want) }
  217. }
  218. return tilesOf(want)
  219. }
  220. static latestSeries = (&n) => { rollDay() return latestTv(index.series, n) }
  221. static latestShows = (&n) => { rollDay() return latestTv(index.shows, n) }
  222. static latestTv = (&list, &n) => {
  223. want = []
  224. for (x of list) {
  225. if (want.length < n && x.key.slice(0, 1) == '1') { want.push(x) }
  226. if (want.length >= n) { return tilesOf(want) }
  227. }
  228. return tilesOf(want)
  229. }
  230. // a page number from the address (`/movies/page/:page`, a String) — anything that is not a whole number ≥ 1 is page 1;
  231. // past the last page is the last page
  232. static pageNumberOf = (&raw, &pages) => {
  233. if (raw == null) { return 1 }
  234. s = '' + raw
  235. if (s == '' || s.length > 6) { return 1 }
  236. let i = 0
  237. while (i < s.length) {
  238. let c = s.charCodeAt(i)
  239. if (c < 48 || c > 57) { return 1 }
  240. i = i + 1
  241. }
  242. p = toNumber(s)
  243. if (p < 1) { return 1 }
  244. return p > pages ? pages : p
  245. }
  246. static pagesOf = (&count) => {
  247. let p = 1
  248. while (p * pageSize < count) { p = p + 1 }
  249. return p
  250. }
  251. // { page, pages, tiles } of one list
  252. static pageOf = (&list, &raw) => {
  253. pages = pagesOf(list.length)
  254. let page = pageNumberOf(raw, pages)
  255. return { page = page pages = pages tiles = tilesOf(list.slice((page - 1) * pageSize, page * pageSize)) }
  256. }
  257. static moviesPage = (&raw) => { rollDay() return pageOf(index.movies, raw) }
  258. static seriesPage = (&raw) => { rollDay() return pageOf(index.series, raw) }
  259. static showsPage = (&raw) => { rollDay() return pageOf(index.shows, raw) }
  260. static catalogCounts = () => { return { movies = index.movies.length series = index.series.length shows = index.shows.length } }
  261. // /genres/<genre> (mission 058): one genre's movies and series, newest first, 24 per page; how many there are
  262. static genrePage = (&genreId, &raw) => {
  263. rollDay()
  264. list = index.genres[genreId]
  265. return pageOf(list != null ? list : [], raw)
  266. }
  267. static genreCount = (&genreId) => {
  268. list = index.genres[genreId]
  269. return list != null ? list.length : 0
  270. }
  271. // /genres (tracker#32, the main menu's Genres): every genre that has public titles, A–Z, with how many — the genres table is 27 rows
  272. static genreList = () => {
  273. keyed = []
  274. for (g of genresTable.find(null, null)) {
  275. let n = genreCount(g.id)
  276. if (n > 0 && g.name != null && g.urlSegment != null) { keyed.push({ key = ('' + g.name).toLowerCase() name = '' + g.name href = '/genres/' + g.urlSegment count = n }) }
  277. }
  278. desc = sortDesc(keyed)
  279. out = []
  280. let i = desc.length - 1
  281. while (i >= 0) { out.push(desc[i]) i = i - 1 }
  282. return out
  283. }

Branches

Latest commits

  • 8751adb8tracker: report 032mre
  • 9bce1f65tracker mission 032: STATUS gate files + the hour-boundary flakemre
  • 718bfb89tracker#37 (mission 032): /people = everyone, last updated first (updatedAt stamped by the person fill; view built at boot, touched people first at once), photo + name tiles (person colour) with the /movies pagination, /people/<letter> removed; photo = our file, tmdbProfile, a cast/crew entry's profile (in-memory map at boot), else the new 'no photo' placeholder; new cast/crew/created_by people keep tmdbProfile; search people rows with the photo; /settings = the heading only; util.hl sortDesc starts from sorted runs (same result, 105k: 1.6 s -> 0.15 s); gates 369/0, 32/0, 52/0, check-theme 0; README/STATUS/LOGmre
  • 96ba683adeploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the scriptmre
  • eb3b9205tracker: report 031mre
  • 9b5d2e89tracker mission 031: README (What it does, Test: four gates + the #32 checks, Files: theme/, new pages), STATUS (real copy, A/B load, how to repeat, open points), LOGmre
  • 39950e4ctracker#32 (mission 031): the WorldAPI theme (theme/ vendored verbatim from layouts.worldapi.org 85b5654; styles.hl inherits it: accent green-dark, type colours 1-6; own base/header rules, row lines, genre-pill and inverted-button frames removed, the season foldable keeps its line; check-theme 21 -> 0, 4th deploy gate; main actions class primary) and the #32 header (theme AppHeader/MainMenu/UserMenu/Sidebar/ContentFirst: desktop brand, search, Series|Shows|Movies|Genres|People, user icon with Unwatched..Settings, Logout; signed out the ident selector, phone the iD icon dropdown; phone menu in the sidebar overlay; marked entry by :has); /find -> /search/<q>, /genres, /people(/<letter>), /settings; main { ContentFirst { slot } } works around the hl:web one-line slot bug; gates 365/0, 32/0, 52/0, check-theme 0mre
  • a386dc92tracker: reports 029 + 030mre
  • 71e0fd7dtracker missions 029 + 030: README (What it does, Files, gate count), STATUS (real-copy numbers, how to repeat, open points), LOGmre
  • d36ea6eatracker#34 + #35 (mission 030): Follow directly under the poster, as wide as the poster (show.hl, styles.hl); the status pill next to a series' title — TVmaze's status (new tvmazeStatus, stored by the sync's TVmaze merge) else TMDB's, TVmaze Ended + TMDB Canceled = Canceled, inverted (filled, dark text, no border), green running / yellow pending / red canceled / muted ended (shows.hl statusOf); the daily delta asks TVmaze's status of an unfollowed series TVmaze's change list names (dailysync.hl syncRunStep, sync.hl syncTvmazeStatus); the status backfill after the details repair (backfill.hl, jobs.hl statusTick; resumable, 550 ms per TVmaze request); gates 354/0, 32/0, 52/0mre
  • 7d7d4487tracker#33 (mission 029): reduced titles — every title TMDB's details never went through this app (no detailsAt, no tmdbSync) is incomplete (shows.hl isIncomplete; the old tracker's migrated rows passed #26's test: 5,697 non-adult on the live copy, 691 series without seasons); the repair job does the visibly reduced first (shows.hl missingParts), the page completes one on open; a title TMDB has no poster for (The Remaining) shows the placeholder; tools/count-incomplete.hl; gate fixtures stand for synced titles (tmdbSync), tests/seed-reduced.hl + #33 checks; gates 347/0, 32/0, 52/0mre
  • 661c2592tracker: report 028mre
  • 27c916fatracker mission 028: README ("Code order", the new file map), STATUS (counts before/after, tests, how to repeat, open), LOGmre
  • d924f398tracker mission 028: comments name the new files (sync.hl, dailysync.hl, backfill.hl, credits.hl, jobs.hl, images.hl …); tools/ref-params.py + tools/lambda-audit.py also scan lib/ (they globbed the root only), lambda-audit counts a plain `x = p` alias like `let x = p`mre
  • 2e89b968tracker mission 028 (code order) 5/5 let: `let` only where a variable is reassigned — 667 never-reassigned lets became plain declarations (project.hl, lib/, components/, tools/, tests/); kept: 264 in loop bodies (a plain declaration there is 'Cannot reassign' on the 2nd pass), 234 reassigned, 27 whose name is also a member/outer/free name (a plain write would rebind it); tools/let-audit.py decides and fixes (README 'Code order'); tests/realdata-m028.{sh,mjs} = the page-output diff on a real copy; gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 54796ff2tracker mission 028 (code order) 4/5 thin faces + last copies: the show page's check/follow faces call lib/watches.hl toggleWatched / toggleSeasonWatched (seasonAllWatched moved there) and lib/follows.hl toggleFollowed; both logins (header selector face, /login/callback) share lib/users.hl userOfCode; todayStr/listOf copies in components and the export readers copied into tools/migrate.hl + tools/old-short-ids.hl now once (lib/util.hl, lib/export.hl); gates 342/0, 32/0, 52/0; old-short-ids output byte-identical, migrate output identicalmre
  • 06b078e3tracker mission 028 (code order) 3/5 project.hl is the map: config, routes, wiring and a feature → file index (914 → 258 lines); the background jobs (daily sync run, backfills, details repair, credits job, merge, short ids, collection seed) moved unchanged into lib/jobs.hl (a class: their state is reassigned every step, a static cannot be; one instance made after the server), the login callback into lib/users.hl, poster/photo serving into lib/images.hl, the /shows/<slug> rule into lib/shows.hl showsMovedPath; route handlers are thin wrappers; gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 94716fd2tracker mission 028 (code order) 2/5 util + topics: lib/util.hl holds envOr, storageDir, postersDir, profilesDir, newId, hexDigits, todayStr, dateOr, textOr, hasId, listOr, firstOf, sortDesc once (were copied into up to 5 files); tmdbsync.hl split into tmdb.hl (TMDB/TVmaze requests), sync.hl (one title's sync), sync-helpers.hl, backfill.hl; details.hl split into details.hl, credits.hl, credits-helpers.hl (isIncomplete to shows.hl); search-helpers.hl (words, query, ranking, slugs); collections.hl (the TMDB collection seed, out of franchises.hl); deltasync.hl renamed dailysync.hl; no behaviour change: gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 186079b0tracker mission 028 (code order) 1/5 move: every root .hl except project.hl into lib/ (styles.hl into components/), import paths only; gates 342/0, 32/0, 52/0; real-copy pages identicalmre
  • 4f47f181tracker: report 027mre