gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2f986ca32f986ca3Log: RT50 passmre2f986ca3/lib/details.hl

19.0 KB

  1. // lib/details.hl — A TITLE'S FULL DETAILS (tracker.worldapi.org#26). Creator: "when i go to an artist and it loads the bibliography
  2. // those loaded movies and series miss most information and it stays that way when i click on them".
  3. //
  4. // The person page's fill (people.hl, #16) adds every title we don't have as a MINIMAL record — title, year, overview, genres,
  5. // type, tmdbId, adult, this one person as cast; no poster, no seasons, no external ids, no homepage/tagline/status
  6. // (`minimal = true`). A search import (search.hl importTitle) is full except for the cast. Here a minimal title gets what a
  7. // full import has, plus its cast:
  8. //
  9. // GET <TMDB>/movie/<tmdbId>?append_to_response=external_ids,credits a movie
  10. // GET <TMDB>/tv/<tmdbId>?append_to_response=external_ids,aggregate_credits a series (cast of ALL seasons, roles merged)
  11. // then sync.hl `syncShowPart(id, <that answer>, …)` — the daily sync's work, without asking for the details again, IN
  12. // STEPS (see `jobs` below): a series' seasons + episodes (`season/1,…`), then the poster (w342), missing imdb/tvdb ids,
  13. // TVmaze lookup + its episode merge (#15); a movie its imdb id + poster
  14. //
  15. // Stored (`withDetailsFields`, shared with the daily sync since tracker#18): title, overview → `tmdbSummary` (`summary` is the
  16. // creator's own text, never written), homepage, tagline, status, genres (by TMDB id, when
  17. // TMDB gives any), release/year/language when we have none, `adult`, the CAST (TMDB's first `castLimit`, in TMDB's order;
  18. // a person we have by TMDB id is linked, a new one is added — people.hl castPersonId; cast entries we had and TMDB does not
  19. // list are kept after them). Then `detailsAt` (ms) = COMPLETE, never asked again (the daily sync keeps a FOLLOWED title fresh).
  20. // TMDB 404 → `detailsCheck = 'TMDB 404'` (never asked again; the page shows what it has). TMDB says ADULT → the flag is
  21. // stored, `detailsCheck = 'adult'`, no poster/seasons (mission 056: an adult title is never public, its poster not kept).
  22. //
  23. // tracker#33 (mission 029): INCOMPLETE is every title TMDB's details never went through (shows.hl isIncomplete) — also the old
  24. // tracker's migrated rows (5,697 on the live copy: never synced, 691 series without seasons); they are completed the same way.
  25. //
  26. // WHO CALLS IT (`completeStep`, again and again until `done`): the show page when an incomplete title is opened
  27. // (components/show.hl face `showComplete`, skeleton meanwhile) and the repair job in jobs.hl (`repairTick`, one step per tick
  28. // on the app's clock, like the adult backfill) for every incomplete title that exists when the app starts. Adult titles are
  29. // left alone by both.
  30. //
  31. // The cast and crew of the same answer: credits.hl (withCredits). Mission 028: the cast + crew parts moved there, `isIncomplete`
  32. // to shows.hl.
  33. import { now } from 'hl:time'
  34. import { env } from 'hl:proc'
  35. import { textOr, listOr } from './util.hl'
  36. import { collectStart } from './slices.hl'
  37. import { showsTable, seasonsTable, episodesTable, personsTable, kindOfTitle, isIncomplete, missingParts } from './shows.hl'
  38. import { tmdbGet, adultOf } from './tmdb.hl'
  39. import { syncShowWith, syncShowPart, persistSync } from './sync.hl'
  40. import { searchTitleChanged, importTitle } from './search.hl'
  41. import { refreshCatalogShow } from './catalog.hl'
  42. import { genreIdsOfTmdb, castPersonId } from './people.hl'
  43. import { warmBlocked } from './warm.hl'
  44. import { withCredits, applyCredits } from './credits.hl'
  45. import { hasCredits, castOf, mainMinOf, crewOf, crewRoleOf } from './credits-helpers.hl'
  46. // what the show page and the repair complete: an incomplete title that is not adult (mission 054/056: never public)
  47. static needsDetails = (&s) => { return isIncomplete(&s) && s.adult != true && s.mergedInto == null }
  48. // every title the repair job has to do (read once when it starts — so a restart goes on with what is left). tracker#33: the ones
  49. // a page shows as reduced first (shows.hl missingParts: no seasons, poster, genres …), then the rest, each in table order
  50. // mission 044: sliced — `detailsRepairScan()` + `collectStep(scan, repairClass)` (slices.hl), the queue = `collected(scan)`
  51. static detailsRepairScan = () => { return collectStart(showsTable) }
  52. static repairClass = (&s) => {
  53. if (!needsDetails(&s)) { return 0 }
  54. return missingParts(&s).length > 0 ? 1 : 2
  55. }
  56. // THE RECORD'S OWN FIELDS from a details answer (tracker#26 completion; tracker#18 the daily sync too — creator: "some shows changed
  57. // descriptions and titles and that did not update"): title (a series' `name`, a movie's `title`), TMDB's overview → `tmdbSummary`,
  58. // homepage, tagline, status, genres (by TMDB id), adult; type / release / year / language only when we have none. A TMDB value
  59. // that is empty never overwrites ours. `summary` is the creator's OWN text — never written here (the page shows summary >
  60. // tmdbSummary > tvmazeSummary, shows.hl summaryOf). The slug (`urlSegment`) never changes with the title.
  61. // mission 070 (conductor): the sync never DESTROYS what the old tracker's migration brought — before TMDB's value replaces a
  62. // migrated record's title / genres / homepage / tagline for the FIRST time, the old one is kept in `migratedTitle` /
  63. // `migratedGenres` / `migratedHomepage` / `migratedTagline` (kept, shown nowhere; set once, a later rename leaves it). A record is
  64. // migrated when its `oldId` is the old tracker's (not `tmdb-…`: search import, filmography fill). `status`, counts, ids and the
  65. // poster are TMDB's state, written by the sync since #9 — not kept.
  66. static isMigratedRecord = (&show) => { return show.oldId != null && ('' + show.oldId).slice(0, 5) != 'tmdb-' }
  67. static sameIdList = (&a, &b) => {
  68. x = listOr(&a)
  69. y = listOr(&b)
  70. if (x.length != y.length) { return false }
  71. let i = 0
  72. while (i < x.length) { if ('' + x[i] != '' + y[i]) { return false } i = i + 1 }
  73. return true
  74. }
  75. static withDetailsFields = (show, &d, &movie) => {
  76. x = show
  77. migrated = isMigratedRecord(&show)
  78. adult = adultOf(&d)
  79. if (adult != null) { x.adult = adult }
  80. title = textOr(movie ? d.title : d.name)
  81. if (title != null) {
  82. if (migrated && x.migratedTitle == null && textOr(&x.title) != null && x.title != title) { x.migratedTitle = x.title }
  83. x.title = title
  84. }
  85. if (textOr(&d.overview) != null) { x.tmdbSummary = d.overview }
  86. if (textOr(&d.homepage) != null) {
  87. if (migrated && x.migratedHomepage == null && textOr(&x.homepage) != null && x.homepage != d.homepage) { x.migratedHomepage = x.homepage }
  88. x.homepage = d.homepage
  89. }
  90. if (textOr(&d.tagline) != null) {
  91. if (migrated && x.migratedTagline == null && textOr(&x.tagline) != null && x.tagline != d.tagline) { x.migratedTagline = x.tagline }
  92. x.tagline = d.tagline
  93. }
  94. if (textOr(&d.status) != null) { x.status = d.status }
  95. tmdbGenreIds = []
  96. for (g of listOr(&d.genres)) { if (g != null && g.id != null) { tmdbGenreIds.push(g.id) } }
  97. genres = genreIdsOfTmdb(&tmdbGenreIds)
  98. if (genres.length > 0) {
  99. if (migrated && x.migratedGenres == null && listOr(&x.genres).length > 0 && !sameIdList(&x.genres, &genres)) { x.migratedGenres = x.genres }
  100. x.genres = genres
  101. }
  102. if (textOr(&x.type) == null) { x.type = movie ? 'movie' : 'series' }
  103. release = textOr(movie ? d.release_date : d.first_air_date)
  104. if (textOr(&x.release) == null && release != null) {
  105. x.release = release
  106. if (release.length >= 4) { x.year = toNumber(release.slice(0, 4)) }
  107. }
  108. if (textOr(&x.language) == null && textOr(&d.original_language) != null) { x.language = d.original_language }
  109. // tracker#33 (mission 032): a movie's runtime in minutes (TMDB's 0 = unknown, not stored)
  110. if (movie && d.runtime != null && hlTypeName(d.runtime) == 'Number' && d.runtime > 0) { x.runtime = d.runtime }
  111. // tracker#21 (merge, mission 070): TMDB's TV type + the kind from the genres just stored (the delta's light step and the
  112. // completion of #26 store it too, not only syncShow / the kind backfill)
  113. if (!movie && textOr(&d.type) != null) {
  114. x.tmdbType = d.type
  115. x.kind = kindOfTitle(&d.type, &x.genres, &x.tvmazeType)
  116. }
  117. return x
  118. }
  119. // THE DAILY SYNC (jobs.hl syncTick, tools/sync-tmdb.hl; tracker#18): after sync.hl `syncShow` (seasons, episodes, poster,
  120. // ids, TVmaze) the record's own fields (withDetailsFields) and its cast + crew (#28) from the SAME details answer (`res.details`)
  121. // — no extra request. Answers { renamed (the title changed: the search re-indexes it), cast, crew, people, ms, error }
  122. static applySynced = (&showId, &d) => {
  123. out = { renamed = false cast = 0 crew = 0 people = 0 ms = 0 error = '' }
  124. t0 = now()
  125. show = showsTable.fetch(showId)
  126. if (show == null || d == null) { return out }
  127. movie = show.type == 'movie'
  128. before = show.title
  129. let x = withDetailsFields(show, &d, &movie)
  130. out.renamed = x.title != before
  131. if (x.adult != true && hasCredits(&d, &movie)) {
  132. w = withCredits(x, &d, &movie)
  133. x = w.show
  134. out.cast = w.cast
  135. out.crew = w.crew
  136. out.people = w.people
  137. }
  138. if (showsTable.update(x.id, x) == null) { out.error = x.title + ': record not stored: ' + showsTable.lastError() }
  139. out.ms = now() - t0
  140. return out
  141. }
  142. // A SERIES IS DONE IN STEPS (real data: The Late Show, 22 seasons / 4,252 episodes, took 10.7 s as one step — hl:web serves one
  143. // request at a time, every page waited): step 1 = the details request (fields + cast); then one step per PART of its seasons
  144. // (≤ `partEpisodes` episodes, ≤ 20 seasons = one TMDB request — sync.hl syncShowPart); the last step = ids, TVmaze, poster,
  145. // `detailsAt`. A movie is one step. Between two steps every other request is served.
  146. // show id → { d (TMDB's answer of step 1), parts ([[season numbers]]), pos, sum } while a series is under way — in memory: a
  147. // restart begins again with the details request; what is stored by then is matched by tmdbId, nothing is duplicated
  148. static jobs = {}
  149. // (TRACKER_DETAILS_PART_EPISODES: the gate sets 1 → one season per step)
  150. static partEpisodes = env('TRACKER_DETAILS_PART_EPISODES') != null ? toNumber(env('TRACKER_DETAILS_PART_EPISODES')) : 300
  151. static emptySum = () => { return { steps = 0 requests = 0 tvmazeRequests = 0 cast = 0 crew = 0 people = 0 newSeasons = 0 newEpisodes = 0 posters = 0 ids = 0 ms = 0 error = '' } }
  152. static addSum = (sum, &r, &ms) => {
  153. o = sum
  154. o.steps = o.steps + 1
  155. o.requests = o.requests + r.requests
  156. o.tvmazeRequests = o.tvmazeRequests + r.tvmazeRequests
  157. o.newSeasons = o.newSeasons + r.newSeasons + r.tvmazeSeasons
  158. o.newEpisodes = o.newEpisodes + r.newEpisodes + r.tvmazeEpisodes
  159. o.posters = o.posters + r.posters
  160. o.ids = o.ids + r.ids
  161. o.ms = o.ms + ms
  162. if (r.error != '') { o.error = r.error }
  163. return o
  164. }
  165. // TMDB's season list → parts of ≤ partEpisodes episodes (a bigger season is a part of its own), ≤ 20 seasons each; which of
  166. // them are really synced (season 0, empty ones) syncShowPart decides
  167. static seasonParts = (&d) => {
  168. parts = []
  169. let cur = []
  170. let n = 0
  171. for (x of listOr(&d.seasons)) {
  172. if (x != null && x.season_number != null) {
  173. c = x.episode_count != null ? x.episode_count : 0
  174. if (cur.length > 0 && (n + c > partEpisodes || cur.length == 20)) {
  175. parts.push(cur)
  176. cur = []
  177. n = 0
  178. }
  179. cur.push(x.season_number)
  180. n = n + c
  181. }
  182. }
  183. if (cur.length > 0) { parts.push(cur) }
  184. return parts
  185. }
  186. // a step's answer: { skipped (nothing to do), done, requests / tvmazeRequests (THIS step's — what the pause counts), adult,
  187. // error ('' = none), retry (a passing failure: no answer, 429, 5xx), sum (the whole title's counts so far: steps, requests,
  188. // cast, newSeasons, newEpisodes, posters, ids, ms) }
  189. static stepOut = () => { return { skipped = false done = false requests = 0 tvmazeRequests = 0 adult = null error = '' retry = false sum = emptySum() } }
  190. // the last step: the title is complete
  191. static markComplete = (&showId, &sum) => {
  192. done = showsTable.fetch(showId)
  193. done.detailsAt = now()
  194. if (sum.error != '') { done.detailsError = sum.error }
  195. ok = showsTable.update(done.id, done) != null
  196. refreshCatalogShow(&done.id)
  197. searchTitleChanged(&done.id)
  198. return ok
  199. }
  200. // step 1: the details request — the record's fields, the cast; a movie also its ids + poster (done); a series' parts are planned.
  201. // SL1: the details request is one step, creating the people of the credits (a title brings 60-150 new ones, ~3 ms each = 450 ms) is
  202. // `peopleStep` (a time budget per step, state in `peoplePre`), and the rest (the record, the ids, the poster) is `finishFirst` —
  203. // the same work in the same order as when it was one step; only the loop is free in between
  204. static firstStep = (show) => {
  205. out = stepOut()
  206. movie = show.type == 'movie'
  207. out.requests = 1
  208. d = tmdbGet((movie ? '/movie/' : '/tv/') + show.tmdbId + '?append_to_response=external_ids,' + (movie ? 'credits' : 'aggregate_credits'))
  209. if (d.failed != null) {
  210. out.error = show.title + ': ' + d.failed
  211. if (d.status == 404) {
  212. show.detailsCheck = 'TMDB 404'
  213. showsTable.update(show.id, show)
  214. out.done = true
  215. } else { out.retry = true }
  216. return out
  217. }
  218. out.adult = adultOf(&d)
  219. if (out.adult == true) {
  220. x = withDetailsFields(show, &d, &movie)
  221. // never public: the flag, no poster, no seasons, no cast (mission 056)
  222. x.detailsCheck = 'adult'
  223. showsTable.update(x.id, x)
  224. refreshCatalogShow(&x.id)
  225. searchTitleChanged(&x.id)
  226. out.done = true
  227. return out
  228. }
  229. list = peopleToCreate(&d, &movie)
  230. if (list.length == 0) { return finishFirst(show, &d, false) }
  231. // a movie's pause is the poster's request alone (its details request was never counted: the step's answer took the poster's)
  232. out.requests = movie ? 0 : 1
  233. peoplePre[show.id] = { d = d list = list pos = 0 }
  234. return out
  235. }
  236. // the people a title's credits will create, in the order withCredits (credits.hl) creates them: the cast that is not a guest, then the
  237. // crew — the same arguments, so each is found as it is and withCredits creates nobody
  238. static peopleToCreate = (&d, &movie) => {
  239. out = []
  240. mainMin = movie ? 0 : mainMinOf(&d)
  241. for (c of castOf(&d, &movie)) {
  242. if (!(c.episodes != null && c.episodes < mainMin)) { out.push({ tmdbId = c.tmdbId name = c.name character = c.character gender = c.gender profile = c.profile }) }
  243. }
  244. for (c of crewOf(&d, &movie)) { out.push({ tmdbId = c.tmdbId name = c.name character = crewRoleOf(&c.job) gender = c.gender profile = c.profile }) }
  245. return out
  246. }
  247. // a title whose people are being created: title id → { d (TMDB's answer), list, pos } — in memory like `jobs` (a restart asks again)
  248. static peoplePre = {}
  249. static peopleStepMs = 40
  250. static peopleStep = (&showId) => {
  251. out = stepOut()
  252. t0 = now()
  253. let i = peoplePre[showId].pos
  254. n = peoplePre[showId].list.length
  255. while (i < n) {
  256. if (i > peoplePre[showId].pos && now() - t0 >= peopleStepMs) { break }
  257. c = peoplePre[showId].list[i]
  258. castPersonId(&c, &showId)
  259. i = i + 1
  260. }
  261. peoplePre[showId].pos = i
  262. if (i < n) { return out }
  263. d = peoplePre[showId].d
  264. peoplePre[showId] = null
  265. return finishFirst(showsTable.fetch(showId), &d, true)
  266. }
  267. // the rest of step 1: the record with its fields + cast + crew stored; a movie its ids + poster (done); a series' parts planned
  268. static finishFirst = (show, &d, later) => {
  269. out = stepOut()
  270. t0 = now()
  271. movie = show.type == 'movie'
  272. out.requests = later ? 0 : 1
  273. let x = withDetailsFields(show, &d, &movie)
  274. out.adult = adultOf(&d)
  275. // mission 025: a movie has no season/episode counts (hasTvLeftovers)
  276. if (movie) { x.seasonsCount = null x.episodesCount = null }
  277. // tracker#28: the whole cast + the crew, from this same answer
  278. w = withCredits(x, &d, &movie)
  279. x = w.show
  280. if (showsTable.update(x.id, x) == null) {
  281. out.error = show.title + ': not updated: ' + showsTable.lastError()
  282. out.retry = true
  283. return out
  284. }
  285. sum = emptySum()
  286. sum.cast = w.cast
  287. sum.crew = w.crew
  288. sum.people = w.people
  289. if (movie) {
  290. // one request was enough: ids + poster now (syncMovie with this answer)
  291. r = syncShowWith(&x.id, &d)
  292. out.requests = r.requests
  293. out.sum = addSum(sum, &r, now() - t0)
  294. if (r.error != '') { out.error = r.error }
  295. markComplete(&x.id, &out.sum)
  296. out.done = true
  297. return out
  298. }
  299. sum.steps = 1
  300. sum.requests = 1
  301. sum.ms = now() - t0
  302. // the next steps need the details, not the credits (a long series' aggregate_credits are ~1 MB, copied on every step)
  303. dd = d
  304. dd.aggregate_credits = null
  305. jobs[x.id] = { d = dd parts = seasonParts(&d) pos = 0 sum = sum }
  306. out.sum = sum
  307. return out
  308. }
  309. // ONE step of completing this title (by our id) — called until `done` (the page's face, the repair job)
  310. static completeStep = (&showId) => {
  311. // mission T17: a person or title it creates needs the complete indexes (no duplicates) — not while the start-up scan runs
  312. if (warmBlocked()) {
  313. blocked = stepOut()
  314. blocked.retry = true
  315. blocked.error = 'tracker is starting up, try again in a minute'
  316. return blocked
  317. }
  318. show = showsTable.fetch(showId)
  319. if (!needsDetails(&show)) {
  320. jobs[showId] = null
  321. peoplePre[showId] = null
  322. out = stepOut()
  323. out.skipped = true
  324. out.done = true
  325. return out
  326. }
  327. if (peoplePre[showId] != null) { return peopleStep(&showId) }
  328. job = jobs[showId]
  329. if (job == null) { return firstStep(show) }
  330. t0 = now()
  331. out = stepOut()
  332. last = job.pos >= job.parts.length
  333. r = syncShowPart(&showId, &job.d, { seasons = last ? [] : job.parts[job.pos] finish = last })
  334. out.requests = r.requests
  335. out.tvmazeRequests = r.tvmazeRequests
  336. if (r.error != '') { out.error = r.error }
  337. j = job
  338. j.sum = addSum(j.sum, &r, now() - t0)
  339. j.pos = j.pos + 1
  340. out.sum = j.sum
  341. if (!last) {
  342. jobs[showId] = j
  343. return out
  344. }
  345. jobs[showId] = null
  346. markComplete(&showId, &j.sum)
  347. out.done = true
  348. return out
  349. }
  350. // the line every completed title logs
  351. static completeLine = (&title, &sum) => {
  352. return '"' + title + '" steps=' + sum.steps + ' requests=' + sum.requests + ' tvmazeRequests=' + sum.tvmazeRequests + ' cast=' + sum.cast + ' newSeasons=' + sum.newSeasons
  353. + ' newEpisodes=' + sum.newEpisodes + ' posters=' + sum.posters + ' ids=' + sum.ids + ' ms=' + sum.ms + ' crew=' + sum.crew + ' people=' + sum.people + (sum.error != '' ? ' error: ' + sum.error : '')
  354. }
  355. // flush shows/seasons/episodes + people (new cast members)
  356. static persistDetails = () => {
  357. persistSync()
  358. personsTable.persist()
  359. return true
  360. }
  361. // SL1: the same writes as persistDetails, ONE table per call (the repair job calls it once per tick: the five writes of 20-420 ms each
  362. // were one 900 ms block every 10 titles). `stage` = the table to write now (1 titles, 2 seasons, 3 episodes, 4 people); answers the next
  363. // stage, 0 after the last
  364. static persistDetailsStage = (&stage) => {
  365. if (stage == 1) { showsTable.persist() }
  366. else if (stage == 2) { seasonsTable.persist() }
  367. else if (stage == 3) { episodesTable.persist() }
  368. else { personsTable.persist() }
  369. return stage >= 4 ? 0 : stage + 1
  370. }
  371. // THE SEARCH IMPORT, COMPLETE (tracker#14 + #28): search.hl importTitle and, for a title it created, the cast + crew from the
  372. // same details answer (credits.hl applyCredits — search.hl cannot call it, this file imports search.hl). Used by the search's
  373. // "Add" (components/search.hl) and the curated franchise seed (franchiseseed.hl, tracker#40). → importTitle's answer
  374. static importWithCredits = (&kind, tmdbId) => {
  375. r = importTitle(&kind, tmdbId)
  376. if (r.error != null || r.created != true || r.details == null) { return r }
  377. c = applyCredits(&r.show, &r.details)
  378. persistDetails()
  379. console.log('search import credits: ' + r.path + ' cast=' + c.cast + ' crew=' + c.crew + ' people=' + c.people + ' ms=' + c.ms + (c.error != '' ? ' error: ' + c.error : ''))
  380. return r
  381. }

Branches

Latest commits

  • 2f986ca3Log: RT50 passmre
  • 1c4715a4RT50 mission: day50 repro retestmre
  • 57d1c0dcLog: day50 retest requestmre
  • 60b1f419Log: Anton #336 result, wake checkmre
  • 919a18aalog: scratchmre
  • 6b504440status tidymre
  • 9332bd10log: DP70 livemre
  • be11daa0DP70 brief fixmre
  • 886a7028DP70 brief, HM67 mergedmre
  • b41cdd6dMerge hm67-searchids: packed search IDsmre
  • 385904eaReport HM67 matched heap measurements and gate proofmre
  • e5c9b659Pack retained search IDs into UTF-8 bytesmre
  • 62a5f6c7HM67 brief, HM66 mergedmre
  • b32b8440Merge hm66-catalogindex: packed catalog indexmre
  • 2db0f7adReport HM66 heap measurements and validationmre
  • 6ce15623Pack catalog lists and membership maps in UTF-8 bytesmre
  • 71134e21HM66 briefmre
  • ed4cbab2status timemre
  • e982c26dlog: HM65 mergedmre
  • 6e72f9d1Merge hm65-fields: episode boot scan reads three fieldsmre