gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7d4b293d7d4b293dtracker#40: "Franchises" in the main menu (desktop header after People, phone sidebar) → /franchises, marked on franchise and timeline pages; gates 365/0, 32/0, 53/0, 24/0, check-theme 0mre7d4b293d/lib/franchiseseed.hl

12.1 KB

  1. // lib/franchiseseed.hl — THE CURATED FRANCHISES (tracker.worldapi.org#40, mission 034). Creator: "you never build timlines and
  2. // franchises". The big franchises (Star Trek, Star Wars, Stargate, MCU, DC …) with their timelines, MOVIES AND SERIES, come from
  3. // a hand-written DATA file in the repo — `data/franchises.json` (TRACKER_FRANCHISE_FILE): per franchise its name, summary,
  4. // the TMDB collections that join it, and its timelines, each with its order ("in-universe" | "release") and its titles as
  5. // [kind ('tv' | 'movie'), TMDB id, "title (year)" — for people only].
  6. //
  7. // franchiseSeedTable pk @id { kind, key, ref, order, franchise, created, why, at } — what the seed made, one row per thing:
  8. // kind 'franchise' key = its name ref = the franchise @id created (true: the seed made it; false: the
  9. // creator's own of that name, adopted)
  10. // kind 'timeline' key = franchise|timeline ref = the timeline @id order + franchise = what the seed LAST WROTE
  11. // (order null: the creator's own of that name)
  12. // kind 'collection' key = collection:<id> ref = the timeline @id franchise (null: it had one already, left alone)
  13. // kind 'missing' key = tv:<id> / movie:<id> why TMDB has no such title (404) — not asked again
  14. // storage/mpackdb/franchiseseed.db
  15. //
  16. // THE JOB (jobs.hl `franchiseSeedTick`, after every other start job — the collection seed last — one step per tick):
  17. // 1. the titles of the file we don't have (search.hl titleIdByTmdb) are IMPORTED, one per step, through the search's own
  18. // import (details.hl importWithCredits: details, seasons + episodes, poster, ids, TVmaze, cast + crew — complete), then
  19. // the pause the daily sync makes after the same requests (tmdb.hl pauseMsAfter). Without TMDB_READ_TOKEN: no imports.
  20. // 2. then ONE build step (no TMDB): each franchise found or made, its collections' timelines attached, each timeline made
  21. // or brought up to date with the titles we have, in the file's order.
  22. // EDITOR CHANGES WIN: a seeded timeline whose order or franchise is no longer what the seed last wrote was edited by the
  23. // creator — it is never written again (`kept`); a franchise or timeline of the same name the creator made is used / left
  24. // alone, never doubled; a collection timeline that has a franchise keeps it. RESUMES after a restart (imports are stored at
  25. // once; the queue = what is still missing); IDEMPOTENT (a second start imports nothing and writes nothing).
  26. import { MPackDB } from 'hl:mpackdb'
  27. import { readFile, exists } from 'hl:fs'
  28. import { now } from 'hl:time'
  29. import { storageDir, envOr, newId } from './util.hl'
  30. import { showById, liveShowOf } from './shows.hl'
  31. import { titleIdByTmdb } from './search.hl'
  32. import { importWithCredits } from './details.hl'
  33. import { ensureFranchises, franchiseById, timelineById, createFranchise, putTimeline, setTimelineFranchise, franchiseByName, timelineByName, timelineOfCollection, storeSeeded, persistFranchises } from './franchises.hl'
  34. static franchiseSeedTable = new MPackDB(file = storageDir + '/franchiseseed.db', primaryKey = '@id', indexes = [])
  35. static franchiseFile = envOr('TRACKER_FRANCHISE_FILE', './data/franchises.json')
  36. // the queue is a plain static list filled in place (a static cannot be reassigned — collections.hl)
  37. static importQueue = []
  38. static seedRows = {}
  39. static fseed = { built = false pos = 0 file = false franchises = 0 timelines = 0 titles = 0 have = 0 imported = 0 failed = 0 missing = 0
  40. requests = 0 tvmazeRequests = 0 franchisesCreated = 0 franchisesAdopted = 0 timelinesCreated = 0 updated = 0 unchanged = 0 kept = 0 empty = 0
  41. placed = 0 collections = 0 writes = 0 error = '' }
  42. static typeOfKind = (&kind) => { return kind == 'tv' ? 'series' : 'movie' }
  43. static titleKeyOf = (&kind, &tmdbId) => { return kind + ':' + tmdbId }
  44. // our show @id for an entry of the file (a merged tombstone answers its keeper), or null
  45. static ourIdOf = (&entry) => {
  46. id = titleIdByTmdb(typeOfKind(entry[0]), entry[1])
  47. s = id != null ? liveShowOf(showById(id)) : null
  48. return s != null ? s.id : null
  49. }
  50. static sameList = (&a, &b) => {
  51. if (a == null || b == null || a.length != b.length) { return false }
  52. let i = 0
  53. while (i < a.length) {
  54. if (a[i] != b[i]) { return false }
  55. i = i + 1
  56. }
  57. return true
  58. }
  59. static noteRow = (kind, key, &ref, &order, &franchise, created, why) => {
  60. old = seedRows[kind + '|' + key]
  61. rec = { id = old != null ? old.id : newId() kind = kind key = key ref = ref order = order franchise = franchise created = created why = why at = now() }
  62. if (old != null) { franchiseSeedTable.update(rec.id, rec) } else { franchiseSeedTable.put(rec) }
  63. seedRows[kind + '|' + key] = rec
  64. return rec
  65. }
  66. static rowOf = (&kind, &key) => { return seedRows[kind + '|' + key] }
  67. // the file → { franchises: [...] }, or null (logged) when it is not there
  68. static readFranchiseFile = () => {
  69. if (!exists(franchiseFile)) {
  70. fseed.error = franchiseFile + ' not found'
  71. return null
  72. }
  73. d = JSON.parse(readFile(franchiseFile))
  74. if (d == null || d.franchises == null) {
  75. fseed.error = franchiseFile + ': no "franchises" list'
  76. return null
  77. }
  78. return d
  79. }
  80. static fileData = { franchises = [] }
  81. // at the job's start: the file read, the seed's rows loaded, the queue = the file's titles we don't have (`imports` false: none)
  82. static franchiseSeedPrepare = (&imports) => {
  83. if (fseed.built) { return importQueue.length }
  84. fseed.built = true
  85. ensureFranchises()
  86. for (r of franchiseSeedTable.find(null, null)) { seedRows[r.kind + '|' + r.key] = r }
  87. d = readFranchiseFile()
  88. if (d == null) { return 0 }
  89. fseed.file = true
  90. fileData.franchises = d.franchises
  91. queued = {}
  92. for (f of d.franchises) {
  93. fseed.franchises = fseed.franchises + 1
  94. for (t of f.timelines) {
  95. fseed.timelines = fseed.timelines + 1
  96. for (e of t.titles) {
  97. let key = titleKeyOf(e[0], e[1])
  98. if (queued[key] != true) {
  99. queued[key] = true
  100. fseed.titles = fseed.titles + 1
  101. if (ourIdOf(e) != null) { fseed.have = fseed.have + 1 }
  102. else if (rowOf('missing', key) != null) { fseed.missing = fseed.missing + 1 }
  103. else if (imports) { importQueue.push(e) }
  104. }
  105. }
  106. }
  107. }
  108. return importQueue.length
  109. }
  110. static franchiseSeedStats = () => { return fseed }
  111. // ---- step 1: ONE import → { requests, tvmazeRequests (what the pause counts), error } --------------------------------------
  112. static importOne = (&entry) => {
  113. out = { requests = 1 tvmazeRequests = 0 error = '' done = false }
  114. key = titleKeyOf(entry[0], entry[1])
  115. fseed.requests = fseed.requests + 1
  116. r = importWithCredits(entry[0], entry[1])
  117. if (r.error != null) {
  118. fseed.failed = fseed.failed + 1
  119. out.error = key + ' (' + entry[2] + '): ' + r.error
  120. // TMDB has no such title: remembered, never asked again (a passing failure is tried again on the next start)
  121. if (r.error.includes('HTTP 404')) { noteRow('missing', key, null, null, null, false, r.error) franchiseSeedTable.persist() }
  122. return out
  123. }
  124. if (r.sync != null) {
  125. out.requests = 1 + r.sync.requests
  126. out.tvmazeRequests = r.sync.tvmazeRequests
  127. fseed.requests = fseed.requests + r.sync.requests
  128. fseed.tvmazeRequests = fseed.tvmazeRequests + r.sync.tvmazeRequests
  129. }
  130. if (r.created == true) { fseed.imported = fseed.imported + 1 } else { fseed.have = fseed.have + 1 }
  131. return out
  132. }
  133. // ---- step 2: the build (no TMDB) ---------------------------------------------------------------------------------------------
  134. // the franchise of the file's entry `f`: the one the seed made / adopted before, else the creator's of that name, else a new one
  135. static franchiseOf = (&f) => {
  136. row = rowOf('franchise', f.name)
  137. if (row != null) { return franchiseById(row.ref) }
  138. mine = franchiseByName(f.name)
  139. if (mine != null) {
  140. fseed.franchisesAdopted = fseed.franchisesAdopted + 1
  141. noteRow('franchise', f.name, mine.id, null, null, false, 'the creator\'s own')
  142. return mine
  143. }
  144. c = createFranchise(f.name, f.summary)
  145. if (c.error != null) {
  146. fseed.error = f.name + ': ' + c.error
  147. return null
  148. }
  149. fseed.franchisesCreated = fseed.franchisesCreated + 1
  150. noteRow('franchise', f.name, c.id, null, null, true, '')
  151. return franchiseById(c.id)
  152. }
  153. // a TMDB collection's timeline (collection seed) joins the franchise — once, and only when it has none yet
  154. static attachCollection = (&collection, &fid) => {
  155. key = 'collection:' + collection
  156. if (rowOf('collection', key) != null) { return false }
  157. t = timelineOfCollection(collection)
  158. if (t == null) { return false }
  159. if (t.franchise != null && t.franchise != '') {
  160. noteRow('collection', key, t.id, null, null, false, 'has a franchise already')
  161. return false
  162. }
  163. r = setTimelineFranchise(t.id, fid)
  164. if (r.error != null) {
  165. fseed.error = key + ': ' + r.error
  166. return false
  167. }
  168. fseed.collections = fseed.collections + 1
  169. noteRow('collection', key, t.id, null, fid, true, '')
  170. return true
  171. }
  172. // one timeline of the file: made, brought up to date, or left alone (the creator's)
  173. static buildTimeline = (&f, &fid, &tl) => {
  174. key = f.name + '|' + tl.name
  175. order = []
  176. for (e of tl.titles) {
  177. let sid = ourIdOf(e)
  178. if (sid != null && !order.includes(sid)) { order.push(sid) }
  179. }
  180. fseed.placed = fseed.placed + order.length
  181. row = rowOf('timeline', key)
  182. if (row == null) {
  183. mine = timelineByName(tl.name)
  184. if (mine != null) {
  185. // the creator made one of that name: theirs, never written by the seed
  186. fseed.kept = fseed.kept + 1
  187. noteRow('timeline', key, mine.id, null, null, false, 'the creator\'s own')
  188. return true
  189. }
  190. // none of its titles here yet (no TMDB token, or every import failed): made on a later start
  191. if (order.length == 0) {
  192. fseed.empty = fseed.empty + 1
  193. return true
  194. }
  195. rec = putTimeline(tl.name, fid, order, 'seed', null, tl.order == 'release' ? 'release' : 'in-universe')
  196. if (rec == null) {
  197. fseed.error = key + ': not stored'
  198. return false
  199. }
  200. fseed.timelinesCreated = fseed.timelinesCreated + 1
  201. fseed.writes = fseed.writes + 1
  202. noteRow('timeline', key, rec.id, order, fid, true, '')
  203. return true
  204. }
  205. t = timelineById(row.ref)
  206. // gone, or the creator's own, or edited since the seed wrote it (its order or franchise changed): editor changes win
  207. if (t == null || row.order == null || !sameList(t.order, row.order) || t.franchise != row.franchise) {
  208. fseed.kept = fseed.kept + 1
  209. return true
  210. }
  211. if (sameList(t.order, order) && t.franchise == fid) {
  212. fseed.unchanged = fseed.unchanged + 1
  213. return true
  214. }
  215. r = storeSeeded(t, order, fid)
  216. if (r.error != null) {
  217. fseed.error = key + ': ' + r.error
  218. return false
  219. }
  220. fseed.updated = fseed.updated + 1
  221. fseed.writes = fseed.writes + 1
  222. noteRow('timeline', key, t.id, order, fid, true, '')
  223. return true
  224. }
  225. static buildAll = () => {
  226. for (f of fileData.franchises) {
  227. let fr = franchiseOf(f)
  228. if (fr != null) {
  229. if (f.collections != null) { for (c of f.collections) { attachCollection(c, fr.id) } }
  230. for (tl of f.timelines) { buildTimeline(f, fr.id, tl) }
  231. }
  232. }
  233. persistFranchises()
  234. franchiseSeedTable.persist()
  235. return true
  236. }
  237. // ONE step: the next import, or — the queue done — the build → { requests, tvmazeRequests, error, done }
  238. static franchiseSeedStep = () => {
  239. if (!fseed.file) { return { requests = 0 tvmazeRequests = 0 error = fseed.error done = true } }
  240. if (fseed.pos < importQueue.length) {
  241. e = importQueue[fseed.pos]
  242. fseed.pos = fseed.pos + 1
  243. // imported meanwhile (the search, a person page): nothing to ask
  244. if (ourIdOf(e) != null) {
  245. fseed.have = fseed.have + 1
  246. return { requests = 0 tvmazeRequests = 0 error = '' done = false }
  247. }
  248. return importOne(e)
  249. }
  250. buildAll()
  251. return { requests = 0 tvmazeRequests = 0 error = fseed.error done = true }
  252. }
  253. static franchiseSeedLine = () => {
  254. s = fseed
  255. return 'franchises=' + s.franchises + ' (created=' + s.franchisesCreated + ' adopted=' + s.franchisesAdopted + ') timelines=' + s.timelines + ' (created=' + s.timelinesCreated
  256. + ' updated=' + s.updated + ' unchanged=' + s.unchanged + ' kept=' + s.kept + ' empty=' + s.empty + ') collections=' + s.collections + ' titles=' + s.titles + ' had=' + s.have
  257. + ' imported=' + s.imported + ' failed=' + s.failed + ' missing=' + s.missing + ' placed=' + s.placed + ' requests=' + s.requests + ' tvmazeRequests=' + s.tvmazeRequests
  258. }

Branches

Latest commits

  • 7d4b293dtracker#40: "Franchises" in the main menu (desktop header after People, phone sidebar) → /franchises, marked on franchise and timeline pages; gates 365/0, 32/0, 53/0, 24/0, check-theme 0mre
  • 93dfb0batracker#40 (mission 034): the curated franchises — data/franchises.json (17 franchises, 31 timelines, 285 TMDB titles, movies + series, in-universe/release order, 12 TMDB collections attached); lib/franchiseseed.hl + jobs.hl franchiseSeedTick (last start job, imports missing titles via details.hl importWithCredits = the search's Add, one per step paced, then one build; franchiseseed.db: editor changes win, the creator's same-name franchise adopted / timeline left alone, 404 remembered, resumable, idempotent); timeline heads 'N titles · in-universe order' (orderKind) and wrap on a phone; series pages show the widget; new gate tests/franchiseseed.mjs (5th in deploy.sh), the others run with TRACKER_FRANCHISE_SEED=0; gates 365/0, 32/0, 52/0, 24/0, check-theme 0; real copy 196 imported, 0 failed, 7 min, restart unchanged=31mre
  • 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
  • dc1d4be4tracker mission 027: Hybriel master 06617221 vendored (plugin allocator fixes 3a781359 + 413f60e4); real copy RSS through first-start jobs + 400 loads flat ~2.55 GB (190aa11d 2.3 -> 5.6 GB), page times <= 1.1x; gates 342/0, 32/0, 52/0mre