@hot-updater/plugin-insights 1.0.0-rc.20

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 (85) hide show
  1. package/LICENSE +33 -0
  2. package/README.md +24 -0
  3. package/client/package.json +7 -0
  4. package/dist/client/eventId.cjs +181 -0
  5. package/dist/client/eventId.mjs +179 -0
  6. package/dist/client/index.cjs +234 -0
  7. package/dist/client/index.d.cts +42 -0
  8. package/dist/client/index.d.mts +42 -0
  9. package/dist/client/index.mjs +233 -0
  10. package/dist/client/sender.cjs +103 -0
  11. package/dist/client/sender.mjs +103 -0
  12. package/dist/client/state.cjs +121 -0
  13. package/dist/client/state.mjs +119 -0
  14. package/dist/internal.cjs +9 -0
  15. package/dist/internal.d.cts +5 -0
  16. package/dist/internal.d.mts +5 -0
  17. package/dist/internal.mjs +5 -0
  18. package/dist/server/contract.cjs +193 -0
  19. package/dist/server/contract.mjs +189 -0
  20. package/dist/server/domain.d.cts +149 -0
  21. package/dist/server/domain.d.mts +149 -0
  22. package/dist/server/errors.cjs +15 -0
  23. package/dist/server/errors.d.cts +6 -0
  24. package/dist/server/errors.d.mts +6 -0
  25. package/dist/server/errors.mjs +14 -0
  26. package/dist/server/eventInput.cjs +284 -0
  27. package/dist/server/eventInput.d.cts +6 -0
  28. package/dist/server/eventInput.d.mts +6 -0
  29. package/dist/server/eventInput.mjs +282 -0
  30. package/dist/server/eventRow.cjs +49 -0
  31. package/dist/server/eventRow.d.cts +64 -0
  32. package/dist/server/eventRow.d.mts +64 -0
  33. package/dist/server/eventRow.mjs +47 -0
  34. package/dist/server/index.cjs +128 -0
  35. package/dist/server/index.d.cts +394 -0
  36. package/dist/server/index.d.mts +394 -0
  37. package/dist/server/index.mjs +121 -0
  38. package/dist/server/model.cjs +17 -0
  39. package/dist/server/model.d.cts +10 -0
  40. package/dist/server/model.d.mts +10 -0
  41. package/dist/server/model.mjs +17 -0
  42. package/dist/server/modelTypes.d.cts +176 -0
  43. package/dist/server/modelTypes.d.mts +176 -0
  44. package/dist/server/overview.cjs +109 -0
  45. package/dist/server/overview.d.cts +15 -0
  46. package/dist/server/overview.d.mts +15 -0
  47. package/dist/server/overview.mjs +107 -0
  48. package/dist/server/provider.cjs +375 -0
  49. package/dist/server/provider.d.cts +6 -0
  50. package/dist/server/provider.d.mts +6 -0
  51. package/dist/server/provider.mjs +374 -0
  52. package/dist/server/queryInput.cjs +93 -0
  53. package/dist/server/queryInput.mjs +91 -0
  54. package/dist/server/reads.cjs +681 -0
  55. package/dist/server/reads.d.cts +60 -0
  56. package/dist/server/reads.d.mts +60 -0
  57. package/dist/server/reads.mjs +674 -0
  58. package/dist/server/recordEvent.cjs +319 -0
  59. package/dist/server/recordEvent.d.cts +17 -0
  60. package/dist/server/recordEvent.d.mts +17 -0
  61. package/dist/server/recordEvent.mjs +316 -0
  62. package/dist/server/routes.cjs +96 -0
  63. package/dist/server/routes.d.cts +36 -0
  64. package/dist/server/routes.d.mts +36 -0
  65. package/dist/server/routes.mjs +95 -0
  66. package/dist/server/schema.cjs +489 -0
  67. package/dist/server/schema.d.cts +706 -0
  68. package/dist/server/schema.d.mts +706 -0
  69. package/dist/server/schema.mjs +483 -0
  70. package/dist/server/testing/eventRow.d.mts +64 -0
  71. package/dist/server/testing/expectInsightsIndex.mjs +18 -0
  72. package/dist/server/testing/fixtures.d.mts +8 -0
  73. package/dist/server/testing/fixtures.mjs +25 -0
  74. package/dist/server/testing/index.d.mts +24 -0
  75. package/dist/server/testing/index.mjs +25 -0
  76. package/dist/server/testing/insightsModelTests.mjs +714 -0
  77. package/dist/server/testing/modelTypes.d.mts +176 -0
  78. package/dist/server/testing/setupInsightsHttpTestSuite.d.mts +11 -0
  79. package/dist/server/testing/setupInsightsHttpTestSuite.mjs +107 -0
  80. package/dist/server/testing/setupInsightsModelTestSuite.d.mts +11 -0
  81. package/dist/server/testing/setupInsightsModelTestSuite.mjs +31 -0
  82. package/dist/server/testing/types.d.mts +26 -0
  83. package/dist/server/types.d.cts +33 -0
  84. package/dist/server/types.d.mts +33 -0
  85. package/package.json +68 -0
@@ -0,0 +1,489 @@
1
+ let _hot_updater_plugin_core_server_plugin = require("@hot-updater/plugin-core/server-plugin");
2
+ //#region src/server/schema.ts
3
+ const HOUR_MS = 36e5;
4
+ const DAY_MS = 864e5;
5
+ const DEFAULT_INSIGHTS_RETENTION = {
6
+ rawDays: 90,
7
+ dailyDays: 400
8
+ };
9
+ const bucketRetention = (days) => ({
10
+ field: "bucket_start_ms",
11
+ days
12
+ });
13
+ const MOVEMENTS = /* @__PURE__ */ new Set([
14
+ "UPDATE_DOWNLOADED",
15
+ "UPDATE_APPLIED",
16
+ "RECOVERED",
17
+ "UPDATE_FAILED"
18
+ ]);
19
+ /** A failed update check: it names no target bundle, so no bundle list shows it. */
20
+ const isFailedCheck = (row) => row.type === "UPDATE_FAILED" && row.metadata?.failure?.stage === "check";
21
+ /**
22
+ * Downloads, applies, recoveries, and update failures, whole. An UNCHANGED
23
+ * report counts as a launch and moves its installation's head, but no list
24
+ * shows it, so no row keeps it.
25
+ */
26
+ const bundleEvents = (days) => (0, _hot_updater_plugin_core_server_plugin.defineTable)({
27
+ id: {
28
+ type: "string",
29
+ maxLength: 36
30
+ },
31
+ type: {
32
+ type: "string",
33
+ maxLength: 32
34
+ },
35
+ install_id: {
36
+ type: "string",
37
+ maxLength: 255
38
+ },
39
+ user_id: {
40
+ type: "string",
41
+ maxLength: 255,
42
+ required: false
43
+ },
44
+ from_release_id: {
45
+ type: "string",
46
+ maxLength: 36,
47
+ required: false
48
+ },
49
+ from_bundle_id: {
50
+ type: "string",
51
+ maxLength: 36,
52
+ required: false
53
+ },
54
+ to_release_id: {
55
+ type: "string",
56
+ maxLength: 36,
57
+ required: false
58
+ },
59
+ to_bundle_id: {
60
+ type: "string",
61
+ maxLength: 36
62
+ },
63
+ platform: {
64
+ type: "string",
65
+ maxLength: 16
66
+ },
67
+ app_version: { type: "string" },
68
+ channel: { type: "string" },
69
+ metadata: { type: "json" },
70
+ received_at_ms: { type: "integer" }
71
+ }, {
72
+ key: ["id"],
73
+ derived: {
74
+ /** The UTC day the event was received in. */
75
+ day: {
76
+ type: "integer",
77
+ compute: (row) => row.received_at_ms - row.received_at_ms % DAY_MS
78
+ },
79
+ /** Set only for events that belong in installation history. */
80
+ movement_install_id: {
81
+ type: "string",
82
+ compute: (row) => MOVEMENTS.has(row.type) ? row.install_id : null
83
+ },
84
+ /**
85
+ * The bundle a bundle filter matches: `from:<bundle>` for RECOVERED,
86
+ * none for a failed check, else `to:<bundle>`.
87
+ */
88
+ bundle_ref: {
89
+ type: "string",
90
+ multi: true,
91
+ compute: (row) => isFailedCheck(row) ? [] : [row.type === "RECOVERED" && row.from_bundle_id !== null ? `from:${row.from_bundle_id}` : `to:${row.to_bundle_id}`]
92
+ }
93
+ },
94
+ indexes: {
95
+ recent: {
96
+ eq: [
97
+ "channel",
98
+ "platform",
99
+ "day"
100
+ ],
101
+ sort: ["received_at_ms"]
102
+ },
103
+ movementsByInstall: {
104
+ eq: ["movement_install_id"],
105
+ sort: ["received_at_ms"]
106
+ },
107
+ byBundle: {
108
+ eq: [
109
+ "platform",
110
+ "channel",
111
+ "type",
112
+ "bundle_ref",
113
+ "day"
114
+ ],
115
+ sort: ["received_at_ms"]
116
+ },
117
+ /** Events of every scope by day, for the console's unfiltered event list. */
118
+ byDay: {
119
+ eq: ["day"],
120
+ sort: ["received_at_ms"]
121
+ }
122
+ },
123
+ retention: {
124
+ field: "day",
125
+ days
126
+ }
127
+ });
128
+ /**
129
+ * Each installation's latest event, whole, so reading it is one point read.
130
+ * Its gauges count it in the UTC day of that event. An update failure moves
131
+ * nothing, so no head holds one.
132
+ */
133
+ const bundleEventHeads = (days) => (0, _hot_updater_plugin_core_server_plugin.defineTable)({
134
+ install_id: {
135
+ type: "string",
136
+ maxLength: 255
137
+ },
138
+ id: {
139
+ type: "string",
140
+ maxLength: 36
141
+ },
142
+ type: {
143
+ type: "string",
144
+ maxLength: 32
145
+ },
146
+ user_id: {
147
+ type: "string",
148
+ maxLength: 255,
149
+ required: false
150
+ },
151
+ from_release_id: {
152
+ type: "string",
153
+ maxLength: 36,
154
+ required: false
155
+ },
156
+ from_bundle_id: {
157
+ type: "string",
158
+ maxLength: 36,
159
+ required: false
160
+ },
161
+ to_release_id: {
162
+ type: "string",
163
+ maxLength: 36,
164
+ required: false
165
+ },
166
+ to_bundle_id: {
167
+ type: "string",
168
+ maxLength: 36
169
+ },
170
+ platform: {
171
+ type: "string",
172
+ maxLength: 16
173
+ },
174
+ app_version: { type: "string" },
175
+ channel: { type: "string" },
176
+ metadata: { type: "json" },
177
+ received_at_ms: { type: "integer" }
178
+ }, {
179
+ key: ["install_id"],
180
+ indexes: { byUser: {
181
+ eq: ["user_id"],
182
+ sort: ["install_id"]
183
+ } },
184
+ retention: {
185
+ field: "received_at_ms",
186
+ days
187
+ }
188
+ });
189
+ /**
190
+ * Shards are keyed by install id. Counters are blind increments and never
191
+ * conflict, so they keep 8. Gauges and sketches are read, merged, and written
192
+ * back. B3's rollout gate on PostgreSQL retried 2.4% of transactions at 8
193
+ * shards and 1.3% at 16. DynamoDB's transactions take longer, so D8's gate
194
+ * on DynamoDB Local retried 2–24% at 16, almost all on gauge rows, and 1–5%
195
+ * with gauges at 32. Sketches stay at 16: each shard row carries 2 KB of
196
+ * registers that every read merges.
197
+ *
198
+ * Every Insights aggregate is `batched`: on DynamoDB and Firestore, which
199
+ * bill each write, an event's changes apply after it commits, merged with
200
+ * other events' into one write per row, on one shard per writer.
201
+ */
202
+ const COUNTER_SHARDS = 8;
203
+ const GAUGE_SHARDS = 32;
204
+ const SKETCH_SHARDS = 16;
205
+ /** Rows keyed by a hashed identity (scope and period) and a bucket. */
206
+ const identityFields = {
207
+ identity: {
208
+ type: "string",
209
+ maxLength: 32
210
+ },
211
+ bucket_start_ms: { type: "integer" }
212
+ };
213
+ const window = {
214
+ eq: ["identity"],
215
+ sort: ["bucket_start_ms"]
216
+ };
217
+ /**
218
+ * Counters of one period kind: hourly, daily, or lifetime at bucket 0. A
219
+ * release or channel row counts downloads, launches, and failed launches,
220
+ * and downloads a patch delivered and those that fell back from one. A
221
+ * release's lifetime row counts its failed updates (`failed_updates`), since
222
+ * no breakdown row outlives the raw period; windowed reads sum
223
+ * `insights_failures` instead.
224
+ */
225
+ const counters = (retention) => (0, _hot_updater_plugin_core_server_plugin.defineAggregate)(identityFields, {
226
+ key: ["identity", "bucket_start_ms"],
227
+ counters: [
228
+ "downloads",
229
+ "launches",
230
+ "failed_launches",
231
+ "failed_updates",
232
+ "patch_downloads",
233
+ "patch_fallbacks"
234
+ ],
235
+ shards: COUNTER_SHARDS,
236
+ batched: true,
237
+ indexes: { window },
238
+ ...retention === void 0 ? {} : { retention }
239
+ });
240
+ /**
241
+ * Unique-installation sketches of one period kind. Rows of a failure
242
+ * identity (`failure` and `check` scopes) keep only `failed_users`, so a
243
+ * release's launch rows never grow by a failure's registers.
244
+ */
245
+ const sketches = (retention) => (0, _hot_updater_plugin_core_server_plugin.defineAggregate)(identityFields, {
246
+ key: ["identity", "bucket_start_ms"],
247
+ distinct: [
248
+ "launch_users",
249
+ "activity_users",
250
+ "failed_users"
251
+ ],
252
+ shards: SKETCH_SHARDS,
253
+ batched: true,
254
+ indexes: { window },
255
+ retention
256
+ });
257
+ /** Installations whose update to a release failed, since its first failure: kept, at bucket 0. */
258
+ const lifetimeSketches = () => (0, _hot_updater_plugin_core_server_plugin.defineAggregate)(identityFields, {
259
+ key: ["identity", "bucket_start_ms"],
260
+ distinct: ["failed_users"],
261
+ shards: SKETCH_SHARDS,
262
+ batched: true,
263
+ indexes: { window }
264
+ });
265
+ /**
266
+ * Update failures by what failed, and recoveries by why the crashed process
267
+ * exited, a row an hour. `release_id` is the failed update's target release,
268
+ * or empty for a failed check or an unknown target, and the recovered
269
+ * release for a recovery. A failure's `stage` and `reason` say where and why
270
+ * it failed, and `detail` what else the client knew, as the JSON array
271
+ * `[resource, httpStatus, originCode, transport]`. A recovery's stage is
272
+ * `launch` and its reason the exit reason, with an empty detail. Every index
273
+ * is in key order, so its counters stay blind increments.
274
+ */
275
+ const insightsFailures = (retention) => (0, _hot_updater_plugin_core_server_plugin.defineAggregate)({
276
+ platform: {
277
+ type: "string",
278
+ maxLength: 16
279
+ },
280
+ channel: { type: "string" },
281
+ bucket_start_ms: { type: "integer" },
282
+ release_id: {
283
+ type: "string",
284
+ maxLength: 36
285
+ },
286
+ stage: {
287
+ type: "string",
288
+ maxLength: 16
289
+ },
290
+ reason: {
291
+ type: "string",
292
+ maxLength: 64
293
+ },
294
+ detail: {
295
+ type: "string",
296
+ maxLength: 160
297
+ }
298
+ }, {
299
+ key: [
300
+ "platform",
301
+ "channel",
302
+ "bucket_start_ms",
303
+ "release_id",
304
+ "stage",
305
+ "reason",
306
+ "detail"
307
+ ],
308
+ counters: ["events"],
309
+ shards: COUNTER_SHARDS,
310
+ batched: true,
311
+ indexes: { byScope: {
312
+ eq: ["platform", "channel"],
313
+ sort: ["bucket_start_ms"]
314
+ } },
315
+ retention
316
+ });
317
+ /** Installations counted in the UTC day of their latest event, so reads sum whole UTC days. */
318
+ const insightsDistribution = (retention) => (0, _hot_updater_plugin_core_server_plugin.defineAggregate)({
319
+ channel: { type: "string" },
320
+ platform: {
321
+ type: "string",
322
+ maxLength: 16
323
+ },
324
+ app_version: { type: "string" },
325
+ release_id: {
326
+ type: "string",
327
+ maxLength: 36
328
+ },
329
+ bucket_start_ms: { type: "integer" }
330
+ }, {
331
+ key: [
332
+ "channel",
333
+ "platform",
334
+ "app_version",
335
+ "release_id",
336
+ "bucket_start_ms"
337
+ ],
338
+ gauges: ["latest_installations"],
339
+ shards: GAUGE_SHARDS,
340
+ batched: true,
341
+ indexes: {
342
+ byScope: {
343
+ eq: ["channel", "platform"],
344
+ sort: ["bucket_start_ms"]
345
+ },
346
+ byVersion: {
347
+ eq: [
348
+ "channel",
349
+ "platform",
350
+ "app_version"
351
+ ],
352
+ sort: ["bucket_start_ms"]
353
+ }
354
+ },
355
+ retention
356
+ });
357
+ /** Latest events by the bundle they came from or went to, bucketed like the distribution. */
358
+ const insightsLatestByBundle = (retention) => (0, _hot_updater_plugin_core_server_plugin.defineAggregate)({
359
+ platform: {
360
+ type: "string",
361
+ maxLength: 16
362
+ },
363
+ channel: { type: "string" },
364
+ bundle_field: {
365
+ type: "string",
366
+ maxLength: 16
367
+ },
368
+ bundle_id: {
369
+ type: "string",
370
+ maxLength: 36
371
+ },
372
+ type: {
373
+ type: "string",
374
+ maxLength: 32
375
+ },
376
+ bucket_start_ms: { type: "integer" }
377
+ }, {
378
+ key: [
379
+ "platform",
380
+ "channel",
381
+ "bundle_field",
382
+ "bundle_id",
383
+ "type",
384
+ "bucket_start_ms"
385
+ ],
386
+ gauges: ["installations"],
387
+ shards: GAUGE_SHARDS,
388
+ batched: true,
389
+ indexes: { byBundle: {
390
+ eq: [
391
+ "platform",
392
+ "channel",
393
+ "bundle_field",
394
+ "bundle_id",
395
+ "type"
396
+ ],
397
+ sort: ["bucket_start_ms"]
398
+ } },
399
+ retention
400
+ });
401
+ /**
402
+ * Stored events by outcome and the bundle a bundle filter matches, a row an
403
+ * hour, which a failed check has none of; and every stored event, a row a UTC
404
+ * day (`DAILY_EVENTS`). Bundle counts sum the hourly rows, and event lists
405
+ * read either to skip days without events.
406
+ */
407
+ const insightsOutcomes = (retention) => (0, _hot_updater_plugin_core_server_plugin.defineAggregate)({
408
+ platform: {
409
+ type: "string",
410
+ maxLength: 16
411
+ },
412
+ channel: { type: "string" },
413
+ type: {
414
+ type: "string",
415
+ maxLength: 32
416
+ },
417
+ bundle_ref: {
418
+ type: "string",
419
+ maxLength: 41
420
+ },
421
+ bucket_start_ms: { type: "integer" }
422
+ }, {
423
+ key: [
424
+ "platform",
425
+ "channel",
426
+ "type",
427
+ "bundle_ref",
428
+ "bucket_start_ms"
429
+ ],
430
+ counters: ["events"],
431
+ shards: COUNTER_SHARDS,
432
+ batched: true,
433
+ indexes: { byRef: {
434
+ eq: [
435
+ "platform",
436
+ "channel",
437
+ "type",
438
+ "bundle_ref"
439
+ ],
440
+ sort: ["bucket_start_ms"]
441
+ } },
442
+ retention
443
+ });
444
+ /**
445
+ * The `insights_outcomes` identity that counts every stored event, for the
446
+ * global event list. Its bucket is a UTC day, not an hour. Every event's
447
+ * platform is ios or android, so no outcome row of a bundle filter shares it.
448
+ */
449
+ const DAILY_EVENTS = {
450
+ platform: "*",
451
+ channel: "",
452
+ type: "",
453
+ bundle_ref: ""
454
+ };
455
+ /**
456
+ * Insights' tables, each expiring after its period. The periods are values
457
+ * the engine applies at runtime; the tables and indexes are the same for
458
+ * any periods.
459
+ */
460
+ const createInsightsSchema = ({ rawDays, dailyDays } = DEFAULT_INSIGHTS_RETENTION) => {
461
+ const hourly = bucketRetention(rawDays);
462
+ const daily = bucketRetention(dailyDays);
463
+ return {
464
+ bundle_events: bundleEvents(rawDays),
465
+ bundle_event_heads: bundleEventHeads(dailyDays),
466
+ /** Hourly counters and sketches: the raw period. */
467
+ insights_overview: counters(hourly),
468
+ insights_sketches: sketches(hourly),
469
+ /** Daily counters and sketches: the daily period. */
470
+ insights_overview_daily: counters(daily),
471
+ insights_sketches_daily: sketches(daily),
472
+ /** A release's counters since its first event, and its failed installations: kept. */
473
+ insights_overview_lifetime: counters(),
474
+ insights_sketches_lifetime: lifetimeSketches(),
475
+ insights_distribution: insightsDistribution(daily),
476
+ insights_latest_by_bundle: insightsLatestByBundle(daily),
477
+ insights_outcomes: insightsOutcomes(hourly),
478
+ insights_failures: insightsFailures(hourly)
479
+ };
480
+ };
481
+ const insightsSchema = createInsightsSchema();
482
+ //#endregion
483
+ exports.DAILY_EVENTS = DAILY_EVENTS;
484
+ exports.DAY_MS = DAY_MS;
485
+ exports.DEFAULT_INSIGHTS_RETENTION = DEFAULT_INSIGHTS_RETENTION;
486
+ exports.HOUR_MS = HOUR_MS;
487
+ exports.createInsightsSchema = createInsightsSchema;
488
+ exports.insightsSchema = insightsSchema;
489
+ exports.isFailedCheck = isFailedCheck;