@jam-overture/loom 0.1.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.
- package/LICENSE +21 -0
- package/README.md +743 -0
- package/dist/catalogue.d.ts +70 -0
- package/dist/catalogue.js +88 -0
- package/dist/cli/args.d.ts +17 -0
- package/dist/cli/args.js +83 -0
- package/dist/cli/filesystem.d.ts +15 -0
- package/dist/cli/filesystem.js +31 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/main.js +30 -0
- package/dist/cli/namespace.d.ts +40 -0
- package/dist/cli/namespace.js +40 -0
- package/dist/cli/plan.d.ts +84 -0
- package/dist/cli/plan.js +113 -0
- package/dist/cli/run.d.ts +17 -0
- package/dist/cli/run.js +75 -0
- package/dist/cli/templates.d.ts +43 -0
- package/dist/cli/templates.js +134 -0
- package/dist/closed-set.d.ts +34 -0
- package/dist/closed-set.js +34 -0
- package/dist/data/adapter.d.ts +123 -0
- package/dist/data/adapter.js +91 -0
- package/dist/data/binding.d.ts +48 -0
- package/dist/data/binding.js +39 -0
- package/dist/data/catalogue.d.ts +26 -0
- package/dist/data/catalogue.js +6 -0
- package/dist/data/index.d.ts +14 -0
- package/dist/data/index.js +14 -0
- package/dist/data/plan.d.ts +58 -0
- package/dist/data/plan.js +0 -0
- package/dist/data/resolution.d.ts +91 -0
- package/dist/data/resolution.js +96 -0
- package/dist/data/resolve.d.ts +46 -0
- package/dist/data/resolve.js +37 -0
- package/dist/data/source.d.ts +40 -0
- package/dist/data/source.js +45 -0
- package/dist/deadline.d.ts +57 -0
- package/dist/deadline.js +78 -0
- package/dist/frame/catalogue.d.ts +24 -0
- package/dist/frame/catalogue.js +5 -0
- package/dist/frame/index.d.ts +12 -0
- package/dist/frame/index.js +12 -0
- package/dist/frame/origin.d.ts +87 -0
- package/dist/frame/origin.js +114 -0
- package/dist/frame/resolution.d.ts +85 -0
- package/dist/frame/resolution.js +79 -0
- package/dist/grammar.d.ts +26 -0
- package/dist/grammar.js +28 -0
- package/dist/ids.d.ts +57 -0
- package/dist/ids.js +104 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +26 -0
- package/dist/interactivity.d.ts +50 -0
- package/dist/interactivity.js +22 -0
- package/dist/interpretation/anthropic.d.ts +22 -0
- package/dist/interpretation/anthropic.js +76 -0
- package/dist/interpretation/client.d.ts +83 -0
- package/dist/interpretation/client.js +2 -0
- package/dist/interpretation/draft.d.ts +234 -0
- package/dist/interpretation/draft.js +84 -0
- package/dist/interpretation/index.d.ts +15 -0
- package/dist/interpretation/index.js +15 -0
- package/dist/interpretation/interpreter.d.ts +62 -0
- package/dist/interpretation/interpreter.js +151 -0
- package/dist/interpretation/materialize.d.ts +42 -0
- package/dist/interpretation/materialize.js +107 -0
- package/dist/interpretation/prompt.d.ts +229 -0
- package/dist/interpretation/prompt.js +285 -0
- package/dist/interpretation/render.d.ts +76 -0
- package/dist/interpretation/render.js +272 -0
- package/dist/interpretation/schema.d.ts +49 -0
- package/dist/interpretation/schema.js +131 -0
- package/dist/json.d.ts +35 -0
- package/dist/json.js +11 -0
- package/dist/paging.d.ts +60 -0
- package/dist/paging.js +49 -0
- package/dist/primitive-type.d.ts +32 -0
- package/dist/primitive-type.js +39 -0
- package/dist/render/addressing.d.ts +59 -0
- package/dist/render/addressing.js +60 -0
- package/dist/render/anchor.d.ts +105 -0
- package/dist/render/anchor.js +84 -0
- package/dist/render/behaviour-adjust.d.ts +62 -0
- package/dist/render/behaviour-adjust.js +78 -0
- package/dist/render/behaviour-copy.d.ts +53 -0
- package/dist/render/behaviour-copy.js +75 -0
- package/dist/render/behaviour-disclose.d.ts +74 -0
- package/dist/render/behaviour-disclose.js +77 -0
- package/dist/render/behaviour-present.d.ts +123 -0
- package/dist/render/behaviour-present.js +173 -0
- package/dist/render/behaviour.d.ts +243 -0
- package/dist/render/behaviour.js +284 -0
- package/dist/render/control.d.ts +81 -0
- package/dist/render/control.js +80 -0
- package/dist/render/decorative.d.ts +62 -0
- package/dist/render/decorative.js +2 -0
- package/dist/render/diagnostics.d.ts +260 -0
- package/dist/render/diagnostics.js +61 -0
- package/dist/render/disclosed.d.ts +37 -0
- package/dist/render/disclosed.js +37 -0
- package/dist/render/editable.d.ts +34 -0
- package/dist/render/editable.js +35 -0
- package/dist/render/frame.d.ts +42 -0
- package/dist/render/frame.js +31 -0
- package/dist/render/index.d.ts +35 -0
- package/dist/render/index.js +35 -0
- package/dist/render/inline-variables.d.ts +77 -0
- package/dist/render/inline-variables.js +220 -0
- package/dist/render/presented.d.ts +82 -0
- package/dist/render/presented.js +82 -0
- package/dist/render/primitive.d.ts +228 -0
- package/dist/render/primitive.js +36 -0
- package/dist/render/props.d.ts +48 -0
- package/dist/render/props.js +2 -0
- package/dist/render/reads.d.ts +73 -0
- package/dist/render/reads.js +45 -0
- package/dist/render/render.d.ts +164 -0
- package/dist/render/render.js +571 -0
- package/dist/render/request.d.ts +111 -0
- package/dist/render/request.js +60 -0
- package/dist/render/text.d.ts +91 -0
- package/dist/render/text.js +44 -0
- package/dist/render/theme.d.ts +61 -0
- package/dist/render/theme.js +52 -0
- package/dist/reserved-props.d.ts +35 -0
- package/dist/reserved-props.js +45 -0
- package/dist/result.d.ts +27 -0
- package/dist/result.js +30 -0
- package/dist/role.d.ts +71 -0
- package/dist/role.js +22 -0
- package/dist/runtime/analysis.d.ts +149 -0
- package/dist/runtime/analysis.js +187 -0
- package/dist/runtime/assessment.d.ts +35 -0
- package/dist/runtime/assessment.js +13 -0
- package/dist/runtime/disposition.d.ts +102 -0
- package/dist/runtime/disposition.js +59 -0
- package/dist/runtime/events.d.ts +187 -0
- package/dist/runtime/events.js +7 -0
- package/dist/runtime/gate.d.ts +43 -0
- package/dist/runtime/gate.js +169 -0
- package/dist/runtime/index.d.ts +27 -0
- package/dist/runtime/index.js +27 -0
- package/dist/runtime/intent.d.ts +60 -0
- package/dist/runtime/intent.js +36 -0
- package/dist/runtime/interpreter.d.ts +117 -0
- package/dist/runtime/interpreter.js +46 -0
- package/dist/runtime/inverse-interpreter.d.ts +90 -0
- package/dist/runtime/inverse-interpreter.js +56 -0
- package/dist/runtime/inverse.d.ts +87 -0
- package/dist/runtime/inverse.js +48 -0
- package/dist/runtime/narration.d.ts +24 -0
- package/dist/runtime/narration.js +36 -0
- package/dist/runtime/nesting.d.ts +31 -0
- package/dist/runtime/nesting.js +46 -0
- package/dist/runtime/pipeline.d.ts +124 -0
- package/dist/runtime/pipeline.js +178 -0
- package/dist/runtime/policy-fingerprint.d.ts +29 -0
- package/dist/runtime/policy-fingerprint.js +119 -0
- package/dist/runtime/policy-source.d.ts +42 -0
- package/dist/runtime/policy-source.js +8 -0
- package/dist/runtime/policy.d.ts +156 -0
- package/dist/runtime/policy.js +107 -0
- package/dist/runtime/proposal.d.ts +423 -0
- package/dist/runtime/proposal.js +109 -0
- package/dist/runtime/redirection.d.ts +53 -0
- package/dist/runtime/redirection.js +38 -0
- package/dist/runtime/repointing.d.ts +71 -0
- package/dist/runtime/repointing.js +0 -0
- package/dist/runtime/reversibility.d.ts +36 -0
- package/dist/runtime/reversibility.js +39 -0
- package/dist/runtime/stake-level.d.ts +33 -0
- package/dist/runtime/stake-level.js +58 -0
- package/dist/runtime/stakes.d.ts +47 -0
- package/dist/runtime/stakes.js +305 -0
- package/dist/runtime/vocabulary.d.ts +122 -0
- package/dist/runtime/vocabulary.js +87 -0
- package/dist/sdk/audit.d.ts +195 -0
- package/dist/sdk/audit.js +128 -0
- package/dist/sdk/catalogue.d.ts +37 -0
- package/dist/sdk/catalogue.js +16 -0
- package/dist/sdk/conformance.d.ts +342 -0
- package/dist/sdk/conformance.js +494 -0
- package/dist/sdk/copy.d.ts +105 -0
- package/dist/sdk/copy.js +90 -0
- package/dist/sdk/definition.d.ts +242 -0
- package/dist/sdk/definition.js +64 -0
- package/dist/sdk/index.d.ts +19 -0
- package/dist/sdk/index.js +19 -0
- package/dist/sdk/interactivity.d.ts +29 -0
- package/dist/sdk/interactivity.js +34 -0
- package/dist/sdk/pairings.d.ts +124 -0
- package/dist/sdk/pairings.js +81 -0
- package/dist/sdk/registry.d.ts +162 -0
- package/dist/sdk/registry.js +329 -0
- package/dist/sdk/selection.d.ts +46 -0
- package/dist/sdk/selection.js +24 -0
- package/dist/sdk/text.d.ts +100 -0
- package/dist/sdk/text.js +105 -0
- package/dist/sdk/vocabulary.d.ts +39 -0
- package/dist/sdk/vocabulary.js +36 -0
- package/dist/signals/broadcast.d.ts +134 -0
- package/dist/signals/broadcast.js +451 -0
- package/dist/signals/collect.d.ts +184 -0
- package/dist/signals/collect.js +196 -0
- package/dist/signals/deliver.d.ts +64 -0
- package/dist/signals/deliver.js +85 -0
- package/dist/signals/fold.d.ts +112 -0
- package/dist/signals/fold.js +76 -0
- package/dist/signals/index.d.ts +31 -0
- package/dist/signals/index.js +31 -0
- package/dist/signals/ingest.d.ts +51 -0
- package/dist/signals/ingest.js +70 -0
- package/dist/signals/intake.d.ts +149 -0
- package/dist/signals/intake.js +154 -0
- package/dist/signals/journal.d.ts +94 -0
- package/dist/signals/journal.js +11 -0
- package/dist/signals/kinds.d.ts +22 -0
- package/dist/signals/kinds.js +22 -0
- package/dist/signals/ledger.d.ts +46 -0
- package/dist/signals/ledger.js +81 -0
- package/dist/signals/memory.d.ts +17 -0
- package/dist/signals/memory.js +99 -0
- package/dist/signals/migrate.d.ts +27 -0
- package/dist/signals/migrate.js +78 -0
- package/dist/signals/postgres.d.ts +7 -0
- package/dist/signals/postgres.js +295 -0
- package/dist/signals/rollup.d.ts +142 -0
- package/dist/signals/rollup.js +0 -0
- package/dist/signals/schema.d.ts +534 -0
- package/dist/signals/schema.js +92 -0
- package/dist/signals/signal.d.ts +381 -0
- package/dist/signals/signal.js +150 -0
- package/dist/signals/tally.d.ts +70 -0
- package/dist/signals/tally.js +2 -0
- package/dist/signals/view.d.ts +51 -0
- package/dist/signals/view.js +54 -0
- package/dist/store/attribution.d.ts +125 -0
- package/dist/store/attribution.js +116 -0
- package/dist/store/database.d.ts +14 -0
- package/dist/store/database.js +2 -0
- package/dist/store/driver.d.ts +29 -0
- package/dist/store/driver.js +39 -0
- package/dist/store/errors.d.ts +56 -0
- package/dist/store/errors.js +37 -0
- package/dist/store/index.d.ts +16 -0
- package/dist/store/index.js +16 -0
- package/dist/store/memory.d.ts +3 -0
- package/dist/store/memory.js +115 -0
- package/dist/store/migrate.d.ts +54 -0
- package/dist/store/migrate.js +122 -0
- package/dist/store/postgres-holds.d.ts +4 -0
- package/dist/store/postgres-holds.js +192 -0
- package/dist/store/postgres.d.ts +8 -0
- package/dist/store/postgres.js +262 -0
- package/dist/store/replay.d.ts +94 -0
- package/dist/store/replay.js +82 -0
- package/dist/store/revert.d.ts +116 -0
- package/dist/store/revert.js +243 -0
- package/dist/store/schema.d.ts +371 -0
- package/dist/store/schema.js +78 -0
- package/dist/store/source.d.ts +23 -0
- package/dist/store/source.js +37 -0
- package/dist/store/store.d.ts +231 -0
- package/dist/store/store.js +52 -0
- package/dist/store/undone.d.ts +18 -0
- package/dist/store/undone.js +89 -0
- package/dist/submit/catalogue.d.ts +20 -0
- package/dist/submit/catalogue.js +5 -0
- package/dist/submit/declaration.d.ts +39 -0
- package/dist/submit/declaration.js +30 -0
- package/dist/submit/endpoint.d.ts +131 -0
- package/dist/submit/endpoint.js +160 -0
- package/dist/submit/index.d.ts +13 -0
- package/dist/submit/index.js +13 -0
- package/dist/submit/plan.d.ts +49 -0
- package/dist/submit/plan.js +47 -0
- package/dist/submit/resolution.d.ts +61 -0
- package/dist/submit/resolution.js +64 -0
- package/dist/submit/resolve.d.ts +45 -0
- package/dist/submit/resolve.js +36 -0
- package/dist/telemetry/calibration.d.ts +132 -0
- package/dist/telemetry/calibration.js +151 -0
- package/dist/telemetry/episode.d.ts +140 -0
- package/dist/telemetry/episode.js +272 -0
- package/dist/telemetry/event.d.ts +2310 -0
- package/dist/telemetry/event.js +405 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +15 -0
- package/dist/telemetry/journal.d.ts +116 -0
- package/dist/telemetry/journal.js +11 -0
- package/dist/telemetry/memory.d.ts +24 -0
- package/dist/telemetry/memory.js +75 -0
- package/dist/telemetry/migrate.d.ts +26 -0
- package/dist/telemetry/migrate.js +40 -0
- package/dist/telemetry/postgres.d.ts +6 -0
- package/dist/telemetry/postgres.js +139 -0
- package/dist/telemetry/retention.d.ts +131 -0
- package/dist/telemetry/retention.js +217 -0
- package/dist/telemetry/schema.d.ts +113 -0
- package/dist/telemetry/schema.js +33 -0
- package/dist/telemetry/sink.d.ts +42 -0
- package/dist/telemetry/sink.js +61 -0
- package/dist/testing/contracts.d.ts +34 -0
- package/dist/testing/contracts.js +34 -0
- package/dist/testing/definitions.d.ts +15 -0
- package/dist/testing/definitions.js +48 -0
- package/dist/testing/doubles.d.ts +94 -0
- package/dist/testing/doubles.js +141 -0
- package/dist/testing/episode-harness.d.ts +49 -0
- package/dist/testing/episode-harness.js +65 -0
- package/dist/testing/filesystem.d.ts +17 -0
- package/dist/testing/filesystem.js +17 -0
- package/dist/testing/fixtures.d.ts +57 -0
- package/dist/testing/fixtures.js +84 -0
- package/dist/testing/hold-contract.d.ts +19 -0
- package/dist/testing/hold-contract.js +374 -0
- package/dist/testing/index.d.ts +41 -0
- package/dist/testing/index.js +41 -0
- package/dist/testing/journal-contract.d.ts +17 -0
- package/dist/testing/journal-contract.js +353 -0
- package/dist/testing/model-replies.d.ts +34 -0
- package/dist/testing/model-replies.js +101 -0
- package/dist/testing/primitives.d.ts +7 -0
- package/dist/testing/primitives.js +29 -0
- package/dist/testing/reader-signal-contract.d.ts +38 -0
- package/dist/testing/reader-signal-contract.js +318 -0
- package/dist/testing/row-security.d.ts +17 -0
- package/dist/testing/row-security.js +21 -0
- package/dist/testing/store-contract.d.ts +25 -0
- package/dist/testing/store-contract.js +500 -0
- package/dist/theme/apply.d.ts +17 -0
- package/dist/theme/apply.js +60 -0
- package/dist/theme/contrast.d.ts +197 -0
- package/dist/theme/contrast.js +259 -0
- package/dist/theme/derive.d.ts +77 -0
- package/dist/theme/derive.js +167 -0
- package/dist/theme/faces.d.ts +73 -0
- package/dist/theme/faces.js +119 -0
- package/dist/theme/font-packs.d.ts +25 -0
- package/dist/theme/font-packs.js +257 -0
- package/dist/theme/index.d.ts +20 -0
- package/dist/theme/index.js +20 -0
- package/dist/theme/lab.d.ts +30 -0
- package/dist/theme/lab.js +46 -0
- package/dist/theme/library.d.ts +114 -0
- package/dist/theme/library.js +300 -0
- package/dist/theme/measure.d.ts +144 -0
- package/dist/theme/measure.js +145 -0
- package/dist/theme/palettes.d.ts +64 -0
- package/dist/theme/palettes.js +497 -0
- package/dist/theme/registry.d.ts +59 -0
- package/dist/theme/registry.js +60 -0
- package/dist/theme/separation.d.ts +177 -0
- package/dist/theme/separation.js +203 -0
- package/dist/theme/style-presets.d.ts +60 -0
- package/dist/theme/style-presets.js +124 -0
- package/dist/theme/theme.d.ts +253 -0
- package/dist/theme/theme.js +171 -0
- package/dist/tree/apply.d.ts +16 -0
- package/dist/tree/apply.js +104 -0
- package/dist/tree/builders.d.ts +19 -0
- package/dist/tree/builders.js +20 -0
- package/dist/tree/compare.d.ts +68 -0
- package/dist/tree/compare.js +75 -0
- package/dist/tree/configuration.d.ts +25 -0
- package/dist/tree/configuration.js +108 -0
- package/dist/tree/delta.d.ts +288 -0
- package/dist/tree/delta.js +88 -0
- package/dist/tree/errors.d.ts +69 -0
- package/dist/tree/errors.js +34 -0
- package/dist/tree/identity.d.ts +117 -0
- package/dist/tree/identity.js +145 -0
- package/dist/tree/index.d.ts +21 -0
- package/dist/tree/index.js +21 -0
- package/dist/tree/inverse.d.ts +24 -0
- package/dist/tree/inverse.js +112 -0
- package/dist/tree/mutation.d.ts +18 -0
- package/dist/tree/mutation.js +49 -0
- package/dist/tree/naming.d.ts +4 -0
- package/dist/tree/naming.js +35 -0
- package/dist/tree/navigation.d.ts +38 -0
- package/dist/tree/navigation.js +83 -0
- package/dist/tree/node.d.ts +103 -0
- package/dist/tree/node.js +46 -0
- package/dist/tree/outline.d.ts +28 -0
- package/dist/tree/outline.js +10 -0
- package/dist/tree/tree.d.ts +83 -0
- package/dist/tree/tree.js +62 -0
- package/dist/write/commit.d.ts +124 -0
- package/dist/write/commit.js +234 -0
- package/dist/write/held.d.ts +674 -0
- package/dist/write/held.js +185 -0
- package/dist/write/index.d.ts +11 -0
- package/dist/write/index.js +11 -0
- package/dist/write/liveness.d.ts +132 -0
- package/dist/write/liveness.js +91 -0
- package/dist/write/revert.d.ts +79 -0
- package/dist/write/revert.js +134 -0
- package/package.json +160 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { cursorPosition, pageEnds } from "../paging.js";
|
|
2
|
+
import { ok } from "../result.js";
|
|
3
|
+
import { systemClock } from "../runtime/events.js";
|
|
4
|
+
import { clampReaderSignalLimit, } from "./journal.js";
|
|
5
|
+
/**
|
|
6
|
+
* The buffer and the counters, in the process.
|
|
7
|
+
*
|
|
8
|
+
* Like the memory journal and the memory store, these are the reference
|
|
9
|
+
* implementations rather than test doubles: the contract suite is written
|
|
10
|
+
* against what they do, and Postgres is checked against the same suite.
|
|
11
|
+
*
|
|
12
|
+
* The clock is a parameter for the reason it is one everywhere else — a
|
|
13
|
+
* `receivedAt` read off the wall clock makes every retention test a race
|
|
14
|
+
* against real time.
|
|
15
|
+
*/
|
|
16
|
+
export const memoryReaderSignalJournal = (clock = systemClock) => {
|
|
17
|
+
let batches = [];
|
|
18
|
+
let nextSeq = 1;
|
|
19
|
+
const positioned = (batch) => ({
|
|
20
|
+
...batch,
|
|
21
|
+
seq: nextSeq++,
|
|
22
|
+
receivedAt: clock.now(),
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
receive: (received) => {
|
|
26
|
+
batches.push(...received.map(positioned));
|
|
27
|
+
return Promise.resolve(ok(undefined));
|
|
28
|
+
},
|
|
29
|
+
read: (request) => {
|
|
30
|
+
const limit = clampReaderSignalLimit(request?.limit);
|
|
31
|
+
const from = cursorPosition(request?.cursor);
|
|
32
|
+
const direction = request?.direction ?? "newer";
|
|
33
|
+
const matching = batches.filter((batch) => (request?.treeId === undefined || batch.treeId === request.treeId) &&
|
|
34
|
+
(from === undefined || (direction === "older" ? batch.seq < from : batch.seq > from)));
|
|
35
|
+
/** A slice rather than a reverse: a page's order never depends on which end it came from. */
|
|
36
|
+
const page = direction === "older" ? matching.slice(-limit) : matching.slice(0, limit);
|
|
37
|
+
return Promise.resolve(ok({
|
|
38
|
+
batches: page,
|
|
39
|
+
...pageEnds(page.map((batch) => batch.seq), direction, {
|
|
40
|
+
beyond: matching.length > page.length,
|
|
41
|
+
resumed: from !== undefined,
|
|
42
|
+
}),
|
|
43
|
+
}));
|
|
44
|
+
},
|
|
45
|
+
/** `nextSeq` is not rewound, so a cursor held across a prune never resumes inside what it has seen. */
|
|
46
|
+
forget: ({ before }) => {
|
|
47
|
+
const kept = batches.filter((batch) => batch.seq >= before);
|
|
48
|
+
const removed = batches.length - kept.length;
|
|
49
|
+
batches = kept;
|
|
50
|
+
return Promise.resolve(ok({ removed }));
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const tallyKey = (tally) => `${tally.treeId} ${tally.revision} ${tally.nodeId}`;
|
|
55
|
+
const endOf = (end) => `${end.nodeId}:${end.kind}`;
|
|
56
|
+
const funnelKey = (answer) => `${answer.treeId} ${answer.revision} ${endOf(answer.pair.from)} ${endOf(answer.pair.to)}`;
|
|
57
|
+
const matches = (row, request) => (request?.treeId === undefined || row.treeId === request.treeId) &&
|
|
58
|
+
(request?.revision === undefined || row.revision === request.revision);
|
|
59
|
+
export const memoryReaderTallyStore = () => {
|
|
60
|
+
const tallies = new Map();
|
|
61
|
+
const funnels = new Map();
|
|
62
|
+
const addTally = (tally, at) => {
|
|
63
|
+
const key = tallyKey(tally);
|
|
64
|
+
const existing = tallies.get(key);
|
|
65
|
+
tallies.set(key, {
|
|
66
|
+
...tally,
|
|
67
|
+
views: (existing?.views ?? 0) + tally.views,
|
|
68
|
+
reached: (existing?.reached ?? 0) + tally.reached,
|
|
69
|
+
engaged: (existing?.engaged ?? 0) + tally.engaged,
|
|
70
|
+
dwellMs: (existing?.dwellMs ?? 0) + tally.dwellMs,
|
|
71
|
+
activations: (existing?.activations ?? 0) + tally.activations,
|
|
72
|
+
opens: (existing?.opens ?? 0) + tally.opens,
|
|
73
|
+
closes: (existing?.closes ?? 0) + tally.closes,
|
|
74
|
+
updatedAt: at,
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
const addFunnel = (answer, at) => {
|
|
78
|
+
const key = funnelKey(answer);
|
|
79
|
+
const existing = funnels.get(key);
|
|
80
|
+
funnels.set(key, {
|
|
81
|
+
...answer,
|
|
82
|
+
reached: (existing?.reached ?? 0) + answer.reached,
|
|
83
|
+
converted: (existing?.converted ?? 0) + answer.converted,
|
|
84
|
+
updatedAt: at,
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
apply: (rollup, at) => {
|
|
89
|
+
for (const tally of rollup.tallies)
|
|
90
|
+
addTally(tally, at);
|
|
91
|
+
for (const answer of rollup.funnels)
|
|
92
|
+
addFunnel(answer, at);
|
|
93
|
+
return Promise.resolve(ok(undefined));
|
|
94
|
+
},
|
|
95
|
+
tallies: (request) => Promise.resolve(ok([...tallies.values()].filter((row) => matches(row, request)))),
|
|
96
|
+
funnels: (request) => Promise.resolve(ok([...funnels.values()].filter((row) => matches(row, request)))),
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { LoomDatabase } from "../store/database.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creating the three reader-signal tables, with row level security switched on.
|
|
4
|
+
*
|
|
5
|
+
* The same shape as the store's and the journal's migrations, and here for the
|
|
6
|
+
* same reason: a DDL statement that lives in a document is a DDL statement a
|
|
7
|
+
* deployment runs a stale copy of.
|
|
8
|
+
*
|
|
9
|
+
* RLS matters here for a reason that is worth stating rather than inheriting.
|
|
10
|
+
* Nothing in these tables identifies a reader — that is 0146's whole argument —
|
|
11
|
+
* but the buffer holds view keys, and a view key plus another deployment's rows
|
|
12
|
+
* is the only combination in Loom that could correlate anything across a
|
|
13
|
+
* tenancy boundary. It expires within hours; the policy does not depend on
|
|
14
|
+
* that.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* The three tables, as statements a host can run.
|
|
18
|
+
*
|
|
19
|
+
* One statement per entry for the same reason the store's DDL is a list: a
|
|
20
|
+
* driver may refuse several commands in a single prepared statement, and
|
|
21
|
+
* transaction-mode pooling — the configuration 0022 requires on serverless — is
|
|
22
|
+
* exactly where that bites.
|
|
23
|
+
*/
|
|
24
|
+
export declare const READER_SIGNALS_DDL: readonly string[];
|
|
25
|
+
/** Idempotent, so running it against a populated database is a no-op. */
|
|
26
|
+
export declare const ensureReaderSignalsSchema: (db: LoomDatabase) => Promise<void>;
|
|
27
|
+
//# sourceMappingURL=migrate.d.ts.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { sql } from "drizzle-orm";
|
|
2
|
+
/**
|
|
3
|
+
* Creating the three reader-signal tables, with row level security switched on.
|
|
4
|
+
*
|
|
5
|
+
* The same shape as the store's and the journal's migrations, and here for the
|
|
6
|
+
* same reason: a DDL statement that lives in a document is a DDL statement a
|
|
7
|
+
* deployment runs a stale copy of.
|
|
8
|
+
*
|
|
9
|
+
* RLS matters here for a reason that is worth stating rather than inheriting.
|
|
10
|
+
* Nothing in these tables identifies a reader — that is 0146's whole argument —
|
|
11
|
+
* but the buffer holds view keys, and a view key plus another deployment's rows
|
|
12
|
+
* is the only combination in Loom that could correlate anything across a
|
|
13
|
+
* tenancy boundary. It expires within hours; the policy does not depend on
|
|
14
|
+
* that.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* The three tables, as statements a host can run.
|
|
18
|
+
*
|
|
19
|
+
* One statement per entry for the same reason the store's DDL is a list: a
|
|
20
|
+
* driver may refuse several commands in a single prepared statement, and
|
|
21
|
+
* transaction-mode pooling — the configuration 0022 requires on serverless — is
|
|
22
|
+
* exactly where that bites.
|
|
23
|
+
*/
|
|
24
|
+
export const READER_SIGNALS_DDL = [
|
|
25
|
+
`CREATE TABLE IF NOT EXISTS loom_reader_signals (
|
|
26
|
+
seq bigserial PRIMARY KEY,
|
|
27
|
+
tree_id text NOT NULL,
|
|
28
|
+
revision integer NOT NULL,
|
|
29
|
+
sent_at bigint NOT NULL,
|
|
30
|
+
view text,
|
|
31
|
+
signals jsonb NOT NULL,
|
|
32
|
+
received_at timestamptz NOT NULL DEFAULT now()
|
|
33
|
+
)`,
|
|
34
|
+
`CREATE INDEX IF NOT EXISTS loom_reader_signals_tree_seq_idx ON loom_reader_signals (tree_id, seq)`,
|
|
35
|
+
`ALTER TABLE loom_reader_signals ENABLE ROW LEVEL SECURITY`,
|
|
36
|
+
`CREATE TABLE IF NOT EXISTS loom_reader_tallies (
|
|
37
|
+
tree_id text NOT NULL,
|
|
38
|
+
revision integer NOT NULL,
|
|
39
|
+
node_id text NOT NULL,
|
|
40
|
+
type text NOT NULL,
|
|
41
|
+
views bigint NOT NULL,
|
|
42
|
+
reached bigint NOT NULL,
|
|
43
|
+
engaged bigint NOT NULL DEFAULT 0,
|
|
44
|
+
dwell_ms bigint NOT NULL,
|
|
45
|
+
activations bigint NOT NULL,
|
|
46
|
+
opens bigint NOT NULL,
|
|
47
|
+
closes bigint NOT NULL,
|
|
48
|
+
updated_at timestamptz NOT NULL,
|
|
49
|
+
PRIMARY KEY (tree_id, revision, node_id)
|
|
50
|
+
)`,
|
|
51
|
+
/**
|
|
52
|
+
* For a deployment whose tallies table predates the counter regions are
|
|
53
|
+
* reported by. Additive and defaulted, so existing rows read as *nobody was
|
|
54
|
+
* measured doing anything in here*, which is what was true of them.
|
|
55
|
+
*/
|
|
56
|
+
`ALTER TABLE loom_reader_tallies ADD COLUMN IF NOT EXISTS engaged bigint NOT NULL DEFAULT 0`,
|
|
57
|
+
`ALTER TABLE loom_reader_tallies ENABLE ROW LEVEL SECURITY`,
|
|
58
|
+
`CREATE TABLE IF NOT EXISTS loom_reader_funnels (
|
|
59
|
+
tree_id text NOT NULL,
|
|
60
|
+
revision integer NOT NULL,
|
|
61
|
+
from_node_id text NOT NULL,
|
|
62
|
+
from_kind text NOT NULL,
|
|
63
|
+
to_node_id text NOT NULL,
|
|
64
|
+
to_kind text NOT NULL,
|
|
65
|
+
reached bigint NOT NULL,
|
|
66
|
+
converted bigint NOT NULL,
|
|
67
|
+
updated_at timestamptz NOT NULL,
|
|
68
|
+
PRIMARY KEY (tree_id, revision, from_node_id, from_kind, to_node_id, to_kind)
|
|
69
|
+
)`,
|
|
70
|
+
`ALTER TABLE loom_reader_funnels ENABLE ROW LEVEL SECURITY`,
|
|
71
|
+
];
|
|
72
|
+
/** Idempotent, so running it against a populated database is a no-op. */
|
|
73
|
+
export const ensureReaderSignalsSchema = async (db) => {
|
|
74
|
+
for (const statement of READER_SIGNALS_DDL) {
|
|
75
|
+
await db.execute(sql.raw(statement));
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
//# sourceMappingURL=migrate.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LoomDatabase } from "../store/database.js";
|
|
2
|
+
import { type ReaderSignalJournal } from "./journal.js";
|
|
3
|
+
import type { ReaderTallyStore } from "./tally.js";
|
|
4
|
+
export * from "./migrate.js";
|
|
5
|
+
export declare const postgresReaderSignalJournal: (db: LoomDatabase) => ReaderSignalJournal;
|
|
6
|
+
export declare const postgresReaderTallyStore: (db: LoomDatabase) => ReaderTallyStore;
|
|
7
|
+
//# sourceMappingURL=postgres.d.ts.map
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { and, asc, desc, eq, gt, lt, sql } from "drizzle-orm";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { nodeIdSchema, treeIdSchema } from "../ids.js";
|
|
4
|
+
import { cursorPosition, pageEnds } from "../paging.js";
|
|
5
|
+
import { primitiveTypeSchema } from "../primitive-type.js";
|
|
6
|
+
import { err, ok } from "../result.js";
|
|
7
|
+
import { clampReaderSignalLimit, } from "./journal.js";
|
|
8
|
+
import { readerSignalKindSchema, readerSignalSchema, viewKeySchema } from "./signal.js";
|
|
9
|
+
import { loomReaderFunnels, loomReaderSignals, loomReaderTallies } from "./schema.js";
|
|
10
|
+
export * from "./migrate.js";
|
|
11
|
+
/**
|
|
12
|
+
* The Drizzle tables are **not** re-exported, unlike the journal's.
|
|
13
|
+
*
|
|
14
|
+
* A deployment needs the two factories below and `ensureReaderSignalsSchema`;
|
|
15
|
+
* a `PgTableWithColumns` is how this file talks to the driver. Publishing the
|
|
16
|
+
* three of them put about 7 KB of generated Drizzle type into the API
|
|
17
|
+
* reference a reader downloads — for `loomReaderTallies` alone, a signature
|
|
18
|
+
* naming all eleven columns and fourteen properties of each — and none of it
|
|
19
|
+
* says anything a host can act on.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* The buffer and the counters, backed by Postgres — the same database the store
|
|
23
|
+
* uses (0022), reached the same way: plain SQL through Drizzle, never PostgREST.
|
|
24
|
+
*
|
|
25
|
+
* Rows are parsed on the way out, never cast, like every other boundary. It
|
|
26
|
+
* matters more here than most: the buffer's `signals` column holds a document a
|
|
27
|
+
* browser composed, and a batch written by an older parser must not reach a
|
|
28
|
+
* rollup unvalidated.
|
|
29
|
+
*/
|
|
30
|
+
const failure = (cause, detail) => ({
|
|
31
|
+
code: "unavailable",
|
|
32
|
+
detail: `${detail}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* A driver hands `timestamptz` back as a `Date` and everything above this
|
|
36
|
+
* boundary speaks ISO strings. Accepting a string too means a driver configured
|
|
37
|
+
* to return one is not a parse failure at 3am.
|
|
38
|
+
*/
|
|
39
|
+
const instantSchema = z
|
|
40
|
+
.union([z.date(), z.string()])
|
|
41
|
+
.transform((value) => (value instanceof Date ? value : new Date(value)))
|
|
42
|
+
.refine((value) => Number.isFinite(value.getTime()), "is not an instant")
|
|
43
|
+
.transform((value) => value.toISOString());
|
|
44
|
+
const receivedSchema = z.object({
|
|
45
|
+
seq: z.number().int().nonnegative(),
|
|
46
|
+
treeId: treeIdSchema,
|
|
47
|
+
revision: z.number().int().nonnegative(),
|
|
48
|
+
sentAt: z.number().int().nonnegative(),
|
|
49
|
+
/** `null` in the column, absent on the batch — a buffer row a sender minted no key for. */
|
|
50
|
+
view: viewKeySchema.nullable().transform((value) => value ?? undefined),
|
|
51
|
+
signals: z.array(readerSignalSchema).min(1),
|
|
52
|
+
receivedAt: instantSchema,
|
|
53
|
+
});
|
|
54
|
+
/**
|
|
55
|
+
* `bigint` columns come back as strings from one driver and numbers from
|
|
56
|
+
* another, and a counter that silently became `"12"` would concatenate on the
|
|
57
|
+
* next read rather than add.
|
|
58
|
+
*/
|
|
59
|
+
const countSchema = z.coerce.number().int().nonnegative();
|
|
60
|
+
const storedTallySchema = z.object({
|
|
61
|
+
treeId: treeIdSchema,
|
|
62
|
+
revision: z.number().int().nonnegative(),
|
|
63
|
+
nodeId: nodeIdSchema,
|
|
64
|
+
type: primitiveTypeSchema,
|
|
65
|
+
views: countSchema,
|
|
66
|
+
reached: countSchema,
|
|
67
|
+
engaged: countSchema,
|
|
68
|
+
dwellMs: countSchema,
|
|
69
|
+
activations: countSchema,
|
|
70
|
+
opens: countSchema,
|
|
71
|
+
closes: countSchema,
|
|
72
|
+
updatedAt: instantSchema,
|
|
73
|
+
});
|
|
74
|
+
const storedFunnelSchema = z
|
|
75
|
+
.object({
|
|
76
|
+
treeId: treeIdSchema,
|
|
77
|
+
revision: z.number().int().nonnegative(),
|
|
78
|
+
fromNodeId: nodeIdSchema,
|
|
79
|
+
fromKind: readerSignalKindSchema,
|
|
80
|
+
toNodeId: nodeIdSchema,
|
|
81
|
+
toKind: readerSignalKindSchema,
|
|
82
|
+
reached: countSchema,
|
|
83
|
+
converted: countSchema,
|
|
84
|
+
updatedAt: instantSchema,
|
|
85
|
+
})
|
|
86
|
+
.transform((row) => ({
|
|
87
|
+
treeId: row.treeId,
|
|
88
|
+
revision: row.revision,
|
|
89
|
+
pair: {
|
|
90
|
+
from: { nodeId: row.fromNodeId, kind: row.fromKind },
|
|
91
|
+
to: { nodeId: row.toNodeId, kind: row.toKind },
|
|
92
|
+
},
|
|
93
|
+
reached: row.reached,
|
|
94
|
+
converted: row.converted,
|
|
95
|
+
updatedAt: row.updatedAt,
|
|
96
|
+
}));
|
|
97
|
+
export const postgresReaderSignalJournal = (db) => ({
|
|
98
|
+
receive: async (batches) => {
|
|
99
|
+
if (batches.length === 0)
|
|
100
|
+
return ok(undefined);
|
|
101
|
+
try {
|
|
102
|
+
await db.insert(loomReaderSignals).values(batches.map((batch) => ({
|
|
103
|
+
treeId: batch.treeId,
|
|
104
|
+
revision: batch.revision,
|
|
105
|
+
sentAt: batch.sentAt,
|
|
106
|
+
view: batch.view ?? null,
|
|
107
|
+
signals: batch.signals,
|
|
108
|
+
})));
|
|
109
|
+
return ok(undefined);
|
|
110
|
+
}
|
|
111
|
+
catch (cause) {
|
|
112
|
+
return err(failure(cause, `could not receive ${batches.length} reader signal batches`));
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
read: async (request) => {
|
|
116
|
+
const limit = clampReaderSignalLimit(request?.limit);
|
|
117
|
+
const from = cursorPosition(request?.cursor);
|
|
118
|
+
const direction = request?.direction ?? "newer";
|
|
119
|
+
try {
|
|
120
|
+
/** `older` scans descending so the newest page is one index seek, then rows go back in arrival order. */
|
|
121
|
+
const rows = await db
|
|
122
|
+
.select({
|
|
123
|
+
seq: loomReaderSignals.seq,
|
|
124
|
+
treeId: loomReaderSignals.treeId,
|
|
125
|
+
revision: loomReaderSignals.revision,
|
|
126
|
+
sentAt: loomReaderSignals.sentAt,
|
|
127
|
+
view: loomReaderSignals.view,
|
|
128
|
+
signals: loomReaderSignals.signals,
|
|
129
|
+
receivedAt: loomReaderSignals.receivedAt,
|
|
130
|
+
})
|
|
131
|
+
.from(loomReaderSignals)
|
|
132
|
+
.where(and(request?.treeId === undefined ? undefined : eq(loomReaderSignals.treeId, request.treeId), from === undefined
|
|
133
|
+
? undefined
|
|
134
|
+
: direction === "older"
|
|
135
|
+
? lt(loomReaderSignals.seq, from)
|
|
136
|
+
: gt(loomReaderSignals.seq, from)))
|
|
137
|
+
.orderBy(direction === "older" ? desc(loomReaderSignals.seq) : asc(loomReaderSignals.seq))
|
|
138
|
+
/** One extra row answers "is there another page" without a count. */
|
|
139
|
+
.limit(limit + 1);
|
|
140
|
+
const taken = rows.slice(0, limit);
|
|
141
|
+
const parsed = z
|
|
142
|
+
.array(receivedSchema)
|
|
143
|
+
.safeParse(direction === "older" ? taken.slice().reverse() : taken);
|
|
144
|
+
if (!parsed.success) {
|
|
145
|
+
return err({
|
|
146
|
+
code: "unavailable",
|
|
147
|
+
detail: `a stored reader signal batch did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
const page = parsed.data;
|
|
151
|
+
return ok({
|
|
152
|
+
batches: page,
|
|
153
|
+
...pageEnds(page.map((batch) => batch.seq), direction, {
|
|
154
|
+
beyond: rows.length > limit,
|
|
155
|
+
resumed: from !== undefined,
|
|
156
|
+
}),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
catch (cause) {
|
|
160
|
+
return err(failure(cause, "could not read reader signals"));
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
/** The count comes from the returned rows, because the two drivers report `rowCount` differently. */
|
|
164
|
+
forget: async ({ before }) => {
|
|
165
|
+
try {
|
|
166
|
+
const removed = await db
|
|
167
|
+
.delete(loomReaderSignals)
|
|
168
|
+
.where(lt(loomReaderSignals.seq, before))
|
|
169
|
+
.returning({ seq: loomReaderSignals.seq });
|
|
170
|
+
return ok({ removed: removed.length });
|
|
171
|
+
}
|
|
172
|
+
catch (cause) {
|
|
173
|
+
return err(failure(cause, `could not forget reader signals below ${before}`));
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
});
|
|
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) => ({
|
|
180
|
+
/**
|
|
181
|
+
* Two upserts in one transaction.
|
|
182
|
+
*
|
|
183
|
+
* `ON CONFLICT … DO UPDATE SET x = table.x + excluded.x` is what makes
|
|
184
|
+
* applying a rollup additive in one statement rather than a read, an add and
|
|
185
|
+
* a write with a race in the middle. Two rollups running at once is not
|
|
186
|
+
* hypothetical — a scheduled one and a manual one is the ordinary way it
|
|
187
|
+
* happens — and this is the version that is correct when they do.
|
|
188
|
+
*/
|
|
189
|
+
apply: async ({ tallies, funnels }, at) => {
|
|
190
|
+
if (tallies.length === 0 && funnels.length === 0)
|
|
191
|
+
return ok(undefined);
|
|
192
|
+
const updatedAt = new Date(at);
|
|
193
|
+
try {
|
|
194
|
+
await db.transaction(async (tx) => {
|
|
195
|
+
if (tallies.length > 0) {
|
|
196
|
+
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,
|
|
210
|
+
updatedAt,
|
|
211
|
+
})))
|
|
212
|
+
.onConflictDoUpdate({
|
|
213
|
+
target: [loomReaderTallies.treeId, loomReaderTallies.revision, loomReaderTallies.nodeId],
|
|
214
|
+
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`,
|
|
223
|
+
updatedAt,
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
if (funnels.length > 0) {
|
|
228
|
+
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,
|
|
239
|
+
updatedAt,
|
|
240
|
+
})))
|
|
241
|
+
.onConflictDoUpdate({
|
|
242
|
+
target: [
|
|
243
|
+
loomReaderFunnels.treeId,
|
|
244
|
+
loomReaderFunnels.revision,
|
|
245
|
+
loomReaderFunnels.fromNodeId,
|
|
246
|
+
loomReaderFunnels.fromKind,
|
|
247
|
+
loomReaderFunnels.toNodeId,
|
|
248
|
+
loomReaderFunnels.toKind,
|
|
249
|
+
],
|
|
250
|
+
set: {
|
|
251
|
+
reached: sql `${loomReaderFunnels.reached} + excluded.reached`,
|
|
252
|
+
converted: sql `${loomReaderFunnels.converted} + excluded.converted`,
|
|
253
|
+
updatedAt,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
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) => {
|
|
265
|
+
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
|
+
});
|
|
274
|
+
}
|
|
275
|
+
catch (cause) {
|
|
276
|
+
return err(failure(cause, "could not read reader tallies"));
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
funnels: async (request) => {
|
|
280
|
+
try {
|
|
281
|
+
const rows = await db.select().from(loomReaderFunnels).where(within(loomReaderFunnels, request));
|
|
282
|
+
const parsed = z.array(storedFunnelSchema).safeParse(rows);
|
|
283
|
+
return parsed.success
|
|
284
|
+
? ok(parsed.data)
|
|
285
|
+
: err({
|
|
286
|
+
code: "unavailable",
|
|
287
|
+
detail: `a stored funnel did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
catch (cause) {
|
|
291
|
+
return err(failure(cause, "could not read reader funnels"));
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
//# sourceMappingURL=postgres.js.map
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { NodeId, TreeId } from "../ids.js";
|
|
2
|
+
import type { PrimitiveType } from "../primitive-type.js";
|
|
3
|
+
import type { ReaderSignalBatch, ReaderSignalKind } from "./signal.js";
|
|
4
|
+
/**
|
|
5
|
+
* Many views, folded into the durable artefact: per-node-per-revision counters,
|
|
6
|
+
* and the funnel pairs a deployment asked about.
|
|
7
|
+
*
|
|
8
|
+
* Raw batches are a short-lived buffer and these are what outlives them
|
|
9
|
+
* ([0146](../../decisions/0146-a-reader-signal-stays-anonymous-and-a-funnel-is-correlated-inside-one-page-view.md)).
|
|
10
|
+
* A 6,000-node page with ten readers emits a batch every few seconds, so
|
|
11
|
+
* keeping raw signals is keeping the largest table in the database to answer
|
|
12
|
+
* questions that are all about totals.
|
|
13
|
+
*
|
|
14
|
+
* **This is where the view key is consumed and where it stops.** Rollup is the
|
|
15
|
+
* only module that reads it, a tally carries no trace of it, and it is dropped
|
|
16
|
+
* with the raw window. That is what makes a funnel answerable without making a
|
|
17
|
+
* reader identifiable: the output says *of 40 views that reached the pricing
|
|
18
|
+
* band, 9 pressed the button*, and there is nothing in it that could say which
|
|
19
|
+
* nine.
|
|
20
|
+
*
|
|
21
|
+
* Pure. It is handed batches and returns counters; reading and writing them is
|
|
22
|
+
* `journal.ts` and `tally.ts`.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* One node at one revision, as every view of it added up.
|
|
26
|
+
*
|
|
27
|
+
* The counters divide into two kinds and the distinction is the whole reason
|
|
28
|
+
* this is not just `fold.ts` over more input. `dwellMs`, `activations`, `opens`
|
|
29
|
+
* and `closes` are **occurrences** — they sum, and one enthusiastic reader can
|
|
30
|
+
* be most of them. `views`, `reached` and `engaged` are **views** — they count
|
|
31
|
+
* distinct page views, so they are the ones a rate may be taken of, and they are
|
|
32
|
+
* only available because a batch carries a view key.
|
|
33
|
+
*
|
|
34
|
+
* Every counter but one is about the node itself. `engaged` is about what
|
|
35
|
+
* happened *under* it, and it is here because a region is never the thing a
|
|
36
|
+
* reader aims at: a press lands on a button, and without it a band's row reports
|
|
37
|
+
* time on screen beside four permanent zeroes.
|
|
38
|
+
*/
|
|
39
|
+
export type ReaderTally = {
|
|
40
|
+
readonly treeId: TreeId;
|
|
41
|
+
readonly revision: number;
|
|
42
|
+
readonly nodeId: NodeId;
|
|
43
|
+
readonly type: PrimitiveType;
|
|
44
|
+
/** Distinct page views that produced any signal at all about this node. */
|
|
45
|
+
readonly views: number;
|
|
46
|
+
/** Distinct page views in which it came into view. The denominator of a funnel. */
|
|
47
|
+
readonly reached: number;
|
|
48
|
+
/**
|
|
49
|
+
* Distinct page views in which a reader used something *inside* this node —
|
|
50
|
+
* pressed a link or button, filled a field, opened or closed a disclosure.
|
|
51
|
+
*
|
|
52
|
+
* The counter regions are reported by, and the only one that needs a signal to
|
|
53
|
+
* say more than which node it is about. A press is filed against the control,
|
|
54
|
+
* and every control in the starter library is an addressed node of its own, so
|
|
55
|
+
* `activations` on a band is nearly always zero and always will be: the band
|
|
56
|
+
* is not the thing anybody pressed. This counts the views whose reader did
|
|
57
|
+
* something under it, at any depth, from the ancestry a delegated signal
|
|
58
|
+
* carries.
|
|
59
|
+
*
|
|
60
|
+
* **Strictly inside.** A button's own `engaged` is 0 while its `activations`
|
|
61
|
+
* is 1; its band's is the other way round. The two never double-count the same
|
|
62
|
+
* node, and a subtree total is the addition.
|
|
63
|
+
*
|
|
64
|
+
* **Zero from a sender that does not walk.** A batch whose delegated signals
|
|
65
|
+
* carry no `within` — synthesised on a server, replayed from a fixture, sent
|
|
66
|
+
* by a host that turned the walk off — adds nothing here, exactly as a batch
|
|
67
|
+
* with no view key adds nothing to `views`.
|
|
68
|
+
*/
|
|
69
|
+
readonly engaged: number;
|
|
70
|
+
readonly dwellMs: number;
|
|
71
|
+
readonly activations: number;
|
|
72
|
+
readonly opens: number;
|
|
73
|
+
readonly closes: number;
|
|
74
|
+
};
|
|
75
|
+
/** One end of a funnel: a node, and what a reader has to have done to it. */
|
|
76
|
+
export type FunnelEnd = {
|
|
77
|
+
readonly nodeId: NodeId;
|
|
78
|
+
readonly kind: ReaderSignalKind;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* A question a deployment names, phrased about the tree rather than about
|
|
82
|
+
* anyone: *of the views that did X to node A, how many did Y to node B*.
|
|
83
|
+
*
|
|
84
|
+
* There is no path and no ordering beyond the two ends. That is a limit and it
|
|
85
|
+
* is the point — a three-step funnel with an order is a route through a page
|
|
86
|
+
* that one reader took, which is the thing 0146 refuses.
|
|
87
|
+
*
|
|
88
|
+
* **An end is the node the signal names, never a region it was inside.** A pair
|
|
89
|
+
* asking for `activated` on a band answers zero, because a band is not pressed;
|
|
90
|
+
* the question *how many of the views that reached this band used something in
|
|
91
|
+
* it* is `reached` and `engaged` on the band's own tally, which is one row and
|
|
92
|
+
* needs no pair to have been configured in advance.
|
|
93
|
+
*/
|
|
94
|
+
export type FunnelPair = {
|
|
95
|
+
readonly from: FunnelEnd;
|
|
96
|
+
readonly to: FunnelEnd;
|
|
97
|
+
};
|
|
98
|
+
export type FunnelAnswer = {
|
|
99
|
+
readonly treeId: TreeId;
|
|
100
|
+
readonly revision: number;
|
|
101
|
+
readonly pair: FunnelPair;
|
|
102
|
+
/** Views that satisfied `from`. */
|
|
103
|
+
readonly reached: number;
|
|
104
|
+
/** Views that satisfied both. Never greater than `reached`. */
|
|
105
|
+
readonly converted: number;
|
|
106
|
+
};
|
|
107
|
+
export type Rollup = {
|
|
108
|
+
readonly tallies: readonly ReaderTally[];
|
|
109
|
+
readonly funnels: readonly FunnelAnswer[];
|
|
110
|
+
/** Distinct page views seen, across every tree and revision in the input. */
|
|
111
|
+
readonly views: number;
|
|
112
|
+
/**
|
|
113
|
+
* Batches that carried no view key.
|
|
114
|
+
*
|
|
115
|
+
* Their occurrences are counted like any other; their views are not, because
|
|
116
|
+
* there is no honest number to add. Treating each uncorrelated batch as its
|
|
117
|
+
* own view would inflate every denominator by however often a page happened
|
|
118
|
+
* to flush, which is a number about the network rather than about readers.
|
|
119
|
+
* Reported so a portal can say the rate is computed over fewer views than the
|
|
120
|
+
* totals, instead of showing a rate that is quietly wrong.
|
|
121
|
+
*/
|
|
122
|
+
readonly uncorrelated: number;
|
|
123
|
+
};
|
|
124
|
+
export declare const EMPTY_ROLLUP: Rollup;
|
|
125
|
+
export type RollupOptions = {
|
|
126
|
+
/**
|
|
127
|
+
* The funnels this deployment asked for. Absent means none: a pair is a
|
|
128
|
+
* question somebody wrote down, and a rollup does not invent questions.
|
|
129
|
+
*/
|
|
130
|
+
readonly pairs?: readonly FunnelPair[];
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Fold a window of stored batches into the counters that outlive them.
|
|
134
|
+
*
|
|
135
|
+
* Batches from different trees and revisions may be mixed, because a window
|
|
136
|
+
* read from the journal is. They are kept apart: a signal about "the fourth
|
|
137
|
+
* section" means nothing once a proposal has moved it, and adding two revisions
|
|
138
|
+
* of a node together is exactly the mistake that makes *before versus after a
|
|
139
|
+
* change* unreadable.
|
|
140
|
+
*/
|
|
141
|
+
export declare const rollUp: (batches: readonly ReaderSignalBatch[], options?: RollupOptions) => Rollup;
|
|
142
|
+
//# sourceMappingURL=rollup.d.ts.map
|
|
Binary file
|