gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit529dd741529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre529dd741/lib/util.hl

3.9 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 face's text argument: the string itself, '' for anything else (a peer may send a list or an object: `'' + x` then aborts the
  28. // request with a 500 that names the source line)
  29. static textArg = (&v) => { return v != null && hlTypeName(v) == 'String' ? v : '' }
  30. // a text value or null when it is none or only white space (the value itself, not trimmed)
  31. static textOr = (&v) => { return v != null && hlTypeName(v) == 'String' && v.trim() != '' ? v : null }
  32. // an id (a string or a number) that is there
  33. static hasId = (&v) => { return v != null && ('' + v).trim() != '' }
  34. static listOr = (&v) => { return v == null ? [] : v }
  35. static countOfList = (&list) => {
  36. if (list == null) { return 0 }
  37. n = list.length
  38. return n == null ? 0 : n
  39. }
  40. static firstOf = (&list) => { return countOfList(&list) > 0 ? list[0] : null }
  41. // ---- sorting, hand-written (no list sort(), hybriel#1): a bottom-up merge sort of { key, … }, newest (largest) key first,
  42. // stable — the public lists (catalog.hl), a person's filmography and /people (people.hl). Tracker#37 (mission 032): it starts
  43. // from the runs that are already in order (not from single entries) — same result, but a list that is nearly sorted (/people:
  44. // 105k people, 3 runs) costs one merge pass: 0.15 s instead of 1.6 s on the live copy
  45. static mergeDesc = (&a, &b) => {
  46. out = []
  47. let i = 0
  48. let j = 0
  49. while (i < a.length && j < b.length) {
  50. if (b[j].key > a[i].key) { out.push(b[j]) j = j + 1 } else { out.push(a[i]) i = i + 1 }
  51. }
  52. while (i < a.length) { out.push(a[i]) i = i + 1 }
  53. while (j < b.length) { out.push(b[j]) j = j + 1 }
  54. return out
  55. }
  56. static sortDesc = (&list) => {
  57. if (list.length == 0) { return [] }
  58. let runs = []
  59. let run = []
  60. for (x of list) {
  61. if (run.length > 0 && x.key > run[run.length - 1].key) { runs.push(run) run = [] }
  62. run.push(x)
  63. }
  64. runs.push(run)
  65. while (runs.length > 1) {
  66. let next = []
  67. let k = 0
  68. while (k + 1 < runs.length) { next.push(mergeDesc(&runs[k], runs[k + 1])) k = k + 2 }
  69. if (k < runs.length) { next.push(runs[k]) }
  70. runs = next
  71. }
  72. return runs[0]
  73. }
  74. // a permanent redirect (project.hl movedTo: the old /my/ addresses, /shows/<slug> of a series or movie, short ids)
  75. 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

  • 529dd741Merge sl1-slices: slice details repair and person fill (SL1)mre
  • 8a83316blog: SL1 result, session drain slowermre
  • 35e93e8dSL1: slice the details repair and person fill: catalog lists changed in place, 40 ms fill steps, people created in steps, writes one table per tickmre
  • 72c9119flog+status: SL1 startedmre
  • d74620fcmission SL1: slice repair and person fillmre
  • 5d542d73log+status: ST1 resultmre
  • 3945eb81ST1: stall diagnosis reportmre
  • 404733aclog: ST1 startedmre
  • 3e3d5a55mission ST1: stall diagnosismre
  • 9c3180eblog+status: RV28d measured after deploymre
  • fdf10b1alog: RV28d deployed on day38bmre
  • 320005e9log: RV28d merged, deploy runningmre
  • a8fef27dRV28d: reportmre
  • 2bae8373RV28d: re-vendor Hybriel set day38b (source 1e554d75, bin 5ff2a46d)mre
  • 16b3354aMerge branch 'main' into rv28cmre
  • 6f4f27d8log: RV28d started (day38b)mre
  • e6cf03ffmission RV28d: rv28c onto day38bmre
  • 3ccaee19status: live sweep freeze, waiting for Apollomre
  • 49f76ecblog: watch toggle slowness = session sweep freeze (tracker#50)mre
  • 3daf6eddlog: day37 live, still waiting for day39mre