gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit99e7c8b999e7c8b9tests: realdata-m035-radii, repro-t38, realdata-060-backfill ports from env (REAL_PORT, REAL_CHROME, REPRO_CHROME), defaults unchangedmre99e7c8b9/lib/util.hl

3.7 KB

  1. // lib/util.hl — small helpers every topic shares (env, paths, dates, lists, text), each ONCE (mission 028: they were copied
  2. // into follows, users, shows, watches, tmdbsync, catalog, franchises, details, people, merge, search, shortids).
  3. import { env } from 'hl:proc'
  4. import { now, timestamp } from 'hl:time'
  5. import { randomBytes } from 'hl:crypto'
  6. import { Response } from 'hl:http1'
  7. // an environment value, trimmed — `fallback` when it is not set or empty
  8. static envOr = (&name, &fallback) => {
  9. v = env(name)
  10. return v != null && v.trim() != '' ? v.trim() : fallback
  11. }
  12. // every table and file of the app's data: TRACKER_STORAGE, else storage/mpackdb beside project.hl (relative paths resolve
  13. // against the ENTRY script)
  14. static storageDir = envOr('TRACKER_STORAGE', './storage/mpackdb')
  15. // the image files: a title's poster (tmdb.hl syncPoster) and a person's photo (people.hl syncProfile), served by images.hl
  16. static postersDir = storageDir + '/posters'
  17. static profilesDir = storageDir + '/profiles'
  18. // a new record id: 16 hex (self-assigned — hl:mpackdb's own generated @id can repeat within one millisecond, hybriel#113)
  19. static newId = () => { return randomBytes(8, 'hex') }
  20. static hexDigits = '0123456789abcdef'
  21. // today as 'YYYY-MM-DD' (UTC, like the rest of the app — hl:time has no time zones)
  22. static todayStr = () => { return timestamp(now()).slice(0, 10) }
  23. // the day `days` from today ('YYYY-MM-DD', UTC; negative = before)
  24. static dayStr = (days) => { return timestamp(now() + days * 86400000).slice(0, 10) }
  25. // a stored date ('YYYY-MM-DD…') or '' when it is none
  26. static dateOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.length >= 4 ? v : '' }
  27. // a text value or null when it is none or only white space (the value itself, not trimmed)
  28. static textOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.trim() != '' ? v : null }
  29. // an id (a string or a number) that is there
  30. static hasId = (&v) => { return v != null && ('' + v).trim() != '' }
  31. static listOr = (&v) => { return v == null ? [] : v }
  32. static countOfList = (&list) => {
  33. if (list == null) { return 0 }
  34. n = list.length
  35. return n == null ? 0 : n
  36. }
  37. static firstOf = (&list) => { return countOfList(list) > 0 ? list[0] : null }
  38. // ---- sorting, hand-written (no list sort(), hybriel#1): a bottom-up merge sort of { key, … }, newest (largest) key first,
  39. // stable — the public lists (catalog.hl), a person's filmography and /people (people.hl). Tracker#37 (mission 032): it starts
  40. // from the runs that are already in order (not from single entries) — same result, but a list that is nearly sorted (/people:
  41. // 105k people, 3 runs) costs one merge pass: 0.15 s instead of 1.6 s on the live copy
  42. static mergeDesc = (&a, &b) => {
  43. out = []
  44. let i = 0
  45. let j = 0
  46. while (i < a.length && j < b.length) {
  47. if (b[j].key > a[i].key) { out.push(b[j]) j = j + 1 } else { out.push(a[i]) i = i + 1 }
  48. }
  49. while (i < a.length) { out.push(a[i]) i = i + 1 }
  50. while (j < b.length) { out.push(b[j]) j = j + 1 }
  51. return out
  52. }
  53. static sortDesc = (&list) => {
  54. if (list.length == 0) { return [] }
  55. let runs = []
  56. let run = []
  57. for (x of list) {
  58. if (run.length > 0 && x.key > run[run.length - 1].key) { runs.push(run) run = [] }
  59. run.push(x)
  60. }
  61. runs.push(run)
  62. while (runs.length > 1) {
  63. let next = []
  64. let k = 0
  65. while (k + 1 < runs.length) { next.push(mergeDesc(runs[k], runs[k + 1])) k = k + 2 }
  66. if (k < runs.length) { next.push(runs[k]) }
  67. runs = next
  68. }
  69. return runs[0]
  70. }
  71. // a permanent redirect (project.hl movedTo: the old /my/ addresses, /shows/<slug> of a series or movie, short ids)
  72. static movedResponse = (&path) => { return new Response('moved to ' + path, { status = 301 headers = { 'Location' = path 'Content-Type' = 'text/plain; charset=utf-8' } }) }

Branches

Latest commits

  • 99e7c8b9tests: realdata-m035-radii, repro-t38, realdata-060-backfill ports from env (REAL_PORT, REAL_CHROME, REPRO_CHROME), defaults unchangedmre
  • 4625aa22re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • a6dfd6ecdeploy: person-lookup gate (tracker#46)mre
  • 33ad4bd5Merge commit 'f57f64f'mre
  • 7d38cec4docker-compose: nofile 65536 (default soft 1024 refused boot: errno 24 in fs.listDir, Hybriel 528bc89f)mre
  • f57f64f0mission 046: report, LOGmre
  • 5965963adeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • efe1b485mission 046: re-vendor Hybriel master 60b2817b (integration 8, #203): plugins mpackdb + web (bin/ git-ignored: hybriel, libhlrt.a, hlc.o, hlrt/ from native/zig-out)mre
  • 53099ffbmission 046: person page reads a slim per-title credit row cache (no whole-record fetch per credit); gate tests/person-lookup.mjsmre
  • 9b3828aemission 046a: report, LOGmre
  • 4b37356emission 046: franchise seed supersedeCollections reads the curated timelines once (10 s one-tick block at the end of boot)mre
  • ac6268e3mission 046a: re-vendor Hybriel 528bc89f (integration 6): plugins mpackdb/time/web (bin/ git-ignored)mre
  • 322b3293mission 046: photoQueue guard for non-row fetch results (damaged persons index)mre
  • e5b99aecmission 046: photoQueue in slices (people.hl photoQueueStart/Step, jobs.hl photoTick), tests/photoqueue.hlmre
  • 31dcf2admission 045: report, LOGmre
  • 9c3971d0tests/persons-skip-count.hlmre
  • 4d72e365mission 045mre
  • b9cae4c9slices: skip and count non-row values from range(); merge persons pass falls back to iterate; guard testmre
  • 3db7dc31merge w44-jobs-slices into w43-trkmemmre
  • 593dab6fmission 043: report, LOGmre