@hot-updater/server 1.0.0-rc.16 → 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 (309) 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 -3
  86. package/dist/db/index.d.cts +7 -3
  87. package/dist/db/index.d.mts +7 -3
  88. package/dist/db/index.mjs +6 -3
  89. package/dist/db/types.d.cts +3 -3
  90. package/dist/db/types.d.mts +3 -3
  91. package/dist/diff.cjs +3 -0
  92. package/dist/diff.d.cts +2 -0
  93. package/dist/diff.d.mts +2 -0
  94. package/dist/diff.mjs +2 -0
  95. package/dist/handler.cjs +2 -14
  96. package/dist/handler.mjs +1 -13
  97. package/dist/handlerReleaseCatalogRoutes.cjs +21 -2
  98. package/dist/handlerReleaseCatalogRoutes.mjs +21 -2
  99. package/dist/index.cjs +2 -6
  100. package/dist/index.d.cts +4 -7
  101. package/dist/index.d.mts +4 -7
  102. package/dist/index.mjs +2 -4
  103. package/dist/package.cjs +1 -1
  104. package/dist/package.mjs +1 -1
  105. package/dist/{apiKeys.cjs → plugins/api-keys/apiKeys.cjs} +1 -1
  106. package/dist/{apiKeys.d.cts → plugins/api-keys/apiKeys.d.cts} +2 -2
  107. package/dist/{apiKeys.d.mts → plugins/api-keys/apiKeys.d.mts} +2 -2
  108. package/dist/{apiKeys.mjs → plugins/api-keys/apiKeys.mjs} +1 -1
  109. package/dist/plugins/api-keys/cli.cjs +115 -0
  110. package/dist/plugins/api-keys/cli.mjs +114 -0
  111. package/dist/plugins/api-keys/index.cjs +9 -6
  112. package/dist/plugins/api-keys/index.d.cts +4 -5
  113. package/dist/plugins/api-keys/index.d.mts +4 -5
  114. package/dist/plugins/api-keys/index.mjs +9 -7
  115. package/dist/plugins/api-keys/model.d.cts +20 -2
  116. package/dist/plugins/api-keys/model.d.mts +20 -2
  117. package/dist/plugins/definePlugin.cjs +1 -1
  118. package/dist/plugins/definePlugin.d.cts +130 -3
  119. package/dist/plugins/definePlugin.d.mts +130 -3
  120. package/dist/plugins/definePlugin.mjs +1 -1
  121. package/dist/plugins/index.cjs +25 -4
  122. package/dist/plugins/index.d.cts +7 -4
  123. package/dist/plugins/index.d.mts +7 -4
  124. package/dist/plugins/index.mjs +6 -4
  125. package/dist/plugins/insights/contract.cjs +193 -0
  126. package/dist/plugins/insights/contract.mjs +189 -0
  127. package/dist/{insights → plugins/insights}/domain.d.cts +60 -15
  128. package/dist/{insights → plugins/insights}/domain.d.mts +60 -15
  129. package/dist/plugins/insights/errors.cjs +15 -0
  130. package/dist/plugins/insights/errors.mjs +14 -0
  131. package/dist/{insights → plugins/insights}/eventInput.cjs +155 -22
  132. package/dist/{insights → plugins/insights}/eventInput.mjs +155 -22
  133. package/dist/plugins/insights/eventRow.cjs +49 -0
  134. package/dist/plugins/insights/eventRow.d.cts +64 -0
  135. package/dist/plugins/insights/eventRow.d.mts +64 -0
  136. package/dist/plugins/insights/eventRow.mjs +47 -0
  137. package/dist/plugins/insights/index.cjs +106 -26
  138. package/dist/plugins/insights/index.d.cts +130 -27
  139. package/dist/plugins/insights/index.d.mts +130 -27
  140. package/dist/plugins/insights/index.mjs +106 -29
  141. package/dist/plugins/insights/model.cjs +2 -2
  142. package/dist/plugins/insights/model.d.cts +1 -1
  143. package/dist/plugins/insights/model.d.mts +1 -1
  144. package/dist/plugins/insights/model.mjs +1 -1
  145. package/dist/plugins/insights/modelTypes.d.cts +176 -0
  146. package/dist/plugins/insights/modelTypes.d.mts +176 -0
  147. package/dist/plugins/insights/overview.cjs +109 -0
  148. package/dist/plugins/insights/overview.d.cts +15 -0
  149. package/dist/plugins/insights/overview.d.mts +15 -0
  150. package/dist/plugins/insights/overview.mjs +107 -0
  151. package/dist/{insights → plugins/insights}/provider.cjs +33 -14
  152. package/dist/{insights → plugins/insights}/provider.d.cts +2 -2
  153. package/dist/{insights → plugins/insights}/provider.d.mts +2 -2
  154. package/dist/{insights → plugins/insights}/provider.mjs +30 -11
  155. package/dist/{insights → plugins/insights}/queryInput.cjs +2 -2
  156. package/dist/{insights → plugins/insights}/queryInput.mjs +2 -2
  157. package/dist/plugins/insights/reads.cjs +298 -88
  158. package/dist/plugins/insights/reads.d.cts +61 -0
  159. package/dist/plugins/insights/reads.d.mts +61 -0
  160. package/dist/plugins/insights/reads.mjs +298 -90
  161. package/dist/plugins/insights/recordEvent.cjs +223 -39
  162. package/dist/plugins/insights/recordEvent.d.cts +9 -4
  163. package/dist/plugins/insights/recordEvent.d.mts +9 -4
  164. package/dist/plugins/insights/recordEvent.mjs +221 -37
  165. package/dist/{insights → plugins/insights}/routes.cjs +3 -30
  166. package/dist/{insights → plugins/insights}/routes.mjs +4 -28
  167. package/dist/plugins/insights/schema.cjs +182 -40
  168. package/dist/plugins/insights/schema.d.cts +446 -6
  169. package/dist/plugins/insights/schema.d.mts +446 -6
  170. package/dist/plugins/insights/schema.mjs +180 -41
  171. package/dist/plugins/insights/testing/eventRow.d.mts +64 -0
  172. package/dist/plugins/insights/testing/expectInsightsIndex.mjs +18 -0
  173. package/dist/plugins/insights/testing/fixtures.d.mts +8 -0
  174. package/dist/plugins/insights/testing/fixtures.mjs +25 -0
  175. package/dist/plugins/insights/testing/index.d.mts +24 -0
  176. package/dist/plugins/insights/testing/index.mjs +25 -0
  177. package/dist/plugins/insights/testing/insightsModelTests.mjs +714 -0
  178. package/dist/plugins/insights/testing/modelTypes.d.mts +176 -0
  179. package/dist/plugins/insights/testing/setupInsightsHttpTestSuite.d.mts +11 -0
  180. package/dist/plugins/insights/testing/setupInsightsHttpTestSuite.mjs +107 -0
  181. package/dist/plugins/insights/testing/setupInsightsModelTestSuite.d.mts +11 -0
  182. package/dist/plugins/insights/testing/setupInsightsModelTestSuite.mjs +31 -0
  183. package/dist/plugins/insights/testing/types.d.mts +26 -0
  184. package/dist/{insights → plugins/insights}/types.d.cts +3 -2
  185. package/dist/{insights → plugins/insights}/types.d.mts +3 -2
  186. package/package.json +18 -7
  187. package/src/adapters/drizzle.conformance.integration.spec.ts +2 -2
  188. package/src/adapters/drizzle.readBudgets.integration.spec.ts +6 -4
  189. package/src/adapters/drizzle.spec.ts +15 -7
  190. package/src/adapters/drizzle.ts +6 -6
  191. package/src/adapters/kysely.conformance.integration.spec.ts +2 -2
  192. package/src/adapters/kysely.readBudgets.integration.spec.ts +4 -2
  193. package/src/adapters/kysely.spec.ts +13 -9
  194. package/src/adapters/kysely.ts +2 -2
  195. package/src/adapters/mongodb.integration.spec.ts +98 -7
  196. package/src/adapters/mongodb.ts +2 -2
  197. package/src/adapters/mongodbAdapter.ts +14 -0
  198. package/src/adapters/prisma.conformance.integration.spec.ts +2 -2
  199. package/src/adapters/prisma.integration.spec.ts +16 -8
  200. package/src/adapters/prisma.readBudgets.integration.spec.ts +5 -2
  201. package/src/adapters/prisma.spec.ts +17 -9
  202. package/src/adapters/prisma.ts +6 -6
  203. package/src/adapters/prismaTestClients.ts +8 -1
  204. package/src/assembly/assemblePlugins.ts +73 -40
  205. package/src/assembly/assembly.spec.ts +63 -11
  206. package/src/assembly/assembly.types.spec.ts +50 -2
  207. package/src/assembly/clientPlugins.ts +113 -0
  208. package/src/assembly/configError.ts +4 -0
  209. package/src/assembly/databasePlugins.ts +11 -5
  210. package/src/assembly/pluginCli.spec.ts +254 -0
  211. package/src/assembly/pluginCli.ts +248 -0
  212. package/src/assembly/{builtInRoutes.spec.ts → pluginRoutes.spec.ts} +10 -37
  213. package/src/createHotUpdaterCore.ts +21 -17
  214. package/src/database/aggregateBatching.spec.ts +577 -0
  215. package/src/database/aggregateBatching.ts +803 -0
  216. package/src/{insights/errors.spec.ts → database/busy.spec.ts} +2 -5
  217. package/src/{insights/errors.ts → database/busy.ts} +1 -13
  218. package/src/database/database.ts +39 -8
  219. package/src/database/definitions.ts +4 -0
  220. package/src/database/engineAggregates.spec.ts +4 -7
  221. package/src/database/engineAggregates.ts +5 -9
  222. package/src/database/engineReads.ts +7 -5
  223. package/src/database/engineTransaction.ts +1 -7
  224. package/src/database/fence.spec.ts +5 -5
  225. package/src/database/fence.ts +4 -0
  226. package/src/database/index.ts +13 -7
  227. package/src/database/kv/kvAdapter.batching.integration.spec.ts +15 -0
  228. package/src/database/kv/kvAdapter.conformance.integration.spec.ts +2 -0
  229. package/src/database/kv/kvAdapter.spec.ts +128 -1
  230. package/src/database/kv/kvAdapter.ts +30 -1
  231. package/src/database/kv/kvTestStore.ts +28 -1
  232. package/src/database/resolveSchema.ts +4 -1
  233. package/src/database/retention.spec.ts +314 -0
  234. package/src/database/retention.ts +129 -0
  235. package/src/database/schema.ts +44 -0
  236. package/src/database/sql/sqlAdapter.integration.spec.ts +4 -7
  237. package/src/database/sql/sqlAdapter.readBudgets.integration.spec.ts +18 -4
  238. package/src/database/sql/sqlAdapter.spec.ts +91 -3
  239. package/src/database/sql/sqlAdapter.ts +20 -0
  240. package/src/database/sql/sqlSchema.ts +81 -0
  241. package/src/databaseTestSuite.spec.ts +9 -4
  242. package/src/db/coreDatabase.ts +153 -0
  243. package/src/db/engineSql.spec.ts +8 -7
  244. package/src/db/index.ts +19 -2
  245. package/src/db/pluginTables.spec.ts +57 -27
  246. package/src/db/types.ts +3 -3
  247. package/src/diff.ts +6 -0
  248. package/src/handler-standalone.integration.spec.ts +2 -3
  249. package/src/handler.ts +1 -32
  250. package/src/handlerReleaseCatalogRoutes.ts +26 -2
  251. package/src/index.ts +0 -10
  252. package/src/{apiKeys.integration.spec.ts → plugins/api-keys/apiKeys.integration.spec.ts} +4 -4
  253. package/src/{apiKeys.spec.ts → plugins/api-keys/apiKeys.spec.ts} +3 -3
  254. package/src/{apiKeys.ts → plugins/api-keys/apiKeys.ts} +1 -1
  255. package/src/plugins/api-keys/apiKeysPlugin.spec.ts +11 -15
  256. package/src/plugins/api-keys/cli.spec.ts +187 -0
  257. package/src/plugins/api-keys/cli.ts +140 -0
  258. package/src/plugins/api-keys/index.ts +48 -41
  259. package/src/plugins/api-keys/model.ts +21 -2
  260. package/src/plugins/definePlugin.ts +144 -2
  261. package/src/plugins/index.ts +20 -2
  262. package/src/plugins/insights/contract.spec.ts +285 -0
  263. package/src/plugins/insights/contract.ts +372 -0
  264. package/src/{insights → plugins/insights}/domain.ts +73 -15
  265. package/src/plugins/insights/errors.ts +11 -0
  266. package/src/{insights → plugins/insights}/eventInput.ts +189 -29
  267. package/src/plugins/insights/eventRow.ts +209 -0
  268. package/src/plugins/insights/index.ts +181 -45
  269. package/src/{insights → plugins/insights}/insights.integration.spec.ts +330 -34
  270. package/src/plugins/insights/insightsRollout.integration.spec.ts +1 -2
  271. package/src/plugins/insights/model.spec.ts +3 -5
  272. package/src/plugins/insights/model.ts +2 -3
  273. package/src/plugins/insights/modelTypes.ts +197 -0
  274. package/src/plugins/insights/overview.spec.ts +64 -0
  275. package/src/plugins/insights/overview.ts +214 -0
  276. package/src/{insights → plugins/insights}/provider.spec.ts +21 -10
  277. package/src/{insights → plugins/insights}/provider.ts +58 -21
  278. package/src/{insights → plugins/insights}/queryInput.ts +2 -2
  279. package/src/plugins/insights/reads.spec.ts +340 -52
  280. package/src/plugins/insights/reads.ts +453 -123
  281. package/src/plugins/insights/recordEvent.spec.ts +474 -20
  282. package/src/plugins/insights/recordEvent.ts +301 -63
  283. package/src/plugins/insights/retention.spec.ts +115 -0
  284. package/src/{insights → plugins/insights}/routes.ts +1 -31
  285. package/src/plugins/insights/schema.ts +356 -176
  286. package/src/plugins/insights/testing/expectInsightsIndex.spec.ts +42 -0
  287. package/src/plugins/insights/testing/expectInsightsIndex.ts +20 -0
  288. package/src/plugins/insights/testing/fixtures.ts +30 -0
  289. package/src/plugins/insights/testing/index.ts +45 -0
  290. package/src/plugins/insights/testing/insightsModelTests.ts +860 -0
  291. package/src/plugins/insights/testing/setupInsightsHttpTestSuite.ts +152 -0
  292. package/src/plugins/insights/testing/setupInsightsModelTestSuite.ts +36 -0
  293. package/src/plugins/insights/testing/types.ts +30 -0
  294. package/src/{insights → plugins/insights}/types.ts +1 -1
  295. package/src/readBudgets.testFixtures.ts +2 -2
  296. package/src/releaseCatalogRoutes.integration.spec.ts +7 -0
  297. package/src/retention.spec.ts +73 -0
  298. package/src/runtime.spec.ts +11 -12
  299. package/src/runtime.testFixtures.ts +7 -2
  300. package/src/testUtilsMutation.integration.spec.ts +2 -1
  301. package/src/testUtilsPackage.integration.spec.ts +7 -3
  302. package/dist/db/builtInDatabase.cjs +0 -104
  303. package/dist/db/builtInDatabase.d.cts +0 -38
  304. package/dist/db/builtInDatabase.d.mts +0 -38
  305. package/dist/db/builtInDatabase.mjs +0 -97
  306. package/dist/plugins/builtIn.cjs +0 -7
  307. package/dist/plugins/builtIn.mjs +0 -6
  308. package/src/db/builtInDatabase.ts +0 -149
  309. package/src/plugins/builtIn.ts +0 -5
@@ -1,22 +1,26 @@
1
- import type { BundleEventRow } from "@hot-updater/plugin-core";
2
- import {
3
- addInsightsDistinct,
4
- assertBundleEventRow,
5
- compareUtf8,
6
- currentInsightsReleaseId,
7
- insightsKey,
8
- insightsOverviewDeltas,
9
- insightsOverviewId,
10
- type InsightsOverviewIdentity,
11
- } from "@hot-updater/plugin-core/internal";
1
+ import { addDistinct, compareUtf8 } from "@hot-updater/plugin-core/internal";
12
2
 
13
3
  import type {
14
4
  HotUpdaterDatabase,
15
5
  HotUpdaterTransaction,
16
6
  } from "../../database/database";
17
- import { DAILY_EVENTS, DAY_MS, HOUR_MS, type InsightsSchema } from "./schema";
7
+ import { assertBundleEventRow } from "./contract";
8
+ import type { BundleEventFailure, BundleEventRow } from "./eventRow";
9
+ import {
10
+ insightsKey,
11
+ insightsOverviewDeltas,
12
+ insightsOverviewId,
13
+ type InsightsOverviewIdentity,
14
+ } from "./overview";
15
+ import {
16
+ DAILY_EVENTS,
17
+ DAY_MS,
18
+ HOUR_MS,
19
+ isFailedCheck,
20
+ type InsightsSchema,
21
+ } from "./schema";
18
22
 
19
- /** The head columns `countHead` reads. */
23
+ /** The head columns `countHead` and `repeatsHead` read. */
20
24
  interface Head {
21
25
  readonly install_id: string;
22
26
  readonly id: string;
@@ -24,34 +28,54 @@ interface Head {
24
28
  readonly platform: string;
25
29
  readonly channel: string;
26
30
  readonly type: string;
31
+ readonly user_id: string | null;
32
+ readonly from_release_id: string | null;
27
33
  readonly from_bundle_id: string | null;
34
+ readonly to_release_id: string | null;
28
35
  readonly to_bundle_id: string;
29
- readonly current_release_id: string | null;
30
36
  readonly app_version: string;
31
37
  }
32
38
 
33
39
  const hourOf = (ms: number) => ms - (ms % HOUR_MS);
34
40
  const dayOf = (ms: number) => ms - (ms % DAY_MS);
35
41
 
36
- /** An overview or sketch row's scope and period; day periods roll up channel and usage rows. */
42
+ /**
43
+ * An overview or sketch row's scope and period; day periods roll up channel
44
+ * and usage rows. The `failure` and `check` scopes key the sketches of
45
+ * installations whose update, or whose update check, failed.
46
+ */
37
47
  export type InsightsIdentityParts = Omit<
38
48
  InsightsOverviewIdentity,
39
- "bucketStartMs" | "periodKind"
40
- > & { readonly periodKind: InsightsOverviewIdentity["periodKind"] | "day" };
49
+ "bucketStartMs" | "periodKind" | "scopeKind"
50
+ > & {
51
+ readonly scopeKind:
52
+ | InsightsOverviewIdentity["scopeKind"]
53
+ | "failure"
54
+ | "check";
55
+ readonly periodKind: InsightsOverviewIdentity["periodKind"] | "day";
56
+ };
41
57
 
42
58
  /** The `identity` of an overview or sketch row: its scope and period, hashed. */
43
59
  export const insightsIdentity = (identity: InsightsIdentityParts): string =>
44
60
  insightsOverviewId({
45
61
  ...identity,
62
+ scopeKind: identity.scopeKind as InsightsOverviewIdentity["scopeKind"],
46
63
  periodKind: identity.periodKind as InsightsOverviewIdentity["periodKind"],
47
64
  bucketStartMs: 0,
48
65
  });
49
66
 
50
- /** The head columns besides its key: the whole event and its current release. */
51
- const headFields = (event: BundleEventRow) => {
52
- const { install_id: _, ...fields } = event;
53
- return { ...fields, current_release_id: currentInsightsReleaseId(event) };
54
- };
67
+ /**
68
+ * The bucket a head's gauges count it in, the UTC day of its event, and the
69
+ * release its distribution row names: the one it runs, which a download
70
+ * leaves at its source.
71
+ */
72
+ const gaugeSlot = (head: Head) => ({
73
+ bucket: dayOf(head.received_at_ms),
74
+ releaseId:
75
+ head.type === "UPDATE_DOWNLOADED"
76
+ ? head.from_release_id
77
+ : head.to_release_id,
78
+ });
55
79
 
56
80
  /**
57
81
  * The `insights_latest_by_bundle` field of a head's (from, to) pair. A head
@@ -64,13 +88,16 @@ export const PAIR_FIELD = "from_to";
64
88
  export const bundlePairKey = (from: string, to: string): string =>
65
89
  insightsKey(`${from.length}:${from}${to.length}:${to}`);
66
90
 
67
- /** Moves a head's gauges: the distribution row, one row per bundle it references, and its pair. */
91
+ /**
92
+ * Moves a head's gauges: the distribution row, one row per bundle it
93
+ * references, and its pair; -1 takes back the head an event replaces.
94
+ */
68
95
  const countHead = (
69
96
  tx: HotUpdaterTransaction<InsightsSchema>,
70
97
  head: Head,
71
98
  delta: 1 | -1,
72
99
  ) => {
73
- const bucket = hourOf(head.received_at_ms);
100
+ const { bucket, releaseId } = gaugeSlot(head);
74
101
  const shardBy = head.install_id;
75
102
  tx.aggregate(
76
103
  "insights_distribution",
@@ -78,7 +105,7 @@ const countHead = (
78
105
  channel: head.channel,
79
106
  platform: head.platform,
80
107
  app_version: head.app_version,
81
- release_id: head.current_release_id ?? "",
108
+ release_id: releaseId ?? "",
82
109
  bucket_start_ms: bucket,
83
110
  },
84
111
  { latest_installations: delta },
@@ -118,10 +145,37 @@ const countHead = (
118
145
  }
119
146
  };
120
147
 
148
+ /**
149
+ * Where each period's rows go, each with its retention: hours for 90 days,
150
+ * days for 13 months, and a release's lifetime counters kept.
151
+ */
152
+ const PERIOD_MODELS = {
153
+ hour: { counters: "insights_overview", sketches: "insights_sketches" },
154
+ day: {
155
+ counters: "insights_overview_daily",
156
+ sketches: "insights_sketches_daily",
157
+ },
158
+ lifetime: { counters: "insights_overview_lifetime", sketches: undefined },
159
+ } as const;
160
+
161
+ /** A download's patch counters: a patch delivered it, or it fell back from one. */
162
+ const patchCounters = (event: BundleEventRow) =>
163
+ event.type !== "UPDATE_DOWNLOADED"
164
+ ? {}
165
+ : {
166
+ ...(event.metadata.delivery === "patch" ? { patch_downloads: 1 } : {}),
167
+ ...(event.metadata.patch_fallback === true
168
+ ? { patch_fallbacks: 1 }
169
+ : {}),
170
+ };
171
+
121
172
  /**
122
173
  * Counters and sketches for one event: release, channel, and usage rows, with
123
- * day rollups for channel and usage; and its outcome rows, its bundle
124
- * filter's hour and every event's day.
174
+ * day rollups for channel and usage. Usage rows are written for the event's
175
+ * platform only, since a read for every platform merges the ios and android
176
+ * sketches; a channel's active installations come from its usage rows, so
177
+ * channel rows keep no sketch of their own. A download's patch counters go
178
+ * in the rows that count it.
125
179
  */
126
180
  const countEvent = (
127
181
  tx: HotUpdaterTransaction<InsightsSchema>,
@@ -129,6 +183,7 @@ const countEvent = (
129
183
  ) => {
130
184
  const shardBy = event.install_id;
131
185
  for (const delta of insightsOverviewDeltas(event)) {
186
+ if (delta.identity.platform === "all") continue;
132
187
  const { bucketStartMs, ...parts } = delta.identity;
133
188
  const periods =
134
189
  parts.scopeKind === "channel" || parts.scopeKind === "usage"
@@ -138,6 +193,7 @@ const countEvent = (
138
193
  ] as const)
139
194
  : ([[parts.periodKind, bucketStartMs]] as const);
140
195
  for (const [periodKind, bucket] of periods) {
196
+ const models = PERIOD_MODELS[periodKind as keyof typeof PERIOD_MODELS];
141
197
  const key = {
142
198
  identity: insightsIdentity({ ...parts, periodKind }),
143
199
  bucket_start_ms: bucket,
@@ -146,54 +202,198 @@ const countEvent = (
146
202
  downloads?: number;
147
203
  launches?: number;
148
204
  failed_launches?: number;
149
- } = Object.fromEntries(
150
- [
151
- ["downloads", delta.downloads],
152
- ["launches", delta.launches],
153
- ["failed_launches", delta.failedLaunches],
154
- ].filter(([, value]) => value !== 0),
155
- );
205
+ patch_downloads?: number;
206
+ patch_fallbacks?: number;
207
+ } = {
208
+ ...Object.fromEntries(
209
+ [
210
+ ["downloads", delta.downloads],
211
+ ["launches", delta.launches],
212
+ ["failed_launches", delta.failedLaunches],
213
+ ].filter(([, value]) => value !== 0),
214
+ ),
215
+ ...(delta.downloads === 0 ? {} : patchCounters(event)),
216
+ };
156
217
  if (Object.keys(counters).length > 0) {
157
- tx.aggregate("insights_overview", key, counters, { shardBy });
218
+ tx.aggregate(models.counters, key, counters, { shardBy });
158
219
  }
159
220
  const sketches = {
160
- ...(delta.launchIdentity === undefined
221
+ ...(delta.launchIdentity === undefined || parts.scopeKind === "channel"
161
222
  ? {}
162
- : { launch_users: addInsightsDistinct(null, delta.launchIdentity) }),
223
+ : { launch_users: addDistinct(null, delta.launchIdentity) }),
163
224
  ...(delta.activityIdentity === undefined
164
225
  ? {}
165
226
  : {
166
- activity_users: addInsightsDistinct(null, delta.activityIdentity),
227
+ activity_users: addDistinct(null, delta.activityIdentity),
167
228
  }),
168
229
  };
169
- if (Object.keys(sketches).length > 0) {
170
- tx.aggregate("insights_sketches", key, sketches, { shardBy });
230
+ if (models.sketches !== undefined && Object.keys(sketches).length > 0) {
231
+ tx.aggregate(models.sketches, key, sketches, { shardBy });
171
232
  }
172
233
  }
173
234
  }
235
+ };
236
+
237
+ /**
238
+ * A stored event's outcome rows: its bundle filter's hour, which a failed
239
+ * check has none of, and every stored event's UTC day.
240
+ */
241
+ const countOutcome = (
242
+ tx: HotUpdaterTransaction<InsightsSchema>,
243
+ event: BundleEventRow,
244
+ ) => {
245
+ const shardBy = event.install_id;
246
+ if (!isFailedCheck(event)) {
247
+ tx.aggregate(
248
+ "insights_outcomes",
249
+ {
250
+ platform: event.platform,
251
+ channel: event.channel,
252
+ type: event.type,
253
+ bundle_ref:
254
+ event.type === "RECOVERED"
255
+ ? `from:${event.from_bundle_id}`
256
+ : `to:${event.to_bundle_id}`,
257
+ bucket_start_ms: hourOf(event.received_at_ms),
258
+ },
259
+ { events: 1 },
260
+ { shardBy },
261
+ );
262
+ }
263
+ // The global event list reads only the days this row counts: a gap costs
264
+ // it one empty day and one read of this row, not a read a day. One more
265
+ // blind increment per event, on the event's shard like the others.
174
266
  tx.aggregate(
175
267
  "insights_outcomes",
268
+ { ...DAILY_EVENTS, bucket_start_ms: dayOf(event.received_at_ms) },
269
+ { events: 1 },
270
+ { shardBy },
271
+ );
272
+ };
273
+
274
+ /** The row check requires an UPDATE_FAILED row's failure; its type leaves it optional. */
275
+ const UNKNOWN_FAILURE: BundleEventFailure = {
276
+ stage: "unknown",
277
+ reason: "unknown",
278
+ };
279
+
280
+ /** A failure's `detail`: what else the client knew, as a JSON array. */
281
+ const failureDetail = (failure: BundleEventFailure): string =>
282
+ JSON.stringify([
283
+ failure.resource ?? null,
284
+ failure.http_status ?? null,
285
+ failure.origin_code ?? null,
286
+ failure.transport ?? null,
287
+ ]);
288
+
289
+ /**
290
+ * An update failure's sketches, breakdown row, and lifetime counter. Its
291
+ * hourly breakdown row is what windowed reads sum, so no hourly or daily
292
+ * counter repeats it. A failed check counts for its channel, in the `check`
293
+ * sketch of installations. Any other failure counts in the channel's
294
+ * `failure` sketch, and for its target release, when it names one, in the
295
+ * release's hourly and lifetime `failure` sketches and its lifetime
296
+ * `failed_updates`, which the bundle detail reads. A failure is no launch
297
+ * and no activity: the installation reports its launch that day.
298
+ */
299
+ const countFailure = (
300
+ tx: HotUpdaterTransaction<InsightsSchema>,
301
+ event: BundleEventRow & { readonly type: "UPDATE_FAILED" },
302
+ ) => {
303
+ const shardBy = event.install_id;
304
+ const failure = event.metadata.failure ?? UNKNOWN_FAILURE;
305
+ const check = failure.stage === "check";
306
+ const hour = hourOf(event.received_at_ms);
307
+ const scope = {
308
+ channel: event.channel,
309
+ platform: event.platform,
310
+ appVersionKind: "all",
311
+ appVersion: "",
312
+ } as const;
313
+ const channel = { ...scope, releaseKind: "all", releaseId: "" } as const;
314
+ const users = { failed_users: addDistinct(null, event.install_id) };
315
+ for (const [periodKind, bucket] of [
316
+ ["hour", hour],
317
+ ["day", dayOf(event.received_at_ms)],
318
+ ] as const) {
319
+ const identity = insightsIdentity({
320
+ ...channel,
321
+ scopeKind: check ? "check" : "failure",
322
+ periodKind,
323
+ });
324
+ tx.aggregate(
325
+ PERIOD_MODELS[periodKind].sketches,
326
+ { identity, bucket_start_ms: bucket },
327
+ users,
328
+ { shardBy },
329
+ );
330
+ }
331
+ const releaseId = check ? null : event.to_release_id;
332
+ if (releaseId !== null) {
333
+ const release = { ...scope, releaseKind: "specific", releaseId } as const;
334
+ for (const [periodKind, bucket, sketches] of [
335
+ ["hour", hour, "insights_sketches"],
336
+ ["lifetime", 0, "insights_sketches_lifetime"],
337
+ ] as const) {
338
+ const key = (scopeKind: InsightsIdentityParts["scopeKind"]) => ({
339
+ identity: insightsIdentity({ ...release, scopeKind, periodKind }),
340
+ bucket_start_ms: bucket,
341
+ });
342
+ tx.aggregate(sketches, key("failure"), users, { shardBy });
343
+ }
344
+ tx.aggregate(
345
+ "insights_overview_lifetime",
346
+ {
347
+ identity: insightsIdentity({
348
+ ...release,
349
+ scopeKind: "release",
350
+ periodKind: "lifetime",
351
+ }),
352
+ bucket_start_ms: 0,
353
+ },
354
+ { failed_updates: 1 },
355
+ { shardBy },
356
+ );
357
+ }
358
+ tx.aggregate(
359
+ "insights_failures",
176
360
  {
177
361
  platform: event.platform,
178
362
  channel: event.channel,
179
- type: event.type,
180
- bundle_ref:
181
- event.type === "RECOVERED"
182
- ? `from:${event.from_bundle_id}`
183
- : `to:${event.to_bundle_id}`,
184
- bucket_start_ms: hourOf(event.received_at_ms),
363
+ bucket_start_ms: hour,
364
+ release_id: releaseId ?? "",
365
+ stage: failure.stage,
366
+ reason: failure.reason,
367
+ detail: failureDetail(failure),
185
368
  },
186
369
  { events: 1 },
187
370
  { shardBy },
188
371
  );
189
- // The global event list reads only the days this row counts: a gap costs
190
- // it one empty day and one read of this row, not a read a day. One more
191
- // blind increment per event, on the event's shard like the others.
372
+ };
373
+
374
+ /**
375
+ * A recovery's exit reason, which Android 11+ reports, in the breakdown of
376
+ * the release whose launch failed: stage `launch`, the reason as its reason.
377
+ */
378
+ const countExit = (
379
+ tx: HotUpdaterTransaction<InsightsSchema>,
380
+ event: BundleEventRow,
381
+ ) => {
382
+ const exit = event.metadata.previous_process_exit;
383
+ if (event.type !== "RECOVERED" || exit === undefined) return;
192
384
  tx.aggregate(
193
- "insights_outcomes",
194
- { ...DAILY_EVENTS, bucket_start_ms: dayOf(event.received_at_ms) },
385
+ "insights_failures",
386
+ {
387
+ platform: event.platform,
388
+ channel: event.channel,
389
+ bucket_start_ms: hourOf(event.received_at_ms),
390
+ release_id: event.from_release_id ?? "",
391
+ stage: "launch",
392
+ reason: exit,
393
+ detail: "",
394
+ },
195
395
  { events: 1 },
196
- { shardBy },
396
+ { shardBy: event.install_id },
197
397
  );
198
398
  };
199
399
 
@@ -202,11 +402,39 @@ const isNewer = (event: Head, head: Head) =>
202
402
  ? event.received_at_ms > head.received_at_ms
203
403
  : compareUtf8(event.id, head.id) > 0;
204
404
 
405
+ /** What an UNCHANGED report must share with its installation's head to repeat it. */
406
+ const REPEATED_FIELDS = [
407
+ "channel",
408
+ "platform",
409
+ "app_version",
410
+ "to_bundle_id",
411
+ "to_release_id",
412
+ "user_id",
413
+ ] as const;
414
+
415
+ /**
416
+ * An UNCHANGED report that repeats its installation's head from the same UTC
417
+ * day: the installation already counts as active that day, on that bundle,
418
+ * so the report records nothing. A download's head does not count, because
419
+ * its installation still ran the bundle it downloaded from.
420
+ */
421
+ const repeatsHead = (event: BundleEventRow, head: Head) =>
422
+ event.type === "UNCHANGED" &&
423
+ head.type !== "UPDATE_DOWNLOADED" &&
424
+ dayOf(event.received_at_ms) === dayOf(head.received_at_ms) &&
425
+ REPEATED_FIELDS.every((field) => event[field] === head[field]);
426
+
205
427
  /**
206
428
  * Records one event in one transaction: one batch read of the event and its
207
429
  * installation's head, one of the gauge and sketch rows it changes, then one
208
- * write. A repeated id changes nothing, whichever installation sends it; an
209
- * older event still counts in its own hour but never replaces the head.
430
+ * write. On a database that batches aggregates (DynamoDB, Firestore), the
431
+ * write holds the event's rows and one log row instead, and a compaction
432
+ * reads and writes the aggregate rows. A stored event's id changes nothing
433
+ * when repeated, whichever installation sends it, and neither does the id
434
+ * of the installation's head. An UNCHANGED report that repeats its head on
435
+ * the same UTC day writes nothing at all. An older event still counts in its
436
+ * own hour but never replaces the head. An update failure is stored and
437
+ * counted, but changes what no installation runs, so it moves no head.
210
438
  */
211
439
  export const recordEvent = (
212
440
  db: HotUpdaterDatabase<InsightsSchema>,
@@ -219,20 +447,30 @@ export const recordEvent = (
219
447
  tx.findOne("bundle_event_heads", { install_id: event.install_id }),
220
448
  ]);
221
449
  // The id is the report's idempotency key: a retry, or any report under
222
- // an id already stored, changes nothing, as analytics ingestion drops
223
- // duplicates.
224
- if (existing !== null) return;
225
- tx.create("bundle_events", event);
450
+ // an id already stored or already the installation's head, changes
451
+ // nothing, as analytics ingestion drops duplicates.
452
+ if (existing !== null || previous?.id === event.id) return;
453
+ if (previous !== null && repeatsHead(event, previous)) return;
454
+ // An UNCHANGED report is a launch: it counts and moves the head, but no
455
+ // event list shows it, so no event row or outcome row keeps it.
456
+ if (event.type !== "UNCHANGED") {
457
+ tx.create("bundle_events", event);
458
+ countOutcome(tx, event);
459
+ }
460
+ if (event.type === "UPDATE_FAILED") {
461
+ countFailure(tx, event);
462
+ return;
463
+ }
226
464
  countEvent(tx, event);
227
- const fields = headFields(event);
228
- const head = { ...fields, install_id: event.install_id };
229
- if (previous !== null && !isNewer(head, previous)) return;
465
+ countExit(tx, event);
466
+ if (previous !== null && !isNewer(event, previous)) return;
230
467
  if (previous === null) {
231
- tx.create("bundle_event_heads", head);
468
+ tx.create("bundle_event_heads", event);
232
469
  } else {
470
+ const { install_id: _, ...fields } = event;
233
471
  countHead(tx, previous, -1);
234
472
  tx.update("bundle_event_heads", previous, fields);
235
473
  }
236
- countHead(tx, head, 1);
474
+ countHead(tx, event, 1);
237
475
  });
238
476
  };
@@ -0,0 +1,115 @@
1
+ import { createMemoryAdapter } from "@hot-updater/plugin-core/internal";
2
+ import { createPluginTestHarness } from "@hot-updater/test-utils";
3
+ import { describe, expect, it } from "vitest";
4
+
5
+ import { HotUpdaterConfigError } from "../../assembly/configError";
6
+ import * as engine from "../../database";
7
+ import { resolveSchema } from "../../database/resolveSchema";
8
+ import { createHotUpdater } from "../../index";
9
+ import { insights } from "./index";
10
+
11
+ const DAY = 86_400_000;
12
+ const HOUR = 3_600_000;
13
+ const T = Date.UTC(2026, 8, 23, 10);
14
+
15
+ /** Each table's retention in days, or null where rows are kept. */
16
+ const periods = (plugin: ReturnType<typeof insights>) =>
17
+ Object.fromEntries(
18
+ resolveSchema([{ id: "insights", schema: plugin.schema }]).tables.map(
19
+ (table) => [
20
+ table.name,
21
+ table.retention ? table.retention.ms / DAY : null,
22
+ ],
23
+ ),
24
+ );
25
+
26
+ describe("insights retention", () => {
27
+ it("keeps raw events and hourly rows 90 days, daily rows and heads 400, lifetime rows always", () => {
28
+ expect(periods(insights())).toEqual({
29
+ bundle_events: 90,
30
+ bundle_event_heads: 400,
31
+ insights_overview: 90,
32
+ insights_sketches: 90,
33
+ insights_overview_daily: 400,
34
+ insights_sketches_daily: 400,
35
+ insights_overview_lifetime: null,
36
+ insights_sketches_lifetime: null,
37
+ insights_distribution: 400,
38
+ insights_latest_by_bundle: 400,
39
+ insights_outcomes: 90,
40
+ insights_failures: 90,
41
+ });
42
+ });
43
+
44
+ it("takes other periods, whose tables are the same", () => {
45
+ const plugin = insights({ retention: { rawDays: 30, dailyDays: 60 } });
46
+
47
+ expect(periods(plugin)).toMatchObject({
48
+ bundle_events: 30,
49
+ bundle_event_heads: 60,
50
+ insights_overview: 30,
51
+ insights_overview_daily: 60,
52
+ insights_overview_lifetime: null,
53
+ insights_distribution: 60,
54
+ insights_outcomes: 30,
55
+ });
56
+ const tables = (value: ReturnType<typeof insights>) =>
57
+ resolveSchema([{ id: "insights", schema: value.schema }]).tables.map(
58
+ ({ retention: _retention, ...table }) => table,
59
+ );
60
+ expect(tables(plugin)).toEqual(tables(insights()));
61
+ expect(periods(insights({ retention: { rawDays: 7 } }))).toMatchObject({
62
+ bundle_events: 7,
63
+ bundle_event_heads: 400,
64
+ });
65
+ });
66
+
67
+ it("refuses periods under a day, fractions, or a daily period under the raw one", () => {
68
+ for (const retention of [
69
+ { rawDays: 0 },
70
+ { dailyDays: 0 },
71
+ { rawDays: 1.5 },
72
+ { rawDays: 30, dailyDays: 10 },
73
+ { rawDays: 500 },
74
+ ]) {
75
+ expect(() => insights({ retention })).toThrow(HotUpdaterConfigError);
76
+ }
77
+ });
78
+
79
+ it("reports its periods through its API and its admin route", async () => {
80
+ const hotUpdater = createHotUpdater({
81
+ clientAccess: "public",
82
+ database: { name: "memory", adapter: createMemoryAdapter() },
83
+ plugins: [insights({ retention: { rawDays: 30, dailyDays: 60 } })],
84
+ });
85
+
86
+ expect(hotUpdater.api.insights.retention).toEqual({
87
+ rawDays: 30,
88
+ dailyDays: 60,
89
+ });
90
+ const response = await hotUpdater.handlers.admin(
91
+ new Request("https://admin.example.com/retention"),
92
+ );
93
+ await expect(response.json()).resolves.toEqual({
94
+ rawDays: 30,
95
+ dailyDays: 60,
96
+ });
97
+ });
98
+
99
+ it("reads hourly rows only within the raw period", async () => {
100
+ const harness = await createPluginTestHarness(
101
+ insights({ retention: { rawDays: 30, dailyDays: 60 } }),
102
+ { engine, adapter: createMemoryAdapter() },
103
+ );
104
+ harness.setNow(() => T);
105
+
106
+ const usage = await harness.api.getAppUsage({
107
+ channel: "production",
108
+ platform: "ios",
109
+ timeRange: { start: T - 40 * DAY, end: T },
110
+ intervalMs: HOUR,
111
+ });
112
+
113
+ expect(usage.coverage).toEqual({ kind: "partial", sinceMs: T - 30 * DAY });
114
+ });
115
+ });
@@ -1,7 +1,7 @@
1
+ import { isDatabaseBusyError } from "../../database/busy";
1
2
  import {
2
3
  InsightsBadRequestError,
3
4
  InsightsPayloadTooLargeError,
4
- isDatabaseBusyError,
5
5
  } from "./errors";
6
6
  import { parseBundleEventRequest } from "./eventInput";
7
7
  import {
@@ -110,36 +110,6 @@ export type InsightsRouteHandler = (
110
110
  request: Request,
111
111
  ) => Promise<Response>;
112
112
 
113
- /** Without the insights plugin, its routes answer 204 and say Insights is off. */
114
- export const insightsDisabled: InsightsRouteHandler = async () =>
115
- new Response(null, {
116
- headers: {
117
- "cache-control": "private, no-store",
118
- "x-hot-updater-insights": "disabled",
119
- },
120
- status: 204,
121
- });
122
-
123
- /** What a server without the insights plugin logs on the first event it drops. */
124
- export const INSIGHTS_OFF_WARNING =
125
- "[hot-updater] Insights is off, so POST /events answered 204 and stored nothing. Add insights() from @hot-updater/server/plugins/insights to plugins to record app events, or set insights: false in the app's HotUpdater.init to stop sending them. This warning prints once per server.";
126
-
127
- /**
128
- * The client's `POST /events` without the insights plugin. It answers as
129
- * {@link insightsDisabled} does, so apps need no change, and warns on the
130
- * first event it drops, so a server that left out `insights()` says so.
131
- */
132
- export const createDroppedEventHandler = (): InsightsRouteHandler => {
133
- let warned = false;
134
- return (params, request) => {
135
- if (!warned) {
136
- warned = true;
137
- console.warn(INSIGHTS_OFF_WARNING);
138
- }
139
- return insightsDisabled(params, request);
140
- };
141
- };
142
-
143
113
  export const createInsightsRouteHandlers = (
144
114
  provider: InsightsProvider,
145
115
  ): Record<InsightsRouteName, InsightsRouteHandler> => ({