gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit593a6350593a6350boot: resumable start-up scan, the server listens first (T17)mre593a6350/lib/people.hl

30.2 KB

  1. // lib/people.hl — THE PERSON PAGE's data (tracker.worldapi.org#16, components/person.hl `/person/<slug>`): a person by slug,
  2. // their facts and filmography, and the ONE-TIME fetch of their whole filmography from TMDB on the first visit. Creator:
  3. // "the movies come from artist scrolls. when one clicks on an artist that has no dataset yet the old version fetched that
  4. // artist and its entire bibilogrqphy so its profile was complete".
  5. //
  6. // personBySlug(slug) the person (slug = `urlSegment`) — a slug → id map built once (15k people, one scan)
  7. // personInfoOf(p) name, photo URL, born/died lines, bio
  8. // filmographyOf(p) their credits (`p.shows`): poster, title, year, role, kind — newest first, adult titles hidden
  9. // personFillStep(id) ONE step of the fetch, called again and again by the page until it answers `done`:
  10. // 1st call: GET <TMDB>/person/<tmdbId>?append_to_response=combined_credits (ONE request: details +
  11. // every cast/crew credit) + the photo (w185, like the posters); the person's facts are updated;
  12. // every next call stores the next `fillBatch` titles; the last one links the credits and marks
  13. // the person complete (`filmographyAt`, ms). hl:web serves one request at a time — between two
  14. // steps every other request is served, so a person with 300 credits never holds the server.
  15. // peoplePage(page) /people (tracker#37): everyone, the one updated last first, 24 per page — a view built once at boot
  16. // personPhotoOf(p) the photo of a /people tile and a search row: our file, else TMDB's (`tmdbProfile`), else the placeholder
  17. //
  18. // COMPLETE = `filmographyAt` set: never fetched again (the daily sync may refresh followed people later — not built).
  19. // Migrated people are NOT complete (153 of them carry the old tracker's credits — kept, the fetch adds the rest).
  20. // A title we don't have → a MINIMAL show record (title, year, type, tmdbId, `adult`, overview, genres by TMDB id, language;
  21. // no seasons, no poster file; `minimal = true` — details.hl completes it when it is opened or by the repair job, tracker#26), shaped like search.hl importTitle's
  22. // (`oldId tmdb-<tv|movie>-<id>`, a free slug), indexed for the search and put into the public lists (catalog.hl).
  23. // The person's credits (`shows`, the migrated field): [{ show, character, posterPath, adult }] — `character` is the role
  24. // text (the characters, then crew jobs like "Director", " / " between them); `posterPath` = TMDB's, so the page can show
  25. // TMDB's small poster (w92, like search's web rows) until we have the file. ADULT (TMDB's flag, mission 054): a row is
  26. // shown only for a PUBLIC title (shows.hl `isPublicTitle`: `adult == false`, unknown hidden like the public lists) whose
  27. // credit is not adult either (mission 056 merge).
  28. // ONE PROCESS PER STORAGE (hybriel#21); new rows get self-assigned 16-hex ids (hybriel#113), like sync.hl.
  29. import { now } from 'hl:time'
  30. import { warm, warmBlocked } from './warm.hl'
  31. import { fetch } from 'hl:fetch'
  32. import { writeFile, mkDir, exists } from 'hl:fs'
  33. import { profilesDir, newId, textOr, listOr, sortDesc } from './util.hl'
  34. import { personsTable, showsTable, genresTable, showById, showYear, posterUrlOf, posterUrlOfName, creditRowLive, isPublicTitle, titlePath, typeWordOf, typeClassOf, liveShowOf } from './shows.hl'
  35. import { tmdbGet, tmdbImageBase } from './tmdb.hl'
  36. import { titleIdByTmdb, indexShow, slugOf, indexPerson } from './search.hl'
  37. import { slugBaseOf } from './search-helpers.hl'
  38. import { refreshCatalogShow, pagesOf, pageNumberOf, pageSize } from './catalog.hl'
  39. import { claimShortId } from './shortids.hl'
  40. import { sliceBudgetMs } from './slices.hl'
  41. // titles stored per step (real data: see STATUS.md ticket #16 for the per-step time)
  42. static fillBatch = 40
  43. // ---- the person -------------------------------------------------------------------------------------------------
  44. static slugIndex = { built = false ids = {} }
  45. // mission 043: one person into the slug map IN PLACE (bootscan.hl's person pass, or the lazy scan below — streamed)
  46. static noteSlugOfPerson = (&p) => {
  47. if (p.urlSegment != null) { slugIndex.ids[p.urlSegment] = p.id }
  48. return null
  49. }
  50. static ensureSlugIndex = () => {
  51. if (!slugIndex.built && !warm.running) {
  52. slugIndex.built = true
  53. for (p of personsTable.iterate(null, null)) { noteSlugOfPerson(p) }
  54. }
  55. return true
  56. }
  57. static personBySlug = (&slug) => {
  58. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  59. ensureSlugIndex()
  60. id = slugIndex.ids[slug]
  61. return id == null ? null : personsTable.fetch(id)
  62. }
  63. static isComplete = (&p) => { return p.filmographyAt != null }
  64. // the page asks TMDB for this person: not complete yet, and TMDB knows them
  65. static needsFill = (&p) => { return !isComplete(p) && p.tmdbId != null }
  66. // the photo: /profiles/<oldId>.<ext> (images.hl) — only when the file is there (the migrated `image` = 'jpg'
  67. // of 129 people never came with a file)
  68. static profileName = (&p) => {
  69. if (p.oldId == null || p.image == null || p.image == '') { return null }
  70. return p.oldId + '.' + p.image
  71. }
  72. static photoUrlOf = (&p) => {
  73. name = profileName(p)
  74. return name != null && exists(profilesDir + '/' + name) ? '/profiles/' + name : ''
  75. }
  76. static monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  77. // '1986-03-04' → '4 March 1986'; a bare year or anything else as it is
  78. static dateText = (&d) => {
  79. t = textOr(d)
  80. if (t == null) { return null }
  81. if (t.length < 10) { return t }
  82. m = toNumber(t.slice(5, 7))
  83. day = toNumber(t.slice(8, 10))
  84. if (m == null || day == null || m < 1 || m > 12) { return t }
  85. return day + ' ' + monthNames[m - 1] + ' ' + t.slice(0, 4)
  86. }
  87. // { name, photoUrl, born, died, bio } — every field a plain string ('' = none)
  88. static personInfoOf = (&p) => {
  89. born = dateText(p.birthDate)
  90. place = textOr(p.birthPlace)
  91. let bornText = ''
  92. if (born != null && place != null) { bornText = 'Born ' + born + ' in ' + place } else if (born != null) { bornText = 'Born ' + born } else if (place != null) { bornText = 'Born in ' + place }
  93. died = dateText(p.deathDate)
  94. return { name = p.name != null ? p.name : '' photoUrl = photoUrlOf(p) born = bornText died = died != null ? 'Died ' + died : ''
  95. bio = textOr(p.biography) != null ? p.biography : '' }
  96. }
  97. // ---- the filmography --------------------------------------------------------------------------------------------
  98. // the credits, one row per title (a title twice — the migrated credit and TMDB's — is ONE row, roles merged), newest
  99. // first by the title's release ('YYYY-MM-DD', else its year), undated last; only public titles (no adult/unknown)
  100. static filmographyOf = (&p) => {
  101. byShow = {}
  102. order = []
  103. for (c of listOr(p.shows)) {
  104. if (c != null && c.show != null) {
  105. let role = textOr(c.character)
  106. let prev = byShow[c.show]
  107. if (prev == null) {
  108. byShow[c.show] = { role = role != null ? role : '' posterPath = textOr(c.posterPath) adult = c.adult == true }
  109. order.push(c.show)
  110. } else {
  111. let x = prev
  112. if (role != null && !(' / ' + x.role + ' / ').includes(' / ' + role + ' / ')) { x.role = x.role == '' ? role : x.role + ' / ' + role }
  113. if (x.posterPath == null) { x.posterPath = textOr(c.posterPath) }
  114. if (c.adult == true) { x.adult = true }
  115. byShow[c.show] = x
  116. }
  117. }
  118. }
  119. list = []
  120. listed = {}
  121. for (sid of order) {
  122. let c = byShow[sid]
  123. // tracker#31: a credit on a merged tombstone is the keeper's (merge.hl repoints them; a keeper listed once)
  124. let s = creditRowLive(sid)
  125. if (s != null && s.public && !c.adult && listed[s.id] != true) {
  126. listed[s.id] = true
  127. let release = s.release
  128. let key = release != null && release.length >= 4 ? release : s.year
  129. let poster = posterUrlOfName(s.posterName)
  130. if (poster == '/posters/none' && c.posterPath != null) { poster = tmdbImageBase + '/w92' + c.posterPath }
  131. list.push({ key = key id = s.id title = s.title year = s.year href = s.href posterUrl = poster
  132. role = c.role typeLabel = s.typeLabel typeClass = s.typeClass })
  133. }
  134. }
  135. out = []
  136. undated = []
  137. for (r of sortDesc(list)) { if (r.key == '') { undated.push(r) } else { out.push(r) } }
  138. for (r of undated) { out.push(r) }
  139. return out
  140. }
  141. // ---- the fetch ------------------------------------------------------------------------------------------------
  142. // person id → { items, pos, credits, added, requests, started, steps } while a fetch is under way (in memory: a restart
  143. // loses it — the titles stored so far are found again by TMDB id, nothing is duplicated)
  144. static jobs = {}
  145. // TMDB genre id → ours (our genres carry TMDB's id)
  146. static genreMap = { built = false ids = {} }
  147. static genreIdsOfTmdb = (&tmdbIds) => {
  148. if (!genreMap.built) {
  149. m = {}
  150. for (g of genresTable.find(null, null)) { if (g.tmdbId != null) { m['' + g.tmdbId] = g.id } }
  151. genreMap.ids = m
  152. genreMap.built = true
  153. }
  154. out = []
  155. for (t of listOr(tmdbIds)) {
  156. let id = genreMap.ids['' + t]
  157. if (id != null && !out.includes(id)) { out.push(id) }
  158. }
  159. return out
  160. }
  161. // TMDB's combined_credits → one item per title (cast AND crew of the same title merged), TV + movies only
  162. static creditItems = (&cc) => {
  163. byKey = {}
  164. order = []
  165. all = []
  166. if (cc != null) {
  167. for (c of listOr(cc.cast)) { all.push({ credit = c role = textOr(c.character) }) }
  168. for (c of listOr(cc.crew)) { all.push({ credit = c role = textOr(c.job) }) }
  169. }
  170. for (a of all) {
  171. let c = a.credit
  172. let kind = c.media_type
  173. if ((kind == 'tv' || kind == 'movie') && c.id != null && hlTypeName(c.id) == 'Number') {
  174. let k = kind + ':' + c.id
  175. let known = byKey[k]
  176. if (known == null) {
  177. let name = kind == 'tv' ? textOr(c.name) : textOr(c.title)
  178. if (name != null) {
  179. byKey[k] = { kind = kind tmdbId = c.id title = name release = textOr(kind == 'tv' ? c.first_air_date : c.release_date)
  180. overview = textOr(c.overview) language = textOr(c.original_language) genreIds = listOr(c.genre_ids)
  181. posterPath = textOr(c.poster_path) adult = c.adult == true role = a.role != null ? a.role : '' }
  182. order.push(k)
  183. }
  184. } else if (a.role != null && !(' / ' + known.role + ' / ').includes(' / ' + a.role + ' / ')) {
  185. let x = known
  186. x.role = x.role == '' ? a.role : x.role + ' / ' + a.role
  187. byKey[k] = x
  188. }
  189. }
  190. }
  191. out = []
  192. for (k of order) { out.push(byKey[k]) }
  193. return out
  194. }
  195. // the photo: w185 → storage/mpackdb/profiles/<oldId>.<ext> — { requests, image, tmdbProfile, failed }
  196. static syncProfile = (&p, profilePath) => {
  197. out = { requests = 0 image = p.image tmdbProfile = p.tmdbProfile failed = null }
  198. if (textOr(profilePath) == null || p.oldId == null) { return out }
  199. parts = profilePath.split('.')
  200. ext = parts[parts.length - 1].toLowerCase()
  201. if (ext != 'jpg' && ext != 'jpeg' && ext != 'png' && ext != 'webp') { return out }
  202. file = profilesDir + '/' + p.oldId + '.' + ext
  203. if (exists(file) && p.tmdbProfile == profilePath && p.image == ext) { return out }
  204. out.requests = 1
  205. r = fetch(tmdbImageBase + '/w185' + profilePath, { headers = { 'user-agent' = 'tracker.worldapi.org (person page)' } timeoutMs = 20000 })
  206. if (r == null || r.status != 200 || r.body == null || r.body.length == 0) {
  207. out.failed = 'photo ' + profilePath + ': ' + (r == null ? 'no answer' : 'HTTP ' + r.status)
  208. return out
  209. }
  210. mkDir(profilesDir)
  211. writeFile(file, toBytes(r.body), 420)
  212. out.image = ext
  213. out.tmdbProfile = profilePath
  214. return out
  215. }
  216. // step 1: TMDB's person + credits; the facts (a TMDB value that is empty never overwrites ours) and the photo
  217. static startFill = (p) => {
  218. t0 = now()
  219. d = tmdbGet('/person/' + p.tmdbId + '?append_to_response=combined_credits')
  220. if (d.failed != null) { return { error = 'TMDB: ' + d.failed } }
  221. items = creditItems(d.combined_credits)
  222. photo = syncProfile(p, d.profile_path)
  223. x = p
  224. if (textOr(d.biography) != null) { x.biography = d.biography }
  225. if (textOr(d.birthday) != null) { x.birthDate = d.birthday }
  226. if (textOr(d.deathday) != null) { x.deathDate = d.deathday }
  227. if (textOr(d.place_of_birth) != null) { x.birthPlace = d.place_of_birth }
  228. if (textOr(x.imdbId) == null && textOr(d.imdb_id) != null) { x.imdbId = d.imdb_id }
  229. if (d.adult != null) { x.adult = d.adult == true }
  230. x.image = photo.image
  231. x.tmdbProfile = photo.tmdbProfile
  232. x.updatedAt = now()
  233. if (personsTable.update(x.id, x) == null) { return { error = 'could not store the person: ' + personsTable.lastError() } }
  234. personTouched(x.id)
  235. if (photo.failed != null) { console.log('person fill: ' + p.name + ': ' + photo.failed) }
  236. jobs[p.id] = { items = items pos = 0 credits = [] added = 0 requests = 1 + photo.requests started = t0 steps = 1 tmdbMs = now() - t0 }
  237. return { done = false pos = 0 total = items.length }
  238. }
  239. // a title we don't have: the minimal record (shape: search.hl importTitle) → its id, or null
  240. static addMinimalTitle = (&cr, &pid, pname) => {
  241. kindType = cr.kind == 'tv' ? 'series' : 'movie'
  242. rel = cr.release
  243. yr = rel != null && rel.length >= 4 ? toNumber(rel.slice(0, 4)) : null
  244. slug = slugOf(cr.title, cr.tmdbId)
  245. record = {
  246. id = newId() title = cr.title summary = null tmdbSummary = cr.overview tmdbId = cr.tmdbId
  247. language = cr.language type = kindType release = rel year = yr image = null
  248. urlSegment = slug episodesCount = null homepage = null imdbId = null seasonsCount = null
  249. status = null tagline = null tvdbId = null tvmzId = null genres = genreIdsOfTmdb(cr.genreIds)
  250. cast = [{ person = pid character = cr.role actor = pname }] seasons = [] oldId = 'tmdb-' + cr.kind + '-' + cr.tmdbId
  251. adult = cr.adult imported = now() minimal = true
  252. }
  253. // mission 060: its short id (shortids.hl — unique across shows and persons)
  254. record.shortId = claimShortId('show', record.id, record.oldId)
  255. if (showsTable.put(record) == null) {
  256. console.log('person fill: "' + cr.title + '" not stored: ' + showsTable.lastError())
  257. return null
  258. }
  259. indexShow(record)
  260. refreshCatalogShow(record.id)
  261. return record.id
  262. }
  263. // every next step: the next `fillBatch` titles; the last one links the credits and marks the person complete
  264. static fillStep = (p, jobIn) => {
  265. job = jobIn
  266. total = job.items.length
  267. let end = job.pos + fillBatch
  268. if (end > total) { end = total }
  269. credits = job.credits
  270. let added = job.added
  271. pid = p.id
  272. pname = p.name
  273. let i = job.pos
  274. while (i < end) {
  275. let item = job.items[i]
  276. let sid = titleIdByTmdb(item.kind == 'tv' ? 'series' : 'movie', item.tmdbId)
  277. if (sid == null) {
  278. sid = addMinimalTitle(item, pid, pname)
  279. if (sid != null) { added = added + 1 }
  280. }
  281. if (sid != null) { credits.push({ show = sid character = item.role posterPath = item.posterPath adult = item.adult }) }
  282. i = i + 1
  283. }
  284. job.pos = end
  285. job.credits = credits
  286. job.added = added
  287. job.steps = job.steps + 1
  288. jobs[pid] = job
  289. if (end < total) { return { done = false pos = end total = total } }
  290. // done: the credits (+ the migrated ones TMDB no longer lists), complete
  291. have = {}
  292. all = []
  293. for (c of credits) { have[c.show] = true all.push(c) }
  294. for (c of listOr(p.shows)) { if (c != null && c.show != null && have[c.show] != true) { all.push(c) } }
  295. x = p
  296. x.shows = all
  297. x.filmographyAt = now()
  298. x.updatedAt = x.filmographyAt
  299. if (personsTable.update(x.id, x) == null) { return { error = 'could not store the filmography: ' + personsTable.lastError() } }
  300. personTouched(x.id)
  301. personsTable.persist()
  302. showsTable.persist()
  303. jobs[pid] = null
  304. console.log('person fill: ' + pname + ' (tmdb ' + p.tmdbId + ') credits=' + total + ' added=' + added + ' requests=' + job.requests + ' steps=' + job.steps + ' tmdbMs=' + job.tmdbMs + ' ms=' + (now() - job.started))
  305. return { done = true pos = total total = total added = added }
  306. }
  307. // one step for the person with this id: { done, pos, total } or { error }
  308. static personFillStep = (&personId) => {
  309. if (warmBlocked()) { return { error = 'tracker is starting up, try again in a minute' } }
  310. p = personsTable.fetch(personId)
  311. if (p == null) { return { error = 'no such person' } }
  312. if (isComplete(p)) { return { done = true } }
  313. if (p.tmdbId == null) { return { error = 'TMDB does not know this person' } }
  314. job = jobs[personId]
  315. if (job == null) { return startFill(p) }
  316. return fillStep(p, job)
  317. }
  318. // ---- people from a title's cast (tracker#26, details.hl completeStep) ------------------------------------------
  319. // TMDB person id → our person id, built once (15k people, one scan), kept up to date by castPersonId
  320. // (flat statics, written key by key like search.hl's haveTmdb: `let m = …ids` would copy the whole map — plain data
  321. // deep-copies on assignment)
  322. static personTmdbBuilt = { yes = false }
  323. static personTmdbIds = {}
  324. static noteTmdbOfPerson = (&p) => {
  325. if (p.tmdbId != null) { personTmdbIds['' + p.tmdbId] = p.id }
  326. return null
  327. }
  328. // bootscan.hl streamed every person through noteSlugOfPerson / noteTmdbOfPerson, every title through noteCastProfiles
  329. static markPeopleIndexesBuilt = (&castPhotos) => {
  330. slugIndex.built = true
  331. personTmdbBuilt.yes = true
  332. peopleView.castProfiles = castPhotos
  333. return true
  334. }
  335. static personIdByTmdb = (&tmdbId) => {
  336. if (!personTmdbBuilt.yes && !warm.running) {
  337. personTmdbBuilt.yes = true
  338. for (p of personsTable.iterate(null, null)) { noteTmdbOfPerson(p) }
  339. }
  340. return personTmdbIds['' + tmdbId]
  341. }
  342. // a free person slug: "Maya-Rudolph", taken → "-2", … (the migrated people's form)
  343. static personSlugOf = (&name, &tmdbId) => {
  344. ensureSlugIndex()
  345. base = slugBaseOf(name, 'person-' + tmdbId)
  346. if (slugIndex.ids[base] == null) { return base }
  347. let n = 2
  348. while (slugIndex.ids[base + '-' + n] != null) { n = n + 1 }
  349. return base + '-' + n
  350. }
  351. // how many people castPersonId added since the start (details.hl counts a title's new people by the difference, tracker#28)
  352. static castPeopleAdded = { n = 0 }
  353. static castPeopleCount = () => { return castPeopleAdded.n }
  354. // our person for one cast entry of TMDB ({ tmdbId, name, character, gender, profile }) — the one we have by TMDB id, else a NEW
  355. // person (name, tmdbId, a free slug, this one credit, TMDB's profile path as `tmdbProfile` (tracker#37: the photo of /people and
  356. // the search); not complete: their page fetches the rest on the first visit like any migrated person). Answers the id, or null
  357. // when it could not be stored. Also a CREW person (tracker#28: `character` = the job, "Director").
  358. static castPersonId = (&c, &showId) => {
  359. if (warmBlocked()) { return null }
  360. have = personIdByTmdb(c.tmdbId)
  361. if (have != null) { return have }
  362. record = { id = newId() name = c.name tmdbId = c.tmdbId urlSegment = personSlugOf(c.name, c.tmdbId) oldId = 'tmdb-person-' + c.tmdbId
  363. gender = c.gender image = null tmdbProfile = textOr(c.profile) adult = false birthDate = null birthPlace = null deathDate = null imdbId = null
  364. shows = showId != null ? [{ show = showId character = c.character }] : [] added = now() }
  365. // mission 062 (tracker#27 for #26/#28): every person the app creates gets its short id at once (shortids.hl)
  366. record.shortId = claimShortId('person', record.id, record.oldId)
  367. if (personsTable.put(record) == null) {
  368. console.log('details: person "' + c.name + '" not stored: ' + personsTable.lastError())
  369. return null
  370. }
  371. personTmdbIds['' + c.tmdbId] = record.id
  372. slugIndex.ids[record.urlSegment] = record.id
  373. indexPerson(record)
  374. personTouched(record.id)
  375. castPeopleAdded.n = castPeopleAdded.n + 1
  376. return record.id
  377. }
  378. // ---- GUEST STARS (mission 062): a series' guests are NOT person records — details.hl stores them on the title ({ tmdbId, actor,
  379. // character, profile, episodes, guest }, `person` only when we have them already). 160k people (most of them one-episode guests)
  380. // cost 3.3 GB of RSS at boot (w061). A guest's link is `/person/tmdb/<tmdbId>?show=<show id>` (project.hl guestRoute): the FIRST
  381. // open creates the person (name + role from that title's guest entry; without one — an old link — TMDB's /person/<id>), with its
  382. // short id, then the person page fills it like any other.
  383. static guestOf = (&showId, &tmdbId) => {
  384. s = showId != null && hlTypeName(showId) == 'String' && showId != '' ? showsTable.fetch(showId) : null
  385. if (s == null) { return null }
  386. for (c of listOr(s.cast)) { if (c != null && c.tmdbId == tmdbId && textOr(c.actor) != null) { return c } }
  387. return null
  388. }
  389. // our person for this TMDB person (created now when we have none) → { id, created } or { error }
  390. static guestPersonId = (tmdbId, showId) => {
  391. if (warmBlocked()) { return { error = 'tracker is starting up, try again in a minute' } }
  392. have = personIdByTmdb(tmdbId)
  393. if (have != null) { return { id = have created = false } }
  394. g = guestOf(showId, tmdbId)
  395. let c = null
  396. let inShow = null
  397. if (g != null) {
  398. c = { tmdbId = tmdbId name = g.actor character = g.character gender = null }
  399. inShow = showId
  400. } else {
  401. d = tmdbGet('/person/' + tmdbId)
  402. if (d.failed != null) { return { error = 'TMDB: ' + d.failed } }
  403. if (textOr(d.name) == null || d.adult == true) { return { error = 'no such person' } }
  404. c = { tmdbId = tmdbId name = d.name character = null gender = d.gender }
  405. }
  406. pid = castPersonId(c, inShow)
  407. if (pid == null) { return { error = 'could not store the person' } }
  408. personsTable.persist()
  409. console.log('guest opened: ' + c.name + ' (tmdb ' + tmdbId + ') → new person ' + pid + (inShow != null ? ' from show ' + inShow : ' from TMDB'))
  410. return { id = pid created = true }
  411. }
  412. // ---- /people (tracker#37, mission 032): everyone we have, the one updated last first ------------------------------------------
  413. // Creator: "/people just lists persons by last one updated with poster name and pagination". LAST UPDATED = the newest of
  414. // `updatedAt` (stamped by the person page's fetch, startFill / fillStep), `filmographyAt` and `added` (a person the app created);
  415. // the migrated people with none of them come last (newest stored first). No adult filter (tracker#32's /people had none either).
  416. // A CACHED VIEW like tracker#30's list caches (mission 025): the order is built ONCE at boot (project.hl warmPeopleView: one pass
  417. // over the people + one sort, and the cast photos below — 105k people, 3–4 s on the live copy), a page only slices its 24 ids and
  418. // fetches those people (~15 ms server time, any page). Kept current without a rebuild: every person the app creates or updates is
  419. // `personTouched` → `recent`; the next page read moves them to the front (one pass over the ids, ~50 ms, only after a change).
  420. static peopleView = { built = false ids = [] recent = [] castProfiles = 0 }
  421. static updatedKeyOf = (&p) => {
  422. let k = p.updatedAt != null ? p.updatedAt : 0
  423. if (p.filmographyAt != null && p.filmographyAt > k) { k = p.filmographyAt }
  424. if (p.added != null && p.added > k) { k = p.added }
  425. return k
  426. }
  427. // one person's entry of the view's list (table order; bootscan.hl's person pass collects them) — mission T17: two parallel arrays (the
  428. // id, the key) instead of an object { id, key } per person (680 K people: 3 live objects each, every collection marks them)
  429. static viewIds = []
  430. static viewKeys = []
  431. static noteViewRow = (&p) => {
  432. viewIds.push(p.id)
  433. viewKeys.push(updatedKeyOf(p))
  434. return null
  435. }
  436. static warmPeopleView = () => {
  437. if (peopleView.built) { return peopleView.ids.length }
  438. peopleView.castProfiles = castProfilesOf()
  439. for (p of personsTable.iterate(null, null)) { noteViewRow(p) }
  440. return finishPeopleView()
  441. }
  442. static finishPeopleView = () => {
  443. // newest stored first: the table is in the order the people were stored, so the list is nearly sorted already (util.hl sortDesc
  444. // keeps runs) and equal keys (the migrated people: 0) end newest stored first. The people with a key above 0 (touched since the
  445. // migration — few) are sorted; sortDesc is stable, so the zeros after them stay in the same newest-stored-first order as when
  446. // everything was sorted together. (A key that is not a plain number >= 0 never happened: then everything is sorted, as before.)
  447. positive = []
  448. zeros = []
  449. let odd = false
  450. let i = viewIds.length - 1
  451. while (i >= 0) {
  452. k = viewKeys[i]
  453. if (hlTypeName(k) != 'Number') { odd = true }
  454. else if (k > 0) { positive.push({ id = viewIds[i] key = k }) }
  455. else if (k == 0) { zeros.push(viewIds[i]) }
  456. else { odd = true }
  457. i = i - 1
  458. }
  459. ids = []
  460. if (odd) {
  461. newestFirst = []
  462. let j = viewIds.length - 1
  463. while (j >= 0) { newestFirst.push({ id = viewIds[j] key = viewKeys[j] }) j = j - 1 }
  464. for (x of sortDesc(newestFirst)) { ids.push(x.id) }
  465. } else {
  466. for (x of sortDesc(positive)) { ids.push(x.id) }
  467. for (id of zeros) { ids.push(id) }
  468. }
  469. peopleView.ids = ids
  470. peopleView.built = true
  471. return ids.length
  472. }
  473. // a person was created or updated: first in /people from the next page read on
  474. static personTouched = (&id) => {
  475. if (peopleView.built && id != null) { peopleView.recent.push(id) }
  476. return null
  477. }
  478. static peopleViewCurrent = () => {
  479. if (!peopleView.built && !warm.running) { warmPeopleView() }
  480. if (peopleView.recent.length == 0) { return true }
  481. seen = {}
  482. out = []
  483. let i = peopleView.recent.length - 1
  484. while (i >= 0) {
  485. let id = peopleView.recent[i]
  486. if (seen[id] != true) { seen[id] = true out.push(id) }
  487. i = i - 1
  488. }
  489. for (id of peopleView.ids) { if (seen[id] != true) { out.push(id) } }
  490. peopleView.ids = out
  491. peopleView.recent = []
  492. return true
  493. }
  494. static peopleViewStats = () => { return { people = peopleView.ids.length castProfiles = peopleView.castProfiles } }
  495. static peopleCount = () => {
  496. peopleViewCurrent()
  497. return peopleView.ids.length
  498. }
  499. // a person's photo for a tile or a search row: our file (photoUrlOf), else TMDB's (`tmdbProfile`, w185 — like the filmography's
  500. // TMDB posters), else TMDB's from a title's cast entry (castProfiles), else the placeholder
  501. static personPhotoOf = (&p) => {
  502. local = photoUrlOf(p)
  503. if (local != '') { return local }
  504. path = textOr(p.tmdbProfile) != null ? p.tmdbProfile : castProfiles[p.id]
  505. return path != null ? tmdbImageBase + '/w185' + path : '/profiles/none'
  506. }
  507. // person id → TMDB's profile path from a title's cast or crew entry (credits.hl stores `profile` there: cast since tracker#28, crew
  508. // since tracker#37) — for the people the credits job created before castPersonId kept it as `tmdbProfile` (live copy 2026-10-04:
  509. // 51,922 of 105,018 people; the crew entries had none yet). Built at boot with the view (one pass over the titles, ~1–3 s). IN
  510. // MEMORY, not written to the people: 51,907 record updates took 511 s at a start (mission 032, measured on the live copy).
  511. // (Flat static written key by key, like the maps above.)
  512. static castProfiles = {}
  513. // one title's cast and crew photos into the map; how many were new (bootscan.hl's title pass sums them)
  514. static noteCastProfiles = (&s) => {
  515. let n = 0
  516. for (c of listOr(s.cast)) {
  517. if (c != null && c.person != null && textOr(c.profile) != null && castProfiles[c.person] == null) {
  518. castProfiles[c.person] = c.profile
  519. n = n + 1
  520. }
  521. }
  522. for (c of listOr(s.crew)) {
  523. if (c != null && c.person != null && textOr(c.profile) != null && castProfiles[c.person] == null) {
  524. castProfiles[c.person] = c.profile
  525. n = n + 1
  526. }
  527. }
  528. return n
  529. }
  530. static castProfilesOf = () => {
  531. let n = 0
  532. // iterate, not find: one title decoded at a time
  533. for (s of showsTable.iterate(null, null)) { n = n + noteCastProfiles(s) }
  534. return n
  535. }
  536. // one page: { page, pages, tiles } — a tile like the title grids' (components/tilelist.hl): photo, name, no year
  537. static peoplePage = (&raw) => {
  538. peopleViewCurrent()
  539. pages = pagesOf(peopleView.ids.length)
  540. page = pageNumberOf(raw, pages)
  541. tiles = []
  542. for (id of peopleView.ids.slice((page - 1) * pageSize, page * pageSize)) {
  543. let p = personsTable.fetch(id)
  544. if (p != null && p.urlSegment != null) {
  545. tiles.push({ href = '/person/' + p.urlSegment posterUrl = personPhotoOf(p) title = p.name != null ? '' + p.name : '' year = '' })
  546. }
  547. }
  548. return { page = page pages = pages tiles = tiles }
  549. }
  550. // ---- THE PHOTO BACKFILL (mission 032, for tracker#37's /people) ----------------------------------------------------------------
  551. // The people the credits job created are mostly CREW with no photo source at all (live copy 2026-10-04: /people pages 1–3 = 72 of
  552. // 72) — one TMDB request per person (`GET /person/<tmdbId>`, ≤ 40 / 10 s, jobs.hl `photoTick`, the last start job) stores TMDB's
  553. // `profile_path` as `tmdbProfile` (shown from TMDB w185 like the cast photos, no file downloaded); TMDB has none → `photoCheck`
  554. // (never asked again), 404 the same. In /people's order (the one updated last first): page 1 is filled first. Not an update of
  555. // the person (`updatedAt` untouched). Resumes after a restart (the queue = who still has no photo source and no check).
  556. static needsPhoto = (&p) => {
  557. return p.tmdbId != null && p.photoCheck == null && textOr(p.tmdbProfile) == null && castProfiles[p.id] == null && photoUrlOf(p) == ''
  558. }
  559. // mission 046: the queue is built in SLICES (lib/slices.hl's idea, ids only): the people view's id list (already in /people's order, in RAM) is
  560. // walked from a cursor, a tick fetches rows until its time budget (sliceBudgetMs) is spent — the old one-shot loop did ~621 K fetches in
  561. // ONE tick. Same queue, same order (the view's order); the view is snapshotted at the start (people added while it runs are the next start's).
  562. static photoQueueStart = () => {
  563. peopleViewCurrent()
  564. return { ids = peopleView.ids pos = 0 out = [] ms = 0 steps = 0 skipped = 0 }
  565. }
  566. // true when the whole list is done
  567. static photoQueueStep = (&c) => {
  568. t0 = now()
  569. n = c.ids.length
  570. while (c.pos < n) {
  571. let id = c.ids[c.pos]
  572. c.pos = c.pos + 1
  573. let p = personsTable.fetch(id)
  574. // the damaged persons index of the real data: fetch() can answer a Number (mission 045, slices.hl): skipped + counted, as sliceNext does
  575. if (p != null && hlTypeName(p) != 'Hybrid') { c.skipped = c.skipped + 1 }
  576. else if (p != null && needsPhoto(p)) { c.out.push(id) }
  577. if (c.pos % 64 == 0 && now() - t0 >= sliceBudgetMs) { break }
  578. }
  579. c.steps = c.steps + 1
  580. c.ms = c.ms + (now() - t0)
  581. return c.pos >= n
  582. }
  583. // ONE person → { requests, found (a photo stored), errors, error, retry (a passing failure: ask again later) }
  584. static photoStep = (&personId) => {
  585. out = { requests = 0 found = false errors = 0 error = '' retry = false }
  586. p = personsTable.fetch(personId)
  587. if (p == null || !needsPhoto(p)) { return out }
  588. out.requests = 1
  589. d = tmdbGet('/person/' + p.tmdbId)
  590. x = p
  591. if (d.failed != null) {
  592. out.errors = 1
  593. out.error = p.name + ': ' + d.failed
  594. if (d.status != 404) { out.retry = true return out }
  595. x.photoCheck = 'TMDB 404'
  596. } else if (textOr(d.profile_path) != null) {
  597. x.tmdbProfile = d.profile_path
  598. out.found = true
  599. } else { x.photoCheck = 'TMDB has none' }
  600. if (personsTable.update(x.id, x) == null) { out.errors = out.errors + 1 out.error = p.name + ': not stored: ' + personsTable.lastError() }
  601. return out
  602. }
  603. static persistPhotos = () => {
  604. personsTable.persist()
  605. return true
  606. }

Branches

Latest commits

  • 593a6350boot: resumable start-up scan, the server listens first (T17)mre
  • be4890a0reports: H2 hydration audit (no test change needed)mre
  • 1f9c8bf4re-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • c1df88eddocker-compose: HL_GC_TABLE_LOAD=62 (tracker mission 048, T16 real-data numbers)mre
  • d0a82867mission 048: report, LOG (HL_GC_TABLE_LOAD=62 measured)mre
  • 2d4be66fre-vendor Hybriel 5df4eb7e (GC: HL_GC_TABLE_LOAD knob, mission 406)mre
  • 99e7c8b9tests: realdata-m035-radii, repro-t38, realdata-060-backfill ports from env (REAL_PORT, REAL_CHROME, REPRO_CHROME), defaults unchangedmre
  • 4625aa22re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • a6dfd6ecdeploy: person-lookup gate (tracker#46)mre
  • 33ad4bd5Merge commit 'f57f64f'mre
  • 7d38cec4docker-compose: nofile 65536 (default soft 1024 refused boot: errno 24 in fs.listDir, Hybriel 528bc89f)mre
  • f57f64f0mission 046: report, LOGmre
  • 5965963adeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • efe1b485mission 046: re-vendor Hybriel master 60b2817b (integration 8, #203): plugins mpackdb + web (bin/ git-ignored: hybriel, libhlrt.a, hlc.o, hlrt/ from native/zig-out)mre
  • 53099ffbmission 046: person page reads a slim per-title credit row cache (no whole-record fetch per credit); gate tests/person-lookup.mjsmre
  • 9b3828aemission 046a: report, LOGmre
  • 4b37356emission 046: franchise seed supersedeCollections reads the curated timelines once (10 s one-tick block at the end of boot)mre
  • ac6268e3mission 046a: re-vendor Hybriel 528bc89f (integration 6): plugins mpackdb/time/web (bin/ git-ignored)mre
  • 322b3293mission 046: photoQueue guard for non-row fetch results (damaged persons index)mre
  • e5b99aecmission 046: photoQueue in slices (people.hl photoQueueStart/Step, jobs.hl photoTick), tests/photoqueue.hlmre