gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit079f376d079f376dMove seven components' styles into Style blocksmre079f376d/lib/catalog.hl

15.7 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 sorted flat lists of alternating id and key slots. 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 { sortEntries, appendEntry } from './catalog-entries.hl'
  30. import { todayStr, dateOr, sortDesc } from './util.hl'
  31. import { warm } from './warm.hl'
  32. import { genresTable, showById, seasonsOfShow, episodesOfSeason, showYear, posterUrlOf, isPublicTitle, titleKind, titlePath, cardOf, posterUrlOfName, refreshTitleCaches } from './shows.hl'
  33. static pageSize = 24
  34. static seriesKey = (&latest, &release) => { return latest != null && latest != '' ? '1' + latest : '0' + release }
  35. // a series in a genre list: the plain date (comparable with a movie's release) — its newest released episode, else its release
  36. static genreKey = (&latest, &release) => { return latest != null && latest != '' ? latest : release }
  37. // ---- the lists' entries (alternating id/key slots, sorted by catalog-entries.hl) -----------------------------------------------------------
  38. // SL1 (stall 2026-11-03): the lists hold every public title (movies 150 K, a genre up to 100 K) and a refresh used to rebuild the
  39. // WHOLE list per title (`placed` / `without` answered a new list, copied once more on the assignment): 120 ms per movie list, 190 ms for
  40. // its genres, per title a person fill or the details repair stored — 40 titles of a filmography blocked the loop for 10 s. Now the list
  41. // is changed IN PLACE through `&`: the title's old entry is found and cut out (one scan, no copy), the new one is put in at the place a
  42. // binary search finds (the lists are sorted by key, largest first). The place is the one the linear form gave: before the first entry
  43. // with a smaller key, so after the entries with an equal key (ties keep their order).
  44. static indexOfId = (&list, &id) => {
  45. let i = 0
  46. while (i < list.length) {
  47. if (list[i] == id) { return i }
  48. i = i + 2
  49. }
  50. return -1
  51. }
  52. // one entry out of the list (a title that is not public, mission 054; or before it is put in again)
  53. static takeOut = (&list, &id) => {
  54. at = indexOfId(&list, &id)
  55. if (at >= 0) { list.splice(at, 2) }
  56. return null
  57. }
  58. // one entry moved to its place: out of the list, then in before the first entry with a smaller key. `absent` = the caller knows the
  59. // list has no entry of this id (a new title; the genre lists after refreshGenres cut the old entries): the scan is left out (it is
  60. // 30 ms for 150 K entries in the real process, the rest of the work a few ms)
  61. static placeIn = (&list, &id, &key, absent) => {
  62. if (absent != true) { takeOut(&list, &id) }
  63. let lo = 0
  64. let hi = list.length / 2
  65. while (lo < hi) {
  66. mid = (lo + hi - (lo + hi) % 2) / 2
  67. if (list[mid * 2 + 1] >= key) { lo = mid + 1 } else { hi = mid }
  68. }
  69. list.splice(lo * 2, 0, key)
  70. list.splice(lo * 2, 0, id)
  71. return null
  72. }
  73. // ---- the index -------------------------------------------------------------------------------------------------
  74. // mission 043: filled by ONE streamed pass per table (bootscan.hl: every show → noteIndexShow, every episode → noteIndexEpisode, then
  75. // finishIndex) — never a whole table in memory. `index` is filled in place (a static cannot be reassigned); `pending` holds what the
  76. // show pass collects for the finish.
  77. static index = { day = '' movies = [] series = [] shows = [] latest = {} upcoming = {} release = {} genres = {} genresOf = {} }
  78. static pending = { seriesIds = [] showIds = [] genreTitles = [] }
  79. static beginIndex = () => {
  80. index.day = todayStr()
  81. return null
  82. }
  83. static noteIndexShow = (&s) => {
  84. if (s.type == 'movie') { if (isPublicTitle(&s)) { appendEntry(&index.movies, &s.id, dateOr(&s.release)) } }
  85. else if (s.type == 'series') {
  86. if (isPublicTitle(&s)) { if (titleKind(&s) == 'show') { pending.showIds.push(s.id) } else { pending.seriesIds.push(s.id) } }
  87. index.release[s.id] = dateOr(&s.release)
  88. }
  89. if ((s.type == 'movie' || s.type == 'series') && isPublicTitle(&s) && s.genres != null && s.genres.length > 0) {
  90. pending.genreTitles.push({ id = s.id movie = s.type == 'movie' release = dateOr(&s.release) genres = s.genres })
  91. }
  92. return null
  93. }
  94. static noteIndexEpisode = (&e) => {
  95. // tracker#31: a merged episode is its keeper episode's twin (same date) — not counted twice
  96. if (e.show != null && e.mergedInto == null && dateOr(&e.release) != '') {
  97. if (e.release <= index.day) {
  98. let l = index.latest[e.show]
  99. if (l == null || e.release > l) { index.latest[e.show] = e.release }
  100. } else {
  101. // mission 037: pushed IN PLACE — `let u = upcoming[x]; u.push(…); upcoming[x] = u` copies the list twice per episode
  102. // (plain data copies on assignment), quadratic in a list's length
  103. if (index.upcoming[e.show] == null) { index.upcoming[e.show] = [] }
  104. index.upcoming[e.show].push(e.release)
  105. }
  106. }
  107. return null
  108. }
  109. static finishIndex = () => {
  110. for (id of pending.seriesIds) { appendEntry(&index.series, &id, seriesKey(&index.latest[id], &index.release[id])) }
  111. for (id of pending.showIds) { appendEntry(&index.shows, &id, seriesKey(&index.latest[id], &index.release[id])) }
  112. byGenre = {}
  113. // mission 037: each genre's list grows IN PLACE. The copy-push-store form (`let list = byGenre[g]` … `byGenre[g] = list`)
  114. // copied the list twice per title — quadratic: with the filmography titles (Drama 25,364 of 59,696 public titles, 2026-10-19)
  115. // the boot spent over 20 minutes here, on the old vendored binary as on the new one, before the first log line
  116. for (t of pending.genreTitles) {
  117. let key = t.movie ? t.release : genreKey(&index.latest[t.id], &t.release)
  118. for (g of t.genres) {
  119. if (byGenre[g] == null) { byGenre[g] = [] }
  120. appendEntry(&byGenre[g], &t.id, &key)
  121. }
  122. index.genresOf[t.id] = t.genres
  123. }
  124. for (g of byGenre.keys()) { index.genres[g] = sortEntries(&byGenre[g]) }
  125. index.movies = sortEntries(&index.movies)
  126. index.series = sortEntries(&index.series)
  127. index.shows = sortEntries(&index.shows)
  128. pending.seriesIds = []
  129. pending.showIds = []
  130. pending.genreTitles = []
  131. return null
  132. }
  133. // a TV list with every key from the current release maps, sorted again
  134. static rekeyed = (&list) => {
  135. out = []
  136. for (let i = 0; i < list.length; i = i + 2) {
  137. appendEntry(&out, &list[i], seriesKey(&index.latest[list[i]], &index.release[list[i]]))
  138. }
  139. return sortEntries(&out)
  140. }
  141. // a new day: upcoming episodes that are out now become their series' newest released one
  142. static rollDay = () => {
  143. // mission T17: not while the start-up scan is still reading the episodes (it classifies them by `index.day`)
  144. if (warm.running && !warm.catalog) { return null }
  145. today = todayStr()
  146. if (index.day == today) { return null }
  147. let moved = false
  148. for (id of index.upcoming.keys()) {
  149. let rest = []
  150. let l = index.latest[id]
  151. for (d of index.upcoming[id]) {
  152. if (d <= today) {
  153. if (l == null || d > l) { l = d }
  154. moved = true
  155. } else { rest.push(d) }
  156. }
  157. index.upcoming[id] = rest
  158. if (l != null) { index.latest[id] = l }
  159. }
  160. if (moved) {
  161. index.series = rekeyed(&index.series)
  162. index.shows = rekeyed(&index.shows)
  163. // the genre lists too: a series' key is its newest released episode (movies keep theirs)
  164. for (g of index.genres.keys()) {
  165. let list = []
  166. for (let i = 0; i < index.genres[g].length; i = i + 2) {
  167. id = index.genres[g][i]
  168. appendEntry(&list, &id, index.release[id] != null ? genreKey(&index.latest[id], &index.release[id]) : index.genres[g][i + 1])
  169. }
  170. index.genres[g] = sortEntries(&list)
  171. }
  172. }
  173. index.day = today
  174. return null
  175. }
  176. // one title out of the lists of the genres it was in, then (when public) into those of its current genres
  177. static refreshGenres = (&show, &shown, key) => {
  178. old = index.genresOf[show.id]
  179. if (old != null) {
  180. for (g of old) { if (index.genres[g] != null) { takeOut(&index.genres[g], &show.id) } }
  181. }
  182. index.genresOf[show.id] = null
  183. if (!shown || show.genres == null || show.genres.length == 0) { return null }
  184. for (g of show.genres) {
  185. if (index.genres[g] == null) { index.genres[g] = [] }
  186. placeIn(&index.genres[g], &show.id, &key, true)
  187. }
  188. index.genresOf[show.id] = show.genres
  189. return null
  190. }
  191. // after the sync (or the adult backfill) touched a show: its entry from its own data again (only that show's
  192. // seasons/episodes are read) — or out of the list when it is not public (adult or unknown, mission 054)
  193. // `isNew` = the title was stored just now (a person fill's filmography): it is in no list yet, so nothing is looked for in them
  194. static refreshCatalogShow = (&showId, isNew) => {
  195. rollDay()
  196. show = showById(&showId)
  197. if (show == null) { return null }
  198. // tracker#30: the lists' slim cache of this title (shows.hl cardOf / episodeRowsOf) from the changed record
  199. refreshTitleCaches(&show)
  200. shown = isPublicTitle(&show)
  201. if (show.type == 'movie') {
  202. if (shown) { placeIn(&index.movies, &show.id, dateOr(&show.release), isNew == true) } else if (isNew != true) { takeOut(&index.movies, &show.id) }
  203. refreshGenres(&show, &shown, dateOr(&show.release))
  204. return null
  205. }
  206. if (show.type != 'series') { return null }
  207. today = index.day
  208. let l = null
  209. up = []
  210. for (s of seasonsOfShow(&show)) {
  211. for (e of episodesOfSeason(&s)) {
  212. if (dateOr(&e.release) != '') {
  213. if (e.release <= today) { if (l == null || e.release > l) { l = e.release } } else { up.push(e.release) }
  214. }
  215. }
  216. }
  217. index.latest[show.id] = l
  218. index.upcoming[show.id] = up
  219. index.release[show.id] = dateOr(&show.release)
  220. // in the list of its kind (#21), out of the other one
  221. key = seriesKey(&l, &index.release[show.id])
  222. isShow = titleKind(&show) == 'show'
  223. if (shown && !isShow) { placeIn(&index.series, &show.id, &key, isNew == true) } else if (isNew != true) { takeOut(&index.series, &show.id) }
  224. if (shown && isShow) { placeIn(&index.shows, &show.id, &key, isNew == true) } else if (isNew != true) { takeOut(&index.shows, &show.id) }
  225. refreshGenres(&show, &shown, genreKey(&l, &index.release[show.id]))
  226. return null
  227. }
  228. // ---- what the pages read ---------------------------------------------------------------------------------------
  229. // one poster tile: poster, title, year, the title's page (/movies/, /series/ or /shows/<slug>, shows.hl titlePath — one
  230. // component for all three, with its Follow button). tracker#30 (mission 025): from the slim cached card (shows.hl cardOf),
  231. // not the whole record with its cast and crew
  232. static tilesOf = (&entries) => {
  233. out = []
  234. for (let i = 0; i < entries.length; i = i + 2) {
  235. c = cardOf(&entries[i])
  236. if (c != null) { out.push({ id = c.id title = c.title year = c.year href = c.href posterUrl = posterUrlOfName(&c.posterName) }) }
  237. }
  238. return out
  239. }
  240. // the homepage: the n latest RELEASED movies (none dated after today) and the n series with the newest released episode
  241. static latestMovies = (&n) => {
  242. rollDay()
  243. want = []
  244. for (let i = 0; i < index.movies.length; i = i + 2) {
  245. if (want.length < n * 2 && index.movies[i + 1] != '' && index.movies[i + 1] <= index.day) { appendEntry(&want, &index.movies[i], &index.movies[i + 1]) }
  246. if (want.length >= n * 2) { return tilesOf(&want) }
  247. }
  248. return tilesOf(&want)
  249. }
  250. static latestSeries = (&n) => { rollDay() return latestTv(&index.series, &n) }
  251. static latestShows = (&n) => { rollDay() return latestTv(&index.shows, &n) }
  252. static latestTv = (&list, &n) => {
  253. want = []
  254. for (let i = 0; i < list.length; i = i + 2) {
  255. if (want.length < n * 2 && list[i + 1].slice(0, 1) == '1') { appendEntry(&want, &list[i], &list[i + 1]) }
  256. if (want.length >= n * 2) { return tilesOf(&want) }
  257. }
  258. return tilesOf(&want)
  259. }
  260. // a page number from the address (`/movies/page/:page`, a String) — anything that is not a whole number ≥ 1 is page 1;
  261. // past the last page is the last page
  262. static pageNumberOf = (&raw, &pages) => {
  263. if (raw == null) { return 1 }
  264. s = '' + raw
  265. if (s == '' || s.length > 6) { return 1 }
  266. let i = 0
  267. while (i < s.length) {
  268. let c = s.charCodeAt(i)
  269. if (c < 48 || c > 57) { return 1 }
  270. i = i + 1
  271. }
  272. p = toNumber(s)
  273. if (p < 1) { return 1 }
  274. return p > pages ? pages : p
  275. }
  276. static pagesOf = (&count) => {
  277. let p = 1
  278. while (p * pageSize < count) { p = p + 1 }
  279. return p
  280. }
  281. // { page, pages, tiles } of one list
  282. static pageOf = (&list, &raw) => {
  283. pages = pagesOf(list.length / 2)
  284. let page = pageNumberOf(&raw, &pages)
  285. return { page = page pages = pages tiles = tilesOf(list.slice((page - 1) * pageSize * 2, page * pageSize * 2)) }
  286. }
  287. static moviesPage = (&raw) => { rollDay() return pageOf(&index.movies, &raw) }
  288. static seriesPage = (&raw) => { rollDay() return pageOf(&index.series, &raw) }
  289. static showsPage = (&raw) => { rollDay() return pageOf(&index.shows, &raw) }
  290. static catalogCounts = () => { return { movies = index.movies.length / 2 series = index.series.length / 2 shows = index.shows.length / 2 } }
  291. // /genres/<genre> (mission 058): one genre's movies and series, newest first, 24 per page; how many there are
  292. static genrePage = (&genreId, &raw) => {
  293. rollDay()
  294. list = index.genres[genreId]
  295. return pageOf(list != null ? list : [], &raw)
  296. }
  297. static genreCount = (&genreId) => {
  298. list = index.genres[genreId]
  299. return list != null ? list.length / 2 : 0
  300. }
  301. // /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
  302. static genreList = () => {
  303. keyed = []
  304. for (g of genresTable.find(null, null)) {
  305. let n = genreCount(&g.id)
  306. if (n > 0 && g.name != null && g.urlSegment != null) { keyed.push({ key = ('' + g.name).toLowerCase() name = '' + g.name href = '/genres/' + g.urlSegment count = n }) }
  307. }
  308. desc = sortDesc(&keyed)
  309. out = []
  310. let i = desc.length - 1
  311. while (i >= 0) { out.push(desc[i]) i = i - 1 }
  312. return out
  313. }

Branches

Latest commits

  • 079f376dMove seven components' styles into Style blocksmre
  • 13dea5belog: #56 live, HM49 and HM58 startedmre
  • a31c8f2fdocs: tracker#36 remaining listmre
  • 975d06d1log: #36 live, DP66 startedmre
  • d6d03fa3mission: T36Bmre
  • e7512d00status: #36 rolled back, retry askedmre
  • c98e2043log: T36A rollbackmre
  • ae905f15Merge t36a: tracker#36 apply rechecks, reportmre
  • 796bfaceT36A: report validated fixes, failed live SSH check and verified rollbackmre
  • 55257852log: #53 live, #56 mergedmre
  • 78b39f1bMerge hm56-rows: tracker#56 list builders insert oncemre
  • 7aea52batmdb fix: protect owners without slugs and record passing gatesmre
  • 9b3c2559tmdb fix: revalidate planned identities and reserve target idsmre
  • 6da612fclog: T36A tool defectmre
  • d56ee0daT36A: report unsafe TMDB apply revalidation and stop before livemre
  • 08e55255log: HM56 done, T36A startedmre
  • 91bcdf0eAvoid per-row list copies and reproduce keyed-row blockersmre
  • 5fd2ac75status: #36 GOmre
  • 0d253b45mission: T36Amre
  • 2aa22d23log: #36 GOmre