tracker
All repositories: gitoria
8.0 KB
// shortids.hl — SHORT IDS (tracker.worldapi.org#27, mission 060): every movie, series and person has its own 5-character id// (`shortId`, like `a9s9a`), shown below its poster/photo; `/<shortId>` answers 301 to its page. Creator: "i wanted to give all// our movies, shows, series and persons own short ids like a9s9a … those ids show below the poster."//// OLD IDS: the old tracker had them on 702 of 9,453 shows (Show.jsonl field `id`, base62 counter '100jh'…'1012y', all of// them [a-z0-9]{5}). `data/old-short-ids.json` (made once by tools/old-short-ids.hl) maps the old Mongo `_id` (= our// `oldId`, tools/migrate.hl) → that id; such a show gets it back. Persons never had one.// NEW IDS: 5 random characters [a-z0-9] (36^5 = 60 M) — never one that is taken or one of the old ids (all 702 are// reserved, even where the show is gone).// UNIQUE ACROSS SHOWS AND PERSONS: one in-memory map `owners` (short id → { kind, id }) over BOTH tables — an mpackdb index// is per table and cannot span two. Built at start by `ensureShortIds` (one scan of each table); every id handed// out goes through it. A record whose short id another record already owns (never seen; logged) gets a new one.// NEW RECORDS get one when they are created: `claimShortId(kind, id, oldId)` in search.hl importTitle and people.hl// addMinimalTitle (the only places the app creates shows) and people.hl castPersonId (mission 062: the only place// the app creates persons — the cast/crew of #26/#28: completion, credits job, daily sync + tools/sync-tmdb.hl,// search import; and a guest star opened for the first time, people.hl guestPersonId).// THE BACKFILL: the records without one (at first every row: 9,453 shows + 15,157 persons) are given theirs in the// background, a time-boxed batch per clock tick (project.hl `shortIdTick` → `shortIdStep`), so pages are served// between two batches; after a restart it goes on with what is still missing. Real data: STATUS.md mission 060.//// ONE PROCESS PER STORAGE (hybriel#21): the map lives in the app's process; the tables are shows.hl's.import { now } from 'hl:time'import { env } from 'hl:proc'import { readFile, exists } from 'hl:fs'import { randomBytes } from 'hl:crypto'import { showsTable, personsTable } from './shows.hl'static shortIdChars = 'abcdefghijklmnopqrstuvwxyz0123456789'static hexDigits = '0123456789abcdef'// short id → { kind ('show' | 'person'), id }static owners = {}// the old tracker's ids: oldId → short id, and every one of them → true (reserved)static oldShortIds = {}static reserved = {}// the records still without a short id, [{ kind, id }], worked off from `state.queuePos`. Filled in place (push) and never// reassigned (a static cannot be) — kept OUT of `state`: reading `state.queue[i]` copied the whole 25k list on every access// (measured: ~3 ms per record instead of ~0.2)static queue = []static state = { built = false queuePos = 0 oldLoaded = 0 scanned = 0 existing = 0 duplicates = 0 ms = 0 assigned = 0 oldKept = 0 }// the file of old ids: TRACKER_OLD_SHORT_IDS, else data/old-short-ids.json beside project.hl (relative paths resolve against the// ENTRY script: a tool in tools/ — sync-tmdb.hl creates people since #28, mission 062 — finds it one folder up)static oldFile = () => {let f = env('TRACKER_OLD_SHORT_IDS')if (f != null && f.trim() != '') { return f.trim() }return !exists('./data/old-short-ids.json') && exists('../data/old-short-ids.json') ? '../data/old-short-ids.json' : './data/old-short-ids.json'}// is this a short id at all (5 characters, a–z, 0–9)?static isShortId = (s) => {if (s == null || hlTypeName(s) != 'String' || s.length != 5) { return false }let i = 0while (i < 5) {if (!shortIdChars.includes(s[i])) { return false }i = i + 1}return true}// 5 random characters a–z0–9, not taken and not an old one. Bytes ≥ 252 are skipped (252 = 7 × 36), so every character// is equally likely.static randomShortId = () => {while (true) {let hex = randomBytes(16, 'hex')let out = ''let i = 0while (i < hex.length && out.length < 5) {let b = hexDigits.indexOf(hex[i]) * 16 + hexDigits.indexOf(hex[i + 1])if (b < 252) { out = out + shortIdChars[b % 36] }i = i + 2}if (out.length == 5 && owners[out] == null && reserved[out] != true) { return out }}}// the short id for a record that has none: its old one when the old tracker had one (and nobody holds it), else a new// one — taken in `owners` at oncestatic claimShortId = (kind, id, oldId) => {ensureShortIds()let sid = oldId != null ? oldShortIds['' + oldId] : nullif (sid != null && owners[sid] == null) { state.oldKept = state.oldKept + 1 } else { sid = randomShortId() }owners[sid] = { kind = kind id = id }return sid}// does this record own its short id? (null / malformed / held by another record → no)static ownsShortId = (kind, r) => {if (!isShortId(r.shortId)) { return false }let o = owners[r.shortId]return o != null && o.kind == kind && o.id == r.id}// one table's rows into `owners`; the rows without a (valid, own) short id into the queuestatic scanTable = (kind, table) => {for (r of table.find(null, null)) {state.scanned = state.scanned + 1if (isShortId(r.shortId) && owners[r.shortId] == null) {owners[r.shortId] = { kind = kind id = r.id }state.existing = state.existing + 1} else {if (r.shortId != null) {state.duplicates = state.duplicates + 1console.log('short ids: ' + kind + ' ' + r.id + ' has "' + r.shortId + '", ' + (isShortId(r.shortId) ? 'taken by ' + owners[r.shortId].kind + ' ' + owners[r.shortId].id : 'not a short id') + ' — gets a new one')}queue.push({ kind = kind id = r.id })}}return true}// at start (project.hl): the old ids, the map of every short id in use, the queue of records without onestatic ensureShortIds = () => {if (state.built) { return true }state.built = truelet t0 = now()let f = oldFile()if (exists(f)) {let m = JSON.parse(readFile(f))let n = 0for (k of m.keys()) {let sid = m[k]if (isShortId(sid)) { oldShortIds[k] = sid reserved[sid] = true n = n + 1 }}state.oldLoaded = n} else { console.log('short ids: no old ids (' + f + ' missing)') }scanTable('show', showsTable)scanTable('person', personsTable)state.ms = now() - t0console.log('short ids: ' + state.scanned + ' records, ' + state.existing + ' with a short id, ' + queue.length + ' without, ' + state.oldLoaded + ' old ids, ' + state.ms + ' ms')return true}static tableOf = (kind) => { return kind == 'person' ? personsTable : showsTable }// the backfill, one batch: records from the queue until `budgetMs` is used up → { done, assigned, left }static shortIdStep = (budgetMs) => {ensureShortIds()let t0 = now()let n = 0while (state.queuePos < queue.length && now() - t0 < budgetMs) {let item = queue[state.queuePos]state.queuePos = state.queuePos + 1let table = tableOf(item.kind)let r = table.fetch(item.id)if (r != null && !ownsShortId(item.kind, r)) {let x = rx.shortId = claimShortId(item.kind, x.id, x.oldId)if (table.update(x.id, x) == null) { console.log('short ids: ' + item.kind + ' ' + x.id + ' not stored: ' + table.lastError()) } else { n = n + 1 }}}state.assigned = state.assigned + nlet done = state.queuePos >= queue.lengthreturn { done = done assigned = n left = queue.length - state.queuePos }}static shortIdStats = () => { return state }static persistShortIds = () => {showsTable.persist()personsTable.persist()return true}static shortIdsLeft = () => { return queue.length - state.queuePos }// `/<shortId>` (project.hl): the page's path, or nullstatic shortIdPath = (sid) => {if (!isShortId(sid)) { return null }ensureShortIds()let o = owners[sid]if (o == null) { return null }let r = tableOf(o.kind).fetch(o.id)if (r == null || r.urlSegment == null) { return null }return (o.kind == 'person' ? '/person/' : '/shows/') + r.urlSegment}
Branches
- mainmain branch
Latest commits
- 20e09d89Merge t18 (tracker#18 delta sync) into main (mission 070): conflicts README/STATUS/show.hl/project.hl/browser.mjs, both sides kept; pageShowOf summary = summaryOfmre
- f83571c3tracker#18 (mission 067): daily sync by change lists — TMDB /tv|movie/changes (since the stored day, paged) + TVmaze /updates/shows → only our changed titles (followed: full step, unfollowed: light step — changed seasons, no TVmaze), full walk on first run / gap > 14 days / failed list; show record refreshed (title, tmdbSummary, tagline, status, genres …; renamed titles re-indexed); summary = the creator's own text (page: summary > tmdbSummary > tvmazeSummary), one-time clear of copied summaries (9,647 on the live copy); gate 261, tests/realdata-018*.mjs, README + STATUSmre
- f2b00674Merge t26 (tracker#26 + #28) into main (mission 062): short ids for every new person (castPersonId, guest route), guest stars stored on the title and created as people only when opened (/person/tmdb/<id>?show=<id> → 302), lean watch/follow clicks (showRow a small object, cast/crew from the slug, watches cached per user, face rows only after a season toggle); gate 311, tests/realdata-062.mjs, README + STATUSmre
- fa1f9dfatracker#29 (mission 063): unwatched check muted grey outline + check (accent only on hover), watched stays solid — no code regression, the accent outline read as ticked; gate checks real checks visibly (computed style + screenshot pixel) on /my/unwatched, show, movie, /my/movies; gate 266, tests/realdata-063.mjs, README + STATUSmre
- 10bb3f93tracker#28 (mission 061): full cast (all seasons, main cast by episodes, guest stars) + crew (created by, directed by, written by, screenplay, story, music) — stored by the details completion, the daily sync, the search import (one details request) and a background credits job (resumes, RSS limit); show page collapsed after 20 with client-side Show all; showBySlug via a slug map; gate 249, tests/realdata-028.mjs, README + STATUSmre
- d8b12d67tracker#27 (mission 060): short ids for movies, series and persons — old 702 kept (data/old-short-ids.json), new random [a-z0-9]{5} unique across both, claimed at creation, background backfill (resumes), shown under poster/photo, /<shortId> → 301; gate 259, tests/realdata-060*, README + STATUSmre
- 25a50bc4tracker#26 (mission 059): titles from a filmography are completed — on open (skeleton, step-wise face showComplete, no reload) and by the in-app details repair (resumes, TMDB-paced, series in parts); cast from TMDB credits; gate 231, tests/realdata-026.mjs, README + STATUSmre
- f14db671tracker#22-#25 (mission 058): episode air dates, season check = all episodes watched, movie watched check (+ /my/movies count), /genres/<genre> pages (movies + series, newest first, paginated); gate 238, tests/realdata-058.mjs, README + STATUSmre
- 1704ec45tracker#17 (mission 057): season caret down/up, skeleton rows while a season loads, sessionless showSeasonEpisodes face (no page re-mount), client-only close; gate 214, tests/realdata-057.mjs, README + STATUSmre
- f2fe3e36mission 056: README + STATUS (merge, fixes, Hybriel 8590df63, real-data check), tests/realdata-056.mjs, tools/check-public-slugs.hlmre
- f40c250emission 056: re-vendor hybriel master 8590df63 (#121, #122); an adult title's page is Not found for non-followers; gate: leave the page before stopping the servermre
- 2b7fdd6cmission 056: signed-out header one row on phones ("Log in", nowrap), backfill skips adult titles' posters, gate checksmre
- 2c53d5efMerge branch 't16-person' (tracker#16 person pages) into main; filmography shows only public titles (054 adult flag), gate race fix (backfill start line)mre
- c171227emission 054: hide adult/unknown titles from the public lists and the search; in-app adult-flag backfill (TMDB details + poster per title, resumes), gate + real-data proofmre
- 139fafd8tracker#16: short bio (4 lines, click = all), real-data check script, README + STATUSmre
- 93be9476tracker#16: person pages /person/<slug> with the filmography fetched from TMDB on the first visit (step by step), gatemre
- 47a3cae6STATUS: mission 053 merge commit idsmre
- dcc5eecaMerge branch 't14-search'mre
- 03edc783Merge branch 't15-tvmaze'mre
- 71b46345tracker#15: numbering check by date or title, placeholder titles in other languages, docs + real-data proofmre