@reventlessdev/reventless-core 3.0.0-alpha.327 → 3.0.0-alpha.328

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 (35) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/package.json +6 -6
  3. package/src/CommandSent.res +22 -0
  4. package/src/CommandSent.res.mjs +22 -0
  5. package/src/Projection.res +26 -1
  6. package/src/Projection.res.mjs +19 -1
  7. package/src/RowChange.res +37 -0
  8. package/src/RowChange.res.mjs +62 -0
  9. package/src/components/Api/GraphQL_FragmentGenerator.res +22 -0
  10. package/src/components/Api/GraphQL_FragmentGenerator.res.mjs +20 -4
  11. package/src/components/Api/GraphQL_SchemaInspector.res.mjs +2 -2
  12. package/src/components/AutomationSlice/AutomationSlice_Callback.res +1 -0
  13. package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +4 -2
  14. package/src/components/EventMapper/EventMapper_Callback.res +8 -2
  15. package/src/components/EventMapper/EventMapper_Callback.res.mjs +3 -2
  16. package/src/components/ExtensionPoint/ExtensionPoint_Callback.res +5 -1
  17. package/src/components/ExtensionPoint/ExtensionPoint_Callback.res.mjs +2 -1
  18. package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res +4 -0
  19. package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res.mjs +3 -1
  20. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +4 -0
  21. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +4 -2
  22. package/src/components/QueryDb/QueryDb_Operations.res +2 -0
  23. package/src/components/QueryDb/QueryDb_Operations.res.mjs +3 -0
  24. package/src/components/Task/Task_Builder.res +5 -3
  25. package/src/components/Task/Task_Builder.res.mjs +2 -1
  26. package/src/plugin/component/Plugin_Callback.res +6 -2
  27. package/src/plugin/component/Plugin_Callback.res.mjs +3 -2
  28. package/src/util/Metrics.res +151 -16
  29. package/src/util/Metrics.res.mjs +123 -3
  30. package/tests/api/GraphQL_FragmentGeneratorTest.res.mjs +2 -2
  31. package/tests/api/IdentitySemanticTest.res.mjs +1 -1
  32. package/tests/querydb/RowChangeTest.res +87 -0
  33. package/tests/querydb/RowChangeTest.res.mjs +157 -0
  34. package/tests/util/MetricsTest.res +72 -0
  35. package/tests/util/MetricsTest.res.mjs +108 -0
@@ -129,6 +129,7 @@ module Make = (ReadModelSpec: Reventless.ReadModel.Spec, Ops: Ops) => {
129
129
  injectSubId(dict, state)
130
130
  injectCompositeIndexAttrs(dict)
131
131
  stampUnionMembers(dict)
132
+ RowChange.stampInto(dict)
132
133
  let json = JSON.Encode.object(dict)
133
134
  await Ops.jsonOps.save(id->ReadModelSpec.Id.toString, json, saveMode, ttl)
134
135
  | None =>
@@ -144,6 +145,7 @@ module Make = (ReadModelSpec: Reventless.ReadModel.Spec, Ops: Ops) => {
144
145
  injectSubId(dict, state)
145
146
  injectCompositeIndexAttrs(dict)
146
147
  stampUnionMembers(dict)
148
+ RowChange.stampInto(dict)
147
149
  let json = JSON.Encode.object(dict)
148
150
  Ok(batch->Array.concat([(id->ReadModelSpec.Id.toString, json, ttl)]))
149
151
  | None =>
@@ -10,6 +10,7 @@ import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_excep
10
10
  import * as Logger$ReventlessCore from "../../util/Logger.res.mjs";
11
11
  import * as Message$ReventlessCore from "../../Message.res.mjs";
12
12
  import * as TaggedUnion$Reventless from "@reventlessdev/reventless-spec/src/components/TaggedUnion.res.mjs";
13
+ import * as RowChange$ReventlessCore from "../../RowChange.res.mjs";
13
14
 
14
15
  let log = Logger$ReventlessCore.fromEnv();
15
16
 
@@ -104,6 +105,7 @@ function Make(ReadModelSpec) {
104
105
  injectSubId(dict, state);
105
106
  injectCompositeIndexAttrs(dict);
106
107
  stampUnionMembers(dict);
108
+ RowChange$ReventlessCore.stampInto(dict);
107
109
  return await Ops.jsonOps.save(ReadModelSpec.Id.toString(id), dict, saveMode, ttl);
108
110
  } else {
109
111
  return {
@@ -130,6 +132,7 @@ function Make(ReadModelSpec) {
130
132
  injectSubId(dict, state);
131
133
  injectCompositeIndexAttrs(dict);
132
134
  stampUnionMembers(dict);
135
+ RowChange$ReventlessCore.stampInto(dict);
133
136
  return {
134
137
  TAG: "Ok",
135
138
  _0: batch.concat([[
@@ -140,9 +140,11 @@ module Make = (
140
140
  )
141
141
  )
142
142
 
143
- let publishCommands: Task.publishCommands = (aggregateName, cmdJsons) => {
144
- (publishToAggregates->Dict.get(aggregateName)->Option.getOrThrow)(cmdJsons)
145
- }
143
+ let publishCommands: Task.publishCommands = (aggregateName, cmdJsons) =>
144
+ CommandSent.announcing(
145
+ ~comp=`Task(${Spec.name})`,
146
+ publishToAggregates->Dict.get(aggregateName)->Option.getOrThrow,
147
+ )(cmdJsons)
146
148
 
147
149
  let config = Spec.setup(queryEngine, queryBucketName, opts)
148
150
 
@@ -11,6 +11,7 @@ import * as Task$ReventlessCore from "./Task.res.mjs";
11
11
  import * as Logger$ReventlessCore from "../../util/Logger.res.mjs";
12
12
  import * as Aggregate$ReventlessCore from "../Aggregate/Aggregate.res.mjs";
13
13
  import * as Component$ReventlessCore from "../Component.res.mjs";
14
+ import * as CommandSent$ReventlessCore from "../../CommandSent.res.mjs";
14
15
  import * as Util_Promise$ReventlessCore from "../../util/Util_Promise.res.mjs";
15
16
  import * as ComponentType$ReventlessCore from "../../ComponentType.res.mjs";
16
17
  import * as RuntimeHints$ReventlessInfra from "@reventlessdev/reventless-infra/src/types/RuntimeHints.res.mjs";
@@ -63,7 +64,7 @@ function Make(Spec) {
63
64
  return Pulumi.output("");
64
65
  }
65
66
  }));
66
- let publishCommands = (aggregateName, cmdJsons) => Stdlib_Option.getOrThrow(publishToAggregates[aggregateName], undefined)(cmdJsons);
67
+ let publishCommands = (aggregateName, cmdJsons) => CommandSent$ReventlessCore.announcing(`Task(` + Spec.name + `)`, Stdlib_Option.getOrThrow(publishToAggregates[aggregateName], undefined))(cmdJsons);
67
68
  let config = Spec.setup(queryEngine, queryBucketName, opts);
68
69
  let sideEffectHandler = Stdlib_Option.map(config.sideEffects, sideEffects => SpecificSideEffectHandler.make(extra$1, sideEffects, Aggregate$ReventlessCore.allEventTopics(allAggregates), allCommandTopics, undefined, queryEngine, scheduler, resourceNaming, undefined, undefined, undefined, opts));
69
70
  Stdlib_Option.forEach(sideEffectHandler, handler => Builder_Helpers$ReventlessCore.registerTaskSideEffectHandler(Component$ReventlessCore.operations(handler).apply(param => {}), SpecificSideEffectHandler.finish));
@@ -14,6 +14,10 @@ module type T = {
14
14
  }
15
15
 
16
16
  module Make = (Spec: Spec): T => {
17
+ // What this dispatcher logs is an event routed to an extension point or an extension, so its
18
+ // lines go by a contract's component name rather than the plugin's.
19
+ let comp = (id: string) => `ExtensionRouting(${id})`
20
+
17
21
  let warnedServices = ref(Belt.Set.String.empty)
18
22
 
19
23
  let knownServiceNames = () =>
@@ -44,7 +48,7 @@ module Make = (Spec: Spec): T => {
44
48
  warnedServices := warnedServices.contents->Belt.Set.String.add(serviceName)
45
49
  let known = knownServiceNames()->Array.join(", ")
46
50
  EffectLogger.logWarn(
47
- ~comp=`Plugin(${id})`,
51
+ ~comp=comp(id),
48
52
  `incoming event has service="${serviceName}" but no handler is registered for it — known services: [${known}]. Likely cause: an ExtensionPointMapping/Extension Delegate.name does not match the source's serviceName (DCB sources use "<plugin>DcbEventLog").`,
49
53
  )
50
54
  }
@@ -76,7 +80,7 @@ module Make = (Spec: Spec): T => {
76
80
  Spec.incomingExtensionEventHandlers->Dict.get(serviceName)->Option.isSome
77
81
  if isHandled {
78
82
  EffectLogger.logInfo(
79
- ~comp=`Plugin(${id})`,
83
+ ~comp=comp(id),
80
84
  `incoming event: ${LogFormat.eventSummary(eventJson')}`,
81
85
  )->Effect.zipRight(
82
86
  handleEventEffect(
@@ -9,6 +9,7 @@ import * as LogFormat$ReventlessCore from "../../util/LogFormat.res.mjs";
9
9
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
10
10
 
11
11
  function Make(Spec) {
12
+ let comp = id => `ExtensionRouting(` + id + `)`;
12
13
  let warnedServices = {
13
14
  contents: undefined
14
15
  };
@@ -27,7 +28,7 @@ function Make(Spec) {
27
28
  return Stdlib_Option.mapOr(Message$ReventlessCore.serviceNameOfMsg(eventJson$p), Effect.succeed(), serviceName => {
28
29
  let isHandled = Stdlib_Option.isSome(Spec.incomingConnectExtensionEventHandlers[serviceName]) || Stdlib_Option.isSome(Spec.outgoingExtensionPointEventHandlers[serviceName]) || Stdlib_Option.isSome(Spec.outgoingExtensionEventHandlers[serviceName]) || Stdlib_Option.isSome(Spec.incomingExtensionEventHandlers[serviceName]);
29
30
  if (isHandled) {
30
- return Effect.zipRight(EffectLogger$ReventlessCore.logInfo(`Plugin(` + id + `)`, undefined, undefined, `incoming event: ` + LogFormat$ReventlessCore.eventSummary(eventJson$p)), Effect.zipRight(handleEventEffect(eventJson$p, Spec.incomingConnectExtensionEventHandlers), Effect.map(Effect.all([
31
+ return Effect.zipRight(EffectLogger$ReventlessCore.logInfo(comp(id), undefined, undefined, `incoming event: ` + LogFormat$ReventlessCore.eventSummary(eventJson$p)), Effect.zipRight(handleEventEffect(eventJson$p, Spec.incomingConnectExtensionEventHandlers), Effect.map(Effect.all([
31
32
  handleEventEffect(eventJson$p, Spec.outgoingExtensionPointEventHandlers),
32
33
  handleEventEffect(eventJson$p, Spec.outgoingExtensionEventHandlers),
33
34
  handleEventEffect(eventJson$p, Spec.incomingExtensionEventHandlers)
@@ -40,7 +41,7 @@ function Make(Spec) {
40
41
  }
41
42
  warnedServices.contents = Belt_SetString.add(warnedServices.contents, serviceName);
42
43
  let known = knownServiceNames().join(", ");
43
- return EffectLogger$ReventlessCore.logWarn(`Plugin(` + id + `)`, undefined, undefined, `incoming event has service="` + serviceName + `" but no handler is registered for it — known services: [` + known + `]. Likely cause: an ExtensionPointMapping/Extension Delegate.name does not match the source's serviceName (DCB sources use "<plugin>DcbEventLog").`);
44
+ return EffectLogger$ReventlessCore.logWarn(comp(id), undefined, undefined, `incoming event has service="` + serviceName + `" but no handler is registered for it — known services: [` + known + `]. Likely cause: an ExtensionPointMapping/Extension Delegate.name does not match the source's serviceName (DCB sources use "<plugin>DcbEventLog").`);
44
45
  }
45
46
  });
46
47
  }));
@@ -1,15 +1,9 @@
1
- // Provider-neutral metric event emission.
2
- //
3
- // Emits one structured JSON line per occurrence — the same posture as `Logger`
4
- // (generic structured stdout, no provider vocabulary). Core does NOT know about
5
- // any provider's metric system: a collector/adapter turns these lines into real
6
- // metrics. On AWS a CloudWatch metric filter (defined in `reventless-aws`,
7
- // deploy-time) matches the discriminator field and extracts the `slice`
8
- // dimension + value; another provider can match the same generic shape.
9
- //
10
- // Suppressed outside a JSON sink so TTY dev runs stay clean. Used by the DCB
11
- // StateChangeSlice callback to surface append-retry / append-conflict rates per
12
- // slice (see docs/analysis/dcb-high-contention-handling.md §6).
1
+ // Provider-neutral metric emission. `emitCount` is the one call that emits a
2
+ // metric; where the count goes is the installed `Sink`. The default writes one
3
+ // JSON line per occurrence to a JSON log sink, which a deployed collector turns
4
+ // into a metric (on AWS a CloudWatch metric filter keys on `reventlessMetric`). A
5
+ // local platform installs `Counts.sink` instead: it counts in memory and writes
6
+ // nothing. See docs/plans/metrics-are-counted-not-logged.md.
13
7
 
14
8
  // Discriminator key a metric collector keys off. Stable — changing it is a
15
9
  // breaking change for any configured metric filter.
@@ -24,9 +18,150 @@ let metricLine = (~metric: string, ~slice: string, ~value: int=1): JSON.t =>
24
18
  ("unit", "Count"->JSON.Encode.string),
25
19
  ])->JSON.Encode.object
26
20
 
27
- // Emits a single count for `metric`, dimensioned by `slice`. No-op outside a
28
- // JSON sink. `value` defaults to 1 (one occurrence).
21
+ /** One occurrence of a metric, as a sink receives it. */
22
+ type sample = {
23
+ metric: string,
24
+ slice: string,
25
+ /** The plugin the slice belongs to, when the registry knows it. */
26
+ plugin?: string,
27
+ value: int,
28
+ unit: string,
29
+ /** In milliseconds since 1970. */
30
+ at: float,
31
+ }
32
+
33
+ module Sink = {
34
+ type t = sample => unit
35
+
36
+ /** The line a deployed collector reads, written only to a JSON log sink. */
37
+ let line: t = s =>
38
+ if Reventless.AnsiStyle.isJsonSink() {
39
+ Console.log(metricLine(~metric=s.metric, ~slice=s.slice, ~value=s.value)->JSON.stringify)
40
+ }
41
+
42
+ let current: ref<t> = ref(line)
43
+
44
+ /** Where every metric goes from now on, installed at a process's start. */
45
+ let install = (sink: t) => current := sink
46
+
47
+ /** Back to the line: for tests. */
48
+ let reset = () => current := line
49
+ }
50
+
51
+ // Emits a single count for `metric`, dimensioned by `slice`. `value` defaults to
52
+ // 1 (one occurrence).
29
53
  let emitCount = (~metric: string, ~slice: string, ~value: int=1): unit =>
30
- if Reventless.AnsiStyle.isJsonSink() {
31
- Console.log(metricLine(~metric, ~slice, ~value)->JSON.stringify)
54
+ Sink.current.contents({
55
+ metric,
56
+ slice,
57
+ plugin: ?Reventless.LogPrefix.lookup(slice),
58
+ value,
59
+ unit: "Count",
60
+ at: Date.now(),
61
+ })
62
+
63
+ /** Metrics counted in memory, per metric, plugin and slice: how often, the sum of
64
+ their values, the last time, and how many in each of the last 60 minutes. Bounded
65
+ by the number of slices, not of commands. */
66
+ module Counts = {
67
+ let minutes = 60
68
+ let minuteMs = 60_000.
69
+
70
+ type entry = {
71
+ metric: string,
72
+ plugin: string,
73
+ slice: string,
74
+ unit: string,
75
+ mutable count: int,
76
+ mutable sum: float,
77
+ mutable lastAt: float,
78
+ /** The counts of the last `minutes` minutes, oldest first; the last is `minute`'s. */
79
+ mutable perMinute: array<int>,
80
+ mutable minute: float,
81
+ }
82
+
83
+ type t = {entries: dict<entry>}
84
+
85
+ /** One count as a reader gets it. */
86
+ type snapshot = {
87
+ metric: string,
88
+ plugin: string,
89
+ slice: string,
90
+ unit: string,
91
+ count: int,
92
+ sum: float,
93
+ lastAt: float,
94
+ perMinute: array<int>,
95
+ }
96
+
97
+ let make = (): t => {entries: Dict.make()}
98
+
99
+ let clear = (t: t) => t.entries->Dict.keysToArray->Array.forEach(k => t.entries->Dict.delete(k))
100
+
101
+ let minuteOf = (at: float) => Math.floor(at /. minuteMs)
102
+
103
+ /** The buckets moved on to `minute`: the minutes since the last count are zero. */
104
+ let rolled = (perMinute: array<int>, ~from: float, ~to_: float): array<int> => {
105
+ let gap = Math.Int.min(minutes, (to_ -. from)->Float.toInt)
106
+ gap <= 0
107
+ ? perMinute
108
+ : perMinute
109
+ ->Array.slice(~start=gap, ~end=minutes)
110
+ ->Array.concat(Array.make(~length=gap, 0))
111
+ }
112
+
113
+ let add = (t: t, s: sample) => {
114
+ let plugin = s.plugin->Option.getOr("")
115
+ let key = `${s.metric}|${plugin}|${s.slice}`
116
+ let minute = minuteOf(s.at)
117
+ let e = switch t.entries->Dict.get(key) {
118
+ | Some(e) => e
119
+ | None =>
120
+ let e = {
121
+ metric: s.metric,
122
+ plugin,
123
+ slice: s.slice,
124
+ unit: s.unit,
125
+ count: 0,
126
+ sum: 0.,
127
+ lastAt: s.at,
128
+ perMinute: Array.make(~length=minutes, 0),
129
+ minute,
130
+ }
131
+ t.entries->Dict.set(key, e)
132
+ e
133
+ }
134
+ e.perMinute = rolled(e.perMinute, ~from=e.minute, ~to_=minute)
135
+ e.minute = Math.max(e.minute, minute)
136
+ let last = minutes - 1
137
+ e.perMinute->Array.setUnsafe(last, e.perMinute->Array.getUnsafe(last) + 1)
138
+ e.count = e.count + 1
139
+ e.sum = e.sum +. s.value->Int.toFloat
140
+ e.lastAt = Math.max(e.lastAt, s.at)
32
141
  }
142
+
143
+ /** The sink that counts into `t` and writes nothing. */
144
+ let sink = (t: t): Sink.t => s => add(t, s)
145
+
146
+ /** The counts at `now`, of the given plugins or of every one, ordered by plugin,
147
+ slice and metric. */
148
+ let read = (t: t, ~now: float, ~plugins: option<array<string>>=?): array<snapshot> =>
149
+ t.entries
150
+ ->Dict.valuesToArray
151
+ ->Array.filter(e => plugins->Option.mapOr(true, ps => ps->Array.includes(e.plugin)))
152
+ ->Array.map((e): snapshot => {
153
+ metric: e.metric,
154
+ plugin: e.plugin,
155
+ slice: e.slice,
156
+ unit: e.unit,
157
+ count: e.count,
158
+ sum: e.sum,
159
+ lastAt: e.lastAt,
160
+ perMinute: rolled(e.perMinute, ~from=e.minute, ~to_=minuteOf(now)),
161
+ })
162
+ ->Array.toSorted((a, b) =>
163
+ `${a.plugin}|${a.slice}|${a.metric}`->String.localeCompare(
164
+ `${b.plugin}|${b.slice}|${b.metric}`,
165
+ )
166
+ )
167
+ }
@@ -1,6 +1,10 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
+ import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
4
+ import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
5
+ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
3
6
  import * as AnsiStyle$Reventless from "@reventlessdev/reventless-spec/src/AnsiStyle.res.mjs";
7
+ import * as LogPrefix$Reventless from "@reventlessdev/reventless-spec/src/LogPrefix.res.mjs";
4
8
 
5
9
  let discriminator = "reventlessMetric";
6
10
 
@@ -26,17 +30,133 @@ function metricLine(metric, slice, valueOpt) {
26
30
  ]);
27
31
  }
28
32
 
29
- function emitCount(metric, slice, valueOpt) {
30
- let value = valueOpt !== undefined ? valueOpt : 1;
33
+ function line(s) {
31
34
  if (AnsiStyle$Reventless.isJsonSink()) {
32
- console.log(JSON.stringify(metricLine(metric, slice, value)));
35
+ console.log(JSON.stringify(metricLine(s.metric, s.slice, s.value)));
33
36
  return;
34
37
  }
35
38
  }
36
39
 
40
+ let current = {
41
+ contents: line
42
+ };
43
+
44
+ function install(sink) {
45
+ current.contents = sink;
46
+ }
47
+
48
+ function reset() {
49
+ current.contents = line;
50
+ }
51
+
52
+ let Sink = {
53
+ line: line,
54
+ current: current,
55
+ install: install,
56
+ reset: reset
57
+ };
58
+
59
+ function emitCount(metric, slice, valueOpt) {
60
+ let value = valueOpt !== undefined ? valueOpt : 1;
61
+ current.contents({
62
+ metric: metric,
63
+ slice: slice,
64
+ plugin: LogPrefix$Reventless.lookup(slice),
65
+ value: value,
66
+ unit: "Count",
67
+ at: Date.now()
68
+ });
69
+ }
70
+
71
+ function make() {
72
+ return {
73
+ entries: {}
74
+ };
75
+ }
76
+
77
+ function clear(t) {
78
+ Object.keys(t.entries).forEach(k => Stdlib_Dict.$$delete(t.entries, k));
79
+ }
80
+
81
+ function minuteOf(at) {
82
+ return Math.floor(at / 60000);
83
+ }
84
+
85
+ function rolled(perMinute, from, to_) {
86
+ let gap = Math.min(60, to_ - from | 0);
87
+ if (gap <= 0) {
88
+ return perMinute;
89
+ } else {
90
+ return perMinute.slice(gap, 60).concat(Stdlib_Array.make(gap, 0));
91
+ }
92
+ }
93
+
94
+ function add(t, s) {
95
+ let plugin = Stdlib_Option.getOr(s.plugin, "");
96
+ let key = s.metric + `|` + plugin + `|` + s.slice;
97
+ let minute = Math.floor(s.at / 60000);
98
+ let e = t.entries[key];
99
+ let e$1;
100
+ if (e !== undefined) {
101
+ e$1 = e;
102
+ } else {
103
+ let e$2 = {
104
+ metric: s.metric,
105
+ plugin: plugin,
106
+ slice: s.slice,
107
+ unit: s.unit,
108
+ count: 0,
109
+ sum: 0,
110
+ lastAt: s.at,
111
+ perMinute: Stdlib_Array.make(60, 0),
112
+ minute: minute
113
+ };
114
+ t.entries[key] = e$2;
115
+ e$1 = e$2;
116
+ }
117
+ e$1.perMinute = rolled(e$1.perMinute, e$1.minute, minute);
118
+ e$1.minute = Math.max(e$1.minute, minute);
119
+ let last = 59;
120
+ e$1.perMinute[last] = e$1.perMinute[last] + 1 | 0;
121
+ e$1.count = e$1.count + 1 | 0;
122
+ e$1.sum = e$1.sum + s.value;
123
+ e$1.lastAt = Math.max(e$1.lastAt, s.at);
124
+ }
125
+
126
+ function sink(t) {
127
+ return s => add(t, s);
128
+ }
129
+
130
+ function read(t, now, plugins) {
131
+ return Object.values(t.entries).filter(e => Stdlib_Option.mapOr(plugins, true, ps => ps.includes(e.plugin))).map(e => ({
132
+ metric: e.metric,
133
+ plugin: e.plugin,
134
+ slice: e.slice,
135
+ unit: e.unit,
136
+ count: e.count,
137
+ sum: e.sum,
138
+ lastAt: e.lastAt,
139
+ perMinute: rolled(e.perMinute, e.minute, Math.floor(now / 60000))
140
+ })).toSorted((a, b) => (a.plugin + `|` + a.slice + `|` + a.metric).localeCompare(b.plugin + `|` + b.slice + `|` + b.metric));
141
+ }
142
+
143
+ let Counts = {
144
+ minutes: 60,
145
+ minuteMs: 60000,
146
+ make: make,
147
+ clear: clear,
148
+ minuteOf: minuteOf,
149
+ rolled: rolled,
150
+ add: add,
151
+ sink: sink,
152
+ read: read
153
+ };
154
+
37
155
  export {
38
156
  discriminator,
39
157
  metricLine,
158
+ Sink,
40
159
  emitCount,
160
+ Counts,
41
161
  }
42
162
  /* AnsiStyle-Reventless Not a pure module */
@@ -361,7 +361,7 @@ globalThis.describe("semantic composites are named once, not once per field", ()
361
361
  globalThis.expect(fragmentTypes.some(t => t.includes("AddProductPrice"))).toBe(false);
362
362
  });
363
363
  globalThis.test("the output position keeps the bare name", () => {
364
- let types = GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Catalog_Product", undefined, false, undefined, Sury.$schema(s => ({
364
+ let types = GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Catalog_Product", undefined, false, undefined, undefined, Sury.$schema(s => ({
365
365
  productId: s.m(Sury.string),
366
366
  price: s.m(Money$Reventless.schema)
367
367
  })));
@@ -579,7 +579,7 @@ globalThis.describe("the by-index door", () => {
579
579
  });
580
580
 
581
581
  globalThis.describe("@internal keeps a field off the generated type", () => {
582
- let types = excludeFields => GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Platform_Widget", excludeFields, undefined, undefined, PsInternalFieldsReadModel$ReventlessCore.stateSchema).join("\n");
582
+ let types = excludeFields => GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Platform_Widget", excludeFields, undefined, undefined, undefined, PsInternalFieldsReadModel$ReventlessCore.stateSchema).join("\n");
583
583
  globalThis.test("the declared fields are absent", () => {
584
584
  globalThis.expect([
585
585
  types(undefined).includes("eventCollector"),
@@ -49,7 +49,7 @@ globalThis.describe("the identity semantic on the JSON Schema", () => {
49
49
  });
50
50
 
51
51
  globalThis.describe("the identity semantic in the SDL", () => {
52
- let sdl = GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Ordering_Order", undefined, false, undefined, orderSchema).join("\n");
52
+ let sdl = GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Ordering_Order", undefined, false, undefined, undefined, orderSchema).join("\n");
53
53
  globalThis.test("an identity field not named `*Id` is an ID", () => {
54
54
  globalThis.expect(sdl.includes("buyer: ID!")).toBe(true);
55
55
  });
@@ -0,0 +1,87 @@
1
+ // A view row carries the change that last wrote it: the flow, the event and the time, stamped
2
+ // by the encoder from what the projection is applying, and passed over when the row is read.
3
+
4
+ open JestGlobals
5
+ open QueryDbFixtures
6
+
7
+ module Ops = QueryDb_Operations.Make(
8
+ ItemQueryDbSpec,
9
+ {
10
+ let jsonOps = mockJsonOps
11
+ },
12
+ )
13
+
14
+ let _ = beforeEach(() => reset())
15
+
16
+ let ops: QueryDb.operations<string, ItemQueryDbSpec.state> = {
17
+ load: Ops.load,
18
+ loadStream: Ops.loadStream,
19
+ save: Ops.save,
20
+ saveBatch: Ops.saveBatch,
21
+ count: Ops.count,
22
+ delete: Ops.delete,
23
+ deleteBatch: Ops.deleteBatch,
24
+ }
25
+
26
+ let changeOf = (id: string): option<(option<string>, option<string>)> =>
27
+ store.contents
28
+ ->Dict.get(id)
29
+ ->Option.flatMap(rows => rows->Array.get(0))
30
+ ->Option.flatMap(JSON.Decode.object)
31
+ ->Option.flatMap(d => d->Dict.get(RowChange.key))
32
+ ->Option.flatMap(JSON.Decode.object)
33
+ ->Option.map(c => (
34
+ c->Dict.get("flow")->Option.flatMap(JSON.Decode.string),
35
+ c->Dict.get("msgId")->Option.flatMap(JSON.Decode.string),
36
+ ))
37
+
38
+ let source = (~flow="flow-1", msgId): Projection.changeSource => {msgId, correlationId: flow}
39
+
40
+ describe("a row's last change", () => {
41
+ testPromise("is the last of the events the projection applied", async () => {
42
+ let _ = await Projection.handleAction(
43
+ ~sources=[source("m-1"), source("m-2")],
44
+ Create("a", ({name: "A", count: 1}: ItemQueryDbSpec.state)),
45
+ ops,
46
+ None,
47
+ )
48
+ expect(changeOf("a"))->toEqual(Some((Some("flow-1"), Some("m-2"))))
49
+ })
50
+
51
+ testPromise("is not written by a save outside a projection", async () => {
52
+ let _ = await Ops.save("a", ({name: "A", count: 1}: ItemQueryDbSpec.state), Init, None)
53
+ expect(changeOf("a"))->toEqual(None)
54
+ })
55
+
56
+ testPromise("is each row's own when a projection writes several at once", async () => {
57
+ let _ = await Promise.all([
58
+ Projection.handleAction(
59
+ ~sources=[source(~flow="flow-a", "m-a")],
60
+ Create("a", ({name: "A", count: 1}: ItemQueryDbSpec.state)),
61
+ ops,
62
+ None,
63
+ ),
64
+ Projection.handleAction(
65
+ ~sources=[source(~flow="flow-b", "m-b")],
66
+ Create("b", ({name: "B", count: 1}: ItemQueryDbSpec.state)),
67
+ ops,
68
+ None,
69
+ ),
70
+ ])
71
+ expect((changeOf("a"), changeOf("b")))->toEqual((
72
+ Some((Some("flow-a"), Some("m-a"))),
73
+ Some((Some("flow-b"), Some("m-b"))),
74
+ ))
75
+ })
76
+
77
+ testPromise("is passed over when the row is read back", async () => {
78
+ let _ = await Projection.handleAction(
79
+ ~sources=[source("m-1")],
80
+ Create("a", ({name: "A", count: 1}: ItemQueryDbSpec.state)),
81
+ ops,
82
+ None,
83
+ )
84
+ let rows = await Ops.loadStream("a")->Stream.runCollect->Effect.runPromise
85
+ expect(rows->Array.map(r => (r.name, r.count)))->toEqual([("A", 1)])
86
+ })
87
+ })