gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit29a26da929a26da9|||PR AP56e: guard hidden show helpers during session syncTracker29a26da9/lib/catalog.hl

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

Branches

Latest commits

  • 29a26da9|||PR AP56e: guard hidden show helpers during session syncTracker
  • 7c9ec236|||PR AP56e: re-vendor Hybriel set day56 (source 36ae83a8, bin c1f595a2)Tracker
  • 4b54f6f9Merge branch 'records-audit' into mainTracker
  • cab4a3bc|||PR records: branch audit, PR-onlyTracker
  • 641e1c4cMerge branch 'records-dp71' into mainTracker
  • 85a6e9f5|||PR records: DP71 checked in ChromeTracker
  • f5be5f18records: gitoria PR-only planTracker
  • 46a37036records: DP71 liveTracker
  • 3bcb7039Status/log: DP71 in progressTracker
  • 5b3a5693Log: SR1b merged via PR 2, DP71 startedTracker
  • 783957c2Merge branch 'sr1-search' into mainTracker
  • 792b9652README: search results come from the routeTracker
  • fd8570b1|||PR search: one server exchange per keystrokeTracker
  • e8f5cbcaLog: SR1 result, SR1bTracker
  • 8ad275a4Log/status: tracker 68, SR1Tracker
  • f5a06a0fLog: gitoria push setup, SR1Tracker
  • d6b109baMission SR1mre
  • 608b7558Log: FF1 merged via PR #1mre
  • 2939d94cMerge branch 'ff1-gate-failfast' into mainTracker
  • c934361a|||PR tests: gates fail fast when the app exits during startmre