tracker
All repositories: gitoria
3.9 KB
// lib/util.hl — small helpers every topic shares (env, paths, dates, lists, text), each ONCE (mission 028: they were copied// into follows, users, shows, watches, tmdbsync, catalog, franchises, details, people, merge, search, shortids).import { env } from 'hl:proc'import { now, timestamp } from 'hl:time'import { randomBytes } from 'hl:crypto'import { Response } from 'hl:http1'// an environment value, trimmed — `fallback` when it is not set or emptystatic envOr = (&name, &fallback) => {v = env(name)return v != null && v.trim() != '' ? v.trim() : fallback}// every table and file of the app's data: TRACKER_STORAGE, else storage/mpackdb beside project.hl (relative paths resolve// against the ENTRY script)static storageDir = envOr('TRACKER_STORAGE', './storage/mpackdb')// the image files: a title's poster (tmdb.hl syncPoster) and a person's photo (people.hl syncProfile), served by images.hlstatic postersDir = storageDir + '/posters'static profilesDir = storageDir + '/profiles'// a new record id: 16 hex (self-assigned — hl:mpackdb's own generated @id can repeat within one millisecond, hybriel#113)static newId = () => { return randomBytes(8, 'hex') }static hexDigits = '0123456789abcdef'// today as 'YYYY-MM-DD' (UTC, like the rest of the app — hl:time has no time zones)static todayStr = () => { return timestamp(now()).slice(0, 10) }// the day `days` from today ('YYYY-MM-DD', UTC; negative = before)static dayStr = (days) => { return timestamp(now() + days * 86400000).slice(0, 10) }// a stored date ('YYYY-MM-DD…') or '' when it is nonestatic dateOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.length >= 4 ? v : '' }// a face's text argument: the string itself, '' for anything else (a peer may send a list or an object: `'' + x` then aborts the// request with a 500 that names the source line)static textArg = (&v) => { return v != null && hlTypeName(v) == 'String' ? v : '' }// a text value or null when it is none or only white space (the value itself, not trimmed)static textOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.trim() != '' ? v : null }// an id (a string or a number) that is therestatic hasId = (&v) => { return v != null && ('' + v).trim() != '' }static listOr = (&v) => { return v == null ? [] : v }static countOfList = (&list) => {if (list == null) { return 0 }n = list.lengthreturn n == null ? 0 : n}static firstOf = (&list) => { return countOfList(&list) > 0 ? list[0] : null }// ---- sorting, hand-written (no list sort(), hybriel#1): a bottom-up merge sort of { key, … }, newest (largest) key first,// stable — the public lists (catalog.hl), a person's filmography and /people (people.hl). Tracker#37 (mission 032): it starts// from the runs that are already in order (not from single entries) — same result, but a list that is nearly sorted (/people:// 105k people, 3 runs) costs one merge pass: 0.15 s instead of 1.6 s on the live copystatic mergeDesc = (&a, &b) => {out = []let i = 0let j = 0while (i < a.length && j < b.length) {if (b[j].key > a[i].key) { out.push(b[j]) j = j + 1 } else { out.push(a[i]) i = i + 1 }}while (i < a.length) { out.push(a[i]) i = i + 1 }while (j < b.length) { out.push(b[j]) j = j + 1 }return out}static sortDesc = (&list) => {if (list.length == 0) { return [] }let runs = []let run = []for (x of list) {if (run.length > 0 && x.key > run[run.length - 1].key) { runs.push(run) run = [] }run.push(x)}runs.push(run)while (runs.length > 1) {let next = []let k = 0while (k + 1 < runs.length) { next.push(mergeDesc(&runs[k], runs[k + 1])) k = k + 2 }if (k < runs.length) { next.push(runs[k]) }runs = next}return runs[0]}// a permanent redirect (project.hl movedTo: the old /my/ addresses, /shows/<slug> of a series or movie, short ids)static movedResponse = (&path) => { return new Response('moved to ' + path, { status = 301 headers = { 'Location' = path 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } }) }
Branches
Latest commits
- 6ea2c6d6log: morning re-measure 11-04mre
- 446290f0log: GC1 livemre
- 8e6aa91dlog: GC1 deploy retrymre
- 87ad2dc1log: GC1 deployingmre
- 53f10fb8Merge branch 'gc1-deploy'mre
- 7fcbf1d2log: GC1 deploy GOmre
- 1fb6664bcompose: TRACKER_QSTATS=1 (GC1 measurement)mre
- 6576c1cdgc1: release the people-view scan lists once the view is builtmre
- 94f8a373gc1: back out the kept episode-id set (no measurable gain on /my/unwatched: 4.69 -> 4.61 GB per 150 loads)mre
- a1a88bd4gc1: search skips the stale-key string while no entry is stale; /my/unwatched asks a kept id set instead of building a key string per episode rowmre
- cd830744gc1: dbSearch: no split / map / list copy per candidate (scores from the stored text, top list touched only when the hit can enter)mre
- 451db7cdgc1: show page and check clicks read the kept watched set instead of building one from a by-value copy of every watchmre
- ddb4127egc1: TRACKER_QSTATS=1 logs queue and cache sizes every 10 s (measurement only)mre
- da60547fstatus: GC1 done, backup bugmre
- e4171021log: GC1 result, backup hard-link bugmre
- f1597214deploy.sh backup: tar --hard-dereference (hot tar stored mpackdb sidecars as hard links to .sessions files)mre
- 6b58571elog: GC windowmre
- 5c8baa1fdeploy lock: hold only around deploy.shmre
- b8dac822log: hybriel#309mre
- bf676d9elog: GC1 startedmre