tracker
All repositories: gitoria
13.6 KB
// lib/franchiseseed.hl — THE CURATED FRANCHISES (tracker.worldapi.org#40, mission 034). Creator: "you never build timlines and// franchises". The big franchises (Star Trek, Star Wars, Stargate, MCU, DC …) with their timelines, MOVIES AND SERIES, come from// a hand-written DATA file in the repo — `data/franchises.json` (TRACKER_FRANCHISE_FILE): per franchise its name, summary,// the TMDB collections that join it, and its timelines, each with its order ("in-universe" | "release") and its titles as// [kind ('tv' | 'movie'), TMDB id, "title (year)" — for people only].//// franchiseSeedTable pk @id { kind, key, ref, order, franchise, created, why, at } — what the seed made, one row per thing:// kind 'franchise' key = its name ref = the franchise @id created (true: the seed made it; false: the// creator's own of that name, adopted)// kind 'timeline' key = franchise|timeline ref = the timeline @id order + franchise = what the seed LAST WROTE// (order null: the creator's own of that name)// kind 'collection' key = collection:<id> ref = the timeline @id franchise (null: it had one already, left alone)// kind 'missing' key = tv:<id> / movie:<id> why TMDB has no such title (404) — not asked again// storage/mpackdb/franchiseseed.db//// THE JOB (jobs.hl `franchiseSeedTick`, after every other start job — the collection seed last — one step per tick):// 1. the titles of the file we don't have (search.hl titleIdByTmdb) are IMPORTED, one per step, through the search's own// import (details.hl importWithCredits: details, seasons + episodes, poster, ids, TVmaze, cast + crew — complete), then// the pause the daily sync makes after the same requests (tmdb.hl pauseMsAfter). Without TMDB_READ_TOKEN: no imports.// 2. then ONE build step (no TMDB): each franchise found or made, its collections' timelines attached, each timeline made// or brought up to date with the titles we have, in the file's order; then every TMDB collection timeline whose titles// are ALL in one curated timeline is hidden (`supersededBy`, franchises.hl seededCoverOf), a partly covered one joins// that franchise.// EDITOR CHANGES WIN: a seeded timeline whose order or franchise is no longer what the seed last wrote was edited by the// creator — it is never written again (`kept`); a franchise or timeline of the same name the creator made is used / left// alone, never doubled; a collection timeline that has a franchise keeps it. RESUMES after a restart (imports are stored at// once; the queue = what is still missing); IDEMPOTENT (a second start imports nothing and writes nothing).import { MPackDB } from 'hl:mpackdb'import { readFile, exists } from 'hl:fs'import { now } from 'hl:time'import { storageDir, envOr, newId } from './util.hl'import { showById, liveShowOf } from './shows.hl'import { titleIdByTmdb } from './search.hl'import { importWithCredits } from './details.hl'import { ensureFranchises, franchiseById, timelineById, timelinesTable, createFranchise, putTimeline, setTimelineFranchise, franchiseByName, timelineByName, timelineOfCollection, storeSeeded, persistFranchises, seededTimelines, seededCoverIn, setSuperseded } from './franchises.hl'static franchiseSeedTable = new MPackDB(file = storageDir + '/franchiseseed.db', primaryKey = '@id', indexes = [])static franchiseFile = envOr('TRACKER_FRANCHISE_FILE', './data/franchises.json')// the queue is a plain static list filled in place (a static cannot be reassigned — collections.hl)static importQueue = []static seedRows = {}static fseed = { built = false pos = 0 file = false franchises = 0 timelines = 0 titles = 0 have = 0 imported = 0 failed = 0 missing = 0requests = 0 tvmazeRequests = 0 franchisesCreated = 0 franchisesAdopted = 0 timelinesCreated = 0 updated = 0 unchanged = 0 kept = 0 empty = 0placed = 0 collections = 0 superseded = 0 unsuperseded = 0 joined = 0 writes = 0 error = '' }static typeOfKind = (&kind) => { return kind == 'tv' ? 'series' : 'movie' }static titleKeyOf = (&kind, &tmdbId) => { return kind + ':' + tmdbId }// our show @id for an entry of the file (a merged tombstone answers its keeper), or nullstatic ourIdOf = (&entry) => {id = titleIdByTmdb(typeOfKind(&entry[0]), &entry[1])s = id != null ? liveShowOf(showById(&id)) : nullreturn s != null ? s.id : null}static sameList = (&a, &b) => {if (a == null || b == null || a.length != b.length) { return false }let i = 0while (i < a.length) {if (a[i] != b[i]) { return false }i = i + 1}return true}static noteRow = (kind, key, &ref, &order, &franchise, created, why) => {old = seedRows[kind + '|' + key]rec = { id = old != null ? old.id : newId() kind = kind key = key ref = ref order = order franchise = franchise created = created why = why at = now() }if (old != null) { franchiseSeedTable.update(rec.id, rec) } else { franchiseSeedTable.put(rec) }seedRows[kind + '|' + key] = recreturn rec}static rowOf = (&kind, &key) => { return seedRows[kind + '|' + key] }// the file → { franchises: [...] }, or null (logged) when it is not therestatic readFranchiseFile = () => {if (!exists(franchiseFile)) {fseed.error = franchiseFile + ' not found'return null}d = JSON.parse(readFile(franchiseFile))if (d == null || d.franchises == null) {fseed.error = franchiseFile + ': no "franchises" list'return null}return d}static fileData = { franchises = [] }// at the job's start: the file read, the seed's rows loaded, the queue = the file's titles we don't have (`imports` false: none)static franchiseSeedPrepare = (&imports) => {if (fseed.built) { return importQueue.length }fseed.built = trueensureFranchises()for (r of franchiseSeedTable.find(null, null)) { seedRows[r.kind + '|' + r.key] = r }d = readFranchiseFile()if (d == null) { return 0 }fseed.file = truefileData.franchises = d.franchisesqueued = {}for (f of d.franchises) {fseed.franchises = fseed.franchises + 1for (t of f.timelines) {fseed.timelines = fseed.timelines + 1for (e of t.titles) {let key = titleKeyOf(&e[0], &e[1])if (queued[key] != true) {queued[key] = truefseed.titles = fseed.titles + 1if (ourIdOf(&e) != null) { fseed.have = fseed.have + 1 }else if (rowOf('missing', &key) != null) { fseed.missing = fseed.missing + 1 }else if (imports) { importQueue.push(e) }}}}}return importQueue.length}static franchiseSeedStats = () => { return fseed }// ---- step 1: ONE import → { requests, tvmazeRequests (what the pause counts), error } --------------------------------------static importOne = (&entry) => {out = { requests = 1 tvmazeRequests = 0 error = '' done = false }key = titleKeyOf(&entry[0], &entry[1])fseed.requests = fseed.requests + 1r = importWithCredits(&entry[0], entry[1])if (r.error != null) {fseed.failed = fseed.failed + 1out.error = key + ' (' + entry[2] + '): ' + r.error// TMDB has no such title: remembered, never asked again (a passing failure is tried again on the next start)if (r.error.includes('HTTP 404')) { noteRow('missing', key, null, null, null, false, r.error) franchiseSeedTable.persist() }return out}if (r.sync != null) {out.requests = 1 + r.sync.requestsout.tvmazeRequests = r.sync.tvmazeRequestsfseed.requests = fseed.requests + r.sync.requestsfseed.tvmazeRequests = fseed.tvmazeRequests + r.sync.tvmazeRequests}if (r.created == true) { fseed.imported = fseed.imported + 1 } else { fseed.have = fseed.have + 1 }return out}// ---- step 2: the build (no TMDB) ---------------------------------------------------------------------------------------------// the franchise of the file's entry `f`: the one the seed made / adopted before, else the creator's of that name, else a new onestatic franchiseOf = (&f) => {row = rowOf('franchise', &f.name)if (row != null) { return franchiseById(&row.ref) }mine = franchiseByName(&f.name)if (mine != null) {fseed.franchisesAdopted = fseed.franchisesAdopted + 1noteRow('franchise', f.name, &mine.id, null, null, false, 'the creator\'s own')return mine}c = createFranchise(f.name, f.summary)if (c.error != null) {fseed.error = f.name + ': ' + c.errorreturn null}fseed.franchisesCreated = fseed.franchisesCreated + 1noteRow('franchise', f.name, &c.id, null, null, true, '')return franchiseById(&c.id)}// a TMDB collection's timeline (collection seed) joins the franchise — once, and only when it has none yetstatic attachCollection = (&collection, &fid) => {key = 'collection:' + collectionif (rowOf('collection', &key) != null) { return false }t = timelineOfCollection(&collection)if (t == null) { return false }if (t.franchise != null && t.franchise != '') {noteRow('collection', key, &t.id, null, null, false, 'has a franchise already')return false}r = setTimelineFranchise(&t.id, fid)if (r.error != null) {fseed.error = key + ': ' + r.errorreturn false}fseed.collections = fseed.collections + 1noteRow('collection', key, &t.id, null, &fid, true, '')return true}// one timeline of the file: made, brought up to date, or left alone (the creator's)static buildTimeline = (&f, &fid, &tl) => {key = f.name + '|' + tl.nameorder = []for (e of tl.titles) {let sid = ourIdOf(&e)if (sid != null && !order.includes(sid)) { order.push(sid) }}fseed.placed = fseed.placed + order.lengthrow = rowOf('timeline', &key)if (row == null) {mine = timelineByName(&tl.name)if (mine != null) {// the creator made one of that name: theirs, never written by the seedfseed.kept = fseed.kept + 1noteRow('timeline', key, &mine.id, null, null, false, 'the creator\'s own')return true}// none of its titles here yet (no TMDB token, or every import failed): made on a later startif (order.length == 0) {fseed.empty = fseed.empty + 1return true}rec = putTimeline(tl.name, &fid, order, 'seed', null, tl.order == 'release' ? 'release' : 'in-universe')if (rec == null) {fseed.error = key + ': not stored'return false}fseed.timelinesCreated = fseed.timelinesCreated + 1fseed.writes = fseed.writes + 1noteRow('timeline', key, &rec.id, &order, &fid, true, '')return true}t = timelineById(&row.ref)// gone, or the creator's own, or edited since the seed wrote it (its order or franchise changed): editor changes winif (t == null || row.order == null || !sameList(&t.order, &row.order) || t.franchise != row.franchise) {fseed.kept = fseed.kept + 1return true}if (sameList(&t.order, &order) && t.franchise == fid) {fseed.unchanged = fseed.unchanged + 1return true}r = storeSeeded(t, order, &fid)if (r.error != null) {fseed.error = key + ': ' + r.errorreturn false}fseed.updated = fseed.updated + 1fseed.writes = fseed.writes + 1noteRow('timeline', key, &t.id, &order, &fid, true, '')return true}// tracker#40: every TMDB collection timeline against the curated ones — all its titles in one → hidden (superseded: no second// widget on First Contact); the cover gone → shown again; partly covered and without a franchise → joins that franchisestatic supersedeCollections = () => {let seeds = seededTimelines()for (t of timelinesTable.find(null, null)) {if (t.tmdbCollection != null && t.order != null) {let cov = seededCoverIn(&seeds, &t.order)if (cov.cover != null && t.supersededBy != cov.cover) {if (setSuperseded(t, &cov.cover).error == null) { fseed.superseded = fseed.superseded + 1 }} else if (cov.cover == null && t.supersededBy != null) {if (setSuperseded(t, null).error == null) { fseed.unsuperseded = fseed.unsuperseded + 1 }} else if (cov.cover == null && cov.franchise != null && (t.franchise == null || t.franchise == '')) {if (setTimelineFranchise(&t.id, cov.franchise).error == null) { fseed.joined = fseed.joined + 1 }}}}return true}static buildAll = () => {for (f of fileData.franchises) {let fr = franchiseOf(&f)if (fr != null) {if (f.collections != null) { for (c of f.collections) { attachCollection(&c, &fr.id) } }for (tl of f.timelines) { buildTimeline(&f, &fr.id, &tl) }}}supersedeCollections()persistFranchises()franchiseSeedTable.persist()return true}// ONE step: the next import, or — the queue done — the build → { requests, tvmazeRequests, error, done }static franchiseSeedStep = () => {if (!fseed.file) { return { requests = 0 tvmazeRequests = 0 error = fseed.error done = true } }if (fseed.pos < importQueue.length) {e = importQueue[fseed.pos]fseed.pos = fseed.pos + 1// imported meanwhile (the search, a person page): nothing to askif (ourIdOf(&e) != null) {fseed.have = fseed.have + 1return { requests = 0 tvmazeRequests = 0 error = '' done = false }}return importOne(&e)}buildAll()return { requests = 0 tvmazeRequests = 0 error = fseed.error done = true }}static franchiseSeedLine = () => {s = fseedreturn 'franchises=' + s.franchises + ' (created=' + s.franchisesCreated + ' adopted=' + s.franchisesAdopted + ') timelines=' + s.timelines + ' (created=' + s.timelinesCreated+ ' updated=' + s.updated + ' unchanged=' + s.unchanged + ' kept=' + s.kept + ' empty=' + s.empty + ') collections=' + s.collections + ' superseded=' + s.superseded + ' unsuperseded=' + s.unsuperseded + ' joined=' + s.joined + ' titles=' + s.titles + ' had=' + s.have+ ' imported=' + s.imported + ' failed=' + s.failed + ' missing=' + s.missing + ' placed=' + s.placed + ' requests=' + s.requests + ' tvmazeRequests=' + s.tvmazeRequests}
Branches
Latest commits
- e0db0d1bHM60 report: tracker#60 packed short-ID ownersmre
- e822524bstatus wordingmre
- 1c7f0480log: HM60/HM61 startedmre
- 2841d215missions HM60 HM61mre
- 289c6ce4HM1 report: heap object breakdownmre
- 2e7d6257log: HM1 startedmre
- 2f790465mission HM1: heap object breakdownmre
- a18957falog: evening re-measure after CBmre
- aac9a98dlog: evening re-measure startmre
- 45163bd4log: person/tmdb error hypothesismre
- cb38de2blog: swap 503 causemre
- fd10c3d5log: CB livemre
- 7557c025log: CB deploy try 2mre
- 06a7534aMerge branch 'cb'mre
- 2f8030d6log: CB gates greenmre
- 498bf644log: CB gates startmre
- f59c2847gc1: bound the five request caches (shows.hl cacheLimit)mre
- 6ea2c6d6log: morning re-measure 11-04mre
- 446290f0log: GC1 livemre
- 8e6aa91dlog: GC1 deploy retrymre