@jam-overture/loom 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (414) hide show
  1. package/README.md +3 -1
  2. package/dist/application/application.d.ts +109 -0
  3. package/dist/application/application.js +97 -0
  4. package/dist/application/memory.d.ts +20 -0
  5. package/dist/application/memory.js +79 -0
  6. package/dist/application/store.d.ts +101 -0
  7. package/dist/application/store.js +25 -0
  8. package/dist/audience/audience.d.ts +36 -0
  9. package/dist/audience/audience.js +41 -0
  10. package/dist/audience/compose.d.ts +112 -0
  11. package/dist/audience/compose.js +134 -0
  12. package/dist/audience/memory.d.ts +14 -0
  13. package/dist/audience/memory.js +71 -0
  14. package/dist/audience/overlay-store.d.ts +343 -0
  15. package/dist/audience/overlay-store.js +217 -0
  16. package/dist/audience/rule.d.ts +121 -0
  17. package/dist/audience/rule.js +78 -0
  18. package/dist/cli/index.d.ts +3 -4
  19. package/dist/cli/index.js +3 -4
  20. package/dist/cli/templates.js +19 -0
  21. package/dist/data/adapter.d.ts +9 -1
  22. package/dist/data/adapter.js +2 -0
  23. package/dist/data/plan.js +0 -0
  24. package/dist/data/resolution.d.ts +23 -1
  25. package/dist/data/resolve.d.ts +31 -4
  26. package/dist/data/resolve.js +11 -3
  27. package/dist/ids.d.ts +10 -0
  28. package/dist/ids.js +11 -1
  29. package/dist/index.d.ts +1 -0
  30. package/dist/index.js +1 -0
  31. package/dist/interpretation/anthropic.d.ts +1 -1
  32. package/dist/interpretation/anthropic.js +2 -22
  33. package/dist/interpretation/client.d.ts +1 -1
  34. package/dist/interpretation/materialize.js +67 -22
  35. package/dist/interpretation/openai-compatible.d.ts +36 -0
  36. package/dist/interpretation/openai-compatible.js +79 -0
  37. package/dist/interpretation/status.d.ts +19 -0
  38. package/dist/interpretation/status.js +26 -0
  39. package/dist/live/index.d.ts +73 -0
  40. package/dist/live/index.js +73 -0
  41. package/dist/live/pages.d.ts +187 -0
  42. package/dist/live/pages.js +161 -0
  43. package/dist/live/pull.d.ts +147 -0
  44. package/dist/live/pull.js +398 -0
  45. package/dist/live/pulling.d.ts +79 -0
  46. package/dist/live/pulling.js +117 -0
  47. package/dist/live/window.d.ts +129 -0
  48. package/dist/live/window.js +132 -0
  49. package/dist/patch.d.ts +54 -0
  50. package/dist/patch.js +44 -0
  51. package/dist/project/closing.d.ts +51 -0
  52. package/dist/project/closing.js +53 -0
  53. package/dist/project/memory.d.ts +20 -0
  54. package/dist/project/memory.js +97 -0
  55. package/dist/project/project.d.ts +90 -0
  56. package/dist/project/project.js +91 -0
  57. package/dist/project/store.d.ts +135 -0
  58. package/dist/project/store.js +41 -0
  59. package/dist/registered.d.ts +64 -0
  60. package/dist/registered.js +118 -0
  61. package/dist/render/addressed.d.ts +35 -0
  62. package/dist/render/addressed.js +79 -0
  63. package/dist/render/behaviour-adjust.d.ts +11 -19
  64. package/dist/render/behaviour-adjust.js +75 -21
  65. package/dist/render/behaviour-present.d.ts +8 -0
  66. package/dist/render/behaviour-present.js +57 -6
  67. package/dist/render/behaviour.d.ts +98 -17
  68. package/dist/render/behaviour.js +91 -26
  69. package/dist/render/control-name.d.ts +68 -0
  70. package/dist/render/control-name.js +37 -0
  71. package/dist/render/diagnostics.d.ts +72 -0
  72. package/dist/render/diagnostics.js +7 -0
  73. package/dist/render/document-theme.d.ts +70 -0
  74. package/dist/render/document-theme.js +139 -0
  75. package/dist/render/editable.d.ts +15 -1
  76. package/dist/render/editable.js +13 -1
  77. package/dist/render/expected.d.ts +98 -0
  78. package/dist/render/expected.js +88 -0
  79. package/dist/render/index.d.ts +4 -0
  80. package/dist/render/index.js +4 -0
  81. package/dist/render/reads.d.ts +11 -0
  82. package/dist/render/reads.js +12 -1
  83. package/dist/render/render.d.ts +11 -0
  84. package/dist/render/render.js +294 -53
  85. package/dist/render/request.d.ts +8 -1
  86. package/dist/render/request.js +1 -0
  87. package/dist/render/slots.d.ts +52 -0
  88. package/dist/render/slots.js +24 -0
  89. package/dist/render/theme.d.ts +54 -1
  90. package/dist/render/theme.js +23 -1
  91. package/dist/render/unshown.d.ts +152 -0
  92. package/dist/render/unshown.js +80 -0
  93. package/dist/runtime/analysis.d.ts +94 -9
  94. package/dist/runtime/analysis.js +54 -9
  95. package/dist/runtime/assessment.d.ts +36 -2
  96. package/dist/runtime/assessment.js +27 -2
  97. package/dist/runtime/auto-apply-ceiling.d.ts +54 -0
  98. package/dist/runtime/auto-apply-ceiling.js +66 -0
  99. package/dist/runtime/checks.d.ts +148 -0
  100. package/dist/runtime/checks.js +112 -0
  101. package/dist/runtime/disposition.d.ts +96 -0
  102. package/dist/runtime/disposition.js +46 -0
  103. package/dist/runtime/gate.js +91 -10
  104. package/dist/runtime/index.d.ts +4 -0
  105. package/dist/runtime/index.js +4 -0
  106. package/dist/runtime/irreversibility.d.ts +64 -0
  107. package/dist/runtime/irreversibility.js +45 -0
  108. package/dist/runtime/nesting.d.ts +13 -1
  109. package/dist/runtime/nesting.js +41 -24
  110. package/dist/runtime/pipeline.d.ts +43 -0
  111. package/dist/runtime/pipeline.js +10 -2
  112. package/dist/runtime/policy-change.d.ts +93 -0
  113. package/dist/runtime/policy-change.js +305 -0
  114. package/dist/runtime/policy-log.d.ts +406 -0
  115. package/dist/runtime/policy-log.js +285 -0
  116. package/dist/runtime/policy.d.ts +8 -6
  117. package/dist/runtime/policy.js +5 -2
  118. package/dist/runtime/repointing.js +0 -0
  119. package/dist/runtime/reversibility.d.ts +11 -9
  120. package/dist/runtime/stakes.d.ts +129 -1
  121. package/dist/runtime/stakes.js +240 -26
  122. package/dist/runtime/vocabulary.d.ts +164 -7
  123. package/dist/runtime/vocabulary.js +181 -8
  124. package/dist/sdk/audit.d.ts +174 -3
  125. package/dist/sdk/audit.js +176 -17
  126. package/dist/sdk/catalogue.d.ts +9 -1
  127. package/dist/sdk/conformance.d.ts +37 -3
  128. package/dist/sdk/conformance.js +25 -4
  129. package/dist/sdk/definition.d.ts +129 -5
  130. package/dist/sdk/definition.js +66 -0
  131. package/dist/sdk/registry.d.ts +45 -2
  132. package/dist/sdk/registry.js +102 -1
  133. package/dist/signals/acted.contract.d.ts +21 -0
  134. package/dist/signals/acted.contract.js +236 -0
  135. package/dist/signals/acted.d.ts +359 -0
  136. package/dist/signals/acted.js +135 -0
  137. package/dist/signals/action-change.d.ts +507 -0
  138. package/dist/signals/action-change.js +265 -0
  139. package/dist/signals/action.d.ts +331 -0
  140. package/dist/signals/action.js +182 -0
  141. package/dist/signals/agenda.d.ts +585 -0
  142. package/dist/signals/agenda.js +295 -0
  143. package/dist/signals/applied-window.contract.d.ts +23 -0
  144. package/dist/signals/applied-window.contract.js +160 -0
  145. package/dist/signals/arrivals.contract.d.ts +21 -0
  146. package/dist/signals/arrivals.contract.js +180 -0
  147. package/dist/signals/arrivals.d.ts +375 -0
  148. package/dist/signals/arrivals.js +254 -0
  149. package/dist/signals/arriving.d.ts +210 -0
  150. package/dist/signals/arriving.js +133 -0
  151. package/dist/signals/audiences.d.ts +528 -0
  152. package/dist/signals/audiences.js +255 -0
  153. package/dist/signals/broadcast.d.ts +33 -17
  154. package/dist/signals/broadcast.js +255 -32
  155. package/dist/signals/change.d.ts +378 -0
  156. package/dist/signals/change.js +327 -0
  157. package/dist/signals/collect.d.ts +169 -34
  158. package/dist/signals/collect.js +186 -38
  159. package/dist/signals/conversion.d.ts +348 -0
  160. package/dist/signals/conversion.js +235 -0
  161. package/dist/signals/copy-change.d.ts +553 -0
  162. package/dist/signals/copy-change.js +377 -0
  163. package/dist/signals/copy.d.ts +337 -0
  164. package/dist/signals/copy.js +211 -0
  165. package/dist/signals/correlated.contract.d.ts +24 -0
  166. package/dist/signals/correlated.contract.js +94 -0
  167. package/dist/signals/counters.d.ts +97 -0
  168. package/dist/signals/counters.js +160 -0
  169. package/dist/signals/deliver.d.ts +23 -4
  170. package/dist/signals/deliver.js +34 -6
  171. package/dist/signals/deployment-change.d.ts +395 -0
  172. package/dist/signals/deployment-change.js +309 -0
  173. package/dist/signals/deployment.d.ts +278 -0
  174. package/dist/signals/deployment.js +178 -0
  175. package/dist/signals/door.d.ts +114 -0
  176. package/dist/signals/door.js +77 -0
  177. package/dist/signals/expire.d.ts +192 -0
  178. package/dist/signals/expire.js +213 -0
  179. package/dist/signals/fold.d.ts +10 -0
  180. package/dist/signals/fold.js +5 -1
  181. package/dist/signals/form-action.d.ts +302 -0
  182. package/dist/signals/form-action.js +169 -0
  183. package/dist/signals/form-copy.d.ts +523 -0
  184. package/dist/signals/form-copy.js +346 -0
  185. package/dist/signals/forms.d.ts +247 -0
  186. package/dist/signals/forms.js +147 -0
  187. package/dist/signals/frontier.d.ts +268 -0
  188. package/dist/signals/frontier.js +145 -0
  189. package/dist/signals/funnel-change.d.ts +548 -0
  190. package/dist/signals/funnel-change.js +361 -0
  191. package/dist/signals/funnel-hours.contract.d.ts +21 -0
  192. package/dist/signals/funnel-hours.contract.js +221 -0
  193. package/dist/signals/funnel-hours.d.ts +293 -0
  194. package/dist/signals/funnel-hours.js +99 -0
  195. package/dist/signals/funnel.d.ts +592 -0
  196. package/dist/signals/funnel.js +231 -0
  197. package/dist/signals/health.d.ts +694 -0
  198. package/dist/signals/health.js +573 -0
  199. package/dist/signals/index.d.ts +60 -0
  200. package/dist/signals/index.js +60 -0
  201. package/dist/signals/ingest.d.ts +56 -1
  202. package/dist/signals/ingest.js +91 -1
  203. package/dist/signals/inside.d.ts +190 -0
  204. package/dist/signals/inside.js +263 -0
  205. package/dist/signals/join.d.ts +56 -0
  206. package/dist/signals/join.js +60 -0
  207. package/dist/signals/journal.d.ts +123 -9
  208. package/dist/signals/kept.contract.d.ts +22 -0
  209. package/dist/signals/kept.contract.js +111 -0
  210. package/dist/signals/kinds.d.ts +8 -6
  211. package/dist/signals/kinds.js +14 -6
  212. package/dist/signals/library-action.d.ts +430 -0
  213. package/dist/signals/library-action.js +336 -0
  214. package/dist/signals/library-agenda-change.d.ts +324 -0
  215. package/dist/signals/library-agenda-change.js +199 -0
  216. package/dist/signals/library-agenda.d.ts +258 -0
  217. package/dist/signals/library-agenda.js +167 -0
  218. package/dist/signals/library-change.d.ts +477 -0
  219. package/dist/signals/library-change.js +335 -0
  220. package/dist/signals/library-copy-change.d.ts +498 -0
  221. package/dist/signals/library-copy-change.js +476 -0
  222. package/dist/signals/library-pace.d.ts +319 -0
  223. package/dist/signals/library-pace.js +305 -0
  224. package/dist/signals/library-reach-change.d.ts +646 -0
  225. package/dist/signals/library-reach-change.js +542 -0
  226. package/dist/signals/library-reach.d.ts +377 -0
  227. package/dist/signals/library-reach.js +227 -0
  228. package/dist/signals/library-senders-change.d.ts +457 -0
  229. package/dist/signals/library-senders-change.js +367 -0
  230. package/dist/signals/library-senders-reach-change.d.ts +486 -0
  231. package/dist/signals/library-senders-reach-change.js +538 -0
  232. package/dist/signals/library-senders-reach.d.ts +340 -0
  233. package/dist/signals/library-senders-reach.js +252 -0
  234. package/dist/signals/library-senders.d.ts +271 -0
  235. package/dist/signals/library-senders.js +180 -0
  236. package/dist/signals/library-word-reach-change.d.ts +567 -0
  237. package/dist/signals/library-word-reach-change.js +564 -0
  238. package/dist/signals/library.d.ts +306 -0
  239. package/dist/signals/library.js +178 -0
  240. package/dist/signals/memory.d.ts +9 -0
  241. package/dist/signals/memory.js +246 -14
  242. package/dist/signals/migrate.d.ts +20 -4
  243. package/dist/signals/migrate.js +269 -8
  244. package/dist/signals/notice.d.ts +389 -0
  245. package/dist/signals/notice.js +250 -0
  246. package/dist/signals/pace-change.d.ts +501 -0
  247. package/dist/signals/pace-change.js +310 -0
  248. package/dist/signals/pace.d.ts +296 -0
  249. package/dist/signals/pace.js +291 -0
  250. package/dist/signals/page-views.d.ts +365 -0
  251. package/dist/signals/page-views.js +218 -0
  252. package/dist/signals/part-pace.d.ts +378 -0
  253. package/dist/signals/part-pace.js +333 -0
  254. package/dist/signals/parts.d.ts +354 -0
  255. package/dist/signals/parts.js +232 -0
  256. package/dist/signals/postgres.d.ts +16 -2
  257. package/dist/signals/postgres.js +753 -76
  258. package/dist/signals/progress.d.ts +212 -0
  259. package/dist/signals/progress.js +150 -0
  260. package/dist/signals/ratios.d.ts +108 -0
  261. package/dist/signals/ratios.js +40 -0
  262. package/dist/signals/reach.d.ts +309 -0
  263. package/dist/signals/reach.js +93 -0
  264. package/dist/signals/readable.d.ts +34 -0
  265. package/dist/signals/readable.js +34 -0
  266. package/dist/signals/readership.d.ts +381 -0
  267. package/dist/signals/readership.js +308 -0
  268. package/dist/signals/region-pages.d.ts +173 -0
  269. package/dist/signals/region-pages.js +62 -0
  270. package/dist/signals/region.d.ts +287 -0
  271. package/dist/signals/region.js +198 -0
  272. package/dist/signals/retention.d.ts +163 -0
  273. package/dist/signals/retention.js +75 -0
  274. package/dist/signals/rhythm.d.ts +302 -0
  275. package/dist/signals/rhythm.js +262 -0
  276. package/dist/signals/rollup.d.ts +194 -4
  277. package/dist/signals/rollup.js +0 -0
  278. package/dist/signals/schema.d.ts +1069 -1
  279. package/dist/signals/schema.js +393 -3
  280. package/dist/signals/signal.d.ts +237 -11
  281. package/dist/signals/signal.js +70 -10
  282. package/dist/signals/silence-census.d.ts +205 -0
  283. package/dist/signals/silence-census.js +87 -0
  284. package/dist/signals/silences.d.ts +1147 -0
  285. package/dist/signals/silences.js +996 -0
  286. package/dist/signals/subtrees.d.ts +123 -0
  287. package/dist/signals/subtrees.js +102 -0
  288. package/dist/signals/tally.d.ts +262 -12
  289. package/dist/signals/unread.d.ts +292 -0
  290. package/dist/signals/unread.js +137 -0
  291. package/dist/signals/window-agenda.d.ts +276 -0
  292. package/dist/signals/window-agenda.js +83 -0
  293. package/dist/signals/window-figures.d.ts +148 -0
  294. package/dist/signals/window-figures.js +42 -0
  295. package/dist/signals/window-funnels.d.ts +88 -0
  296. package/dist/signals/window-funnels.js +47 -0
  297. package/dist/signals/window.d.ts +866 -0
  298. package/dist/signals/window.js +693 -0
  299. package/dist/signals/words.d.ts +24 -0
  300. package/dist/signals/words.js +24 -0
  301. package/dist/store/cache.d.ts +101 -0
  302. package/dist/store/cache.js +149 -0
  303. package/dist/store/history.d.ts +82 -0
  304. package/dist/store/history.js +36 -0
  305. package/dist/store/index.d.ts +30 -0
  306. package/dist/store/index.js +30 -0
  307. package/dist/store/memory.d.ts +2 -1
  308. package/dist/store/memory.js +48 -12
  309. package/dist/store/migrate.d.ts +106 -0
  310. package/dist/store/migrate.js +323 -7
  311. package/dist/store/postgres-applications.d.ts +4 -0
  312. package/dist/store/postgres-applications.js +197 -0
  313. package/dist/store/postgres-audience.d.ts +14 -0
  314. package/dist/store/postgres-audience.js +272 -0
  315. package/dist/store/postgres-holds.d.ts +10 -1
  316. package/dist/store/postgres-holds.js +116 -101
  317. package/dist/store/postgres-policy-log.d.ts +14 -0
  318. package/dist/store/postgres-policy-log.js +240 -0
  319. package/dist/store/postgres-projects.d.ts +4 -0
  320. package/dist/store/postgres-projects.js +217 -0
  321. package/dist/store/postgres.d.ts +17 -1
  322. package/dist/store/postgres.js +60 -9
  323. package/dist/store/replay.d.ts +102 -1
  324. package/dist/store/replay.js +190 -19
  325. package/dist/store/reseed.d.ts +238 -0
  326. package/dist/store/reseed.js +308 -0
  327. package/dist/store/schema.d.ts +762 -2
  328. package/dist/store/schema.js +218 -5
  329. package/dist/store/source.d.ts +57 -1
  330. package/dist/store/source.js +65 -1
  331. package/dist/store/store.d.ts +29 -6
  332. package/dist/submit/resolve.d.ts +1 -1
  333. package/dist/submit/resolve.js +2 -1
  334. package/dist/telemetry/calibration.d.ts +25 -0
  335. package/dist/telemetry/calibration.js +27 -1
  336. package/dist/telemetry/event.d.ts +391 -33
  337. package/dist/telemetry/event.js +84 -20
  338. package/dist/telemetry/index.d.ts +2 -0
  339. package/dist/telemetry/index.js +2 -0
  340. package/dist/telemetry/journal.d.ts +104 -3
  341. package/dist/telemetry/journal.js +9 -0
  342. package/dist/telemetry/lookup.d.ts +27 -0
  343. package/dist/telemetry/lookup.js +18 -0
  344. package/dist/telemetry/memory.js +33 -1
  345. package/dist/telemetry/migrate.js +22 -1
  346. package/dist/telemetry/postgres.d.ts +10 -1
  347. package/dist/telemetry/postgres.js +188 -90
  348. package/dist/telemetry/remeasure.d.ts +78 -0
  349. package/dist/telemetry/remeasure.js +83 -0
  350. package/dist/telemetry/retention.d.ts +2 -2
  351. package/dist/telemetry/schema.d.ts +17 -0
  352. package/dist/telemetry/schema.js +27 -1
  353. package/dist/telemetry/sink.d.ts +2 -2
  354. package/dist/telemetry/window.d.ts +135 -0
  355. package/dist/telemetry/window.js +101 -0
  356. package/dist/testing/application-contract.d.ts +14 -0
  357. package/dist/testing/application-contract.js +270 -0
  358. package/dist/testing/audience-overlay-contract.d.ts +31 -0
  359. package/dist/testing/audience-overlay-contract.js +354 -0
  360. package/dist/testing/contracts.d.ts +13 -7
  361. package/dist/testing/contracts.js +13 -7
  362. package/dist/testing/definitions.d.ts +78 -0
  363. package/dist/testing/definitions.js +134 -9
  364. package/dist/testing/doubles.d.ts +103 -0
  365. package/dist/testing/doubles.js +143 -0
  366. package/dist/testing/index.d.ts +2 -2
  367. package/dist/testing/index.js +2 -2
  368. package/dist/testing/journal-contract.js +297 -6
  369. package/dist/testing/model-contract.d.ts +110 -0
  370. package/dist/testing/model-contract.js +150 -0
  371. package/dist/testing/policy-log-contract.d.ts +26 -0
  372. package/dist/testing/policy-log-contract.js +344 -0
  373. package/dist/testing/project-contract.d.ts +14 -0
  374. package/dist/testing/project-contract.js +266 -0
  375. package/dist/testing/reader-signal-contract.d.ts +27 -2
  376. package/dist/testing/reader-signal-contract.js +475 -5
  377. package/dist/testing/store-contract.d.ts +7 -1
  378. package/dist/testing/store-contract.js +84 -3
  379. package/dist/theme/apply.d.ts +20 -1
  380. package/dist/theme/apply.js +5 -3
  381. package/dist/theme/contrast.d.ts +19 -14
  382. package/dist/theme/contrast.js +181 -17
  383. package/dist/theme/derive.d.ts +22 -0
  384. package/dist/theme/derive.js +54 -1
  385. package/dist/theme/library.js +1 -1
  386. package/dist/theme/measure.d.ts +34 -0
  387. package/dist/theme/measure.js +30 -1
  388. package/dist/theme/palettes.js +7 -7
  389. package/dist/theme/ramp.d.ts +64 -0
  390. package/dist/theme/ramp.js +114 -0
  391. package/dist/theme/registry.d.ts +9 -0
  392. package/dist/theme/registry.js +11 -1
  393. package/dist/theme/separation.d.ts +97 -3
  394. package/dist/theme/separation.js +75 -0
  395. package/dist/tree/apply.js +13 -0
  396. package/dist/tree/compare.d.ts +9 -0
  397. package/dist/tree/compare.js +18 -0
  398. package/dist/tree/mutation.d.ts +18 -0
  399. package/dist/tree/mutation.js +24 -12
  400. package/dist/tree/navigation.d.ts +5 -0
  401. package/dist/tree/navigation.js +32 -10
  402. package/dist/tree/outline.d.ts +7 -0
  403. package/dist/tree/outline.js +9 -5
  404. package/dist/write/computed.d.ts +60 -0
  405. package/dist/write/computed.js +61 -0
  406. package/dist/write/held.d.ts +59 -0
  407. package/dist/write/index.d.ts +5 -2
  408. package/dist/write/index.js +5 -2
  409. package/dist/write/reseed.d.ts +151 -0
  410. package/dist/write/reseed.js +184 -0
  411. package/dist/write/revert.js +2 -16
  412. package/package.json +25 -3
  413. package/dist/runtime/inverse-interpreter.d.ts +0 -90
  414. package/dist/runtime/inverse-interpreter.js +0 -56
@@ -1,17 +1,22 @@
1
- import { and, asc, desc, eq, gt, lt, sql } from "drizzle-orm";
1
+ import { and, asc, desc, eq, gt, gte, isNotNull, isNull, lt, min, sql } from "drizzle-orm";
2
2
  import { z } from "zod";
3
+ import { DEFAULT_AUDIENCE_ID, audienceIdSchema } from "../audience/audience.js";
3
4
  import { nodeIdSchema, treeIdSchema } from "../ids.js";
4
5
  import { cursorPosition, pageEnds } from "../paging.js";
5
6
  import { primitiveTypeSchema } from "../primitive-type.js";
7
+ import { DEFAULT_APPLICATION_ID, } from "../application/application.js";
6
8
  import { err, ok } from "../result.js";
9
+ import { actedKey, addFunnelHourCounts, funnelHourKey, addActedCounts, addArrivalCounts, addFunnelAnswers, addPageViewCounts, addRegionCounts, addTallies, arrivalKey, foldedBy, funnelKey, pageViewKey, regionKey, tallyKey, } from "./counters.js";
10
+ import { arrivalHoursOf } from "./arrivals.js";
7
11
  import { clampReaderSignalLimit, } from "./journal.js";
12
+ import { readerRegionSchema, } from "./region.js";
8
13
  import { readerSignalKindSchema, readerSignalSchema, viewKeySchema } from "./signal.js";
9
- import { loomReaderFunnels, loomReaderSignals, loomReaderTallies } from "./schema.js";
14
+ import { loomReaderActedHours, loomReaderArrivals, loomReaderFunnelHours, loomReaderFunnels, loomReaderPageViews, loomReaderRegions, loomReaderSignals, loomReaderTallies, } from "./schema.js";
10
15
  export * from "./migrate.js";
11
16
  /**
12
17
  * The Drizzle tables are **not** re-exported, unlike the journal's.
13
18
  *
14
- * A deployment needs the two factories below and `ensureReaderSignalsSchema`;
19
+ * A deployment needs the three factories below and `ensureReaderSignalsSchema`;
15
20
  * a `PgTableWithColumns` is how this file talks to the driver. Publishing the
16
21
  * three of them put about 7 KB of generated Drizzle type into the API
17
22
  * reference a reader downloads — for `loomReaderTallies` alone, a signature
@@ -27,6 +32,16 @@ export * from "./migrate.js";
27
32
  * browser composed, and a batch written by an older parser must not reach a
28
33
  * rollup unvalidated.
29
34
  */
35
+ /**
36
+ * Each of the three factories below is a public function that resolves the
37
+ * application and a private one that has it.
38
+ *
39
+ * The split is what keeps `application` a plain `ApplicationId` everywhere it
40
+ * is read rather than an optional unwrapped at each use, so a query cannot
41
+ * filter on `undefined` by forgetting the default. It also leaves the bodies
42
+ * where they were: scoping these tables changed the lines that name a column
43
+ * and nothing else, which is what a reader of the diff should be able to see.
44
+ */
30
45
  const failure = (cause, detail) => ({
31
46
  code: "unavailable",
32
47
  detail: `${detail}: ${cause instanceof Error ? cause.message : String(cause)}`,
@@ -43,6 +58,8 @@ const instantSchema = z
43
58
  .transform((value) => value.toISOString());
44
59
  const receivedSchema = z.object({
45
60
  seq: z.number().int().nonnegative(),
61
+ /** What the batch was filed under, which is always a value — see `ReceivedBatch.audienceId`. */
62
+ audienceId: audienceIdSchema,
46
63
  treeId: treeIdSchema,
47
64
  revision: z.number().int().nonnegative(),
48
65
  sentAt: z.number().int().nonnegative(),
@@ -50,6 +67,14 @@ const receivedSchema = z.object({
50
67
  view: viewKeySchema.nullable().transform((value) => value ?? undefined),
51
68
  signals: z.array(readerSignalSchema).min(1),
52
69
  receivedAt: instantSchema,
70
+ /**
71
+ * `null` in the column, absent on the row — a delivery no fold has counted
72
+ * (0351). The same nullable-to-absent transform `view` takes, for the same
73
+ * reason: an optional field is how the rest of the subsystem spells *there is
74
+ * nothing here*, and two spellings of that is how a `null` comes to be read
75
+ * as a value.
76
+ */
77
+ foldedAt: instantSchema.nullable().transform((value) => value ?? undefined),
53
78
  });
54
79
  /**
55
80
  * `bigint` columns come back as strings from one driver and numbers from
@@ -58,6 +83,7 @@ const receivedSchema = z.object({
58
83
  */
59
84
  const countSchema = z.coerce.number().int().nonnegative();
60
85
  const storedTallySchema = z.object({
86
+ audienceId: audienceIdSchema,
61
87
  treeId: treeIdSchema,
62
88
  revision: z.number().int().nonnegative(),
63
89
  nodeId: nodeIdSchema,
@@ -69,10 +95,12 @@ const storedTallySchema = z.object({
69
95
  activations: countSchema,
70
96
  opens: countSchema,
71
97
  closes: countSchema,
98
+ completions: countSchema,
72
99
  updatedAt: instantSchema,
73
100
  });
74
101
  const storedFunnelSchema = z
75
102
  .object({
103
+ audienceId: audienceIdSchema,
76
104
  treeId: treeIdSchema,
77
105
  revision: z.number().int().nonnegative(),
78
106
  fromNodeId: nodeIdSchema,
@@ -84,6 +112,7 @@ const storedFunnelSchema = z
84
112
  updatedAt: instantSchema,
85
113
  })
86
114
  .transform((row) => ({
115
+ audienceId: row.audienceId,
87
116
  treeId: row.treeId,
88
117
  revision: row.revision,
89
118
  pair: {
@@ -94,12 +123,54 @@ const storedFunnelSchema = z
94
123
  converted: row.converted,
95
124
  updatedAt: row.updatedAt,
96
125
  }));
97
- export const postgresReaderSignalJournal = (db) => ({
126
+ /**
127
+ * `folded_at` is absent rather than `null` on the way out, because *nothing
128
+ * attests a fold* is one state and a reading should not have two spellings of
129
+ * it to check (0274). A row written before the column existed and one no fold
130
+ * has touched are the same answer.
131
+ */
132
+ const storedPageViewsSchema = z
133
+ .object({
134
+ audienceId: audienceIdSchema,
135
+ treeId: treeIdSchema,
136
+ revision: z.number().int().nonnegative(),
137
+ opened: countSchema,
138
+ appearances: countSchema,
139
+ updatedAt: instantSchema,
140
+ foldedAt: instantSchema.nullable().default(null),
141
+ })
142
+ .transform((row) => ({
143
+ audienceId: row.audienceId,
144
+ treeId: row.treeId,
145
+ revision: row.revision,
146
+ opened: row.opened,
147
+ appearances: row.appearances,
148
+ updatedAt: row.updatedAt,
149
+ ...(row.foldedAt === null ? {} : { foldedAt: row.foldedAt }),
150
+ }));
151
+ /**
152
+ * Every read is filtered by the application this handle speaks for (0269), and
153
+ * every write sets it.
154
+ *
155
+ * A reader is filed against the application whose page they read. Folding two
156
+ * would add two customers' counts together, and the addition is not
157
+ * recoverable from the rows afterwards — which is why this landed before
158
+ * anything could construct a second application rather than after.
159
+ */
160
+ export const postgresReaderSignalJournal = (db, scope = {}) => journalIn(db, scope.application ?? DEFAULT_APPLICATION_ID);
161
+ const journalIn = (db, application) => ({
98
162
  receive: async (batches) => {
99
163
  if (batches.length === 0)
100
164
  return ok(undefined);
101
165
  try {
102
166
  await db.insert(loomReaderSignals).values(batches.map((batch) => ({
167
+ applicationId: application,
168
+ /**
169
+ * Resolved here rather than defaulted by the column, so the row a
170
+ * fold reads says what it was filed under whichever sender wrote it
171
+ * (0284).
172
+ */
173
+ audienceId: batch.audience ?? DEFAULT_AUDIENCE_ID,
103
174
  treeId: batch.treeId,
104
175
  revision: batch.revision,
105
176
  sentAt: batch.sentAt,
@@ -121,15 +192,34 @@ export const postgresReaderSignalJournal = (db) => ({
121
192
  const rows = await db
122
193
  .select({
123
194
  seq: loomReaderSignals.seq,
195
+ audienceId: loomReaderSignals.audienceId,
124
196
  treeId: loomReaderSignals.treeId,
125
197
  revision: loomReaderSignals.revision,
126
198
  sentAt: loomReaderSignals.sentAt,
127
199
  view: loomReaderSignals.view,
128
200
  signals: loomReaderSignals.signals,
129
201
  receivedAt: loomReaderSignals.receivedAt,
202
+ foldedAt: loomReaderSignals.foldedAt,
130
203
  })
131
204
  .from(loomReaderSignals)
132
- .where(and(request?.treeId === undefined ? undefined : eq(loomReaderSignals.treeId, request.treeId), from === undefined
205
+ .where(and(eq(loomReaderSignals.applicationId, application), request?.treeId === undefined ? undefined : eq(loomReaderSignals.treeId, request.treeId),
206
+ /**
207
+ * Served by the partial index where it is `false`, which is the
208
+ * fold's read and the one that would otherwise scan the kept window
209
+ * (0351).
210
+ */
211
+ request?.counted === undefined
212
+ ? undefined
213
+ : request.counted
214
+ ? isNotNull(loomReaderSignals.foldedAt)
215
+ : isNull(loomReaderSignals.foldedAt),
216
+ /**
217
+ * One page view's deliveries (0355), served by the partial index on
218
+ * the column. A request naming a key that no row carries is an
219
+ * empty page, which is what a page view whose rows have been swept
220
+ * is.
221
+ */
222
+ request?.view === undefined ? undefined : eq(loomReaderSignals.view, request.view), from === undefined
133
223
  ? undefined
134
224
  : direction === "older"
135
225
  ? lt(loomReaderSignals.seq, from)
@@ -160,12 +250,43 @@ export const postgresReaderSignalJournal = (db) => ({
160
250
  return err(failure(cause, "could not read reader signals"));
161
251
  }
162
252
  },
253
+ /**
254
+ * One `UPDATE`, carrying its own idempotence in the `WHERE` (0351).
255
+ *
256
+ * `folded_at IS NULL` is what makes a repeat of this statement a no-op rather
257
+ * than a rewrite: the caller has already made the counters durable before it
258
+ * gets here, so a retry must not move an instant that a reading has been
259
+ * taken against. It is also what makes the reported count honest — rows this
260
+ * call stamped, not rows below the position.
261
+ *
262
+ * Scoped to the application like every other statement in this journal: `seq`
263
+ * is database-wide, so an unscoped `UPDATE` would stamp another deployment's
264
+ * uncounted deliveries and make them eligible for its retention sweep.
265
+ */
266
+ counted: async ({ before, at }) => {
267
+ try {
268
+ const stamped = await db
269
+ .update(loomReaderSignals)
270
+ .set({ foldedAt: new Date(at) })
271
+ .where(and(eq(loomReaderSignals.applicationId, application), lt(loomReaderSignals.seq, before), isNull(loomReaderSignals.foldedAt)))
272
+ .returning({ seq: loomReaderSignals.seq });
273
+ return ok({ counted: stamped.length });
274
+ }
275
+ catch (cause) {
276
+ return err(failure(cause, `could not record the fold of reader signals below ${before}`));
277
+ }
278
+ },
163
279
  /** The count comes from the returned rows, because the two drivers report `rowCount` differently. */
164
280
  forget: async ({ before }) => {
165
281
  try {
166
282
  const removed = await db
167
283
  .delete(loomReaderSignals)
168
- .where(lt(loomReaderSignals.seq, before))
284
+ /**
285
+ * Scoped like every read, so retention run for one application cannot
286
+ * drop another's buffer. `seq` is database-wide, so an unfiltered
287
+ * `DELETE` here would have taken every application's rows below it.
288
+ */
289
+ .where(and(eq(loomReaderSignals.applicationId, application), lt(loomReaderSignals.seq, before)))
169
290
  .returning({ seq: loomReaderSignals.seq });
170
291
  return ok({ removed: removed.length });
171
292
  }
@@ -174,121 +295,677 @@ export const postgresReaderSignalJournal = (db) => ({
174
295
  }
175
296
  },
176
297
  });
177
- /** Both counter tables are filtered the same way, on the two columns they share. */
178
- const within = (columns, request) => and(request?.treeId === undefined ? undefined : eq(columns.treeId, request.treeId), request?.revision === undefined ? undefined : eq(columns.revision, request.revision));
179
- export const postgresReaderTallyStore = (db) => ({
298
+ /**
299
+ * Every counter table is filtered the same way, on the four columns they all
300
+ * share.
301
+ *
302
+ * The application is first and is not optional, unlike the three a request may
303
+ * narrow by: a reading always happens inside one application, and taking it
304
+ * here rather than at each call site is what stops a counter query being
305
+ * written without it.
306
+ *
307
+ * **The audience is one of the optional three** (0284), which is the difference
308
+ * between the two dimensions in one line: no reading spans two applications,
309
+ * and a reading that compares two audiences is the point of serving two.
310
+ */
311
+ const within = (columns, application, request) => and(eq(columns.applicationId, application), request?.audience === undefined ? undefined : eq(columns.audienceId, request.audience), request?.treeId === undefined ? undefined : eq(columns.treeId, request.treeId), request?.revision === undefined ? undefined : eq(columns.revision, request.revision));
312
+ /**
313
+ * An hour comes back as a `Date` and is read as the instant it began, in
314
+ * milliseconds — which is what the vocabulary above this boundary counts in,
315
+ * because an hour is a bucket arithmetic is done on rather than a label.
316
+ */
317
+ const hourSchema = z
318
+ .union([z.date(), z.string()])
319
+ .transform((value) => (value instanceof Date ? value : new Date(value)))
320
+ .refine((value) => Number.isFinite(value.getTime()), "is not an instant")
321
+ .transform((value) => value.getTime());
322
+ const storedArrivalSchema = z.object({
323
+ audienceId: audienceIdSchema,
324
+ treeId: treeIdSchema,
325
+ revision: z.number().int().nonnegative(),
326
+ hour: hourSchema,
327
+ views: countSchema,
328
+ updatedAt: instantSchema,
329
+ });
330
+ /**
331
+ * The four end columns become the pair the reading matches on, exactly as the
332
+ * funnel row's do — one spelling of what the two ends are, so the hourly slice
333
+ * and the row it slices cannot disagree about which question they answer.
334
+ */
335
+ const storedFunnelHourSchema = z
336
+ .object({
337
+ audienceId: audienceIdSchema,
338
+ treeId: treeIdSchema,
339
+ revision: z.number().int().nonnegative(),
340
+ fromNodeId: nodeIdSchema,
341
+ fromKind: readerSignalKindSchema,
342
+ toNodeId: nodeIdSchema,
343
+ toKind: readerSignalKindSchema,
344
+ hour: hourSchema,
345
+ reached: countSchema,
346
+ converted: countSchema,
347
+ updatedAt: instantSchema,
348
+ })
349
+ .transform((row) => ({
350
+ audienceId: row.audienceId,
351
+ treeId: row.treeId,
352
+ revision: row.revision,
353
+ pair: {
354
+ from: { nodeId: row.fromNodeId, kind: row.fromKind },
355
+ to: { nodeId: row.toNodeId, kind: row.toKind },
356
+ },
357
+ hour: row.hour,
358
+ reached: row.reached,
359
+ converted: row.converted,
360
+ updatedAt: row.updatedAt,
361
+ }));
362
+ const storedActedSchema = z.object({
363
+ audienceId: audienceIdSchema,
364
+ treeId: treeIdSchema,
365
+ revision: z.number().int().nonnegative(),
366
+ nodeId: nodeIdSchema,
367
+ type: primitiveTypeSchema,
368
+ hour: hourSchema,
369
+ activations: countSchema,
370
+ opens: countSchema,
371
+ closes: countSchema,
372
+ completions: countSchema,
373
+ updatedAt: instantSchema,
374
+ });
375
+ /** Scoped on the same terms as the journal — see its note. */
376
+ export const postgresReaderTallyStore = (db, scope = {}) => tallyStoreIn(db, scope.application ?? DEFAULT_APPLICATION_ID);
377
+ /**
378
+ * The entries of a window that have anything to write, each with its audience
379
+ * resolved.
380
+ *
381
+ * Two entries naming one audience are left as two: they are two commands in
382
+ * one transaction, and a command's effect is visible to the next one, so they
383
+ * add — which is what `ReaderTallyStore.applyWindow` promises and what the
384
+ * memory store does by writing through a `Map`. Gathering them first would have
385
+ * been fewer statements and **the same answer**, so it would have been a line
386
+ * no test could fail on (0231). The guarantee that matters is per command, and
387
+ * `foldedBy` below is where it is kept: `ON CONFLICT … DO UPDATE` refuses to
388
+ * affect one row twice inside a single command, which is the disagreement
389
+ * `counters.ts` was bitten by.
390
+ */
391
+ const writable = (window) => window
392
+ .filter(({ tallies, funnels, appearances = [], acted = [], funnelHours = [] }) => tallies.length > 0 ||
393
+ funnels.length > 0 ||
394
+ appearances.length > 0 ||
395
+ acted.length > 0 ||
396
+ funnelHours.length > 0)
397
+ .map((counters) => ({ ...counters, audience: counters.audience ?? DEFAULT_AUDIENCE_ID }));
398
+ const tallyStoreIn = (db, application) => {
180
399
  /**
181
- * Two upserts in one transaction.
400
+ * Three upserts per audience, every audience in one transaction.
182
401
  *
183
402
  * `ON CONFLICT … DO UPDATE SET x = table.x + excluded.x` is what makes
184
403
  * applying a rollup additive in one statement rather than a read, an add and
185
404
  * a write with a race in the middle. Two rollups running at once is not
186
405
  * hypothetical — a scheduled one and a manual one is the ordinary way it
187
406
  * happens — and this is the version that is correct when they do.
407
+ *
408
+ * **The transaction spans the audiences and not just the statements** (0316).
409
+ * A window folded per audience and written in separate transactions has a
410
+ * state between them where some audiences are durable and the rest are not,
411
+ * and the buffer still holds the window — so the next run adds the durable
412
+ * ones again, permanently and invisibly. One transaction removes the state.
188
413
  */
189
- apply: async ({ tallies, funnels }, at) => {
190
- if (tallies.length === 0 && funnels.length === 0)
414
+ const applyWindow = async (window, at) => {
415
+ const groups = writable(window);
416
+ if (groups.length === 0)
191
417
  return ok(undefined);
192
418
  const updatedAt = new Date(at);
193
419
  try {
194
420
  await db.transaction(async (tx) => {
195
- if (tallies.length > 0) {
421
+ for (const { audience, tallies, funnels, appearances = [], acted = [], funnelHours = [], } of groups) {
422
+ if (tallies.length > 0) {
423
+ await tx
424
+ .insert(loomReaderTallies)
425
+ .values(foldedBy(tallies, tallyKey, addTallies).map((tally) => ({
426
+ applicationId: application,
427
+ audienceId: audience,
428
+ treeId: tally.treeId,
429
+ revision: tally.revision,
430
+ nodeId: tally.nodeId,
431
+ type: tally.type,
432
+ views: tally.views,
433
+ reached: tally.reached,
434
+ engaged: tally.engaged,
435
+ dwellMs: tally.dwellMs,
436
+ activations: tally.activations,
437
+ opens: tally.opens,
438
+ closes: tally.closes,
439
+ completions: tally.completions,
440
+ updatedAt,
441
+ })))
442
+ .onConflictDoUpdate({
443
+ target: [
444
+ loomReaderTallies.applicationId,
445
+ loomReaderTallies.audienceId,
446
+ loomReaderTallies.treeId,
447
+ loomReaderTallies.revision,
448
+ loomReaderTallies.nodeId,
449
+ ],
450
+ set: {
451
+ type: sql `excluded.type`,
452
+ views: sql `${loomReaderTallies.views} + excluded.views`,
453
+ reached: sql `${loomReaderTallies.reached} + excluded.reached`,
454
+ engaged: sql `${loomReaderTallies.engaged} + excluded.engaged`,
455
+ dwellMs: sql `${loomReaderTallies.dwellMs} + excluded.dwell_ms`,
456
+ activations: sql `${loomReaderTallies.activations} + excluded.activations`,
457
+ opens: sql `${loomReaderTallies.opens} + excluded.opens`,
458
+ closes: sql `${loomReaderTallies.closes} + excluded.closes`,
459
+ completions: sql `${loomReaderTallies.completions} + excluded.completions`,
460
+ updatedAt,
461
+ },
462
+ });
463
+ }
464
+ if (acted.length > 0) {
465
+ /**
466
+ * The hourly slice of the four occurrence counters above
467
+ * (`acted.ts`), in the same transaction and from the same fold —
468
+ * so a part's hours sum to the part's own counters rather than to
469
+ * whatever survived a second write.
470
+ */
471
+ await tx
472
+ .insert(loomReaderActedHours)
473
+ .values(foldedBy(acted, actedKey, addActedCounts).map((count) => ({
474
+ applicationId: application,
475
+ audienceId: audience,
476
+ treeId: count.treeId,
477
+ revision: count.revision,
478
+ nodeId: count.nodeId,
479
+ hour: new Date(count.hour),
480
+ type: count.type,
481
+ activations: count.activations,
482
+ opens: count.opens,
483
+ closes: count.closes,
484
+ completions: count.completions,
485
+ updatedAt,
486
+ })))
487
+ .onConflictDoUpdate({
488
+ target: [
489
+ loomReaderActedHours.applicationId,
490
+ loomReaderActedHours.audienceId,
491
+ loomReaderActedHours.treeId,
492
+ loomReaderActedHours.revision,
493
+ loomReaderActedHours.nodeId,
494
+ loomReaderActedHours.hour,
495
+ ],
496
+ set: {
497
+ type: sql `excluded.type`,
498
+ activations: sql `${loomReaderActedHours.activations} + excluded.activations`,
499
+ opens: sql `${loomReaderActedHours.opens} + excluded.opens`,
500
+ closes: sql `${loomReaderActedHours.closes} + excluded.closes`,
501
+ completions: sql `${loomReaderActedHours.completions} + excluded.completions`,
502
+ updatedAt,
503
+ },
504
+ });
505
+ }
506
+ if (appearances.length > 0) {
507
+ /**
508
+ * In the same transaction as the tallies, because the two numbers a
509
+ * reading subtracts have to describe the same window: a page-view row
510
+ * that moved while the counters it is compared against did not would
511
+ * report a straddle that never happened.
512
+ */
513
+ await tx
514
+ .insert(loomReaderPageViews)
515
+ .values(foldedBy(appearances, pageViewKey, addPageViewCounts).map((count) => ({
516
+ applicationId: application,
517
+ audienceId: audience,
518
+ treeId: count.treeId,
519
+ revision: count.revision,
520
+ appearances: count.views,
521
+ updatedAt,
522
+ foldedAt: updatedAt,
523
+ })))
524
+ .onConflictDoUpdate({
525
+ target: [
526
+ loomReaderPageViews.applicationId,
527
+ loomReaderPageViews.audienceId,
528
+ loomReaderPageViews.treeId,
529
+ loomReaderPageViews.revision,
530
+ ],
531
+ set: {
532
+ appearances: sql `${loomReaderPageViews.appearances} + excluded.appearances`,
533
+ updatedAt,
534
+ /**
535
+ * The fold's own timestamp, which only this writer sets (0274).
536
+ * The door's upsert below leaves the column out of its `set`
537
+ * entirely, so an arrival can never make a stalled collection
538
+ * look like a fresh one.
539
+ */
540
+ foldedAt: updatedAt,
541
+ },
542
+ });
543
+ }
544
+ if (funnels.length > 0) {
545
+ await tx
546
+ .insert(loomReaderFunnels)
547
+ .values(foldedBy(funnels, funnelKey, addFunnelAnswers).map((answer) => ({
548
+ applicationId: application,
549
+ audienceId: audience,
550
+ treeId: answer.treeId,
551
+ revision: answer.revision,
552
+ fromNodeId: answer.pair.from.nodeId,
553
+ fromKind: answer.pair.from.kind,
554
+ toNodeId: answer.pair.to.nodeId,
555
+ toKind: answer.pair.to.kind,
556
+ reached: answer.reached,
557
+ converted: answer.converted,
558
+ updatedAt,
559
+ })))
560
+ .onConflictDoUpdate({
561
+ target: [
562
+ loomReaderFunnels.applicationId,
563
+ loomReaderFunnels.audienceId,
564
+ loomReaderFunnels.treeId,
565
+ loomReaderFunnels.revision,
566
+ loomReaderFunnels.fromNodeId,
567
+ loomReaderFunnels.fromKind,
568
+ loomReaderFunnels.toNodeId,
569
+ loomReaderFunnels.toKind,
570
+ ],
571
+ set: {
572
+ reached: sql `${loomReaderFunnels.reached} + excluded.reached`,
573
+ converted: sql `${loomReaderFunnels.converted} + excluded.converted`,
574
+ updatedAt,
575
+ },
576
+ });
577
+ }
578
+ if (funnelHours.length > 0) {
579
+ /**
580
+ * The hourly cut of the answers above (`funnel-hours.ts`), in the
581
+ * same transaction and from the same fold — so a pair's hours sum
582
+ * to the pair's own row rather than to whatever survived a second
583
+ * write.
584
+ */
585
+ await tx
586
+ .insert(loomReaderFunnelHours)
587
+ .values(foldedBy(funnelHours, funnelHourKey, addFunnelHourCounts).map((count) => ({
588
+ applicationId: application,
589
+ audienceId: audience,
590
+ treeId: count.treeId,
591
+ revision: count.revision,
592
+ fromNodeId: count.pair.from.nodeId,
593
+ fromKind: count.pair.from.kind,
594
+ toNodeId: count.pair.to.nodeId,
595
+ toKind: count.pair.to.kind,
596
+ hour: new Date(count.hour),
597
+ reached: count.reached,
598
+ converted: count.converted,
599
+ updatedAt,
600
+ })))
601
+ .onConflictDoUpdate({
602
+ target: [
603
+ loomReaderFunnelHours.applicationId,
604
+ loomReaderFunnelHours.audienceId,
605
+ loomReaderFunnelHours.treeId,
606
+ loomReaderFunnelHours.revision,
607
+ loomReaderFunnelHours.fromNodeId,
608
+ loomReaderFunnelHours.fromKind,
609
+ loomReaderFunnelHours.toNodeId,
610
+ loomReaderFunnelHours.toKind,
611
+ loomReaderFunnelHours.hour,
612
+ ],
613
+ set: {
614
+ reached: sql `${loomReaderFunnelHours.reached} + excluded.reached`,
615
+ converted: sql `${loomReaderFunnelHours.converted} + excluded.converted`,
616
+ updatedAt,
617
+ },
618
+ });
619
+ }
620
+ }
621
+ });
622
+ return ok(undefined);
623
+ }
624
+ catch (cause) {
625
+ const tallies = groups.reduce((total, group) => total + group.tallies.length, 0);
626
+ const funnels = groups.reduce((total, group) => total + group.funnels.length, 0);
627
+ return err(failure(cause, `could not apply ${tallies} tallies and ${funnels} funnels across ${groups.length} ${groups.length === 1 ? "audience" : "audiences"}`));
628
+ }
629
+ };
630
+ return {
631
+ applyWindow,
632
+ apply: (counters, at, audience = DEFAULT_AUDIENCE_ID) => applyWindow([{ ...counters, audience }], at),
633
+ /**
634
+ * One upsert, additive in the statement, for the reason the regions are: this
635
+ * runs on the request path and two readers arriving together is the ordinary
636
+ * case rather than the unlucky one.
637
+ *
638
+ * It touches one column and leaves the other where it was, so a door and a
639
+ * rollup writing the same row at the same instant cannot lose each other's
640
+ * number.
641
+ *
642
+ * **`folded_at` is absent from the insert and from the `set`**, which is the
643
+ * whole of what makes it readable as a fold's age (0274): an arrival must
644
+ * never move it, and a row this write creates attests no fold rather than
645
+ * attesting one at the moment a reader arrived.
646
+ */
647
+ opened: async (openings, at, audience = DEFAULT_AUDIENCE_ID) => {
648
+ if (openings.length === 0)
649
+ return ok(undefined);
650
+ const hours = arrivalHoursOf(openings, at);
651
+ if (!hours.ok)
652
+ return hours;
653
+ const updatedAt = new Date(at);
654
+ try {
655
+ /**
656
+ * **Two upserts in one transaction**, which is the pair the interface
657
+ * promises: the revision's total and the hour it arrived in are the
658
+ * same arrivals under two keys, and a door that moved one without the
659
+ * other would leave an hourly chart that does not sum to the page-view
660
+ * count it is drawn beside — permanently, because a count cannot be
661
+ * uncounted (0158).
662
+ */
663
+ await db.transaction(async (tx) => {
196
664
  await tx
197
- .insert(loomReaderTallies)
198
- .values(tallies.map((tally) => ({
199
- treeId: tally.treeId,
200
- revision: tally.revision,
201
- nodeId: tally.nodeId,
202
- type: tally.type,
203
- views: tally.views,
204
- reached: tally.reached,
205
- engaged: tally.engaged,
206
- dwellMs: tally.dwellMs,
207
- activations: tally.activations,
208
- opens: tally.opens,
209
- closes: tally.closes,
665
+ .insert(loomReaderPageViews)
666
+ .values(foldedBy(openings, pageViewKey, addPageViewCounts).map((count) => ({
667
+ applicationId: application,
668
+ audienceId: audience,
669
+ treeId: count.treeId,
670
+ revision: count.revision,
671
+ opened: count.views,
210
672
  updatedAt,
211
673
  })))
212
674
  .onConflictDoUpdate({
213
- target: [loomReaderTallies.treeId, loomReaderTallies.revision, loomReaderTallies.nodeId],
675
+ target: [
676
+ loomReaderPageViews.applicationId,
677
+ loomReaderPageViews.audienceId,
678
+ loomReaderPageViews.treeId,
679
+ loomReaderPageViews.revision,
680
+ ],
214
681
  set: {
215
- type: sql `excluded.type`,
216
- views: sql `${loomReaderTallies.views} + excluded.views`,
217
- reached: sql `${loomReaderTallies.reached} + excluded.reached`,
218
- engaged: sql `${loomReaderTallies.engaged} + excluded.engaged`,
219
- dwellMs: sql `${loomReaderTallies.dwellMs} + excluded.dwell_ms`,
220
- activations: sql `${loomReaderTallies.activations} + excluded.activations`,
221
- opens: sql `${loomReaderTallies.opens} + excluded.opens`,
222
- closes: sql `${loomReaderTallies.closes} + excluded.closes`,
682
+ opened: sql `${loomReaderPageViews.opened} + excluded.opened`,
223
683
  updatedAt,
224
684
  },
225
685
  });
226
- }
227
- if (funnels.length > 0) {
228
686
  await tx
229
- .insert(loomReaderFunnels)
230
- .values(funnels.map((answer) => ({
231
- treeId: answer.treeId,
232
- revision: answer.revision,
233
- fromNodeId: answer.pair.from.nodeId,
234
- fromKind: answer.pair.from.kind,
235
- toNodeId: answer.pair.to.nodeId,
236
- toKind: answer.pair.to.kind,
237
- reached: answer.reached,
238
- converted: answer.converted,
687
+ .insert(loomReaderArrivals)
688
+ .values(foldedBy(hours.value, arrivalKey, addArrivalCounts).map((count) => ({
689
+ applicationId: application,
690
+ audienceId: audience,
691
+ treeId: count.treeId,
692
+ revision: count.revision,
693
+ hour: new Date(count.hour),
694
+ views: count.views,
239
695
  updatedAt,
240
696
  })))
241
697
  .onConflictDoUpdate({
242
698
  target: [
243
- loomReaderFunnels.treeId,
244
- loomReaderFunnels.revision,
245
- loomReaderFunnels.fromNodeId,
246
- loomReaderFunnels.fromKind,
247
- loomReaderFunnels.toNodeId,
248
- loomReaderFunnels.toKind,
699
+ loomReaderArrivals.applicationId,
700
+ loomReaderArrivals.audienceId,
701
+ loomReaderArrivals.treeId,
702
+ loomReaderArrivals.revision,
703
+ loomReaderArrivals.hour,
249
704
  ],
250
705
  set: {
251
- reached: sql `${loomReaderFunnels.reached} + excluded.reached`,
252
- converted: sql `${loomReaderFunnels.converted} + excluded.converted`,
706
+ views: sql `${loomReaderArrivals.views} + excluded.views`,
253
707
  updatedAt,
254
708
  },
255
709
  });
710
+ });
711
+ return ok(undefined);
712
+ }
713
+ catch (cause) {
714
+ return err(failure(cause, `could not count ${openings.length} page views`));
715
+ }
716
+ },
717
+ tallies: async (request) => {
718
+ try {
719
+ const rows = await db.select().from(loomReaderTallies).where(within(loomReaderTallies, application, request));
720
+ const parsed = z.array(storedTallySchema).safeParse(rows);
721
+ return parsed.success
722
+ ? ok(parsed.data)
723
+ : err({
724
+ code: "unavailable",
725
+ detail: `a stored tally did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
726
+ });
727
+ }
728
+ catch (cause) {
729
+ return err(failure(cause, "could not read reader tallies"));
730
+ }
731
+ },
732
+ funnels: async (request) => {
733
+ try {
734
+ const rows = await db.select().from(loomReaderFunnels).where(within(loomReaderFunnels, application, request));
735
+ const parsed = z.array(storedFunnelSchema).safeParse(rows);
736
+ return parsed.success
737
+ ? ok(parsed.data)
738
+ : err({
739
+ code: "unavailable",
740
+ detail: `a stored funnel did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
741
+ });
742
+ }
743
+ catch (cause) {
744
+ return err(failure(cause, "could not read reader funnels"));
745
+ }
746
+ },
747
+ pageViews: async (request) => {
748
+ try {
749
+ const rows = await db
750
+ .select()
751
+ .from(loomReaderPageViews)
752
+ .where(within(loomReaderPageViews, application, request));
753
+ const parsed = z.array(storedPageViewsSchema).safeParse(rows);
754
+ return parsed.success
755
+ ? ok(parsed.data)
756
+ : err({
757
+ code: "unavailable",
758
+ detail: `a stored page-view count did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
759
+ });
760
+ }
761
+ catch (cause) {
762
+ return err(failure(cause, "could not read reader page views"));
763
+ }
764
+ },
765
+ /**
766
+ * Two statements and one call: the hours in the range, and the first hour
767
+ * the same narrowing has at all.
768
+ *
769
+ * The second is a `MIN` over an index whose leading column is the
770
+ * application, so it costs a lookup rather than a scan — and it is the only
771
+ * way to tell an empty range apart from a deployment that was not counting
772
+ * through it. Not in one statement with a window function, because the two
773
+ * answers have different shapes and a row carrying both would make every
774
+ * caller unpack a figure repeated on every row.
775
+ */
776
+ arrivals: async (request) => {
777
+ const scope = within(loomReaderArrivals, application, request);
778
+ try {
779
+ const rows = await db
780
+ .select()
781
+ .from(loomReaderArrivals)
782
+ .where(and(scope, request?.from === undefined
783
+ ? undefined
784
+ : gte(loomReaderArrivals.hour, new Date(request.from)), request?.to === undefined
785
+ ? undefined
786
+ : lt(loomReaderArrivals.hour, new Date(request.to))));
787
+ const parsed = z.array(storedArrivalSchema).safeParse(rows);
788
+ if (!parsed.success) {
789
+ return err({
790
+ code: "unavailable",
791
+ detail: `a stored arrival did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
792
+ });
256
793
  }
257
- });
794
+ const [earliest] = await db
795
+ .select({ hour: min(loomReaderArrivals.hour) })
796
+ .from(loomReaderArrivals)
797
+ .where(scope);
798
+ const begins = hourSchema.safeParse(earliest?.hour);
799
+ return ok({
800
+ hours: parsed.data,
801
+ ...(begins.success ? { begins: begins.data } : {}),
802
+ });
803
+ }
804
+ catch (cause) {
805
+ return err(failure(cause, "could not read reader arrivals"));
806
+ }
807
+ },
808
+ /**
809
+ * The rows in the range and the first hour the scope ever counted, as the
810
+ * arrivals read above answers them — with the part in **both** narrowings.
811
+ *
812
+ * A goal made today over a deployment that has counted for a year must
813
+ * report the first hour *that part* was acted in; taking the beginning over
814
+ * the deployment would draw a year of noughts behind a figure that has
815
+ * existed for an afternoon.
816
+ */
817
+ acted: async (request) => {
818
+ const scope = and(within(loomReaderActedHours, application, request), request?.nodeId === undefined ? undefined : eq(loomReaderActedHours.nodeId, request.nodeId));
819
+ try {
820
+ const rows = await db
821
+ .select()
822
+ .from(loomReaderActedHours)
823
+ .where(and(scope, request?.from === undefined
824
+ ? undefined
825
+ : gte(loomReaderActedHours.hour, new Date(request.from)), request?.to === undefined
826
+ ? undefined
827
+ : lt(loomReaderActedHours.hour, new Date(request.to))));
828
+ const parsed = z.array(storedActedSchema).safeParse(rows);
829
+ if (!parsed.success) {
830
+ return err({
831
+ code: "unavailable",
832
+ detail: `a stored action hour did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
833
+ });
834
+ }
835
+ const [earliest] = await db
836
+ .select({ hour: min(loomReaderActedHours.hour) })
837
+ .from(loomReaderActedHours)
838
+ .where(scope);
839
+ const begins = hourSchema.safeParse(earliest?.hour);
840
+ return ok({
841
+ hours: parsed.data,
842
+ ...(begins.success ? { begins: begins.data } : {}),
843
+ });
844
+ }
845
+ catch (cause) {
846
+ return err(failure(cause, "could not read what readers did by hour"));
847
+ }
848
+ },
849
+ /**
850
+ * The same two answers again, narrowed to one funnel as well.
851
+ *
852
+ * The pair narrows **both** halves, for the reason the part does on the
853
+ * read above: a step set this afternoon on a deployment that has counted
854
+ * for a year must report that it has been measurable since this afternoon,
855
+ * or a comparison of two fortnights would draw a fortnight of noughts
856
+ * before the question existed and call the step a collapse.
857
+ *
858
+ * `pages` on a requested pair is not a narrowing and is not read: a scope
859
+ * decides which rows a fold writes and is no part of what a row means
860
+ * (0418), so a scoped question and an unscoped one are the same row and the
861
+ * same read.
862
+ */
863
+ funnelHours: async (request) => {
864
+ const pair = request?.pair;
865
+ const scope = and(within(loomReaderFunnelHours, application, request), pair === undefined
866
+ ? undefined
867
+ : and(eq(loomReaderFunnelHours.fromNodeId, pair.from.nodeId), eq(loomReaderFunnelHours.fromKind, pair.from.kind), eq(loomReaderFunnelHours.toNodeId, pair.to.nodeId), eq(loomReaderFunnelHours.toKind, pair.to.kind)));
868
+ try {
869
+ const rows = await db
870
+ .select()
871
+ .from(loomReaderFunnelHours)
872
+ .where(and(scope, request?.from === undefined
873
+ ? undefined
874
+ : gte(loomReaderFunnelHours.hour, new Date(request.from)), request?.to === undefined
875
+ ? undefined
876
+ : lt(loomReaderFunnelHours.hour, new Date(request.to))));
877
+ const parsed = z.array(storedFunnelHourSchema).safeParse(rows);
878
+ if (!parsed.success) {
879
+ return err({
880
+ code: "unavailable",
881
+ detail: `a stored funnel hour did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
882
+ });
883
+ }
884
+ const [earliest] = await db
885
+ .select({ hour: min(loomReaderFunnelHours.hour) })
886
+ .from(loomReaderFunnelHours)
887
+ .where(scope);
888
+ const begins = hourSchema.safeParse(earliest?.hour);
889
+ return ok({
890
+ hours: parsed.data,
891
+ ...(begins.success ? { begins: begins.data } : {}),
892
+ });
893
+ }
894
+ catch (cause) {
895
+ return err(failure(cause, "could not read a funnel by hour"));
896
+ }
897
+ },
898
+ };
899
+ };
900
+ const storedRegionSchema = z.object({
901
+ audienceId: audienceIdSchema,
902
+ treeId: treeIdSchema,
903
+ revision: z.number().int().nonnegative(),
904
+ /**
905
+ * Parsed on the way out like every other column, which here is a closed set
906
+ * of two-letter codes and one word. A row holding anything else was not
907
+ * written by this runtime, and reading it as a region would put it on a screen.
908
+ */
909
+ region: readerRegionSchema,
910
+ views: countSchema,
911
+ updatedAt: instantSchema,
912
+ });
913
+ /** Scoped on the same terms as the journal — see its note. */
914
+ export const postgresReaderRegionStore = (db, scope = {}) => regionStoreIn(db, scope.application ?? DEFAULT_APPLICATION_ID);
915
+ const regionStoreIn = (db, application) => ({
916
+ /**
917
+ * One upsert, additive in the statement, for the reason the tallies are: this
918
+ * runs on the request path and two deliveries arriving together is the
919
+ * ordinary case rather than the unlucky one.
920
+ */
921
+ count: async (counts, at, audience = DEFAULT_AUDIENCE_ID) => {
922
+ if (counts.length === 0)
258
923
  return ok(undefined);
259
- }
260
- catch (cause) {
261
- return err(failure(cause, `could not apply ${tallies.length} tallies and ${funnels.length} funnels`));
262
- }
263
- },
264
- tallies: async (request) => {
924
+ const updatedAt = new Date(at);
265
925
  try {
266
- const rows = await db.select().from(loomReaderTallies).where(within(loomReaderTallies, request));
267
- const parsed = z.array(storedTallySchema).safeParse(rows);
268
- return parsed.success
269
- ? ok(parsed.data)
270
- : err({
271
- code: "unavailable",
272
- detail: `a stored tally did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
273
- });
926
+ await db
927
+ .insert(loomReaderRegions)
928
+ .values(foldedBy(counts, regionKey, addRegionCounts).map((count) => ({
929
+ applicationId: application,
930
+ audienceId: audience,
931
+ treeId: count.treeId,
932
+ revision: count.revision,
933
+ region: count.region,
934
+ views: count.views,
935
+ updatedAt,
936
+ })))
937
+ .onConflictDoUpdate({
938
+ target: [
939
+ loomReaderRegions.applicationId,
940
+ loomReaderRegions.audienceId,
941
+ loomReaderRegions.treeId,
942
+ loomReaderRegions.revision,
943
+ loomReaderRegions.region,
944
+ ],
945
+ set: {
946
+ views: sql `${loomReaderRegions.views} + excluded.views`,
947
+ updatedAt,
948
+ },
949
+ });
950
+ return ok(undefined);
274
951
  }
275
952
  catch (cause) {
276
- return err(failure(cause, "could not read reader tallies"));
953
+ return err(failure(cause, `could not count ${counts.length} reader regions`));
277
954
  }
278
955
  },
279
- funnels: async (request) => {
956
+ regions: async (request) => {
280
957
  try {
281
- const rows = await db.select().from(loomReaderFunnels).where(within(loomReaderFunnels, request));
282
- const parsed = z.array(storedFunnelSchema).safeParse(rows);
958
+ const rows = await db.select().from(loomReaderRegions).where(within(loomReaderRegions, application, request));
959
+ const parsed = z.array(storedRegionSchema).safeParse(rows);
283
960
  return parsed.success
284
961
  ? ok(parsed.data)
285
962
  : err({
286
963
  code: "unavailable",
287
- detail: `a stored funnel did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
964
+ detail: `a stored region did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
288
965
  });
289
966
  }
290
967
  catch (cause) {
291
- return err(failure(cause, "could not read reader funnels"));
968
+ return err(failure(cause, "could not read reader regions"));
292
969
  }
293
970
  },
294
971
  });