tracker
All repositories: gitoria
11.9 KB
// lib/dailysync.hl — WHICH TITLES THE DAILY SYNC ASKS FOR (tracker.worldapi.org#18; deltasync.hl before mission 028). Creator:// "it checks every show in a request to the api? cause they have an endpoint that gives a delta on bulk". Before: 2 requests// per FOLLOWED show every day (256 for 126 shows), the other titles never. Now the daily run (jobs.hl syncTick,// tools/sync-tmdb.hl --delta) first asks what changed since the last run and syncs ONLY those titles of ours — followed or not://// GET <TMDB_BASE_URL>/tv/changes?start_date=<last run day>&end_date=<today>&page=<n> changed TV ids, 100 per page// GET <TMDB_BASE_URL>/movie/changes?start_date=…&end_date=…&page=<n> changed movie ids, 100 per page// GET <TVMAZE_BASE_URL>/updates/shows?since=day|week|month { "<tvmaze id>": <updated at> }//// → intersected with our titles (search.hl's index: TMDB id by kind, TVmaze id — no table scan) → the sync's queue, FOLLOWED// titles first. One change-list request per clock step (jobs.hl), 260 ms per TMDB / 550 ms per TVmaze request between them.// A FOLLOWED title gets the full step (all seasons, TVmaze — as before #18); one NOBODY follows the light step (sync.hl// syncShowDelta: details, ids, poster, record, cast + only the seasons that can have changed, no TVmaze) — so TVmaze's list// only brings followed series — tracker#35 (mission 030): and, for a series NOBODY follows, its TVmaze STATUS (the page's pill// prefers TVmaze's): named by TVmaze's list alone → a status-only step (sync.hl syncTvmazeStatus, one TVmaze request), named by// both lists → the light step asks it too. `syncRunStep` picks the step.// Left out: titles without a TMDB id, incomplete ones (shows.hl isIncomplete — the details repair completes them) and ADULT// ones nobody follows (never public, their posters are not kept — mission 056).//// THE LAST RUN: `<storage>/sync-state.txt` = the day ('YYYY-MM-DD', UTC) the last finished run STARTED (its change window began// 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)// when there is no such day (the first run), when it is more than `maxDeltaDays` (14: TMDB's change lists reach 14 days back)// ago, or when a change list cannot be read (then the day is NOT advanced: the next run asks for the missed days too).import { fetch } from 'hl:fetch'import { now } from 'hl:time'import { readFile, writeFile, exists } from 'hl:fs'import { storageDir, textOr, hasId } from './util.hl'import { showsTable, isIncomplete } from './shows.hl'import { tmdbGet, tvmazeBase, numIdOr } from './tmdb.hl'import { syncShow, syncShowDelta, syncTvmazeStatus } from './sync.hl'import { dayNumber } from './sync-helpers.hl'import { allFollowedShowIds } from './follows.hl'import { titleIdByTmdb, titleIdByTvmaze } from './search.hl'static maxDeltaDays = 14// TMDB answers at most 500 pages of a liststatic maxPages = 500static stateFile = storageDir + '/sync-state.txt'// the day of the last finished run, or nullstatic lastSyncDay = () => {if (!exists(stateFile)) { return null }t = readFile(stateFile)if (t == null || hlTypeName(t) != 'String') { return null }d = t.trim()return dayNumber(d) != null ? d.slice(0, 10) : null}static markSynced = (&day) => {writeFile(stateFile, day, 420)return true}// ---- the plan of one run (one run at a time: the app's clock or the tool) --------------------------------------------------// flat statics written key by key (like people.hl's maps); `run.no` tells this run's entries from older onesstatic run = { no = 0 mode = '' why = '' since = '' until = '' tvmazeSince = 'day' phase = 'done' page = 1 pages = 1requests = 0 tvmazeRequests = 0 tvChanged = 0 movieChanged = 0 tvmazeChanged = 0 count = 0 followed = 0 statusOnly = 0 failed = '' }static followedRun = {}static pickedRun = {}static pickedAt = {}// tracker#35: named by TVmaze's change list (this run) · picked for its TVmaze status alonestatic mazeRun = {}static statusOnlyRun = {}static followedList = { ids = [] }// a run starts: { mode ('delta' | 'full'), why, since, days }. 'delta' → call planStep() until it answers done, then planIds().// `sinceDay` (the tool's --since) stands in for the stored last day; null = the stored onestatic planStart = (today, &sinceDay) => {run.no = run.no + 1run.since = ''run.until = todayrun.phase = 'done'run.page = 1run.pages = 1run.requests = 0run.tvmazeRequests = 0run.tvChanged = 0run.movieChanged = 0run.tvmazeChanged = 0run.count = 0run.followed = 0run.statusOnly = 0run.failed = ''last = sinceDay != null ? sinceDay : lastSyncDay()days = last != null && dayNumber(today) != null ? dayNumber(today) - dayNumber(last) : nullif (last == null) {run.mode = 'full'run.why = 'no last run'} else if (days == null || days > maxDeltaDays || days < 0) {run.mode = 'full'run.why = 'last run ' + last + ', ' + days + ' days ago > ' + maxDeltaDays} else {run.mode = 'delta'run.why = 'since ' + last + ' (' + days + (days == 1 ? ' day' : ' days') + ')'run.since = lastrun.tvmazeSince = days <= 1 ? 'day' : (days <= 7 ? 'week' : 'month')run.phase = 'tv'ids = allFollowedShowIds()for (id of ids) { followedRun[id] = run.no }followedList.ids = ids}return { mode = run.mode why = run.why since = run.since days = days }}// is this title followed by anyone (as of this run's start)? — such a title gets the FULL step (all seasons + TVmaze, as// before #18), one nobody follows the LIGHT one (sync.hl syncShowDelta: changed seasons, no TVmaze)static isFollowedInRun = (&showId) => { return followedRun[showId] == run.no }// one of ours that changed → into the queue once, when the daily sync may touch it. `viaTvmaze`: found by TVmaze's list — only// a followed series counts there (TVmaze is merged for the followed ones only)static pick = (showId, &viaTvmaze) => {if (showId == null || pickedRun[showId] == run.no) { return false }s = showsTable.fetch(showId)followed = followedRun[showId] == run.noif (s == null || s.tmdbId == null || isIncomplete(s) || (s.adult == true && !followed) || (viaTvmaze && !followed)) { return false }pickedRun[showId] = run.nopickedAt['' + run.count] = showIdrun.count = run.count + 1if (followed) { run.followed = run.followed + 1 }return true}// tracker#35: a series nobody follows, only on TVmaze's list → into the queue for its TVmaze status alone (one picked by TMDB's// list already gets it with its light step)static pickStatus = (showId) => {if (showId == null || pickedRun[showId] == run.no || followedRun[showId] == run.no) { return false }s = showsTable.fetch(showId)if (s == null || s.type != 'series' || !hasId(s.tvmzId) || s.adult == true || s.mergedInto != null || isIncomplete(s)) { return false }pickedRun[showId] = run.nostatusOnlyRun[showId] = run.nopickedAt['' + run.count] = showIdrun.count = run.count + 1run.statusOnly = run.statusOnly + 1return true}// 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// TVmaze's list → its TVmaze status; any other → the light step (+ the TVmaze status when TVmaze's list named it too)static syncRunStep = (&showId, &delta) => {if (!delta || followedRun[showId] == run.no) { return syncShow(showId) }if (statusOnlyRun[showId] == run.no) { return syncTvmazeStatus(showId) }return syncShowDelta(showId, mazeRun[showId] == run.no)}static isStatusOnlyInRun = (&showId) => { return statusOnlyRun[showId] == run.no }// ONE change-list request (TMDB tv page, TMDB movie page or TVmaze's list): { done, requests, tvmazeRequests, failed ('' = none) }static planStep = () => {out = { done = false requests = 0 tvmazeRequests = 0 failed = '' }if (run.phase == 'tv' || run.phase == 'movie') {kind = run.phaseout.requests = 1run.requests = run.requests + 1let j = tmdbGet('/' + kind + '/changes?start_date=' + run.since + '&end_date=' + run.until + '&page=' + run.page)if (j.failed != null || j.results == null) {run.failed = j.failed != null ? j.failed : 'no results in /' + kind + '/changes'run.phase = 'done'out.failed = run.failedout.done = truereturn out}type = kind == 'tv' ? 'series' : 'movie'for (c of j.results) {if (c != null && numIdOr(c.id) != null) {if (kind == 'tv') { run.tvChanged = run.tvChanged + 1 } else { run.movieChanged = run.movieChanged + 1 }let sid = titleIdByTmdb(type, c.id)if (sid != null) { pick(sid, false) }}}run.pages = j.total_pages != null && hlTypeName(j.total_pages) == 'Number' ? j.total_pages : 1if (run.pages > maxPages) { run.pages = maxPages }if (run.page < run.pages) { run.page = run.page + 1 } else {run.phase = kind == 'tv' ? 'movie' : 'tvmaze'run.page = 1run.pages = 1}return out}if (run.phase == 'tvmaze') {out.tvmazeRequests = 1run.tvmazeRequests = run.tvmazeRequests + 1run.phase = 'done'f = fetch(tvmazeBase + '/updates/shows?since=' + run.tvmazeSince, { headers = { 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 30000 })let j = f != null && f.status == 200 ? f.json() : nullif (j == null) {run.failed = 'TVmaze: ' + (f == null || f.status == null || f.status == 0 ? 'no answer' : 'HTTP ' + f.status) + ' for /updates/shows?since=' + run.tvmazeSinceout.failed = run.failedout.done = truereturn out}for (k of j.keys()) {run.tvmazeChanged = run.tvmazeChanged + 1let sid = titleIdByTvmaze(k)if (sid != null) {mazeRun[sid] = run.noif (!pick(sid, true)) { pickStatus(sid) }}}}out.done = truereturn out}// the queue: the changed titles of ours, the followed ones first (in follow-table order), then the rest (in change-list order)static planIds = () => {out = []for (id of followedList.ids) { if (pickedRun[id] == run.no) { out.push(id) } }let i = 0while (i < run.count) {let id = pickedAt['' + i]if (followedRun[id] != run.no) { out.push(id) }i = i + 1}return out}// the log line of a finished planstatic planLine = () => {return 'changes ' + run.why + ': tmdb tv=' + run.tvChanged + ' movie=' + run.movieChanged + ' tvmaze(' + run.tvmazeSince + ')=' + run.tvmazeChanged+ ' → ' + run.count + ' of our titles (' + run.followed + ' followed' + (run.statusOnly > 0 ? ', ' + run.statusOnly + ' TVmaze status only' : '') + '), changeRequests=' + run.requests + ' tvmazeChangeRequests=' + run.tvmazeRequests+ (run.failed != '' ? ' FAILED: ' + run.failed : '')}// ---- the copied summaries (tracker#18, one time; mission 070: MOVED, never deleted) -------------------------------------------// 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// no text of the creator's — it is MOVED to `migratedSummary` (kept in the record, shown nowhere) and `summary` is emptied, so the// CURRENT tmdbSummary shows (shows.hl summaryOf). Nothing is lost: the conductor's rule (mission 070) — no step destroys migrated// data. A `summary` that differs is the creator's own and stays. ONCE: `<storage>/summaries-moved.txt` records that it ran (and// the counts). → { ran, moved, kept, errors, ms }static movedFile = storageDir + '/summaries-moved.txt'static moveCopiedSummaries = () => {out = { ran = false moved = 0 kept = 0 errors = 0 ms = 0 }if (exists(movedFile)) { return out }out.ran = truet0 = now()for (s of showsTable.find(null, null)) {if (textOr(s.summary) != null) {if (s.summary == s.tmdbSummary) {let x = sx.migratedSummary = s.summaryx.summary = nullif (showsTable.update(x.id, x) == null) { out.errors = out.errors + 1 } else { out.moved = out.moved + 1 }} else { out.kept = out.kept + 1 }}}showsTable.persist()out.ms = now() - t0writeFile(movedFile, 'moved=' + out.moved + ' kept=' + out.kept + ' errors=' + out.errors, 420)return out}
Branches
Latest commits
- 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
- 5bd14e12mission 040: theme 76f99f6 gates + E2E green; 44 px fixes (skeleton row, type-word, genre-link/pill); README, reportmre
- 77cfa45cmission 040: partial report (theme/modal/pagination re-copied, tilelist onPage, 44 px taps; gates new + E2E pending)mre
- 4b14822fe2e-theme: menu marker by colourmre
- 09780f4emission 040 WIP: theme, modal, pagination, tokens re-copied from layouts 76f99f6; tilelist uses onPage; tap sizes 44 px for tracker's own small buttons; tests/e2e-theme.mjsmre
- fb80bb8chotfix: open the 5 show stores without compaction (compact = false) — compact-on-open left another handle on stale offsets, every read gave a bare Number (hybriel mpackdb refresh after a shrink); live down 2026-10-20 05:11mre
- e00d3bf7paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
- a8c7f234tracker#36 (mission 039): tools/tmdb-check.hl survives a dropped connection (on Error, asked again, failed lines) and resumes with --from N; tests/tmdbcheck.mjs 6/6 against a fake TMDBmre
- 9c230516mission brief w39mre
- 3609121dmission 038 docs + report (quota stop): README/kinds.md for #43 #42 #36, STATUS, LOG; HEAD browser gate 374/8 in the in-app daily run block (hour-boundary suspected, rerun pending), other gates green; #36 real run needs TMDB_READ_TOKENmre
- a5d06e79tracker#36 (mission 038): tools/tmdb-check.hl + lib/tmdbcheck.hl — every live migrated title's migrated title + year against its TMDB entry (one GET /movie|tv/<id>?append_to_response=alternative_titles, 260 ms apart, passing failures asked once more): ok / ambiguous (one of title, year disagrees) / clear (both, or 404); read-only; token only from TMDB_READ_TOKEN; --stored = a title-only preview from the TMDB names already stored (no request). Tested against a fake TMDB (.scratch/w38/t36): 7 titles, 3 ok, 2 ambiguous, 2 clear, data unchangedmre
- 982554f3tracker#42 (mission 038): series pages — an unknown slug no longer scans every title (lib/shows.hl showBySlug: the slug map only; new titles are noted by refreshTitleCaches, which both title puts already reach): 10.7–15.7 s → 0.013 s on the real copy (the one-request-at-a-time server was blocked meanwhile); cast/crew rows take name + slug from a person-link cache (personLinkOf) instead of whole person records (SNL: 303 cast with 4,761 credits, ~49k allocations per render) — allocations per series page −5…−40 %; HTML of 20 pages identical to beforemre
- 4586b6e3docker-compose: init: true (hybriel#156)mre
- 085c2e5ftracker#43 (mission 038): TVmaze's type makes Variety / Talk Show / Game Show / Reality / News / Panel Show / Award Show / Sports titles Shows (TMDB calls Saturday Night Live UK Scripted) — the #43 part of parked w37-wip a79c8c1 only (no re-vendor, no cache volume): tvmazeType stored by the sync and the TVmaze status backfill (asks every series with a TVmaze id once), kindOfTitle/kindWithTvmaze, tools/tvmaze-types.hl, gate fixture Variety Gate Series; start waits 150 smre
- 4fa303b5mission brief w38mre
- 975f6843tracker#41 (mission 037b): no request tap — an old /movies|/series slug renders the keeper's page (slug map) with og:url naming the keeper's address, /shows/ 301; no moved slug in any page's HTML. Real copy 168/168 old addresses, 0 moved slugs in 54 pages, 300x + 1000x stress without a crash; gates on 06617221 380/0, 32/0, 53/0, 229/0, 26/0, theme 0mre
- 6decff4ftracker#41 (mission 037): moved slugs no longer listed in every page's HTML — a request tap answers their 301 by a slug-map lookup (shows.hl movedTitlePath); catalog.hl buildIndex pushes genre lists in place (the quadratic copy hung the boot > 20 min on today's data); gate: no moved slug in 19 page kinds signed out/in, immediate 301 after a merge. Gates on hybriel 06617221 green (380/0, 32/0, 53/0, 229/0, 26/0, theme 0); real copy 84/84 301, 0 moved slugs in 54 pages. NOT deployable: the tap faults after ~38 answers on 06617221 (report 037).mre
- 35f7a872mission brief 037mre
- 5cbcddd7tracker mission 036: recheck of the 16 review tickets — 15 WORKS, #39 PARTLY; review packages + bugs in reports/036-review-recheck.md (no code change)mre