gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita386dc92a386dc92tracker: reports 029 + 030mrea386dc92/lib/dailysync.hl

11.9 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 — tracker#35 (mission 030): and, for a series NOBODY follows, its TVmaze STATUS (the page's pill
  15. // prefers TVmaze's): named by TVmaze's list alone → a status-only step (sync.hl syncTvmazeStatus, one TVmaze request), named by
  16. // both lists → the light step asks it too. `syncRunStep` picks the step.
  17. // Left out: titles without a TMDB id, incomplete ones (shows.hl isIncomplete — the details repair completes them) and ADULT
  18. // ones nobody follows (never public, their posters are not kept — mission 056).
  19. //
  20. // THE LAST RUN: `<storage>/sync-state.txt` = the day ('YYYY-MM-DD', UTC) the last finished run STARTED (its change window began
  21. // 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)
  22. // when there is no such day (the first run), when it is more than `maxDeltaDays` (14: TMDB's change lists reach 14 days back)
  23. // ago, or when a change list cannot be read (then the day is NOT advanced: the next run asks for the missed days too).
  24. import { fetch } from 'hl:fetch'
  25. import { now } from 'hl:time'
  26. import { readFile, writeFile, exists } from 'hl:fs'
  27. import { storageDir, textOr, hasId } from './util.hl'
  28. import { showsTable, isIncomplete } from './shows.hl'
  29. import { tmdbGet, tvmazeBase, numIdOr } from './tmdb.hl'
  30. import { syncShow, syncShowDelta, syncTvmazeStatus } from './sync.hl'
  31. import { dayNumber } from './sync-helpers.hl'
  32. import { allFollowedShowIds } from './follows.hl'
  33. import { titleIdByTmdb, titleIdByTvmaze } from './search.hl'
  34. static maxDeltaDays = 14
  35. // TMDB answers at most 500 pages of a list
  36. static maxPages = 500
  37. static stateFile = storageDir + '/sync-state.txt'
  38. // the day of the last finished run, or null
  39. static lastSyncDay = () => {
  40. if (!exists(stateFile)) { return null }
  41. t = readFile(stateFile)
  42. if (t == null || hlTypeName(t) != 'String') { return null }
  43. d = t.trim()
  44. return dayNumber(d) != null ? d.slice(0, 10) : null
  45. }
  46. static markSynced = (&day) => {
  47. writeFile(stateFile, day, 420)
  48. return true
  49. }
  50. // ---- the plan of one run (one run at a time: the app's clock or the tool) --------------------------------------------------
  51. // flat statics written key by key (like people.hl's maps); `run.no` tells this run's entries from older ones
  52. static run = { no = 0 mode = '' why = '' since = '' until = '' tvmazeSince = 'day' phase = 'done' page = 1 pages = 1
  53. requests = 0 tvmazeRequests = 0 tvChanged = 0 movieChanged = 0 tvmazeChanged = 0 count = 0 followed = 0 statusOnly = 0 failed = '' }
  54. static followedRun = {}
  55. static pickedRun = {}
  56. static pickedAt = {}
  57. // tracker#35: named by TVmaze's change list (this run) · picked for its TVmaze status alone
  58. static mazeRun = {}
  59. static statusOnlyRun = {}
  60. static followedList = { ids = [] }
  61. // a run starts: { mode ('delta' | 'full'), why, since, days }. 'delta' → call planStep() until it answers done, then planIds().
  62. // `sinceDay` (the tool's --since) stands in for the stored last day; null = the stored one
  63. static planStart = (today, &sinceDay) => {
  64. run.no = run.no + 1
  65. run.since = ''
  66. run.until = today
  67. run.phase = 'done'
  68. run.page = 1
  69. run.pages = 1
  70. run.requests = 0
  71. run.tvmazeRequests = 0
  72. run.tvChanged = 0
  73. run.movieChanged = 0
  74. run.tvmazeChanged = 0
  75. run.count = 0
  76. run.followed = 0
  77. run.statusOnly = 0
  78. run.failed = ''
  79. last = sinceDay != null ? sinceDay : lastSyncDay()
  80. days = last != null && dayNumber(today) != null ? dayNumber(today) - dayNumber(last) : null
  81. if (last == null) {
  82. run.mode = 'full'
  83. run.why = 'no last run'
  84. } else if (days == null || days > maxDeltaDays || days < 0) {
  85. run.mode = 'full'
  86. run.why = 'last run ' + last + ', ' + days + ' days ago > ' + maxDeltaDays
  87. } else {
  88. run.mode = 'delta'
  89. run.why = 'since ' + last + ' (' + days + (days == 1 ? ' day' : ' days') + ')'
  90. run.since = last
  91. run.tvmazeSince = days <= 1 ? 'day' : (days <= 7 ? 'week' : 'month')
  92. run.phase = 'tv'
  93. ids = allFollowedShowIds()
  94. for (id of ids) { followedRun[id] = run.no }
  95. followedList.ids = ids
  96. }
  97. return { mode = run.mode why = run.why since = run.since days = days }
  98. }
  99. // is this title followed by anyone (as of this run's start)? — such a title gets the FULL step (all seasons + TVmaze, as
  100. // before #18), one nobody follows the LIGHT one (sync.hl syncShowDelta: changed seasons, no TVmaze)
  101. static isFollowedInRun = (&showId) => { return followedRun[showId] == run.no }
  102. // one of ours that changed → into the queue once, when the daily sync may touch it. `viaTvmaze`: found by TVmaze's list — only
  103. // a followed series counts there (TVmaze is merged for the followed ones only)
  104. static pick = (showId, &viaTvmaze) => {
  105. if (showId == null || pickedRun[showId] == run.no) { return false }
  106. s = showsTable.fetch(showId)
  107. followed = followedRun[showId] == run.no
  108. if (s == null || s.tmdbId == null || isIncomplete(s) || (s.adult == true && !followed) || (viaTvmaze && !followed)) { return false }
  109. pickedRun[showId] = run.no
  110. pickedAt['' + run.count] = showId
  111. run.count = run.count + 1
  112. if (followed) { run.followed = run.followed + 1 }
  113. return true
  114. }
  115. // tracker#35: a series nobody follows, only on TVmaze's list → into the queue for its TVmaze status alone (one picked by TMDB's
  116. // list already gets it with its light step)
  117. static pickStatus = (showId) => {
  118. if (showId == null || pickedRun[showId] == run.no || followedRun[showId] == run.no) { return false }
  119. s = showsTable.fetch(showId)
  120. if (s == null || s.type != 'series' || !hasId(s.tvmzId) || s.adult == true || s.mergedInto != null || isIncomplete(s)) { return false }
  121. pickedRun[showId] = run.no
  122. statusOnlyRun[showId] = run.no
  123. pickedAt['' + run.count] = showId
  124. run.count = run.count + 1
  125. run.statusOnly = run.statusOnly + 1
  126. return true
  127. }
  128. // ONE title of the run (jobs.hl syncTick, tools/sync-tmdb.hl): a full walk or a followed title → the full step; a series only on
  129. // TVmaze's list → its TVmaze status; any other → the light step (+ the TVmaze status when TVmaze's list named it too)
  130. static syncRunStep = (&showId, &delta) => {
  131. if (!delta || followedRun[showId] == run.no) { return syncShow(showId) }
  132. if (statusOnlyRun[showId] == run.no) { return syncTvmazeStatus(showId) }
  133. return syncShowDelta(showId, mazeRun[showId] == run.no)
  134. }
  135. static isStatusOnlyInRun = (&showId) => { return statusOnlyRun[showId] == run.no }
  136. // ONE change-list request (TMDB tv page, TMDB movie page or TVmaze's list): { done, requests, tvmazeRequests, failed ('' = none) }
  137. static planStep = () => {
  138. out = { done = false requests = 0 tvmazeRequests = 0 failed = '' }
  139. if (run.phase == 'tv' || run.phase == 'movie') {
  140. kind = run.phase
  141. out.requests = 1
  142. run.requests = run.requests + 1
  143. let j = tmdbGet('/' + kind + '/changes?start_date=' + run.since + '&end_date=' + run.until + '&page=' + run.page)
  144. if (j.failed != null || j.results == null) {
  145. run.failed = j.failed != null ? j.failed : 'no results in /' + kind + '/changes'
  146. run.phase = 'done'
  147. out.failed = run.failed
  148. out.done = true
  149. return out
  150. }
  151. type = kind == 'tv' ? 'series' : 'movie'
  152. for (c of j.results) {
  153. if (c != null && numIdOr(c.id) != null) {
  154. if (kind == 'tv') { run.tvChanged = run.tvChanged + 1 } else { run.movieChanged = run.movieChanged + 1 }
  155. let sid = titleIdByTmdb(type, c.id)
  156. if (sid != null) { pick(sid, false) }
  157. }
  158. }
  159. run.pages = j.total_pages != null && hlTypeName(j.total_pages) == 'Number' ? j.total_pages : 1
  160. if (run.pages > maxPages) { run.pages = maxPages }
  161. if (run.page < run.pages) { run.page = run.page + 1 } else {
  162. run.phase = kind == 'tv' ? 'movie' : 'tvmaze'
  163. run.page = 1
  164. run.pages = 1
  165. }
  166. return out
  167. }
  168. if (run.phase == 'tvmaze') {
  169. out.tvmazeRequests = 1
  170. run.tvmazeRequests = run.tvmazeRequests + 1
  171. run.phase = 'done'
  172. f = fetch(tvmazeBase + '/updates/shows?since=' + run.tvmazeSince, { headers = { 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 30000 })
  173. let j = f != null && f.status == 200 ? f.json() : null
  174. if (j == null) {
  175. run.failed = 'TVmaze: ' + (f == null || f.status == null || f.status == 0 ? 'no answer' : 'HTTP ' + f.status) + ' for /updates/shows?since=' + run.tvmazeSince
  176. out.failed = run.failed
  177. out.done = true
  178. return out
  179. }
  180. for (k of j.keys()) {
  181. run.tvmazeChanged = run.tvmazeChanged + 1
  182. let sid = titleIdByTvmaze(k)
  183. if (sid != null) {
  184. mazeRun[sid] = run.no
  185. if (!pick(sid, true)) { pickStatus(sid) }
  186. }
  187. }
  188. }
  189. out.done = true
  190. return out
  191. }
  192. // the queue: the changed titles of ours, the followed ones first (in follow-table order), then the rest (in change-list order)
  193. static planIds = () => {
  194. out = []
  195. for (id of followedList.ids) { if (pickedRun[id] == run.no) { out.push(id) } }
  196. let i = 0
  197. while (i < run.count) {
  198. let id = pickedAt['' + i]
  199. if (followedRun[id] != run.no) { out.push(id) }
  200. i = i + 1
  201. }
  202. return out
  203. }
  204. // the log line of a finished plan
  205. static planLine = () => {
  206. return 'changes ' + run.why + ': tmdb tv=' + run.tvChanged + ' movie=' + run.movieChanged + ' tvmaze(' + run.tvmazeSince + ')=' + run.tvmazeChanged
  207. + ' → ' + run.count + ' of our titles (' + run.followed + ' followed' + (run.statusOnly > 0 ? ', ' + run.statusOnly + ' TVmaze status only' : '') + '), changeRequests=' + run.requests + ' tvmazeChangeRequests=' + run.tvmazeRequests
  208. + (run.failed != '' ? ' FAILED: ' + run.failed : '')
  209. }
  210. // ---- the copied summaries (tracker#18, one time; mission 070: MOVED, never deleted) -------------------------------------------
  211. // 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
  212. // no text of the creator's — it is MOVED to `migratedSummary` (kept in the record, shown nowhere) and `summary` is emptied, so the
  213. // CURRENT tmdbSummary shows (shows.hl summaryOf). Nothing is lost: the conductor's rule (mission 070) — no step destroys migrated
  214. // data. A `summary` that differs is the creator's own and stays. ONCE: `<storage>/summaries-moved.txt` records that it ran (and
  215. // the counts). → { ran, moved, kept, errors, ms }
  216. static movedFile = storageDir + '/summaries-moved.txt'
  217. static moveCopiedSummaries = () => {
  218. out = { ran = false moved = 0 kept = 0 errors = 0 ms = 0 }
  219. if (exists(movedFile)) { return out }
  220. out.ran = true
  221. t0 = now()
  222. for (s of showsTable.find(null, null)) {
  223. if (textOr(s.summary) != null) {
  224. if (s.summary == s.tmdbSummary) {
  225. let x = s
  226. x.migratedSummary = s.summary
  227. x.summary = null
  228. if (showsTable.update(x.id, x) == null) { out.errors = out.errors + 1 } else { out.moved = out.moved + 1 }
  229. } else { out.kept = out.kept + 1 }
  230. }
  231. }
  232. showsTable.persist()
  233. out.ms = now() - t0
  234. writeFile(movedFile, 'moved=' + out.moved + ' kept=' + out.kept + ' errors=' + out.errors, 420)
  235. return out
  236. }

Branches

Latest commits

  • a386dc92tracker: reports 029 + 030mre
  • 71e0fd7dtracker missions 029 + 030: README (What it does, Files, gate count), STATUS (real-copy numbers, how to repeat, open points), LOGmre
  • d36ea6eatracker#34 + #35 (mission 030): Follow directly under the poster, as wide as the poster (show.hl, styles.hl); the status pill next to a series' title — TVmaze's status (new tvmazeStatus, stored by the sync's TVmaze merge) else TMDB's, TVmaze Ended + TMDB Canceled = Canceled, inverted (filled, dark text, no border), green running / yellow pending / red canceled / muted ended (shows.hl statusOf); the daily delta asks TVmaze's status of an unfollowed series TVmaze's change list names (dailysync.hl syncRunStep, sync.hl syncTvmazeStatus); the status backfill after the details repair (backfill.hl, jobs.hl statusTick; resumable, 550 ms per TVmaze request); gates 354/0, 32/0, 52/0mre
  • 7d7d4487tracker#33 (mission 029): reduced titles — every title TMDB's details never went through this app (no detailsAt, no tmdbSync) is incomplete (shows.hl isIncomplete; the old tracker's migrated rows passed #26's test: 5,697 non-adult on the live copy, 691 series without seasons); the repair job does the visibly reduced first (shows.hl missingParts), the page completes one on open; a title TMDB has no poster for (The Remaining) shows the placeholder; tools/count-incomplete.hl; gate fixtures stand for synced titles (tmdbSync), tests/seed-reduced.hl + #33 checks; gates 347/0, 32/0, 52/0mre
  • 661c2592tracker: report 028mre
  • 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