gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit31dcf2ad31dcf2admission 045: report, LOGmre31dcf2ad/lib/catalog.hl

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

Branches

Latest commits

  • 31dcf2admission 045: report, LOGmre
  • 9c3971d0tests/persons-skip-count.hlmre
  • 4d72e365mission 045mre
  • b9cae4c9slices: skip and count non-row values from range(); merge persons pass falls back to iterate; guard testmre
  • 3db7dc31merge w44-jobs-slices into w43-trkmemmre
  • 593dab6fmission 043: report, LOGmre
  • 1a5a4818boot scans back to iterate (slices measured slower: titles 114 s, episodes 141 s, persons failed); lambda per rowmre
  • 03d29893boot scans read primary-key slices (lib/slices.hl from mission 044): iterate is eager too; people view log line back; allShows/allEpisodes stay for toolsmre
  • 23375c58mission 044: report, LOG, STATUSmre
  • c6fd928emission 044: 4096 slices for tables over 200 K rows, 15 ms budget, TRACKER_SLICE_DEPTH for testsmre
  • fb057680mission 044: jobs.hl imports mergeLeft (gate found it), mission file, slices benchmre
  • 0d6293ebmission 043: mission filemre
  • d25bd14amission 044: sliced merge prepare + sliced collections prepare; jobs-dump old/new check + bulk fixturemre
  • 633ee210mission 044: sliced table scans (slices.hl, pk-range slices) for the adult/kind/status/art/credits/details-repair queuesmre
  • 55bbd619boot: one streamed pass per table (lib/bootscan.hl) instead of find(null,null) per module; follows/watches/franchises scans streammre
  • 1d465d3ere-vendor plugins to Hybriel master f9614007 (mpackdb #202 copy fix, libmpackdb sha 6fb3b9d6)mre
  • 6648ba44mission 042: real-browser E2E on ce6f2e0a re-vendor 69/0 at 390 + 1280mre
  • 3ed3a0efmission 042: gates old/new equal (browser #41 route-table regex accepts unescaped hl:web output), real-data TTFB, E2E blocked by lisbeth lockmre
  • d5b61e90LOG 042mre
  • 4acdbaccmission 042: memory comparison old vs ce6f2e0a (stopped: warm VmHWM higher), reportmre