gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9c3971d09c3971d0tests/persons-skip-count.hlmre9c3971d0/lib/details.hl

15.7 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, 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 } from './people.hl'
  43. import { withCredits, applyCredits } from './credits.hl'
  44. import { hasCredits } from './credits-helpers.hl'
  45. // what the show page and the repair complete: an incomplete title that is not adult (mission 054/056: never public)
  46. static needsDetails = (&s) => { return isIncomplete(s) && s.adult != true && s.mergedInto == null }
  47. // 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
  48. // a page shows as reduced first (shows.hl missingParts: no seasons, poster, genres …), then the rest, each in table order
  49. // mission 044: sliced — `detailsRepairScan()` + `collectStep(scan, repairClass)` (slices.hl), the queue = `collected(scan)`
  50. static detailsRepairScan = () => { return collectStart(showsTable) }
  51. static repairClass = (&s) => {
  52. if (!needsDetails(s)) { return 0 }
  53. return missingParts(s).length > 0 ? 1 : 2
  54. }
  55. // THE RECORD'S OWN FIELDS from a details answer (tracker#26 completion; tracker#18 the daily sync too — creator: "some shows changed
  56. // descriptions and titles and that did not update"): title (a series' `name`, a movie's `title`), TMDB's overview → `tmdbSummary`,
  57. // homepage, tagline, status, genres (by TMDB id), adult; type / release / year / language only when we have none. A TMDB value
  58. // that is empty never overwrites ours. `summary` is the creator's OWN text — never written here (the page shows summary >
  59. // tmdbSummary > tvmazeSummary, shows.hl summaryOf). The slug (`urlSegment`) never changes with the title.
  60. // mission 070 (conductor): the sync never DESTROYS what the old tracker's migration brought — before TMDB's value replaces a
  61. // migrated record's title / genres / homepage / tagline for the FIRST time, the old one is kept in `migratedTitle` /
  62. // `migratedGenres` / `migratedHomepage` / `migratedTagline` (kept, shown nowhere; set once, a later rename leaves it). A record is
  63. // migrated when its `oldId` is the old tracker's (not `tmdb-…`: search import, filmography fill). `status`, counts, ids and the
  64. // poster are TMDB's state, written by the sync since #9 — not kept.
  65. static isMigratedRecord = (&show) => { return show.oldId != null && ('' + show.oldId).slice(0, 5) != 'tmdb-' }
  66. static sameIdList = (&a, &b) => {
  67. x = listOr(a)
  68. y = listOr(b)
  69. if (x.length != y.length) { return false }
  70. let i = 0
  71. while (i < x.length) { if ('' + x[i] != '' + y[i]) { return false } i = i + 1 }
  72. return true
  73. }
  74. static withDetailsFields = (show, &d, &movie) => {
  75. x = show
  76. migrated = isMigratedRecord(show)
  77. adult = adultOf(d)
  78. if (adult != null) { x.adult = adult }
  79. let title = textOr(movie ? d.title : d.name)
  80. if (title != null) {
  81. if (migrated && x.migratedTitle == null && textOr(x.title) != null && x.title != title) { x.migratedTitle = x.title }
  82. x.title = title
  83. }
  84. if (textOr(d.overview) != null) { x.tmdbSummary = d.overview }
  85. if (textOr(d.homepage) != null) {
  86. if (migrated && x.migratedHomepage == null && textOr(x.homepage) != null && x.homepage != d.homepage) { x.migratedHomepage = x.homepage }
  87. x.homepage = d.homepage
  88. }
  89. if (textOr(d.tagline) != null) {
  90. if (migrated && x.migratedTagline == null && textOr(x.tagline) != null && x.tagline != d.tagline) { x.migratedTagline = x.tagline }
  91. x.tagline = d.tagline
  92. }
  93. if (textOr(d.status) != null) { x.status = d.status }
  94. tmdbGenreIds = []
  95. for (g of listOr(d.genres)) { if (g != null && g.id != null) { tmdbGenreIds.push(g.id) } }
  96. genres = genreIdsOfTmdb(tmdbGenreIds)
  97. if (genres.length > 0) {
  98. if (migrated && x.migratedGenres == null && listOr(x.genres).length > 0 && !sameIdList(x.genres, genres)) { x.migratedGenres = x.genres }
  99. x.genres = genres
  100. }
  101. if (textOr(x.type) == null) { x.type = movie ? 'movie' : 'series' }
  102. release = textOr(movie ? d.release_date : d.first_air_date)
  103. if (textOr(x.release) == null && release != null) {
  104. x.release = release
  105. if (release.length >= 4) { x.year = toNumber(release.slice(0, 4)) }
  106. }
  107. if (textOr(x.language) == null && textOr(d.original_language) != null) { x.language = d.original_language }
  108. // tracker#33 (mission 032): a movie's runtime in minutes (TMDB's 0 = unknown, not stored)
  109. if (movie && d.runtime != null && hlTypeName(d.runtime) == 'Number' && d.runtime > 0) { x.runtime = d.runtime }
  110. // tracker#21 (merge, mission 070): TMDB's TV type + the kind from the genres just stored (the delta's light step and the
  111. // completion of #26 store it too, not only syncShow / the kind backfill)
  112. if (!movie && textOr(d.type) != null) {
  113. x.tmdbType = d.type
  114. x.kind = kindOfTitle(d.type, x.genres, x.tvmazeType)
  115. }
  116. return x
  117. }
  118. // THE DAILY SYNC (jobs.hl syncTick, tools/sync-tmdb.hl; tracker#18): after sync.hl `syncShow` (seasons, episodes, poster,
  119. // ids, TVmaze) the record's own fields (withDetailsFields) and its cast + crew (#28) from the SAME details answer (`res.details`)
  120. // — no extra request. Answers { renamed (the title changed: the search re-indexes it), cast, crew, people, ms, error }
  121. static applySynced = (&showId, &d) => {
  122. out = { renamed = false cast = 0 crew = 0 people = 0 ms = 0 error = '' }
  123. t0 = now()
  124. show = showsTable.fetch(showId)
  125. if (show == null || d == null) { return out }
  126. movie = show.type == 'movie'
  127. before = show.title
  128. let x = withDetailsFields(show, d, movie)
  129. out.renamed = x.title != before
  130. if (x.adult != true && hasCredits(d, movie)) {
  131. w = withCredits(x, d, movie)
  132. x = w.show
  133. out.cast = w.cast
  134. out.crew = w.crew
  135. out.people = w.people
  136. }
  137. if (showsTable.update(x.id, x) == null) { out.error = x.title + ': record not stored: ' + showsTable.lastError() }
  138. out.ms = now() - t0
  139. return out
  140. }
  141. // 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
  142. // request at a time, every page waited): step 1 = the details request (fields + cast); then one step per PART of its seasons
  143. // (≤ `partEpisodes` episodes, ≤ 20 seasons = one TMDB request — sync.hl syncShowPart); the last step = ids, TVmaze, poster,
  144. // `detailsAt`. A movie is one step. Between two steps every other request is served.
  145. // show id → { d (TMDB's answer of step 1), parts ([[season numbers]]), pos, sum } while a series is under way — in memory: a
  146. // restart begins again with the details request; what is stored by then is matched by tmdbId, nothing is duplicated
  147. static jobs = {}
  148. // (TRACKER_DETAILS_PART_EPISODES: the gate sets 1 → one season per step)
  149. static partEpisodes = env('TRACKER_DETAILS_PART_EPISODES') != null ? toNumber(env('TRACKER_DETAILS_PART_EPISODES')) : 300
  150. 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 = '' } }
  151. static addSum = (sum, &r, &ms) => {
  152. o = sum
  153. o.steps = o.steps + 1
  154. o.requests = o.requests + r.requests
  155. o.tvmazeRequests = o.tvmazeRequests + r.tvmazeRequests
  156. o.newSeasons = o.newSeasons + r.newSeasons + r.tvmazeSeasons
  157. o.newEpisodes = o.newEpisodes + r.newEpisodes + r.tvmazeEpisodes
  158. o.posters = o.posters + r.posters
  159. o.ids = o.ids + r.ids
  160. o.ms = o.ms + ms
  161. if (r.error != '') { o.error = r.error }
  162. return o
  163. }
  164. // TMDB's season list → parts of ≤ partEpisodes episodes (a bigger season is a part of its own), ≤ 20 seasons each; which of
  165. // them are really synced (season 0, empty ones) syncShowPart decides
  166. static seasonParts = (&d) => {
  167. parts = []
  168. let cur = []
  169. let n = 0
  170. for (x of listOr(d.seasons)) {
  171. if (x != null && x.season_number != null) {
  172. let c = x.episode_count != null ? x.episode_count : 0
  173. if (cur.length > 0 && (n + c > partEpisodes || cur.length == 20)) {
  174. parts.push(cur)
  175. cur = []
  176. n = 0
  177. }
  178. cur.push(x.season_number)
  179. n = n + c
  180. }
  181. }
  182. if (cur.length > 0) { parts.push(cur) }
  183. return parts
  184. }
  185. // a step's answer: { skipped (nothing to do), done, requests / tvmazeRequests (THIS step's — what the pause counts), adult,
  186. // error ('' = none), retry (a passing failure: no answer, 429, 5xx), sum (the whole title's counts so far: steps, requests,
  187. // cast, newSeasons, newEpisodes, posters, ids, ms) }
  188. static stepOut = () => { return { skipped = false done = false requests = 0 tvmazeRequests = 0 adult = null error = '' retry = false sum = emptySum() } }
  189. // the last step: the title is complete
  190. static markComplete = (&showId, &sum) => {
  191. done = showsTable.fetch(showId)
  192. done.detailsAt = now()
  193. if (sum.error != '') { done.detailsError = sum.error }
  194. ok = showsTable.update(done.id, done) != null
  195. refreshCatalogShow(done.id)
  196. searchTitleChanged(done.id)
  197. return ok
  198. }
  199. // step 1: the details request — the record's fields, the cast; a movie also its ids + poster (done); a series' parts are planned
  200. static firstStep = (show) => {
  201. out = stepOut()
  202. t0 = now()
  203. movie = show.type == 'movie'
  204. out.requests = 1
  205. d = tmdbGet((movie ? '/movie/' : '/tv/') + show.tmdbId + '?append_to_response=external_ids,' + (movie ? 'credits' : 'aggregate_credits'))
  206. if (d.failed != null) {
  207. out.error = show.title + ': ' + d.failed
  208. if (d.status == 404) {
  209. show.detailsCheck = 'TMDB 404'
  210. showsTable.update(show.id, show)
  211. out.done = true
  212. } else { out.retry = true }
  213. return out
  214. }
  215. out.adult = adultOf(d)
  216. let x = withDetailsFields(show, d, movie)
  217. // mission 025: a movie has no season/episode counts (hasTvLeftovers)
  218. if (movie) { x.seasonsCount = null x.episodesCount = null }
  219. if (out.adult == true) {
  220. // never public: the flag, no poster, no seasons, no cast (mission 056)
  221. x.detailsCheck = 'adult'
  222. showsTable.update(x.id, x)
  223. refreshCatalogShow(x.id)
  224. searchTitleChanged(x.id)
  225. out.done = true
  226. return out
  227. }
  228. // tracker#28: the whole cast + the crew, from this same answer
  229. w = withCredits(x, d, movie)
  230. x = w.show
  231. if (showsTable.update(x.id, x) == null) {
  232. out.error = show.title + ': not updated: ' + showsTable.lastError()
  233. out.retry = true
  234. return out
  235. }
  236. sum = emptySum()
  237. sum.cast = w.cast
  238. sum.crew = w.crew
  239. sum.people = w.people
  240. if (movie) {
  241. // one request was enough: ids + poster now (syncMovie with this answer)
  242. r = syncShowWith(x.id, d)
  243. out.requests = r.requests
  244. out.sum = addSum(sum, r, now() - t0)
  245. if (r.error != '') { out.error = r.error }
  246. markComplete(x.id, out.sum)
  247. out.done = true
  248. return out
  249. }
  250. sum.steps = 1
  251. sum.requests = 1
  252. sum.ms = now() - t0
  253. // the next steps need the details, not the credits (a long series' aggregate_credits are ~1 MB, copied on every step)
  254. dd = d
  255. dd.aggregate_credits = null
  256. jobs[x.id] = { d = dd parts = seasonParts(d) pos = 0 sum = sum }
  257. out.sum = sum
  258. return out
  259. }
  260. // ONE step of completing this title (by our id) — called until `done` (the page's face, the repair job)
  261. static completeStep = (&showId) => {
  262. show = showsTable.fetch(showId)
  263. if (!needsDetails(show)) {
  264. jobs[showId] = null
  265. let out = stepOut()
  266. out.skipped = true
  267. out.done = true
  268. return out
  269. }
  270. job = jobs[showId]
  271. if (job == null) { return firstStep(show) }
  272. t0 = now()
  273. let out = stepOut()
  274. last = job.pos >= job.parts.length
  275. r = syncShowPart(showId, job.d, { seasons = last ? [] : job.parts[job.pos] finish = last })
  276. out.requests = r.requests
  277. out.tvmazeRequests = r.tvmazeRequests
  278. if (r.error != '') { out.error = r.error }
  279. j = job
  280. j.sum = addSum(j.sum, r, now() - t0)
  281. j.pos = j.pos + 1
  282. out.sum = j.sum
  283. if (!last) {
  284. jobs[showId] = j
  285. return out
  286. }
  287. jobs[showId] = null
  288. markComplete(showId, j.sum)
  289. out.done = true
  290. return out
  291. }
  292. // the line every completed title logs
  293. static completeLine = (&title, &sum) => {
  294. return '"' + title + '" steps=' + sum.steps + ' requests=' + sum.requests + ' tvmazeRequests=' + sum.tvmazeRequests + ' cast=' + sum.cast + ' newSeasons=' + sum.newSeasons
  295. + ' newEpisodes=' + sum.newEpisodes + ' posters=' + sum.posters + ' ids=' + sum.ids + ' ms=' + sum.ms + ' crew=' + sum.crew + ' people=' + sum.people + (sum.error != '' ? ' error: ' + sum.error : '')
  296. }
  297. // flush shows/seasons/episodes + people (new cast members)
  298. static persistDetails = () => {
  299. persistSync()
  300. personsTable.persist()
  301. return true
  302. }
  303. // THE SEARCH IMPORT, COMPLETE (tracker#14 + #28): search.hl importTitle and, for a title it created, the cast + crew from the
  304. // same details answer (credits.hl applyCredits — search.hl cannot call it, this file imports search.hl). Used by the search's
  305. // "Add" (components/search.hl) and the curated franchise seed (franchiseseed.hl, tracker#40). → importTitle's answer
  306. static importWithCredits = (&kind, tmdbId) => {
  307. r = importTitle(kind, tmdbId)
  308. if (r.error != null || r.created != true || r.details == null) { return r }
  309. c = applyCredits(r.show, r.details)
  310. persistDetails()
  311. console.log('search import credits: ' + r.path + ' cast=' + c.cast + ' crew=' + c.crew + ' people=' + c.people + ' ms=' + c.ms + (c.error != '' ? ' error: ' + c.error : ''))
  312. return r
  313. }

Branches

Latest commits

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