gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit27c916fa27c916fatracker mission 028: README ("Code order", the new file map), STATUS (counts before/after, tests, how to repeat, open), LOGmre27c916fa/lib/dailysync.hl

10.0 KB

  1. // lib/dailysync.hl — WHICH TITLES THE DAILY SYNC ASKS FOR (tracker.worldapi.org#18; deltasync.hl before mission 028). Creator:
  2. // "it checks every show in a request to the api? cause they have an endpoint that gives a delta on bulk". Before: 2 requests
  3. // per FOLLOWED show every day (256 for 126 shows), the other titles never. Now the daily run (jobs.hl syncTick,
  4. // tools/sync-tmdb.hl --delta) first asks what changed since the last run and syncs ONLY those titles of ours — followed or not:
  5. //
  6. // GET <TMDB_BASE_URL>/tv/changes?start_date=<last run day>&end_date=<today>&page=<n> changed TV ids, 100 per page
  7. // GET <TMDB_BASE_URL>/movie/changes?start_date=…&end_date=…&page=<n> changed movie ids, 100 per page
  8. // GET <TVMAZE_BASE_URL>/updates/shows?since=day|week|month { "<tvmaze id>": <updated at> }
  9. //
  10. // → intersected with our titles (search.hl's index: TMDB id by kind, TVmaze id — no table scan) → the sync's queue, FOLLOWED
  11. // titles first. One change-list request per clock step (jobs.hl), 260 ms per TMDB / 550 ms per TVmaze request between them.
  12. // A FOLLOWED title gets the full step (all seasons, TVmaze — as before #18); one NOBODY follows the light step (sync.hl
  13. // syncShowDelta: details, ids, poster, record, cast + only the seasons that can have changed, no TVmaze) — so TVmaze's list
  14. // only brings followed series.
  15. // Left out: titles without a TMDB id, incomplete ones (shows.hl isIncomplete — the details repair completes them) and ADULT
  16. // ones nobody follows (never public, their posters are not kept — mission 056).
  17. //
  18. // THE LAST RUN: `<storage>/sync-state.txt` = the day ('YYYY-MM-DD', UTC) the last finished run STARTED (its change window began
  19. // at 00:00 of that day, so two runs overlap by a few hours, never leave a gap). FULL WALK (every followed show, as before #18)
  20. // when there is no such day (the first run), when it is more than `maxDeltaDays` (14: TMDB's change lists reach 14 days back)
  21. // ago, or when a change list cannot be read (then the day is NOT advanced: the next run asks for the missed days too).
  22. import { fetch } from 'hl:fetch'
  23. import { now } from 'hl:time'
  24. import { readFile, writeFile, exists } from 'hl:fs'
  25. import { storageDir, textOr } from './util.hl'
  26. import { showsTable, isIncomplete } from './shows.hl'
  27. import { tmdbGet, tvmazeBase, numIdOr } from './tmdb.hl'
  28. import { dayNumber } from './sync-helpers.hl'
  29. import { allFollowedShowIds } from './follows.hl'
  30. import { titleIdByTmdb, titleIdByTvmaze } from './search.hl'
  31. static maxDeltaDays = 14
  32. // TMDB answers at most 500 pages of a list
  33. static maxPages = 500
  34. static stateFile = storageDir + '/sync-state.txt'
  35. // the day of the last finished run, or null
  36. static lastSyncDay = () => {
  37. if (!exists(stateFile)) { return null }
  38. t = readFile(stateFile)
  39. if (t == null || hlTypeName(t) != 'String') { return null }
  40. d = t.trim()
  41. return dayNumber(d) != null ? d.slice(0, 10) : null
  42. }
  43. static markSynced = (&day) => {
  44. writeFile(stateFile, day, 420)
  45. return true
  46. }
  47. // ---- the plan of one run (one run at a time: the app's clock or the tool) --------------------------------------------------
  48. // flat statics written key by key (like people.hl's maps); `run.no` tells this run's entries from older ones
  49. static run = { no = 0 mode = '' why = '' since = '' until = '' tvmazeSince = 'day' phase = 'done' page = 1 pages = 1
  50. requests = 0 tvmazeRequests = 0 tvChanged = 0 movieChanged = 0 tvmazeChanged = 0 count = 0 followed = 0 failed = '' }
  51. static followedRun = {}
  52. static pickedRun = {}
  53. static pickedAt = {}
  54. static followedList = { ids = [] }
  55. // a run starts: { mode ('delta' | 'full'), why, since, days }. 'delta' → call planStep() until it answers done, then planIds().
  56. // `sinceDay` (the tool's --since) stands in for the stored last day; null = the stored one
  57. static planStart = (today, &sinceDay) => {
  58. run.no = run.no + 1
  59. run.since = ''
  60. run.until = today
  61. run.phase = 'done'
  62. run.page = 1
  63. run.pages = 1
  64. run.requests = 0
  65. run.tvmazeRequests = 0
  66. run.tvChanged = 0
  67. run.movieChanged = 0
  68. run.tvmazeChanged = 0
  69. run.count = 0
  70. run.followed = 0
  71. run.failed = ''
  72. last = sinceDay != null ? sinceDay : lastSyncDay()
  73. days = last != null && dayNumber(today) != null ? dayNumber(today) - dayNumber(last) : null
  74. if (last == null) {
  75. run.mode = 'full'
  76. run.why = 'no last run'
  77. } else if (days == null || days > maxDeltaDays || days < 0) {
  78. run.mode = 'full'
  79. run.why = 'last run ' + last + ', ' + days + ' days ago > ' + maxDeltaDays
  80. } else {
  81. run.mode = 'delta'
  82. run.why = 'since ' + last + ' (' + days + (days == 1 ? ' day' : ' days') + ')'
  83. run.since = last
  84. run.tvmazeSince = days <= 1 ? 'day' : (days <= 7 ? 'week' : 'month')
  85. run.phase = 'tv'
  86. ids = allFollowedShowIds()
  87. for (id of ids) { followedRun[id] = run.no }
  88. followedList.ids = ids
  89. }
  90. return { mode = run.mode why = run.why since = run.since days = days }
  91. }
  92. // is this title followed by anyone (as of this run's start)? — such a title gets the FULL step (all seasons + TVmaze, as
  93. // before #18), one nobody follows the LIGHT one (sync.hl syncShowDelta: changed seasons, no TVmaze)
  94. static isFollowedInRun = (&showId) => { return followedRun[showId] == run.no }
  95. // one of ours that changed → into the queue once, when the daily sync may touch it. `viaTvmaze`: found by TVmaze's list — only
  96. // a followed series counts there (TVmaze is merged for the followed ones only)
  97. static pick = (showId, &viaTvmaze) => {
  98. if (showId == null || pickedRun[showId] == run.no) { return false }
  99. s = showsTable.fetch(showId)
  100. followed = followedRun[showId] == run.no
  101. if (s == null || s.tmdbId == null || isIncomplete(s) || (s.adult == true && !followed) || (viaTvmaze && !followed)) { return false }
  102. pickedRun[showId] = run.no
  103. pickedAt['' + run.count] = showId
  104. run.count = run.count + 1
  105. if (followed) { run.followed = run.followed + 1 }
  106. return true
  107. }
  108. // ONE change-list request (TMDB tv page, TMDB movie page or TVmaze's list): { done, requests, tvmazeRequests, failed ('' = none) }
  109. static planStep = () => {
  110. out = { done = false requests = 0 tvmazeRequests = 0 failed = '' }
  111. if (run.phase == 'tv' || run.phase == 'movie') {
  112. kind = run.phase
  113. out.requests = 1
  114. run.requests = run.requests + 1
  115. let j = tmdbGet('/' + kind + '/changes?start_date=' + run.since + '&end_date=' + run.until + '&page=' + run.page)
  116. if (j.failed != null || j.results == null) {
  117. run.failed = j.failed != null ? j.failed : 'no results in /' + kind + '/changes'
  118. run.phase = 'done'
  119. out.failed = run.failed
  120. out.done = true
  121. return out
  122. }
  123. type = kind == 'tv' ? 'series' : 'movie'
  124. for (c of j.results) {
  125. if (c != null && numIdOr(c.id) != null) {
  126. if (kind == 'tv') { run.tvChanged = run.tvChanged + 1 } else { run.movieChanged = run.movieChanged + 1 }
  127. let sid = titleIdByTmdb(type, c.id)
  128. if (sid != null) { pick(sid, false) }
  129. }
  130. }
  131. run.pages = j.total_pages != null && hlTypeName(j.total_pages) == 'Number' ? j.total_pages : 1
  132. if (run.pages > maxPages) { run.pages = maxPages }
  133. if (run.page < run.pages) { run.page = run.page + 1 } else {
  134. run.phase = kind == 'tv' ? 'movie' : 'tvmaze'
  135. run.page = 1
  136. run.pages = 1
  137. }
  138. return out
  139. }
  140. if (run.phase == 'tvmaze') {
  141. out.tvmazeRequests = 1
  142. run.tvmazeRequests = run.tvmazeRequests + 1
  143. run.phase = 'done'
  144. f = fetch(tvmazeBase + '/updates/shows?since=' + run.tvmazeSince, { headers = { 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 30000 })
  145. let j = f != null && f.status == 200 ? f.json() : null
  146. if (j == null) {
  147. run.failed = 'TVmaze: ' + (f == null || f.status == null || f.status == 0 ? 'no answer' : 'HTTP ' + f.status) + ' for /updates/shows?since=' + run.tvmazeSince
  148. out.failed = run.failed
  149. out.done = true
  150. return out
  151. }
  152. for (k of j.keys()) {
  153. run.tvmazeChanged = run.tvmazeChanged + 1
  154. let sid = titleIdByTvmaze(k)
  155. if (sid != null) { pick(sid, true) }
  156. }
  157. }
  158. out.done = true
  159. return out
  160. }
  161. // the queue: the changed titles of ours, the followed ones first (in follow-table order), then the rest (in change-list order)
  162. static planIds = () => {
  163. out = []
  164. for (id of followedList.ids) { if (pickedRun[id] == run.no) { out.push(id) } }
  165. let i = 0
  166. while (i < run.count) {
  167. let id = pickedAt['' + i]
  168. if (followedRun[id] != run.no) { out.push(id) }
  169. i = i + 1
  170. }
  171. return out
  172. }
  173. // the log line of a finished plan
  174. static planLine = () => {
  175. return 'changes ' + run.why + ': tmdb tv=' + run.tvChanged + ' movie=' + run.movieChanged + ' tvmaze(' + run.tvmazeSince + ')=' + run.tvmazeChanged
  176. + ' → ' + run.count + ' of our titles (' + run.followed + ' followed), changeRequests=' + run.requests + ' tvmazeChangeRequests=' + run.tvmazeRequests
  177. + (run.failed != '' ? ' FAILED: ' + run.failed : '')
  178. }
  179. // ---- the copied summaries (tracker#18, one time; mission 070: MOVED, never deleted) -------------------------------------------
  180. // The migration copied TMDB's text into `summary` too (9,648 of 10,011 titles on the live copy, 2026-10-01): such a `summary` is
  181. // no text of the creator's — it is MOVED to `migratedSummary` (kept in the record, shown nowhere) and `summary` is emptied, so the
  182. // CURRENT tmdbSummary shows (shows.hl summaryOf). Nothing is lost: the conductor's rule (mission 070) — no step destroys migrated
  183. // data. A `summary` that differs is the creator's own and stays. ONCE: `<storage>/summaries-moved.txt` records that it ran (and
  184. // the counts). → { ran, moved, kept, errors, ms }
  185. static movedFile = storageDir + '/summaries-moved.txt'
  186. static moveCopiedSummaries = () => {
  187. out = { ran = false moved = 0 kept = 0 errors = 0 ms = 0 }
  188. if (exists(movedFile)) { return out }
  189. out.ran = true
  190. t0 = now()
  191. for (s of showsTable.find(null, null)) {
  192. if (textOr(s.summary) != null) {
  193. if (s.summary == s.tmdbSummary) {
  194. let x = s
  195. x.migratedSummary = s.summary
  196. x.summary = null
  197. if (showsTable.update(x.id, x) == null) { out.errors = out.errors + 1 } else { out.moved = out.moved + 1 }
  198. } else { out.kept = out.kept + 1 }
  199. }
  200. }
  201. showsTable.persist()
  202. out.ms = now() - t0
  203. writeFile(movedFile, 'moved=' + out.moved + ' kept=' + out.kept + ' errors=' + out.errors, 420)
  204. return out
  205. }

Branches

Latest commits

  • 27c916fatracker mission 028: README ("Code order", the new file map), STATUS (counts before/after, tests, how to repeat, open), LOGmre
  • d924f398tracker mission 028: comments name the new files (sync.hl, dailysync.hl, backfill.hl, credits.hl, jobs.hl, images.hl …); tools/ref-params.py + tools/lambda-audit.py also scan lib/ (they globbed the root only), lambda-audit counts a plain `x = p` alias like `let x = p`mre
  • 2e89b968tracker mission 028 (code order) 5/5 let: `let` only where a variable is reassigned — 667 never-reassigned lets became plain declarations (project.hl, lib/, components/, tools/, tests/); kept: 264 in loop bodies (a plain declaration there is 'Cannot reassign' on the 2nd pass), 234 reassigned, 27 whose name is also a member/outer/free name (a plain write would rebind it); tools/let-audit.py decides and fixes (README 'Code order'); tests/realdata-m028.{sh,mjs} = the page-output diff on a real copy; gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 54796ff2tracker mission 028 (code order) 4/5 thin faces + last copies: the show page's check/follow faces call lib/watches.hl toggleWatched / toggleSeasonWatched (seasonAllWatched moved there) and lib/follows.hl toggleFollowed; both logins (header selector face, /login/callback) share lib/users.hl userOfCode; todayStr/listOf copies in components and the export readers copied into tools/migrate.hl + tools/old-short-ids.hl now once (lib/util.hl, lib/export.hl); gates 342/0, 32/0, 52/0; old-short-ids output byte-identical, migrate output identicalmre
  • 06b078e3tracker mission 028 (code order) 3/5 project.hl is the map: config, routes, wiring and a feature → file index (914 → 258 lines); the background jobs (daily sync run, backfills, details repair, credits job, merge, short ids, collection seed) moved unchanged into lib/jobs.hl (a class: their state is reassigned every step, a static cannot be; one instance made after the server), the login callback into lib/users.hl, poster/photo serving into lib/images.hl, the /shows/<slug> rule into lib/shows.hl showsMovedPath; route handlers are thin wrappers; gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 94716fd2tracker mission 028 (code order) 2/5 util + topics: lib/util.hl holds envOr, storageDir, postersDir, profilesDir, newId, hexDigits, todayStr, dateOr, textOr, hasId, listOr, firstOf, sortDesc once (were copied into up to 5 files); tmdbsync.hl split into tmdb.hl (TMDB/TVmaze requests), sync.hl (one title's sync), sync-helpers.hl, backfill.hl; details.hl split into details.hl, credits.hl, credits-helpers.hl (isIncomplete to shows.hl); search-helpers.hl (words, query, ranking, slugs); collections.hl (the TMDB collection seed, out of franchises.hl); deltasync.hl renamed dailysync.hl; no behaviour change: gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 186079b0tracker mission 028 (code order) 1/5 move: every root .hl except project.hl into lib/ (styles.hl into components/), import paths only; gates 342/0, 32/0, 52/0; real-copy pages identicalmre
  • 4f47f181tracker: report 027mre
  • dc1d4be4tracker mission 027: Hybriel master 06617221 vendored (plugin allocator fixes 3a781359 + 413f60e4); real copy RSS through first-start jobs + 400 loads flat ~2.55 GB (190aa11d 2.3 -> 5.6 GB), page times <= 1.1x; gates 342/0, 32/0, 52/0mre
  • 84e1b3e1tracker: reports 025 + 026mre
  • dc40d859tracker#31 (mission 026): duplicate titles merged — the 68 type+tmdbId pairs held by 157 records were the old tracker's (all migrated); merge.hl repair job (own clock, before the TMDB jobs) keeps one keeper per title (follows/watches > old short id > oldest), moves follows, watches, seasons, cast, credits, timelines, tombstones the rest (mergedInto, never deleted), slugs + short ids 301 to the keeper; stray seasons merged into their listed twin (Reacher S3 watches) or linked when watched; search import re-checks before its put; deploy.sh waits up to 90 s for 200; real copy 68 -> 0 dup ids, az5b2 follows/watches equal; gates 342/0, 32/0, 52/0mre
  • 2667da05tracker#30 (mission 025): /my/ pages from slim cached title cards, episode rows and watch sets (after the jobs /my/series 1.8 s -> 0.06 s, /my/unwatched 4.1 -> 0.18 s); timeline page shows its name once; franchise widget under the poster/title; movies with TV leftovers (First Contact) go through the details repair; tools/count-tmdb-ids.hl; gates 327/0, 52/0, 32/0mre
  • 3909810dantcolony#40: mission references in README/STATUS/docs point to the moved missionsmre
  • e63b1d28antcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 1cda451dtracker: Hybriel master 190aa11d (#127 both shapes, GC correctness fc838894) — conductor adopts despite /my/series 2.4x after jobs (memory 8.0 → 1.7 GB boot); see reports/071mre
  • c1fa2f2etracker (mission 071): Hybriel master 190aa11d measured on the real copy vs the live binary 8590df63 — NOT adopted (after the first-start jobs /my/series 2.4x slower, /series 1.6x, RSS swings 7.4-12.2 GB; fresh it is flat at 1.7-2.2 GB and /my/unwatched faster), vendor stays 8efba065, candidate kept in .scratch/w071/vendor-190aa11d; tests/kinds.mjs: collection seed off (its TMDB request broke check 1 in 1 of 4 runs); tests/realdata-071.sh + realdata-071-bench.mjs + tools/realdata-071-table.py; README + STATUS (numbers, how to repeat); gates 325/0, 32/0, 50/0mre
  • 8081350atracker docs (mission 070): README (summary, Config HL_GC_BYTES — kept at Hybriel's default, the 256 MiB setting is taken out of docker-compose.yml again: the jobs grew to 12+ GB with it too, see STATUS), Test (three gates), Deploy (first start ~50 min: kinds then seed, restart once after collections done, memory numbers), Vendored Hybriel 8efba065 + #48 audit, Files; STATUS mission 070 entry (merges, migrated counts, lambda audit, gates, RSS old vs new, how to repeat, open points); docs/kinds.md + docs/franchises.md job order; tests/realdata-070-*.sh, tools/count-migrated.hl, tools/ref-params.py, tools/lambda-audit.pymre
  • 1ad19c8ctracker: re-vendor Hybriel master 8efba065 (#126 GC by bytes, #48 lambda parameters copy) (mission 070): bin/hybriel sha256 50361e95…, plugins core crypto data fetch fs http http1 mpackdb proc smtp time web; lambda audit: 13 lambdas change a passed record/list (11 through a local alias), no caller relies on it — unchanged; 319 read-only lambda parameters get & (no copy per call: /my/schedule 2.35 → 0.46 s, /my/unwatched 13.7 → 5.7 s on the real copy); /my/unwatched one merge sort instead of n² inserts; docker-compose HL_GC_BYTES=268435456; deploy.sh runs kinds.mjs + franchises.mjs too (default ports 8700–8710); tests/realdata-070.mjs; gates browser 325/0, kinds 32/0, franchises 50/0mre
  • 46b21389tracker (mission 070, conductor): the sync never destroys migrated data — the one-time summary step MOVES a copied summary to migratedSummary (marker summaries-moved.txt) instead of clearing it; a migrated record's first title/genres/homepage/tagline TMDB replaces → migratedTitle/migratedGenres/migratedHomepage/migratedTagline, a migrated season's/episode's title/summary → migratedTitle/migratedSummary (set once); tests/peek-shows.hl prints them; gate 325/0mre
  • 749019b0Merge t19 (tracker#19 franchises + timelines) into main (mission 070): conflicts README/STATUS/show.hl/project.hl/browser.mjs/faketmdb.mjs, both sides kept; franchise/timeline pages get #20's typed heading (Franchise | …, Timeline | …), their title links via titlePath (/movies|/series|/shows); the collection seed waits for repair, kinds and credits too; franchises.mjs URLs + 2 new checks; gates browser 323/0, kinds 32/0, franchises 50/0mre