@hot-updater/server 1.0.0-rc.17 → 1.0.0-rc.18

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 (304) hide show
  1. package/dist/adapters/drizzle.cjs +5 -5
  2. package/dist/adapters/drizzle.mjs +4 -4
  3. package/dist/adapters/kysely.cjs +3 -3
  4. package/dist/adapters/kysely.mjs +2 -2
  5. package/dist/adapters/mongodb.cjs +3 -3
  6. package/dist/adapters/mongodb.mjs +2 -2
  7. package/dist/adapters/mongodbAdapter.cjs +19 -1
  8. package/dist/adapters/mongodbAdapter.mjs +19 -1
  9. package/dist/adapters/prisma.cjs +5 -5
  10. package/dist/adapters/prisma.mjs +4 -4
  11. package/dist/assembly/assemblePlugins.cjs +47 -28
  12. package/dist/assembly/assemblePlugins.d.cts +3 -7
  13. package/dist/assembly/assemblePlugins.d.mts +3 -7
  14. package/dist/assembly/assemblePlugins.mjs +46 -27
  15. package/dist/assembly/clientPlugins.cjs +86 -0
  16. package/dist/assembly/clientPlugins.d.cts +16 -0
  17. package/dist/assembly/clientPlugins.d.mts +16 -0
  18. package/dist/assembly/clientPlugins.mjs +86 -0
  19. package/dist/assembly/configError.cjs +7 -0
  20. package/dist/assembly/configError.d.cts +7 -0
  21. package/dist/assembly/configError.d.mts +7 -0
  22. package/dist/assembly/configError.mjs +7 -0
  23. package/dist/assembly/databasePlugins.cjs +5 -1
  24. package/dist/assembly/databasePlugins.d.cts +5 -5
  25. package/dist/assembly/databasePlugins.d.mts +5 -5
  26. package/dist/assembly/databasePlugins.mjs +5 -1
  27. package/dist/assembly/pluginCli.cjs +125 -0
  28. package/dist/assembly/pluginCli.d.cts +59 -0
  29. package/dist/assembly/pluginCli.d.mts +59 -0
  30. package/dist/assembly/pluginCli.mjs +121 -0
  31. package/dist/createHotUpdaterCore.cjs +14 -16
  32. package/dist/createHotUpdaterCore.d.cts +10 -3
  33. package/dist/createHotUpdaterCore.d.mts +10 -3
  34. package/dist/createHotUpdaterCore.mjs +11 -13
  35. package/dist/database/aggregateBatching.cjs +626 -0
  36. package/dist/database/aggregateBatching.d.cts +10 -0
  37. package/dist/database/aggregateBatching.d.mts +10 -0
  38. package/dist/database/aggregateBatching.mjs +625 -0
  39. package/dist/{insights/errors.cjs → database/busy.cjs} +2 -15
  40. package/dist/database/busy.d.cts +12 -0
  41. package/dist/database/busy.d.mts +12 -0
  42. package/dist/{insights/errors.mjs → database/busy.mjs} +3 -14
  43. package/dist/database/database.cjs +22 -3
  44. package/dist/database/database.d.cts +15 -2
  45. package/dist/database/database.d.mts +15 -2
  46. package/dist/database/database.mjs +22 -3
  47. package/dist/database/definitions.d.cts +10 -0
  48. package/dist/database/definitions.d.mts +10 -0
  49. package/dist/database/engine.d.cts +2 -1
  50. package/dist/database/engine.d.mts +2 -1
  51. package/dist/database/engineAggregates.cjs +7 -4
  52. package/dist/database/engineAggregates.mjs +8 -5
  53. package/dist/database/engineReads.cjs +5 -3
  54. package/dist/database/engineReads.mjs +6 -4
  55. package/dist/database/engineTransaction.cjs +2 -6
  56. package/dist/database/engineTransaction.mjs +2 -6
  57. package/dist/database/fence.cjs +2 -1
  58. package/dist/database/fence.mjs +2 -1
  59. package/dist/database/index.cjs +17 -8
  60. package/dist/database/index.d.cts +5 -4
  61. package/dist/database/index.d.mts +5 -4
  62. package/dist/database/index.mjs +6 -5
  63. package/dist/database/kv/kvAdapter.cjs +15 -3
  64. package/dist/database/kv/kvAdapter.d.cts +8 -0
  65. package/dist/database/kv/kvAdapter.d.mts +8 -0
  66. package/dist/database/kv/kvAdapter.mjs +16 -4
  67. package/dist/database/resolveSchema.cjs +4 -1
  68. package/dist/database/resolveSchema.d.cts +1 -1
  69. package/dist/database/resolveSchema.d.mts +1 -1
  70. package/dist/database/resolveSchema.mjs +4 -1
  71. package/dist/database/retention.cjs +92 -0
  72. package/dist/database/retention.mjs +90 -0
  73. package/dist/database/schema.cjs +5 -2
  74. package/dist/database/schema.d.cts +30 -1
  75. package/dist/database/schema.d.mts +30 -1
  76. package/dist/database/schema.mjs +5 -2
  77. package/dist/database/sql/sqlAdapter.cjs +8 -0
  78. package/dist/database/sql/sqlAdapter.mjs +9 -1
  79. package/dist/database/sql/sqlSchema.cjs +56 -0
  80. package/dist/database/sql/sqlSchema.mjs +55 -1
  81. package/dist/db/coreDatabase.cjs +85 -0
  82. package/dist/db/coreDatabase.d.cts +57 -0
  83. package/dist/db/coreDatabase.d.mts +57 -0
  84. package/dist/db/coreDatabase.mjs +78 -0
  85. package/dist/db/index.cjs +11 -1
  86. package/dist/db/index.d.cts +7 -2
  87. package/dist/db/index.d.mts +7 -2
  88. package/dist/db/index.mjs +6 -2
  89. package/dist/db/types.d.cts +3 -3
  90. package/dist/db/types.d.mts +3 -3
  91. package/dist/handler.cjs +2 -14
  92. package/dist/handler.mjs +1 -13
  93. package/dist/handlerReleaseCatalogRoutes.cjs +21 -2
  94. package/dist/handlerReleaseCatalogRoutes.mjs +21 -2
  95. package/dist/index.cjs +2 -6
  96. package/dist/index.d.cts +4 -7
  97. package/dist/index.d.mts +4 -7
  98. package/dist/index.mjs +2 -4
  99. package/dist/package.cjs +1 -1
  100. package/dist/package.mjs +1 -1
  101. package/dist/{apiKeys.cjs → plugins/api-keys/apiKeys.cjs} +1 -1
  102. package/dist/{apiKeys.d.cts → plugins/api-keys/apiKeys.d.cts} +2 -2
  103. package/dist/{apiKeys.d.mts → plugins/api-keys/apiKeys.d.mts} +2 -2
  104. package/dist/{apiKeys.mjs → plugins/api-keys/apiKeys.mjs} +1 -1
  105. package/dist/plugins/api-keys/cli.cjs +115 -0
  106. package/dist/plugins/api-keys/cli.mjs +114 -0
  107. package/dist/plugins/api-keys/index.cjs +9 -6
  108. package/dist/plugins/api-keys/index.d.cts +4 -5
  109. package/dist/plugins/api-keys/index.d.mts +4 -5
  110. package/dist/plugins/api-keys/index.mjs +9 -7
  111. package/dist/plugins/api-keys/model.d.cts +20 -2
  112. package/dist/plugins/api-keys/model.d.mts +20 -2
  113. package/dist/plugins/definePlugin.cjs +1 -1
  114. package/dist/plugins/definePlugin.d.cts +130 -3
  115. package/dist/plugins/definePlugin.d.mts +130 -3
  116. package/dist/plugins/definePlugin.mjs +1 -1
  117. package/dist/plugins/index.cjs +25 -4
  118. package/dist/plugins/index.d.cts +7 -4
  119. package/dist/plugins/index.d.mts +7 -4
  120. package/dist/plugins/index.mjs +6 -4
  121. package/dist/plugins/insights/contract.cjs +193 -0
  122. package/dist/plugins/insights/contract.mjs +189 -0
  123. package/dist/{insights → plugins/insights}/domain.d.cts +60 -15
  124. package/dist/{insights → plugins/insights}/domain.d.mts +60 -15
  125. package/dist/plugins/insights/errors.cjs +15 -0
  126. package/dist/plugins/insights/errors.mjs +14 -0
  127. package/dist/{insights → plugins/insights}/eventInput.cjs +155 -22
  128. package/dist/{insights → plugins/insights}/eventInput.mjs +155 -22
  129. package/dist/plugins/insights/eventRow.cjs +49 -0
  130. package/dist/plugins/insights/eventRow.d.cts +64 -0
  131. package/dist/plugins/insights/eventRow.d.mts +64 -0
  132. package/dist/plugins/insights/eventRow.mjs +47 -0
  133. package/dist/plugins/insights/index.cjs +106 -26
  134. package/dist/plugins/insights/index.d.cts +130 -27
  135. package/dist/plugins/insights/index.d.mts +130 -27
  136. package/dist/plugins/insights/index.mjs +106 -29
  137. package/dist/plugins/insights/model.cjs +2 -2
  138. package/dist/plugins/insights/model.d.cts +1 -1
  139. package/dist/plugins/insights/model.d.mts +1 -1
  140. package/dist/plugins/insights/model.mjs +1 -1
  141. package/dist/plugins/insights/modelTypes.d.cts +176 -0
  142. package/dist/plugins/insights/modelTypes.d.mts +176 -0
  143. package/dist/plugins/insights/overview.cjs +109 -0
  144. package/dist/plugins/insights/overview.d.cts +15 -0
  145. package/dist/plugins/insights/overview.d.mts +15 -0
  146. package/dist/plugins/insights/overview.mjs +107 -0
  147. package/dist/{insights → plugins/insights}/provider.cjs +33 -14
  148. package/dist/{insights → plugins/insights}/provider.d.cts +2 -2
  149. package/dist/{insights → plugins/insights}/provider.d.mts +2 -2
  150. package/dist/{insights → plugins/insights}/provider.mjs +30 -11
  151. package/dist/{insights → plugins/insights}/queryInput.cjs +2 -2
  152. package/dist/{insights → plugins/insights}/queryInput.mjs +2 -2
  153. package/dist/plugins/insights/reads.cjs +298 -88
  154. package/dist/plugins/insights/reads.d.cts +61 -0
  155. package/dist/plugins/insights/reads.d.mts +61 -0
  156. package/dist/plugins/insights/reads.mjs +298 -90
  157. package/dist/plugins/insights/recordEvent.cjs +223 -39
  158. package/dist/plugins/insights/recordEvent.d.cts +9 -4
  159. package/dist/plugins/insights/recordEvent.d.mts +9 -4
  160. package/dist/plugins/insights/recordEvent.mjs +221 -37
  161. package/dist/{insights → plugins/insights}/routes.cjs +3 -30
  162. package/dist/{insights → plugins/insights}/routes.mjs +4 -28
  163. package/dist/plugins/insights/schema.cjs +182 -40
  164. package/dist/plugins/insights/schema.d.cts +446 -6
  165. package/dist/plugins/insights/schema.d.mts +446 -6
  166. package/dist/plugins/insights/schema.mjs +180 -41
  167. package/dist/plugins/insights/testing/eventRow.d.mts +64 -0
  168. package/dist/plugins/insights/testing/expectInsightsIndex.mjs +18 -0
  169. package/dist/plugins/insights/testing/fixtures.d.mts +8 -0
  170. package/dist/plugins/insights/testing/fixtures.mjs +25 -0
  171. package/dist/plugins/insights/testing/index.d.mts +24 -0
  172. package/dist/plugins/insights/testing/index.mjs +25 -0
  173. package/dist/plugins/insights/testing/insightsModelTests.mjs +714 -0
  174. package/dist/plugins/insights/testing/modelTypes.d.mts +176 -0
  175. package/dist/plugins/insights/testing/setupInsightsHttpTestSuite.d.mts +11 -0
  176. package/dist/plugins/insights/testing/setupInsightsHttpTestSuite.mjs +107 -0
  177. package/dist/plugins/insights/testing/setupInsightsModelTestSuite.d.mts +11 -0
  178. package/dist/plugins/insights/testing/setupInsightsModelTestSuite.mjs +31 -0
  179. package/dist/plugins/insights/testing/types.d.mts +26 -0
  180. package/dist/{insights → plugins/insights}/types.d.cts +3 -2
  181. package/dist/{insights → plugins/insights}/types.d.mts +3 -2
  182. package/package.json +14 -7
  183. package/src/adapters/drizzle.conformance.integration.spec.ts +2 -2
  184. package/src/adapters/drizzle.readBudgets.integration.spec.ts +6 -4
  185. package/src/adapters/drizzle.spec.ts +15 -7
  186. package/src/adapters/drizzle.ts +6 -6
  187. package/src/adapters/kysely.conformance.integration.spec.ts +2 -2
  188. package/src/adapters/kysely.readBudgets.integration.spec.ts +4 -2
  189. package/src/adapters/kysely.spec.ts +13 -9
  190. package/src/adapters/kysely.ts +2 -2
  191. package/src/adapters/mongodb.integration.spec.ts +98 -7
  192. package/src/adapters/mongodb.ts +2 -2
  193. package/src/adapters/mongodbAdapter.ts +14 -0
  194. package/src/adapters/prisma.conformance.integration.spec.ts +2 -2
  195. package/src/adapters/prisma.integration.spec.ts +16 -8
  196. package/src/adapters/prisma.readBudgets.integration.spec.ts +5 -2
  197. package/src/adapters/prisma.spec.ts +17 -9
  198. package/src/adapters/prisma.ts +6 -6
  199. package/src/adapters/prismaTestClients.ts +8 -1
  200. package/src/assembly/assemblePlugins.ts +73 -40
  201. package/src/assembly/assembly.spec.ts +63 -11
  202. package/src/assembly/assembly.types.spec.ts +50 -2
  203. package/src/assembly/clientPlugins.ts +113 -0
  204. package/src/assembly/configError.ts +4 -0
  205. package/src/assembly/databasePlugins.ts +11 -5
  206. package/src/assembly/pluginCli.spec.ts +254 -0
  207. package/src/assembly/pluginCli.ts +248 -0
  208. package/src/assembly/{builtInRoutes.spec.ts → pluginRoutes.spec.ts} +10 -37
  209. package/src/createHotUpdaterCore.ts +21 -17
  210. package/src/database/aggregateBatching.spec.ts +577 -0
  211. package/src/database/aggregateBatching.ts +803 -0
  212. package/src/{insights/errors.spec.ts → database/busy.spec.ts} +2 -5
  213. package/src/{insights/errors.ts → database/busy.ts} +1 -13
  214. package/src/database/database.ts +39 -8
  215. package/src/database/definitions.ts +4 -0
  216. package/src/database/engineAggregates.spec.ts +4 -7
  217. package/src/database/engineAggregates.ts +5 -9
  218. package/src/database/engineReads.ts +7 -5
  219. package/src/database/engineTransaction.ts +1 -7
  220. package/src/database/fence.spec.ts +5 -5
  221. package/src/database/fence.ts +4 -0
  222. package/src/database/index.ts +13 -7
  223. package/src/database/kv/kvAdapter.batching.integration.spec.ts +15 -0
  224. package/src/database/kv/kvAdapter.conformance.integration.spec.ts +2 -0
  225. package/src/database/kv/kvAdapter.spec.ts +128 -1
  226. package/src/database/kv/kvAdapter.ts +30 -1
  227. package/src/database/kv/kvTestStore.ts +28 -1
  228. package/src/database/resolveSchema.ts +4 -1
  229. package/src/database/retention.spec.ts +314 -0
  230. package/src/database/retention.ts +129 -0
  231. package/src/database/schema.ts +44 -0
  232. package/src/database/sql/sqlAdapter.integration.spec.ts +4 -7
  233. package/src/database/sql/sqlAdapter.readBudgets.integration.spec.ts +18 -4
  234. package/src/database/sql/sqlAdapter.spec.ts +91 -3
  235. package/src/database/sql/sqlAdapter.ts +20 -0
  236. package/src/database/sql/sqlSchema.ts +81 -0
  237. package/src/databaseTestSuite.spec.ts +9 -4
  238. package/src/db/coreDatabase.ts +153 -0
  239. package/src/db/engineSql.spec.ts +8 -7
  240. package/src/db/index.ts +19 -1
  241. package/src/db/pluginTables.spec.ts +57 -27
  242. package/src/db/types.ts +3 -3
  243. package/src/handler-standalone.integration.spec.ts +2 -3
  244. package/src/handler.ts +1 -32
  245. package/src/handlerReleaseCatalogRoutes.ts +26 -2
  246. package/src/index.ts +0 -10
  247. package/src/{apiKeys.integration.spec.ts → plugins/api-keys/apiKeys.integration.spec.ts} +4 -4
  248. package/src/{apiKeys.spec.ts → plugins/api-keys/apiKeys.spec.ts} +3 -3
  249. package/src/{apiKeys.ts → plugins/api-keys/apiKeys.ts} +1 -1
  250. package/src/plugins/api-keys/apiKeysPlugin.spec.ts +11 -15
  251. package/src/plugins/api-keys/cli.spec.ts +187 -0
  252. package/src/plugins/api-keys/cli.ts +140 -0
  253. package/src/plugins/api-keys/index.ts +48 -41
  254. package/src/plugins/api-keys/model.ts +21 -2
  255. package/src/plugins/definePlugin.ts +144 -2
  256. package/src/plugins/index.ts +20 -2
  257. package/src/plugins/insights/contract.spec.ts +285 -0
  258. package/src/plugins/insights/contract.ts +372 -0
  259. package/src/{insights → plugins/insights}/domain.ts +73 -15
  260. package/src/plugins/insights/errors.ts +11 -0
  261. package/src/{insights → plugins/insights}/eventInput.ts +189 -29
  262. package/src/plugins/insights/eventRow.ts +209 -0
  263. package/src/plugins/insights/index.ts +181 -45
  264. package/src/{insights → plugins/insights}/insights.integration.spec.ts +330 -34
  265. package/src/plugins/insights/insightsRollout.integration.spec.ts +1 -2
  266. package/src/plugins/insights/model.spec.ts +3 -5
  267. package/src/plugins/insights/model.ts +2 -3
  268. package/src/plugins/insights/modelTypes.ts +197 -0
  269. package/src/plugins/insights/overview.spec.ts +64 -0
  270. package/src/plugins/insights/overview.ts +214 -0
  271. package/src/{insights → plugins/insights}/provider.spec.ts +21 -10
  272. package/src/{insights → plugins/insights}/provider.ts +58 -21
  273. package/src/{insights → plugins/insights}/queryInput.ts +2 -2
  274. package/src/plugins/insights/reads.spec.ts +340 -52
  275. package/src/plugins/insights/reads.ts +453 -123
  276. package/src/plugins/insights/recordEvent.spec.ts +474 -20
  277. package/src/plugins/insights/recordEvent.ts +301 -63
  278. package/src/plugins/insights/retention.spec.ts +115 -0
  279. package/src/{insights → plugins/insights}/routes.ts +1 -31
  280. package/src/plugins/insights/schema.ts +356 -176
  281. package/src/plugins/insights/testing/expectInsightsIndex.spec.ts +42 -0
  282. package/src/plugins/insights/testing/expectInsightsIndex.ts +20 -0
  283. package/src/plugins/insights/testing/fixtures.ts +30 -0
  284. package/src/plugins/insights/testing/index.ts +45 -0
  285. package/src/plugins/insights/testing/insightsModelTests.ts +860 -0
  286. package/src/plugins/insights/testing/setupInsightsHttpTestSuite.ts +152 -0
  287. package/src/plugins/insights/testing/setupInsightsModelTestSuite.ts +36 -0
  288. package/src/plugins/insights/testing/types.ts +30 -0
  289. package/src/{insights → plugins/insights}/types.ts +1 -1
  290. package/src/readBudgets.testFixtures.ts +2 -2
  291. package/src/releaseCatalogRoutes.integration.spec.ts +7 -0
  292. package/src/retention.spec.ts +73 -0
  293. package/src/runtime.spec.ts +11 -12
  294. package/src/runtime.testFixtures.ts +7 -2
  295. package/src/testUtilsMutation.integration.spec.ts +2 -1
  296. package/src/testUtilsPackage.integration.spec.ts +7 -3
  297. package/dist/db/builtInDatabase.cjs +0 -104
  298. package/dist/db/builtInDatabase.d.cts +0 -38
  299. package/dist/db/builtInDatabase.d.mts +0 -38
  300. package/dist/db/builtInDatabase.mjs +0 -97
  301. package/dist/plugins/builtIn.cjs +0 -7
  302. package/dist/plugins/builtIn.mjs +0 -6
  303. package/src/db/builtInDatabase.ts +0 -149
  304. package/src/plugins/builtIn.ts +0 -5
@@ -1,8 +1,7 @@
1
1
  import { DatabaseSync } from "node:sqlite";
2
2
 
3
- import type { BundleEventRow } from "@hot-updater/plugin-core";
4
3
  import {
5
- countInsightsDistinct,
4
+ countDistinct,
6
5
  createMemoryAdapter,
7
6
  type DatabaseAdapter,
8
7
  type WriteOp,
@@ -16,6 +15,7 @@ import { createKvAdapter } from "../../database/kv/kvAdapter";
16
15
  import { createMemoryKeyValueStore } from "../../database/kv/kvTestStore";
17
16
  import { createSqlAdapter } from "../../database/sql/sqlAdapter";
18
17
  import { sqliteExecutor } from "../../database/sql/sqlTestExecutors";
18
+ import type { BundleEventRow } from "./eventRow";
19
19
  import {
20
20
  insights,
21
21
  insightsIdentity,
@@ -28,6 +28,7 @@ const HOUR = 3_600_000;
28
28
  const DAY = 86_400_000;
29
29
  const T = Date.UTC(2026, 8, 23, 10, 15);
30
30
  const hour = (ms: number) => ms - (ms % HOUR);
31
+ const day = (ms: number) => ms - (ms % DAY);
31
32
 
32
33
  const uuid = (n: number) =>
33
34
  `00000000-0000-7000-8000-${String(n).padStart(12, "0")}`;
@@ -49,7 +50,6 @@ const event = (
49
50
  app_version: "1.0.0",
50
51
  channel: "production",
51
52
  metadata: {
52
- username: null,
53
53
  cohort: "1",
54
54
  update_strategy: "appVersion",
55
55
  fingerprint_hash: null,
@@ -59,6 +59,35 @@ const event = (
59
59
  ...overrides,
60
60
  }) as BundleEventRow;
61
61
 
62
+ const unchanged = (
63
+ n: number,
64
+ overrides: Partial<BundleEventRow> = {},
65
+ ): BundleEventRow =>
66
+ event(n, {
67
+ type: "UNCHANGED",
68
+ from_release_id: null,
69
+ from_bundle_id: null,
70
+ metadata: { ...event(n).metadata, update_strategy: null },
71
+ ...overrides,
72
+ } as Partial<BundleEventRow>);
73
+
74
+ /** An update failure from install-2, which runs bundle-2 and targets bundle-3. */
75
+ const failure = (
76
+ n: number,
77
+ failed: Record<string, unknown>,
78
+ overrides: Partial<BundleEventRow> = {},
79
+ ): BundleEventRow =>
80
+ event(n, {
81
+ type: "UPDATE_FAILED",
82
+ install_id: "install-2",
83
+ from_release_id: "release-2",
84
+ from_bundle_id: "bundle-2",
85
+ to_release_id: "release-3",
86
+ to_bundle_id: "bundle-3",
87
+ metadata: { ...event(n).metadata, failure: failed },
88
+ ...overrides,
89
+ } as Partial<BundleEventRow>);
90
+
62
91
  const backends: [string, () => DatabaseAdapter][] = [
63
92
  ["memory", () => createMemoryAdapter()],
64
93
  [
@@ -102,10 +131,26 @@ describe.each(backends)("insights recordEvent on %s", (_name, adapter) => {
102
131
  range: { gte: bucket, lte: bucket },
103
132
  limit: 100,
104
133
  });
105
- const overview = async (key: string, bucket: number) =>
106
- (await db.findAggregates("insights_overview", at(key, bucket))).rows[0];
107
- const sketch = async (key: string, bucket: number) =>
108
- (await db.findAggregates("insights_sketches", at(key, bucket))).rows[0];
134
+ /** Each period's rows live in an aggregate of their own, with its retention. */
135
+ const counters = {
136
+ hour: "insights_overview",
137
+ day: "insights_overview_daily",
138
+ lifetime: "insights_overview_lifetime",
139
+ } as const;
140
+ const sketches = {
141
+ hour: "insights_sketches",
142
+ day: "insights_sketches_daily",
143
+ } as const;
144
+ const overview = async (
145
+ key: string,
146
+ bucket: number,
147
+ period: keyof typeof counters = "hour",
148
+ ) => (await db.findAggregates(counters[period], at(key, bucket))).rows[0];
149
+ const sketch = async (
150
+ key: string,
151
+ bucket: number,
152
+ period: keyof typeof sketches = "hour",
153
+ ) => (await db.findAggregates(sketches[period], at(key, bucket))).rows[0];
109
154
  const distribution = async () =>
110
155
  (
111
156
  await db.findAggregates("insights_distribution", {
@@ -154,6 +199,30 @@ describe.each(backends)("insights recordEvent on %s", (_name, adapter) => {
154
199
  limit: 100,
155
200
  })
156
201
  ).rows;
202
+ /** Update failures and recovery exits by what failed, which the breakdown reads. */
203
+ const failures = async () =>
204
+ (
205
+ await db.findAggregates("insights_failures", {
206
+ index: "byScope",
207
+ where: { platform: "ios", channel: "production" },
208
+ limit: 100,
209
+ })
210
+ ).rows;
211
+ const failedUsers = async (
212
+ key: string,
213
+ bucket: number,
214
+ period: "hour" | "day" | "lifetime" = "hour",
215
+ ) =>
216
+ countDistinct(
217
+ (
218
+ await db.findAggregates(
219
+ period === "lifetime"
220
+ ? "insights_sketches_lifetime"
221
+ : sketches[period],
222
+ at(key, bucket),
223
+ )
224
+ ).rows[0]?.failed_users,
225
+ );
157
226
  return {
158
227
  ...harness,
159
228
  db,
@@ -163,6 +232,8 @@ describe.each(backends)("insights recordEvent on %s", (_name, adapter) => {
163
232
  byBundle,
164
233
  outcomes,
165
234
  everyEvent,
235
+ failures,
236
+ failedUsers,
166
237
  };
167
238
  };
168
239
 
@@ -188,14 +259,15 @@ describe.each(backends)("insights recordEvent on %s", (_name, adapter) => {
188
259
  });
189
260
  await expect(
190
261
  db.findOne("bundle_event_heads", { install_id: "install-1" }),
191
- ).resolves.toEqual({ ...event(1), current_release_id: "release-2" });
262
+ ).resolves.toEqual(event(1));
263
+ // Gauges count the head in the UTC day of its event.
192
264
  await expect(distribution()).resolves.toEqual([
193
265
  {
194
266
  channel: "production",
195
267
  platform: "ios",
196
268
  app_version: "1.0.0",
197
269
  release_id: "release-2",
198
- bucket_start_ms: hour(T),
270
+ bucket_start_ms: day(T),
199
271
  latest_installations: 1,
200
272
  },
201
273
  ]);
@@ -225,29 +297,51 @@ describe.each(backends)("insights recordEvent on %s", (_name, adapter) => {
225
297
  periodKind: "lifetime",
226
298
  }),
227
299
  0,
300
+ "lifetime",
228
301
  ),
229
302
  ).resolves.toMatchObject(counters);
303
+ const releaseHour = await sketch(
304
+ identity({ ...release, releaseId: "release-2", periodKind: "hour" }),
305
+ hour(T),
306
+ );
307
+ expect(countDistinct(releaseHour!.launch_users)).toBe(1);
230
308
  for (const [periodKind, bucket] of [
231
309
  ["hour", hour(T)],
232
310
  ["day", T - (T % DAY)],
233
311
  ] as const) {
234
312
  await expect(
235
- overview(identity({ periodKind }), bucket),
313
+ overview(identity({ periodKind }), bucket, periodKind),
236
314
  ).resolves.toMatchObject(counters);
237
- const launches = await sketch(identity({ periodKind }), bucket);
238
- expect(countInsightsDistinct(launches!.launch_users)).toBe(1);
239
- for (const platform of ["ios", "all"] as const) {
315
+ // A channel's active installations come from its usage rows.
316
+ await expect(
317
+ sketch(identity({ periodKind }), bucket, periodKind),
318
+ ).resolves.toBeUndefined();
319
+ for (const appVersionKind of ["specific", "all"] as const) {
240
320
  const usage = await sketch(
241
321
  identity({
242
322
  scopeKind: "usage",
243
- platform,
244
- appVersionKind: "specific",
245
- appVersion: "1.0.0",
323
+ appVersionKind,
324
+ appVersion: appVersionKind === "specific" ? "1.0.0" : "",
246
325
  periodKind,
247
326
  }),
248
327
  bucket,
328
+ periodKind,
249
329
  );
250
- expect(countInsightsDistinct(usage!.activity_users)).toBe(1);
330
+ expect(countDistinct(usage!.activity_users)).toBe(1);
331
+ // Every platform's usage merges the platforms' rows on read.
332
+ await expect(
333
+ sketch(
334
+ identity({
335
+ scopeKind: "usage",
336
+ platform: "all",
337
+ appVersionKind,
338
+ appVersion: appVersionKind === "specific" ? "1.0.0" : "",
339
+ periodKind,
340
+ }),
341
+ bucket,
342
+ periodKind,
343
+ ),
344
+ ).resolves.toBeUndefined();
251
345
  }
252
346
  }
253
347
  });
@@ -319,12 +413,12 @@ describe.each(backends)("insights recordEvent on %s", (_name, adapter) => {
319
413
  id: uuid(2),
320
414
  user_id: null,
321
415
  type: "UPDATE_DOWNLOADED",
322
- current_release_id: "release-2",
323
416
  });
417
+ // A download leaves the installation on release-2, in the same UTC day.
324
418
  await expect(distribution()).resolves.toMatchObject([
325
419
  {
326
420
  release_id: "release-2",
327
- bucket_start_ms: hour(T + 2 * HOUR),
421
+ bucket_start_ms: day(T),
328
422
  latest_installations: 1,
329
423
  },
330
424
  ]);
@@ -348,10 +442,369 @@ describe.each(backends)("insights recordEvent on %s", (_name, adapter) => {
348
442
  await expect(distribution()).resolves.toMatchObject([
349
443
  {
350
444
  release_id: "release-2",
351
- bucket_start_ms: hour(T + 2 * HOUR),
445
+ bucket_start_ms: day(T),
352
446
  latest_installations: 1,
353
447
  },
354
448
  ]);
449
+
450
+ // The next UTC day's event takes the head out of this day's rows.
451
+ await api.recordEvent(event(5, { received_at_ms: T + DAY }));
452
+ await expect(distribution()).resolves.toMatchObject([
453
+ {
454
+ release_id: "release-2",
455
+ bucket_start_ms: day(T + DAY),
456
+ latest_installations: 1,
457
+ },
458
+ ]);
459
+ });
460
+
461
+ it("records an UNCHANGED report as a launch that no event or outcome row keeps", async () => {
462
+ const { api, db, overview, outcomes, everyEvent, byBundle } = await setup();
463
+ await api.recordEvent(unchanged(1));
464
+
465
+ await expect(db.findOne("bundle_events", { id: uuid(1) })).resolves.toBe(
466
+ null,
467
+ );
468
+ await expect(
469
+ db.findOne("bundle_event_heads", { install_id: "install-1" }),
470
+ ).resolves.toEqual(unchanged(1));
471
+ await expect(outcomes("UNCHANGED", "to:bundle-2")).resolves.toEqual([]);
472
+ await expect(everyEvent()).resolves.toEqual([]);
473
+ await expect(
474
+ overview(identity({ periodKind: "day" }), day(T), "day"),
475
+ ).resolves.toMatchObject({ launches: 1 });
476
+ await expect(
477
+ byBundle("to_bundle_id", "bundle-2", "UNCHANGED"),
478
+ ).resolves.toMatchObject([{ bucket_start_ms: day(T), installations: 1 }]);
479
+ await expect(
480
+ api.listEvents({
481
+ filter: { kind: "all" },
482
+ sinceMs: day(T),
483
+ beforeReceivedAtMs: day(T) + DAY,
484
+ limit: 10,
485
+ }),
486
+ ).resolves.toEqual([]);
487
+ });
488
+
489
+ it("writes nothing for an UNCHANGED report that repeats its head the same UTC day", async () => {
490
+ const calls: string[] = [];
491
+ const { api, db, overview } = await setup((inner) => ({
492
+ ...inner,
493
+ write: async (ops: readonly WriteOp[]) => {
494
+ calls.push("write");
495
+ return inner.write(ops);
496
+ },
497
+ }));
498
+ await api.recordEvent(event(1));
499
+ calls.length = 0;
500
+ // Relaunches on the bundle the apply moved to, later that day.
501
+ await api.recordEvent(unchanged(2, { received_at_ms: T + 5 * HOUR }));
502
+ await api.recordEvent(unchanged(3, { received_at_ms: T + 6 * HOUR }));
503
+ expect(calls).toEqual([]);
504
+ await expect(
505
+ db.findOne("bundle_event_heads", { install_id: "install-1" }),
506
+ ).resolves.toMatchObject({ id: uuid(1), type: "UPDATE_APPLIED" });
507
+ await expect(
508
+ overview(identity({ periodKind: "day" }), day(T), "day"),
509
+ ).resolves.toMatchObject({ launches: 1 });
510
+
511
+ // An UNCHANGED report keeps no event row, so a retry that lands the next
512
+ // UTC day is known by the head's id.
513
+ await api.recordEvent(unchanged(4, { received_at_ms: T + DAY }));
514
+ calls.length = 0;
515
+ await api.recordEvent(unchanged(4, { received_at_ms: T + 2 * DAY }));
516
+ expect(calls).toEqual([]);
517
+ await expect(
518
+ overview(identity({ periodKind: "day" }), day(T + 2 * DAY), "day"),
519
+ ).resolves.toBeUndefined();
520
+ });
521
+
522
+ it("records an UNCHANGED report again for a new UTC day, bundle, user, or a download's head", async () => {
523
+ const { api, db, overview } = await setup();
524
+ const head = () =>
525
+ db.findOne("bundle_event_heads", { install_id: "install-1" });
526
+ const launches = async (bucket: number) =>
527
+ (await overview(identity({ periodKind: "day" }), bucket, "day"))
528
+ ?.launches;
529
+
530
+ await api.recordEvent(unchanged(1));
531
+ await api.recordEvent(unchanged(2, { user_id: "user-2" }));
532
+ await expect(head()).resolves.toMatchObject({ id: uuid(2) });
533
+ await api.recordEvent(
534
+ unchanged(3, { to_bundle_id: "bundle-3", to_release_id: "release-3" }),
535
+ );
536
+ await expect(head()).resolves.toMatchObject({ id: uuid(3) });
537
+ await expect(launches(day(T))).resolves.toBe(3);
538
+
539
+ // A download's head still runs the bundle it came from.
540
+ await api.recordEvent(
541
+ event(4, {
542
+ type: "UPDATE_DOWNLOADED",
543
+ from_release_id: "release-3",
544
+ from_bundle_id: "bundle-3",
545
+ to_release_id: "release-4",
546
+ to_bundle_id: "bundle-4",
547
+ received_at_ms: T + HOUR,
548
+ }),
549
+ );
550
+ await api.recordEvent(
551
+ unchanged(5, {
552
+ to_bundle_id: "bundle-4",
553
+ to_release_id: "release-4",
554
+ received_at_ms: T + 2 * HOUR,
555
+ }),
556
+ );
557
+ await expect(head()).resolves.toMatchObject({ id: uuid(5) });
558
+
559
+ await api.recordEvent(
560
+ unchanged(6, {
561
+ to_bundle_id: "bundle-4",
562
+ to_release_id: "release-4",
563
+ received_at_ms: T + DAY,
564
+ }),
565
+ );
566
+ await expect(head()).resolves.toMatchObject({ id: uuid(6) });
567
+ await expect(launches(day(T + DAY))).resolves.toBe(1);
568
+ });
569
+
570
+ it("records an update failure for its target release and channel, and moves no head", async () => {
571
+ const {
572
+ api,
573
+ db,
574
+ overview,
575
+ sketch,
576
+ outcomes,
577
+ everyEvent,
578
+ failures,
579
+ failedUsers,
580
+ } = await setup();
581
+ await api.recordEvent(event(1, { install_id: "install-2" }));
582
+ const failed = {
583
+ stage: "download",
584
+ reason: "http",
585
+ resource: "artifact",
586
+ http_status: 403,
587
+ origin_code: "AccessDenied",
588
+ };
589
+ await api.recordEvent(failure(2, failed));
590
+
591
+ await expect(
592
+ db.findOne("bundle_events", { id: uuid(2) }),
593
+ ).resolves.toMatchObject({
594
+ movement_install_id: "install-2",
595
+ bundle_ref: ["to:bundle-3"],
596
+ });
597
+ await expect(
598
+ db.findOne("bundle_event_heads", { install_id: "install-2" }),
599
+ ).resolves.toMatchObject({ id: uuid(1), type: "UPDATE_APPLIED" });
600
+ await expect(
601
+ outcomes("UPDATE_FAILED", "to:bundle-3"),
602
+ ).resolves.toMatchObject([{ bucket_start_ms: hour(T), events: 1 }]);
603
+ await expect(everyEvent()).resolves.toMatchObject([{ events: 2 }]);
604
+ await expect(
605
+ api.listEvents({
606
+ filter: { kind: "installationMovement", installId: "install-2" },
607
+ beforeReceivedAtMs: T + HOUR,
608
+ limit: 10,
609
+ }),
610
+ ).resolves.toMatchObject([{ id: uuid(2) }, { id: uuid(1) }]);
611
+
612
+ const release = {
613
+ scopeKind: "release",
614
+ releaseKind: "specific",
615
+ releaseId: "release-3",
616
+ } as const;
617
+ // Only the lifetime row counts the failure: windowed reads sum the
618
+ // breakdown's hourly rows.
619
+ await expect(
620
+ overview(identity({ ...release, periodKind: "lifetime" }), 0, "lifetime"),
621
+ ).resolves.toMatchObject({ failed_updates: 1, launches: 0 });
622
+ await expect(
623
+ overview(identity({ ...release, periodKind: "hour" }), hour(T)),
624
+ ).resolves.toBeUndefined();
625
+ for (const [periodKind, bucket] of [
626
+ ["hour", hour(T)],
627
+ ["lifetime", 0],
628
+ ] as const) {
629
+ await expect(
630
+ failedUsers(
631
+ identity({ ...release, scopeKind: "failure", periodKind }),
632
+ bucket,
633
+ periodKind,
634
+ ),
635
+ ).resolves.toBe(1);
636
+ }
637
+ for (const [periodKind, bucket] of [
638
+ ["hour", hour(T)],
639
+ ["day", day(T)],
640
+ ] as const) {
641
+ // A failure is no launch, and no counter: the channel's launch is the
642
+ // apply's.
643
+ await expect(
644
+ overview(identity({ periodKind }), bucket, periodKind),
645
+ ).resolves.toMatchObject({ failed_updates: 0, launches: 1 });
646
+ await expect(
647
+ failedUsers(
648
+ identity({ scopeKind: "failure", periodKind }),
649
+ bucket,
650
+ periodKind,
651
+ ),
652
+ ).resolves.toBe(1);
653
+ // Nor activity: install-2 counts once, from its apply.
654
+ const usage = await sketch(
655
+ identity({ scopeKind: "usage", periodKind }),
656
+ bucket,
657
+ periodKind,
658
+ );
659
+ expect(countDistinct(usage!.activity_users)).toBe(1);
660
+ }
661
+ await expect(failures()).resolves.toEqual([
662
+ {
663
+ platform: "ios",
664
+ channel: "production",
665
+ bucket_start_ms: hour(T),
666
+ release_id: "release-3",
667
+ stage: "download",
668
+ reason: "http",
669
+ detail: JSON.stringify(["artifact", 403, "AccessDenied", null]),
670
+ events: 1,
671
+ },
672
+ ]);
673
+ });
674
+
675
+ it("counts a failed check for its channel only, and in no bundle list", async () => {
676
+ const { api, db, overview, outcomes, everyEvent, failures, failedUsers } =
677
+ await setup();
678
+ await api.recordEvent(
679
+ failure(
680
+ 1,
681
+ { stage: "check", reason: "invalid_response", resource: "catalog" },
682
+ { to_release_id: null, to_bundle_id: "bundle-2" },
683
+ ),
684
+ );
685
+
686
+ await expect(
687
+ db.findOne("bundle_events", { id: uuid(1) }),
688
+ ).resolves.toMatchObject({
689
+ movement_install_id: "install-2",
690
+ bundle_ref: [],
691
+ });
692
+ await expect(
693
+ db.findOne("bundle_event_heads", { install_id: "install-2" }),
694
+ ).resolves.toBeNull();
695
+ await expect(outcomes("UPDATE_FAILED", "to:bundle-2")).resolves.toEqual([]);
696
+ await expect(everyEvent()).resolves.toMatchObject([{ events: 1 }]);
697
+ const scope = { platform: "ios", channel: "production" } as const;
698
+ await expect(
699
+ api.listEvents({
700
+ filter: {
701
+ kind: "bundle",
702
+ ...scope,
703
+ type: "UPDATE_FAILED",
704
+ toBundleId: "bundle-2",
705
+ },
706
+ sinceMs: day(T),
707
+ beforeReceivedAtMs: T + HOUR,
708
+ limit: 10,
709
+ }),
710
+ ).resolves.toEqual([]);
711
+ await expect(
712
+ api.listEvents({
713
+ filter: { kind: "all" },
714
+ sinceMs: day(T),
715
+ beforeReceivedAtMs: T + HOUR,
716
+ limit: 10,
717
+ }),
718
+ ).resolves.toMatchObject([{ id: uuid(1) }]);
719
+ for (const [periodKind, bucket] of [
720
+ ["hour", hour(T)],
721
+ ["day", day(T)],
722
+ ] as const) {
723
+ await expect(
724
+ overview(identity({ periodKind }), bucket, periodKind),
725
+ ).resolves.toBeUndefined();
726
+ await expect(
727
+ failedUsers(
728
+ identity({ scopeKind: "check", periodKind }),
729
+ bucket,
730
+ periodKind,
731
+ ),
732
+ ).resolves.toBe(1);
733
+ await expect(
734
+ failedUsers(
735
+ identity({ scopeKind: "failure", periodKind }),
736
+ bucket,
737
+ periodKind,
738
+ ),
739
+ ).resolves.toBe(0);
740
+ }
741
+ await expect(failures()).resolves.toMatchObject([
742
+ {
743
+ release_id: "",
744
+ stage: "check",
745
+ reason: "invalid_response",
746
+ detail: JSON.stringify(["catalog", null, null, null]),
747
+ events: 1,
748
+ },
749
+ ]);
750
+ });
751
+
752
+ it("counts a download a patch delivered, and one that fell back from a patch", async () => {
753
+ const { api, overview } = await setup();
754
+ const download = (n: number, metadata: Record<string, unknown>) =>
755
+ event(n, {
756
+ type: "UPDATE_DOWNLOADED",
757
+ install_id: `install-${n}`,
758
+ metadata: { ...event(n).metadata, ...metadata },
759
+ } as Partial<BundleEventRow>);
760
+ await api.recordEvent(download(1, { delivery: "patch" }));
761
+ await api.recordEvent(
762
+ download(2, { delivery: "archive", patch_fallback: true }),
763
+ );
764
+ await api.recordEvent(download(3, { delivery: "manifest" }));
765
+
766
+ const counted = { downloads: 3, patch_downloads: 1, patch_fallbacks: 1 };
767
+ const release = {
768
+ scopeKind: "release",
769
+ releaseKind: "specific",
770
+ releaseId: "release-2",
771
+ } as const;
772
+ await expect(
773
+ overview(identity({ ...release, periodKind: "hour" }), hour(T)),
774
+ ).resolves.toMatchObject(counted);
775
+ await expect(
776
+ overview(identity({ ...release, periodKind: "lifetime" }), 0, "lifetime"),
777
+ ).resolves.toMatchObject(counted);
778
+ await expect(
779
+ overview(identity({ periodKind: "day" }), day(T), "day"),
780
+ ).resolves.toMatchObject(counted);
781
+ });
782
+
783
+ it("keeps a recovery's exit reason in the breakdown of the release that failed to launch", async () => {
784
+ const { api, failures } = await setup();
785
+ await api.recordEvent(
786
+ event(1, {
787
+ type: "RECOVERED",
788
+ metadata: { ...event(1).metadata, previous_process_exit: "CRASH" },
789
+ } as Partial<BundleEventRow>),
790
+ );
791
+ // A recovery without one, as on iOS, has no row.
792
+ await api.recordEvent(
793
+ event(2, { type: "RECOVERED", install_id: "install-2" }),
794
+ );
795
+
796
+ await expect(failures()).resolves.toEqual([
797
+ {
798
+ platform: "ios",
799
+ channel: "production",
800
+ bucket_start_ms: hour(T),
801
+ release_id: "release-1",
802
+ stage: "launch",
803
+ reason: "CRASH",
804
+ detail: "",
805
+ events: 1,
806
+ },
807
+ ]);
355
808
  });
356
809
 
357
810
  it("reads the event and head in one round, gauge and sketch rows in a second, and writes once", async () => {
@@ -374,6 +827,7 @@ describe.each(backends)("insights recordEvent on %s", (_name, adapter) => {
374
827
  "get bundle_events",
375
828
  "get bundle_event_heads",
376
829
  "get insights_sketches",
830
+ "get insights_sketches_daily",
377
831
  "get insights_distribution",
378
832
  "get insights_latest_by_bundle",
379
833
  "write",