gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchrevendor-0f396652f03dfbfcmission 040: re-vendor Hybriel 0f396652 (static build): plugins, compose cache volume/HL_CACHE_DIR/tmpfs, README, reportmrerevendor-0f396652/lib/shortids.hl

8.4 KB

  1. // lib/shortids.hl — SHORT IDS (tracker.worldapi.org#27, mission 060): every movie, series and person has its own 5-character id
  2. // (`shortId`, like `a9s9a`), shown below its poster/photo; `/<shortId>` answers 301 to its page. Creator: "i wanted to give all
  3. // our movies, shows, series and persons own short ids like a9s9a … those ids show below the poster."
  4. //
  5. // OLD IDS: the old tracker had them on 702 of 9,453 shows (Show.jsonl field `id`, base62 counter '100jh'…'1012y', all of
  6. // them [a-z0-9]{5}). `data/old-short-ids.json` (made once by tools/old-short-ids.hl) maps the old Mongo `_id` (= our
  7. // `oldId`, tools/migrate.hl) → that id; such a show gets it back. Persons never had one.
  8. // 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
  9. // reserved, even where the show is gone).
  10. // UNIQUE ACROSS SHOWS AND PERSONS: one in-memory map `owners` (short id → { kind, id }) over BOTH tables — an mpackdb index
  11. // is per table and cannot span two. Built at start by `ensureShortIds` (one scan of each table); every id handed
  12. // out goes through it. A record whose short id another record already owns (never seen; logged) gets a new one.
  13. // NEW RECORDS get one when they are created: `claimShortId(kind, id, oldId)` in search.hl importTitle and people.hl
  14. // addMinimalTitle (the only places the app creates shows) and people.hl castPersonId (mission 062: the only place
  15. // the app creates persons — the cast/crew of #26/#28: completion, credits job, daily sync + tools/sync-tmdb.hl,
  16. // search import; and a guest star opened for the first time, people.hl guestPersonId).
  17. // THE BACKFILL: the records without one (at first every row: 9,453 shows + 15,157 persons) are given theirs in the
  18. // background, a time-boxed batch per clock tick (jobs.hl `shortIdTick` → `shortIdStep`), so pages are served
  19. // between two batches; after a restart it goes on with what is still missing. Real data: STATUS.md mission 060.
  20. //
  21. // ONE PROCESS PER STORAGE (hybriel#21): the map lives in the app's process; the tables are shows.hl's.
  22. import { now } from 'hl:time'
  23. import { env } from 'hl:proc'
  24. import { readFile, exists } from 'hl:fs'
  25. import { randomBytes } from 'hl:crypto'
  26. import { hexDigits } from './util.hl'
  27. import { showsTable, personsTable, titlePath, liveShowOf } from './shows.hl'
  28. static shortIdChars = 'abcdefghijklmnopqrstuvwxyz0123456789'
  29. // short id → { kind ('show' | 'person'), id }
  30. static owners = {}
  31. // the old tracker's ids: oldId → short id, and every one of them → true (reserved)
  32. static oldShortIds = {}
  33. static reserved = {}
  34. // the records still without a short id, [{ kind, id }], worked off from `state.queuePos`. Filled in place (push) and never
  35. // reassigned (a static cannot be) — kept OUT of `state`: reading `state.queue[i]` copied the whole 25k list on every access
  36. // (measured: ~3 ms per record instead of ~0.2)
  37. static queue = []
  38. static state = { built = false queuePos = 0 oldLoaded = 0 scanned = 0 existing = 0 duplicates = 0 ms = 0 assigned = 0 oldKept = 0 }
  39. // the file of old ids: TRACKER_OLD_SHORT_IDS, else data/old-short-ids.json beside project.hl (relative paths resolve against the
  40. // ENTRY script: a tool in tools/ — sync-tmdb.hl creates people since #28, mission 062 — finds it one folder up)
  41. static oldFile = () => {
  42. f = env('TRACKER_OLD_SHORT_IDS')
  43. if (f != null && f.trim() != '') { return f.trim() }
  44. return !exists('./data/old-short-ids.json') && exists('../data/old-short-ids.json') ? '../data/old-short-ids.json' : './data/old-short-ids.json'
  45. }
  46. // is this a short id at all (5 characters, a–z, 0–9)?
  47. static isShortId = (&s) => {
  48. if (s == null || hlTypeName(s) != 'String' || s.length != 5) { return false }
  49. let i = 0
  50. while (i < 5) {
  51. if (!shortIdChars.includes(s[i])) { return false }
  52. i = i + 1
  53. }
  54. return true
  55. }
  56. // 5 random characters a–z0–9, not taken and not an old one. Bytes ≥ 252 are skipped (252 = 7 × 36), so every character
  57. // is equally likely.
  58. static randomShortId = () => {
  59. while (true) {
  60. let hex = randomBytes(16, 'hex')
  61. let out = ''
  62. let i = 0
  63. while (i < hex.length && out.length < 5) {
  64. let b = hexDigits.indexOf(hex[i]) * 16 + hexDigits.indexOf(hex[i + 1])
  65. if (b < 252) { out = out + shortIdChars[b % 36] }
  66. i = i + 2
  67. }
  68. if (out.length == 5 && owners[out] == null && reserved[out] != true) { return out }
  69. }
  70. }
  71. // 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
  72. // one — taken in `owners` at once
  73. static claimShortId = (kind, id, &oldId) => {
  74. ensureShortIds()
  75. let sid = oldId != null ? oldShortIds['' + oldId] : null
  76. if (sid != null && owners[sid] == null) { state.oldKept = state.oldKept + 1 } else { sid = randomShortId() }
  77. owners[sid] = { kind = kind id = id }
  78. return sid
  79. }
  80. // tracker#31 (merge.hl picks the keeper of duplicates): is this record's short id the one the old tracker gave it?
  81. static hasOldShortId = (&r) => {
  82. ensureShortIds()
  83. return r.oldId != null && r.shortId != null && oldShortIds['' + r.oldId] == r.shortId
  84. }
  85. // does this record own its short id? (null / malformed / held by another record → no)
  86. static ownsShortId = (&kind, &r) => {
  87. if (!isShortId(r.shortId)) { return false }
  88. o = owners[r.shortId]
  89. return o != null && o.kind == kind && o.id == r.id
  90. }
  91. // one table's rows into `owners`; the rows without a (valid, own) short id into the queue
  92. static scanTable = (kind, &table) => {
  93. for (r of table.find(null, null)) {
  94. state.scanned = state.scanned + 1
  95. if (isShortId(r.shortId) && owners[r.shortId] == null) {
  96. owners[r.shortId] = { kind = kind id = r.id }
  97. state.existing = state.existing + 1
  98. } else {
  99. if (r.shortId != null) {
  100. state.duplicates = state.duplicates + 1
  101. console.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')
  102. }
  103. queue.push({ kind = kind id = r.id })
  104. }
  105. }
  106. return true
  107. }
  108. // at start (project.hl): the old ids, the map of every short id in use, the queue of records without one
  109. static ensureShortIds = () => {
  110. if (state.built) { return true }
  111. state.built = true
  112. t0 = now()
  113. f = oldFile()
  114. if (exists(f)) {
  115. m = JSON.parse(readFile(f))
  116. let n = 0
  117. for (k of m.keys()) {
  118. let sid = m[k]
  119. if (isShortId(sid)) { oldShortIds[k] = sid reserved[sid] = true n = n + 1 }
  120. }
  121. state.oldLoaded = n
  122. } else { console.log('short ids: no old ids (' + f + ' missing)') }
  123. scanTable('show', showsTable)
  124. scanTable('person', personsTable)
  125. state.ms = now() - t0
  126. console.log('short ids: ' + state.scanned + ' records, ' + state.existing + ' with a short id, ' + queue.length + ' without, ' + state.oldLoaded + ' old ids, ' + state.ms + ' ms')
  127. return true
  128. }
  129. static tableOf = (&kind) => { return kind == 'person' ? personsTable : showsTable }
  130. // the backfill, one batch: records from the queue until `budgetMs` is used up → { done, assigned, left }
  131. static shortIdStep = (&budgetMs) => {
  132. ensureShortIds()
  133. t0 = now()
  134. let n = 0
  135. while (state.queuePos < queue.length && now() - t0 < budgetMs) {
  136. let item = queue[state.queuePos]
  137. state.queuePos = state.queuePos + 1
  138. let table = tableOf(item.kind)
  139. let r = table.fetch(item.id)
  140. if (r != null && !ownsShortId(item.kind, r)) {
  141. let x = r
  142. x.shortId = claimShortId(item.kind, x.id, x.oldId)
  143. if (table.update(x.id, x) == null) { console.log('short ids: ' + item.kind + ' ' + x.id + ' not stored: ' + table.lastError()) } else { n = n + 1 }
  144. }
  145. }
  146. state.assigned = state.assigned + n
  147. done = state.queuePos >= queue.length
  148. return { done = done assigned = n left = queue.length - state.queuePos }
  149. }
  150. static shortIdStats = () => { return state }
  151. static persistShortIds = () => {
  152. showsTable.persist()
  153. personsTable.persist()
  154. return true
  155. }
  156. static shortIdsLeft = () => { return queue.length - state.queuePos }
  157. // `/<shortId>` (project.hl): the page's path, or null
  158. static shortIdPath = (&sid) => {
  159. if (!isShortId(sid)) { return null }
  160. ensureShortIds()
  161. o = owners[sid]
  162. if (o == null) { return null }
  163. // tracker#31: a merged tombstone's short id leads to its keeper
  164. r = o.kind == 'person' ? personsTable.fetch(o.id) : liveShowOf(showsTable.fetch(o.id))
  165. if (r == null || r.urlSegment == null) { return null }
  166. // a title's own page (/movies/, /series/, /shows/<slug>, tracker.worldapi.org#21)
  167. return o.kind == 'person' ? '/person/' + r.urlSegment : titlePath(r)
  168. }

Branches

Latest commits

  • f03dfbfcmission 040: re-vendor Hybriel 0f396652 (static build): plugins, compose cache volume/HL_CACHE_DIR/tmpfs, README, reportmre
  • 5d4683b4mission brief 040mre
  • 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
  • 9e18c2d0mission brief (w36)mre
  • 216fac57missions 028-035 + reports 033/034 (were untracked)mre
  • 5cb85d75deploy.sh: a backup taken while a background job writes (tar exit 1) is a warning; archive checked with gzip -tmre
  • 9abda75dtracker: report 035mre
  • 12595e47mission 035: theme re-vendored from layouts.worldapi.org 0222f67 (two corner radii: radiusSmall 5px, radiusLarge 10px); the tracker's 18 own radii -> radiusSmall/radiusLarge (--layout-radius is gone); check-theme 0; gates 379/0, 32/0, 53/0, 229/0, 26/0; real copy: every computed radius in {0, 5px, 10px, 50%}mre