gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8e5f2cef8e5f2ceflog: SL1 deployedmre8e5f2cef/lib/franchiseseed.hl

13.6 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; then every TMDB collection timeline whose titles
  22. // are ALL in one curated timeline is hidden (`supersededBy`, franchises.hl seededCoverOf), a partly covered one joins
  23. // that franchise.
  24. // EDITOR CHANGES WIN: a seeded timeline whose order or franchise is no longer what the seed last wrote was edited by the
  25. // creator — it is never written again (`kept`); a franchise or timeline of the same name the creator made is used / left
  26. // alone, never doubled; a collection timeline that has a franchise keeps it. RESUMES after a restart (imports are stored at
  27. // once; the queue = what is still missing); IDEMPOTENT (a second start imports nothing and writes nothing).
  28. import { MPackDB } from 'hl:mpackdb'
  29. import { readFile, exists } from 'hl:fs'
  30. import { now } from 'hl:time'
  31. import { storageDir, envOr, newId } from './util.hl'
  32. import { showById, liveShowOf } from './shows.hl'
  33. import { titleIdByTmdb } from './search.hl'
  34. import { importWithCredits } from './details.hl'
  35. import { ensureFranchises, franchiseById, timelineById, timelinesTable, createFranchise, putTimeline, setTimelineFranchise, franchiseByName, timelineByName, timelineOfCollection, storeSeeded, persistFranchises, seededTimelines, seededCoverIn, setSuperseded } from './franchises.hl'
  36. static franchiseSeedTable = new MPackDB(file = storageDir + '/franchiseseed.db', primaryKey = '@id', indexes = [])
  37. static franchiseFile = envOr('TRACKER_FRANCHISE_FILE', './data/franchises.json')
  38. // the queue is a plain static list filled in place (a static cannot be reassigned — collections.hl)
  39. static importQueue = []
  40. static seedRows = {}
  41. static fseed = { built = false pos = 0 file = false franchises = 0 timelines = 0 titles = 0 have = 0 imported = 0 failed = 0 missing = 0
  42. requests = 0 tvmazeRequests = 0 franchisesCreated = 0 franchisesAdopted = 0 timelinesCreated = 0 updated = 0 unchanged = 0 kept = 0 empty = 0
  43. placed = 0 collections = 0 superseded = 0 unsuperseded = 0 joined = 0 writes = 0 error = '' }
  44. static typeOfKind = (&kind) => { return kind == 'tv' ? 'series' : 'movie' }
  45. static titleKeyOf = (&kind, &tmdbId) => { return kind + ':' + tmdbId }
  46. // our show @id for an entry of the file (a merged tombstone answers its keeper), or null
  47. static ourIdOf = (&entry) => {
  48. id = titleIdByTmdb(typeOfKind(&entry[0]), &entry[1])
  49. s = id != null ? liveShowOf(showById(&id)) : null
  50. return s != null ? s.id : null
  51. }
  52. static sameList = (&a, &b) => {
  53. if (a == null || b == null || a.length != b.length) { return false }
  54. let i = 0
  55. while (i < a.length) {
  56. if (a[i] != b[i]) { return false }
  57. i = i + 1
  58. }
  59. return true
  60. }
  61. static noteRow = (kind, key, &ref, &order, &franchise, created, why) => {
  62. old = seedRows[kind + '|' + key]
  63. rec = { id = old != null ? old.id : newId() kind = kind key = key ref = ref order = order franchise = franchise created = created why = why at = now() }
  64. if (old != null) { franchiseSeedTable.update(rec.id, rec) } else { franchiseSeedTable.put(rec) }
  65. seedRows[kind + '|' + key] = rec
  66. return rec
  67. }
  68. static rowOf = (&kind, &key) => { return seedRows[kind + '|' + key] }
  69. // the file → { franchises: [...] }, or null (logged) when it is not there
  70. static readFranchiseFile = () => {
  71. if (!exists(franchiseFile)) {
  72. fseed.error = franchiseFile + ' not found'
  73. return null
  74. }
  75. d = JSON.parse(readFile(franchiseFile))
  76. if (d == null || d.franchises == null) {
  77. fseed.error = franchiseFile + ': no "franchises" list'
  78. return null
  79. }
  80. return d
  81. }
  82. static fileData = { franchises = [] }
  83. // 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)
  84. static franchiseSeedPrepare = (&imports) => {
  85. if (fseed.built) { return importQueue.length }
  86. fseed.built = true
  87. ensureFranchises()
  88. for (r of franchiseSeedTable.find(null, null)) { seedRows[r.kind + '|' + r.key] = r }
  89. d = readFranchiseFile()
  90. if (d == null) { return 0 }
  91. fseed.file = true
  92. fileData.franchises = d.franchises
  93. queued = {}
  94. for (f of d.franchises) {
  95. fseed.franchises = fseed.franchises + 1
  96. for (t of f.timelines) {
  97. fseed.timelines = fseed.timelines + 1
  98. for (e of t.titles) {
  99. let key = titleKeyOf(&e[0], &e[1])
  100. if (queued[key] != true) {
  101. queued[key] = true
  102. fseed.titles = fseed.titles + 1
  103. if (ourIdOf(&e) != null) { fseed.have = fseed.have + 1 }
  104. else if (rowOf('missing', &key) != null) { fseed.missing = fseed.missing + 1 }
  105. else if (imports) { importQueue.push(e) }
  106. }
  107. }
  108. }
  109. }
  110. return importQueue.length
  111. }
  112. static franchiseSeedStats = () => { return fseed }
  113. // ---- step 1: ONE import → { requests, tvmazeRequests (what the pause counts), error } --------------------------------------
  114. static importOne = (&entry) => {
  115. out = { requests = 1 tvmazeRequests = 0 error = '' done = false }
  116. key = titleKeyOf(&entry[0], &entry[1])
  117. fseed.requests = fseed.requests + 1
  118. r = importWithCredits(&entry[0], entry[1])
  119. if (r.error != null) {
  120. fseed.failed = fseed.failed + 1
  121. out.error = key + ' (' + entry[2] + '): ' + r.error
  122. // TMDB has no such title: remembered, never asked again (a passing failure is tried again on the next start)
  123. if (r.error.includes('HTTP 404')) { noteRow('missing', key, null, null, null, false, r.error) franchiseSeedTable.persist() }
  124. return out
  125. }
  126. if (r.sync != null) {
  127. out.requests = 1 + r.sync.requests
  128. out.tvmazeRequests = r.sync.tvmazeRequests
  129. fseed.requests = fseed.requests + r.sync.requests
  130. fseed.tvmazeRequests = fseed.tvmazeRequests + r.sync.tvmazeRequests
  131. }
  132. if (r.created == true) { fseed.imported = fseed.imported + 1 } else { fseed.have = fseed.have + 1 }
  133. return out
  134. }
  135. // ---- step 2: the build (no TMDB) ---------------------------------------------------------------------------------------------
  136. // 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
  137. static franchiseOf = (&f) => {
  138. row = rowOf('franchise', &f.name)
  139. if (row != null) { return franchiseById(&row.ref) }
  140. mine = franchiseByName(&f.name)
  141. if (mine != null) {
  142. fseed.franchisesAdopted = fseed.franchisesAdopted + 1
  143. noteRow('franchise', f.name, &mine.id, null, null, false, 'the creator\'s own')
  144. return mine
  145. }
  146. c = createFranchise(f.name, f.summary)
  147. if (c.error != null) {
  148. fseed.error = f.name + ': ' + c.error
  149. return null
  150. }
  151. fseed.franchisesCreated = fseed.franchisesCreated + 1
  152. noteRow('franchise', f.name, &c.id, null, null, true, '')
  153. return franchiseById(&c.id)
  154. }
  155. // a TMDB collection's timeline (collection seed) joins the franchise — once, and only when it has none yet
  156. static attachCollection = (&collection, &fid) => {
  157. key = 'collection:' + collection
  158. if (rowOf('collection', &key) != null) { return false }
  159. t = timelineOfCollection(&collection)
  160. if (t == null) { return false }
  161. if (t.franchise != null && t.franchise != '') {
  162. noteRow('collection', key, &t.id, null, null, false, 'has a franchise already')
  163. return false
  164. }
  165. r = setTimelineFranchise(&t.id, fid)
  166. if (r.error != null) {
  167. fseed.error = key + ': ' + r.error
  168. return false
  169. }
  170. fseed.collections = fseed.collections + 1
  171. noteRow('collection', key, &t.id, null, &fid, true, '')
  172. return true
  173. }
  174. // one timeline of the file: made, brought up to date, or left alone (the creator's)
  175. static buildTimeline = (&f, &fid, &tl) => {
  176. key = f.name + '|' + tl.name
  177. order = []
  178. for (e of tl.titles) {
  179. let sid = ourIdOf(&e)
  180. if (sid != null && !order.includes(sid)) { order.push(sid) }
  181. }
  182. fseed.placed = fseed.placed + order.length
  183. row = rowOf('timeline', &key)
  184. if (row == null) {
  185. mine = timelineByName(&tl.name)
  186. if (mine != null) {
  187. // the creator made one of that name: theirs, never written by the seed
  188. fseed.kept = fseed.kept + 1
  189. noteRow('timeline', key, &mine.id, null, null, false, 'the creator\'s own')
  190. return true
  191. }
  192. // none of its titles here yet (no TMDB token, or every import failed): made on a later start
  193. if (order.length == 0) {
  194. fseed.empty = fseed.empty + 1
  195. return true
  196. }
  197. rec = putTimeline(tl.name, &fid, order, 'seed', null, tl.order == 'release' ? 'release' : 'in-universe')
  198. if (rec == null) {
  199. fseed.error = key + ': not stored'
  200. return false
  201. }
  202. fseed.timelinesCreated = fseed.timelinesCreated + 1
  203. fseed.writes = fseed.writes + 1
  204. noteRow('timeline', key, &rec.id, &order, &fid, true, '')
  205. return true
  206. }
  207. t = timelineById(&row.ref)
  208. // gone, or the creator's own, or edited since the seed wrote it (its order or franchise changed): editor changes win
  209. if (t == null || row.order == null || !sameList(&t.order, &row.order) || t.franchise != row.franchise) {
  210. fseed.kept = fseed.kept + 1
  211. return true
  212. }
  213. if (sameList(&t.order, &order) && t.franchise == fid) {
  214. fseed.unchanged = fseed.unchanged + 1
  215. return true
  216. }
  217. r = storeSeeded(t, order, &fid)
  218. if (r.error != null) {
  219. fseed.error = key + ': ' + r.error
  220. return false
  221. }
  222. fseed.updated = fseed.updated + 1
  223. fseed.writes = fseed.writes + 1
  224. noteRow('timeline', key, &t.id, &order, &fid, true, '')
  225. return true
  226. }
  227. // tracker#40: every TMDB collection timeline against the curated ones — all its titles in one → hidden (superseded: no second
  228. // widget on First Contact); the cover gone → shown again; partly covered and without a franchise → joins that franchise
  229. static supersedeCollections = () => {
  230. let seeds = seededTimelines()
  231. for (t of timelinesTable.find(null, null)) {
  232. if (t.tmdbCollection != null && t.order != null) {
  233. let cov = seededCoverIn(&seeds, &t.order)
  234. if (cov.cover != null && t.supersededBy != cov.cover) {
  235. if (setSuperseded(t, &cov.cover).error == null) { fseed.superseded = fseed.superseded + 1 }
  236. } else if (cov.cover == null && t.supersededBy != null) {
  237. if (setSuperseded(t, null).error == null) { fseed.unsuperseded = fseed.unsuperseded + 1 }
  238. } else if (cov.cover == null && cov.franchise != null && (t.franchise == null || t.franchise == '')) {
  239. if (setTimelineFranchise(&t.id, cov.franchise).error == null) { fseed.joined = fseed.joined + 1 }
  240. }
  241. }
  242. }
  243. return true
  244. }
  245. static buildAll = () => {
  246. for (f of fileData.franchises) {
  247. let fr = franchiseOf(&f)
  248. if (fr != null) {
  249. if (f.collections != null) { for (c of f.collections) { attachCollection(&c, &fr.id) } }
  250. for (tl of f.timelines) { buildTimeline(&f, &fr.id, &tl) }
  251. }
  252. }
  253. supersedeCollections()
  254. persistFranchises()
  255. franchiseSeedTable.persist()
  256. return true
  257. }
  258. // ONE step: the next import, or — the queue done — the build → { requests, tvmazeRequests, error, done }
  259. static franchiseSeedStep = () => {
  260. if (!fseed.file) { return { requests = 0 tvmazeRequests = 0 error = fseed.error done = true } }
  261. if (fseed.pos < importQueue.length) {
  262. e = importQueue[fseed.pos]
  263. fseed.pos = fseed.pos + 1
  264. // imported meanwhile (the search, a person page): nothing to ask
  265. if (ourIdOf(&e) != null) {
  266. fseed.have = fseed.have + 1
  267. return { requests = 0 tvmazeRequests = 0 error = '' done = false }
  268. }
  269. return importOne(&e)
  270. }
  271. buildAll()
  272. return { requests = 0 tvmazeRequests = 0 error = fseed.error done = true }
  273. }
  274. static franchiseSeedLine = () => {
  275. s = fseed
  276. return 'franchises=' + s.franchises + ' (created=' + s.franchisesCreated + ' adopted=' + s.franchisesAdopted + ') timelines=' + s.timelines + ' (created=' + s.timelinesCreated
  277. + ' updated=' + s.updated + ' unchanged=' + s.unchanged + ' kept=' + s.kept + ' empty=' + s.empty + ') collections=' + s.collections + ' superseded=' + s.superseded + ' unsuperseded=' + s.unsuperseded + ' joined=' + s.joined + ' titles=' + s.titles + ' had=' + s.have
  278. + ' imported=' + s.imported + ' failed=' + s.failed + ' missing=' + s.missing + ' placed=' + s.placed + ' requests=' + s.requests + ' tvmazeRequests=' + s.tvmazeRequests
  279. }

Branches

Latest commits

  • 8e5f2ceflog: SL1 deployedmre
  • 29f34212log: SL1 deploy startedmre
  • 529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre
  • 8a83316blog: SL1 result, session drain slowermre
  • 35e93e8dSL1: slice the details repair and person fill: catalog lists changed in place, 40 ms fill steps, people created in steps, writes one table per tickmre
  • 72c9119flog+status: SL1 startedmre
  • d74620fcmission SL1: slice repair and person fillmre
  • 5d542d73log+status: ST1 resultmre
  • 3945eb81ST1: stall diagnosis reportmre
  • 404733aclog: ST1 startedmre
  • 3e3d5a55mission ST1: stall diagnosismre
  • 9c3180eblog+status: RV28d measured after deploymre
  • fdf10b1alog: RV28d deployed on day38bmre
  • 320005e9log: RV28d merged, deploy runningmre
  • a8fef27dRV28d: reportmre
  • 2bae8373RV28d: re-vendor Hybriel set day38b (source 1e554d75, bin 5ff2a46d)mre
  • 16b3354aMerge branch 'main' into rv28cmre
  • 6f4f27d8log: RV28d started (day38b)mre
  • e6cf03ffmission RV28d: rv28c onto day38bmre
  • 3ccaee19status: live sweep freeze, waiting for Apollomre