gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit31dcf2ad31dcf2admission 045: report, LOGmre31dcf2ad/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

  • 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
  • 1a5a4818boot scans back to iterate (slices measured slower: titles 114 s, episodes 141 s, persons failed); lambda per rowmre
  • 03d29893boot scans read primary-key slices (lib/slices.hl from mission 044): iterate is eager too; people view log line back; allShows/allEpisodes stay for toolsmre
  • 23375c58mission 044: report, LOG, STATUSmre
  • c6fd928emission 044: 4096 slices for tables over 200 K rows, 15 ms budget, TRACKER_SLICE_DEPTH for testsmre
  • fb057680mission 044: jobs.hl imports mergeLeft (gate found it), mission file, slices benchmre
  • 0d6293ebmission 043: mission filemre
  • d25bd14amission 044: sliced merge prepare + sliced collections prepare; jobs-dump old/new check + bulk fixturemre
  • 633ee210mission 044: sliced table scans (slices.hl, pk-range slices) for the adult/kind/status/art/credits/details-repair queuesmre
  • 55bbd619boot: one streamed pass per table (lib/bootscan.hl) instead of find(null,null) per module; follows/watches/franchises scans streammre
  • 1d465d3ere-vendor plugins to Hybriel master f9614007 (mpackdb #202 copy fix, libmpackdb sha 6fb3b9d6)mre
  • 6648ba44mission 042: real-browser E2E on ce6f2e0a re-vendor 69/0 at 390 + 1280mre
  • 3ed3a0efmission 042: gates old/new equal (browser #41 route-table regex accepts unescaped hl:web output), real-data TTFB, E2E blocked by lisbeth lockmre
  • d5b61e90LOG 042mre
  • 4acdbaccmission 042: memory comparison old vs ce6f2e0a (stopped: warm VmHWM higher), reportmre