@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
@@ -0,0 +1,197 @@
1
+ import type { BundleEventRow } from "./eventRow";
2
+
3
+ export interface InsightsEventCursor {
4
+ readonly receivedAtMs: number;
5
+ readonly id: string;
6
+ }
7
+
8
+ export interface InsightsScope {
9
+ readonly platform: "ios" | "android";
10
+ readonly channel: string;
11
+ }
12
+
13
+ /**
14
+ * Raw event predicates; recovery is attributed to the source bundle. An
15
+ * UNCHANGED report is kept as no event, so no filter names it.
16
+ */
17
+ export type InsightsBundleEventFilter = InsightsScope &
18
+ (
19
+ | { readonly type: "RECOVERED"; readonly fromBundleId: string }
20
+ | {
21
+ readonly type: "UPDATE_DOWNLOADED" | "UPDATE_APPLIED" | "UPDATE_FAILED";
22
+ readonly toBundleId: string;
23
+ }
24
+ );
25
+
26
+ export type InsightsEventFilter =
27
+ | { readonly kind: "all" }
28
+ | {
29
+ readonly kind: "installationMovement";
30
+ readonly installId: string;
31
+ }
32
+ | ({ readonly kind: "bundle" } & InsightsBundleEventFilter);
33
+
34
+ export interface InsightsRecordEventInput {
35
+ readonly event: BundleEventRow;
36
+ }
37
+
38
+ export interface InsightsListEventsInput {
39
+ readonly filter: InsightsEventFilter;
40
+ readonly sinceMs?: number;
41
+ readonly beforeReceivedAtMs: number;
42
+ readonly after?: InsightsEventCursor;
43
+ readonly limit: number;
44
+ }
45
+
46
+ export type InsightsFindLatestEventsInput =
47
+ | { readonly installId: string }
48
+ | {
49
+ readonly userId: string;
50
+ readonly afterInstallId?: string;
51
+ readonly limit: number;
52
+ };
53
+
54
+ export interface InsightsCountLatestEventsInput extends InsightsScope {
55
+ /** A UTC day's start: latest events are counted by the day they fall in. */
56
+ readonly sinceMs: number;
57
+ /** Optional OR of one or two fixed predicates; count a matching event once. */
58
+ readonly bundle?: readonly {
59
+ readonly field: "from_bundle_id" | "to_bundle_id";
60
+ readonly value: string;
61
+ readonly types: readonly BundleEventRow["type"][];
62
+ }[];
63
+ }
64
+
65
+ export interface InsightsCountEventsInput {
66
+ readonly filter: InsightsBundleEventFilter;
67
+ readonly sinceMs: number;
68
+ readonly beforeReceivedAtMs: number;
69
+ }
70
+
71
+ export interface ReleaseReference {
72
+ readonly releaseId: string;
73
+ readonly platform: "ios" | "android";
74
+ readonly channel: string;
75
+ }
76
+
77
+ export interface InsightsTimeRange {
78
+ /** Inclusive UTC Unix timestamp in milliseconds. */
79
+ readonly start: number;
80
+ /** Exclusive UTC Unix timestamp in milliseconds. */
81
+ readonly end: number;
82
+ }
83
+
84
+ export type InsightsCoverage =
85
+ | { readonly kind: "complete"; readonly sinceMs: number }
86
+ | { readonly kind: "partial"; readonly sinceMs: number | null };
87
+
88
+ export interface ReleaseActivityMetrics {
89
+ readonly downloads: number;
90
+ readonly launches: number;
91
+ readonly failedLaunches: number;
92
+ /** Omitted for lifetime-only bundle-row reads. */
93
+ readonly uniqueUsers?: number;
94
+ /** Daily points, present only for period reads. */
95
+ readonly series?: readonly {
96
+ readonly startMs: number;
97
+ readonly launches: number;
98
+ readonly failedLaunches: number;
99
+ }[];
100
+ }
101
+
102
+ export type InsightsGetReleaseActivityInput =
103
+ | {
104
+ readonly releases: readonly ReleaseReference[];
105
+ readonly timeRange?: InsightsTimeRange;
106
+ readonly scope?: never;
107
+ }
108
+ | {
109
+ readonly scope: InsightsScope;
110
+ readonly timeRange: InsightsTimeRange;
111
+ readonly releases?: never;
112
+ };
113
+
114
+ export interface InsightsGetReleaseActivityResult {
115
+ readonly coverage: InsightsCoverage;
116
+ readonly data: readonly {
117
+ readonly release?: ReleaseReference;
118
+ readonly scope?: InsightsScope;
119
+ readonly metrics: ReleaseActivityMetrics;
120
+ }[];
121
+ readonly measuredAtMs: number;
122
+ }
123
+
124
+ export interface InsightsGetAppUsageInput {
125
+ readonly channel: string;
126
+ readonly platform: "all" | "ios" | "android";
127
+ readonly appVersion?: string;
128
+ readonly timeRange: InsightsTimeRange;
129
+ readonly intervalMs: number;
130
+ }
131
+
132
+ export interface InsightsGetAppUsageResult {
133
+ readonly coverage: InsightsCoverage;
134
+ readonly activeInstallations: number;
135
+ readonly points: readonly {
136
+ readonly startMs: number;
137
+ readonly installations: number;
138
+ }[];
139
+ readonly appVersions: readonly string[];
140
+ readonly versions: readonly {
141
+ readonly name: string;
142
+ readonly installations: number;
143
+ }[];
144
+ readonly platforms: readonly {
145
+ readonly name: string;
146
+ readonly installations: number;
147
+ }[];
148
+ readonly bundleDistribution: readonly {
149
+ readonly appVersion: string;
150
+ readonly platform: "ios" | "android";
151
+ readonly releaseId: string | null;
152
+ readonly installations: number;
153
+ }[];
154
+ readonly measuredAtMs: number;
155
+ }
156
+
157
+ export interface InsightsModel {
158
+ /**
159
+ * Persist the immutable event once. A private latest-event index, if used,
160
+ * advances atomically for a greater (received_at_ms, id). Duplicate event
161
+ * IDs are complete no-ops, whichever installation sends them.
162
+ * Retry the identical prepared input after an ambiguous commit outcome.
163
+ */
164
+ recordEvent(input: InsightsRecordEventInput): Promise<void>;
165
+ /**
166
+ * Descending (received_at_ms, id), in [sinceMs ?? 0, beforeReceivedAtMs).
167
+ * Apply filters and the exclusive cursor before limit (1..101). Return the
168
+ * complete matching prefix; native continuation pages must not truncate it.
169
+ * A global or bundle list may reject a range longer than 90 × 24 hours;
170
+ * the server asks for older events with an earlier range instead.
171
+ */
172
+ listEvents(
173
+ input: InsightsListEventsInput,
174
+ ): Promise<readonly BundleEventRow[]>;
175
+ /**
176
+ * Choose each installation's greatest (received_at_ms, id) before filtering.
177
+ * Exact installation lookup returns zero or one canonical event. User lookup
178
+ * returns current membership ordered by UTF-8 installation ID, exclusive
179
+ * afterInstallId and limit 1..101. Check lagging index candidates against
180
+ * canonical state; newly indexed associations may temporarily be absent.
181
+ */
182
+ findLatestEvents(
183
+ input: InsightsFindLatestEventsInput,
184
+ ): Promise<readonly BundleEventRow[]>;
185
+ /** Count latest events once per installation, then apply the supplied predicates. */
186
+ countLatestEvents(input: InsightsCountLatestEventsInput): Promise<number>;
187
+ /** Count accepted reports in [sinceMs, beforeReceivedAtMs), across all pages. */
188
+ countEvents(input: InsightsCountEventsInput): Promise<number>;
189
+ /** Read maintained release/scope summaries without scanning raw events. */
190
+ getReleaseActivity(
191
+ input: InsightsGetReleaseActivityInput,
192
+ ): Promise<InsightsGetReleaseActivityResult>;
193
+ /** Read maintained App usage summaries and latest-report distribution. */
194
+ getAppUsage(
195
+ input: InsightsGetAppUsageInput,
196
+ ): Promise<InsightsGetAppUsageResult>;
197
+ }
@@ -0,0 +1,64 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import type { BundleEventRow } from "./eventRow";
4
+ import { insightsOverviewDeltas } from "./overview";
5
+
6
+ const event = (type: BundleEventRow["type"]): BundleEventRow =>
7
+ ({
8
+ id: "01993e00-0000-7000-8000-000000000001",
9
+ type,
10
+ install_id: "install-a",
11
+ user_id: null,
12
+ from_release_id: type === "UNCHANGED" ? null : "source-release",
13
+ from_bundle_id: type === "UNCHANGED" ? null : "source-bundle",
14
+ to_release_id: "destination-release",
15
+ to_bundle_id: "destination-bundle",
16
+ platform: "ios",
17
+ app_version: "1.0.0",
18
+ channel: "production",
19
+ metadata: {
20
+ cohort: "default",
21
+ update_strategy: null,
22
+ fingerprint_hash: null,
23
+ sdk_version: null,
24
+ },
25
+ received_at_ms: 3_600_001,
26
+ }) as BundleEventRow;
27
+
28
+ describe("Insights overview event contributions", () => {
29
+ it("attributes recovery failure to the source and readiness to the destination", () => {
30
+ const rows = insightsOverviewDeltas(event("RECOVERED"));
31
+ const lifetime = rows.filter(
32
+ ({ identity }) =>
33
+ identity.scopeKind === "release" && identity.periodKind === "lifetime",
34
+ );
35
+ expect(lifetime).toEqual(
36
+ expect.arrayContaining([
37
+ expect.objectContaining({
38
+ identity: expect.objectContaining({ releaseId: "source-release" }),
39
+ failedLaunches: 1,
40
+ launches: 0,
41
+ }),
42
+ expect.objectContaining({
43
+ identity: expect.objectContaining({
44
+ releaseId: "destination-release",
45
+ }),
46
+ failedLaunches: 0,
47
+ launches: 1,
48
+ }),
49
+ ]),
50
+ );
51
+ });
52
+
53
+ it("counts downloads without treating them as launches", () => {
54
+ const lifetime = insightsOverviewDeltas(event("UPDATE_DOWNLOADED")).find(
55
+ ({ identity }) =>
56
+ identity.scopeKind === "release" && identity.periodKind === "lifetime",
57
+ );
58
+ expect(lifetime).toMatchObject({
59
+ downloads: 1,
60
+ launches: 0,
61
+ failedLaunches: 0,
62
+ });
63
+ });
64
+ });
@@ -0,0 +1,214 @@
1
+ import type { BundleEventRow } from "./eventRow";
2
+
3
+ export type InsightsOverviewIdentity = {
4
+ readonly scopeKind: "release" | "channel" | "usage" | "distribution";
5
+ readonly releaseKind: "all" | "specific" | "embedded";
6
+ readonly releaseId: string;
7
+ readonly channel: string;
8
+ readonly platform: "all" | "ios" | "android";
9
+ readonly appVersionKind: "all" | "specific";
10
+ readonly appVersion: string;
11
+ readonly periodKind: "lifetime" | "hour" | "latest";
12
+ readonly bucketStartMs: number;
13
+ };
14
+
15
+ export type InsightsOverviewDelta = {
16
+ readonly identity: InsightsOverviewIdentity;
17
+ readonly downloads: number;
18
+ readonly launches: number;
19
+ readonly failedLaunches: number;
20
+ readonly launchIdentity?: string;
21
+ readonly activityIdentity?: string;
22
+ };
23
+
24
+ const encodeIdentity = (identity: InsightsOverviewIdentity): string =>
25
+ [
26
+ identity.scopeKind,
27
+ identity.releaseKind,
28
+ identity.releaseId,
29
+ identity.channel,
30
+ identity.platform,
31
+ identity.appVersionKind,
32
+ identity.appVersion,
33
+ identity.periodKind,
34
+ String(identity.bucketStartMs),
35
+ ]
36
+ .map((value) => `${new TextEncoder().encode(value).length}:${value}`)
37
+ .join("");
38
+
39
+ const fnv = (value: string, seed: bigint): string => {
40
+ let result = seed;
41
+ for (const byte of new TextEncoder().encode(value)) {
42
+ result ^= BigInt(byte);
43
+ result = BigInt.asUintN(64, result * 0x100000001b3n);
44
+ }
45
+ return result.toString(16).padStart(16, "0");
46
+ };
47
+
48
+ /** A 128-bit hex key for a string: two FNV-1a 64 hashes with different seeds. */
49
+ export const insightsKey = (value: string): string =>
50
+ `${fnv(value, 0xcbf29ce484222325n)}${fnv(value, 0x84222325cbf29ce4n)}`;
51
+
52
+ export const insightsOverviewId = (
53
+ identity: InsightsOverviewIdentity,
54
+ ): string => insightsKey(encodeIdentity(identity));
55
+
56
+ export const insightsOverviewValues = (identity: InsightsOverviewIdentity) => ({
57
+ id: insightsOverviewId(identity),
58
+ scope_kind: identity.scopeKind,
59
+ release_kind: identity.releaseKind,
60
+ release_id: identity.releaseId,
61
+ channel: identity.channel,
62
+ platform: identity.platform,
63
+ app_version_kind: identity.appVersionKind,
64
+ app_version: identity.appVersion,
65
+ period_kind: identity.periodKind,
66
+ bucket_start_ms: identity.bucketStartMs,
67
+ });
68
+
69
+ const hour = (value: number): number =>
70
+ Math.floor(value / 3_600_000) * 3_600_000;
71
+
72
+ export const currentInsightsReleaseId = (
73
+ event: BundleEventRow,
74
+ ): string | null =>
75
+ event.type === "UPDATE_DOWNLOADED"
76
+ ? event.from_release_id
77
+ : event.to_release_id;
78
+
79
+ export const insightsDistributionIdentity = (
80
+ event: BundleEventRow,
81
+ ): InsightsOverviewIdentity => {
82
+ const releaseId = currentInsightsReleaseId(event);
83
+ return {
84
+ scopeKind: "distribution",
85
+ releaseKind: releaseId === null ? "embedded" : "specific",
86
+ releaseId: releaseId ?? "",
87
+ channel: event.channel,
88
+ platform: event.platform,
89
+ appVersionKind: "specific",
90
+ appVersion: event.app_version,
91
+ periodKind: "latest",
92
+ bucketStartMs: hour(event.received_at_ms),
93
+ };
94
+ };
95
+
96
+ export const insightsOverviewDeltas = (
97
+ event: BundleEventRow,
98
+ ): readonly InsightsOverviewDelta[] => {
99
+ const bucketStartMs = hour(event.received_at_ms);
100
+ const counters = new Map<string, InsightsOverviewDelta>();
101
+ const append = (
102
+ identity: InsightsOverviewIdentity,
103
+ values: Pick<
104
+ InsightsOverviewDelta,
105
+ "downloads" | "launches" | "failedLaunches"
106
+ > & {
107
+ readonly launchIdentity?: string;
108
+ readonly activityIdentity?: string;
109
+ },
110
+ ) => {
111
+ const key = encodeIdentity(identity);
112
+ const previous = counters.get(key);
113
+ counters.set(key, {
114
+ identity,
115
+ downloads: (previous?.downloads ?? 0) + values.downloads,
116
+ launches: (previous?.launches ?? 0) + values.launches,
117
+ failedLaunches: (previous?.failedLaunches ?? 0) + values.failedLaunches,
118
+ launchIdentity: previous?.launchIdentity ?? values.launchIdentity,
119
+ activityIdentity: previous?.activityIdentity ?? values.activityIdentity,
120
+ });
121
+ };
122
+ const metric = (
123
+ releaseId: string | null,
124
+ values: Pick<
125
+ InsightsOverviewDelta,
126
+ "downloads" | "launches" | "failedLaunches"
127
+ >,
128
+ ) => {
129
+ const launchIdentity = values.launches > 0 ? event.install_id : undefined;
130
+ if (releaseId !== null) {
131
+ for (const [periodKind, start] of [
132
+ ["lifetime", 0],
133
+ ["hour", bucketStartMs],
134
+ ] as const) {
135
+ append(
136
+ {
137
+ scopeKind: "release",
138
+ releaseKind: "specific",
139
+ releaseId,
140
+ channel: event.channel,
141
+ platform: event.platform,
142
+ appVersionKind: "all",
143
+ appVersion: "",
144
+ periodKind,
145
+ bucketStartMs: start,
146
+ },
147
+ { ...values, ...(periodKind === "hour" ? { launchIdentity } : {}) },
148
+ );
149
+ }
150
+ }
151
+ append(
152
+ {
153
+ scopeKind: "channel",
154
+ releaseKind: "all",
155
+ releaseId: "",
156
+ channel: event.channel,
157
+ platform: event.platform,
158
+ appVersionKind: "all",
159
+ appVersion: "",
160
+ periodKind: "hour",
161
+ bucketStartMs,
162
+ },
163
+ { ...values, launchIdentity },
164
+ );
165
+ };
166
+
167
+ if (event.type === "UPDATE_DOWNLOADED") {
168
+ metric(event.to_release_id, {
169
+ downloads: 1,
170
+ launches: 0,
171
+ failedLaunches: 0,
172
+ });
173
+ } else if (event.type !== "UPDATE_FAILED") {
174
+ // A failed update is no launch. recordEvent counts a failure itself and
175
+ // asks for no deltas of it.
176
+ metric(event.to_release_id, {
177
+ downloads: 0,
178
+ launches: 1,
179
+ failedLaunches: 0,
180
+ });
181
+ if (event.type === "RECOVERED") {
182
+ metric(event.from_release_id, {
183
+ downloads: 0,
184
+ launches: 0,
185
+ failedLaunches: 1,
186
+ });
187
+ }
188
+ }
189
+
190
+ for (const platform of [event.platform, "all"] as const) {
191
+ for (const appVersionKind of ["specific", "all"] as const) {
192
+ append(
193
+ {
194
+ scopeKind: "usage",
195
+ releaseKind: "all",
196
+ releaseId: "",
197
+ channel: event.channel,
198
+ platform,
199
+ appVersionKind,
200
+ appVersion: appVersionKind === "specific" ? event.app_version : "",
201
+ periodKind: "hour",
202
+ bucketStartMs,
203
+ },
204
+ {
205
+ downloads: 0,
206
+ launches: 0,
207
+ failedLaunches: 0,
208
+ activityIdentity: event.install_id,
209
+ },
210
+ );
211
+ }
212
+ }
213
+ return [...counters.values()];
214
+ };
@@ -1,7 +1,8 @@
1
- import type { BundleEventRow, InsightsModel } from "@hot-updater/plugin-core";
2
1
  import { afterEach, describe, expect, it, vi } from "vitest";
3
2
 
4
3
  import { InsightsBadRequestError } from "./errors";
4
+ import type { BundleEventRow } from "./eventRow";
5
+ import type { InsightsModel } from "./modelTypes";
5
6
  import { createInsightsProvider } from "./provider";
6
7
 
7
8
  const eventId = (index: number) =>
@@ -34,7 +35,6 @@ const eventRow = (
34
35
  fingerprint_hash: null,
35
36
  sdk_version: "2.0.0",
36
37
  update_strategy: "appVersion",
37
- username: "Jane",
38
38
  },
39
39
 
40
40
  from_bundle_id: "bundle-before",
@@ -408,7 +408,7 @@ describe("createInsightsProvider", () => {
408
408
  expect(fixture.countEvents).not.toHaveBeenCalled();
409
409
  });
410
410
 
411
- it("counts whole hours that end with the current one", async () => {
411
+ it("counts from the UTC day the window reaches into to the end of the current hour", async () => {
412
412
  vi.useFakeTimers();
413
413
  vi.setSystemTime(new Date("2026-08-12T10:25:00.000Z"));
414
414
  const fixture = createModel();
@@ -421,15 +421,15 @@ describe("createInsightsProvider", () => {
421
421
  channel: "production",
422
422
  });
423
423
 
424
- const end = Date.parse("2026-08-12T11:00:00.000Z");
424
+ const start = Date.parse("2026-08-11T00:00:00.000Z");
425
425
  expect(overview).toMatchObject({
426
- beforeReceivedAtMs: end,
427
- sinceMs: end - 24 * 60 * 60 * 1_000,
426
+ beforeReceivedAtMs: Date.parse("2026-08-12T11:00:00.000Z"),
427
+ sinceMs: start,
428
428
  });
429
429
  expect(fixture.countLatestEvents).toHaveBeenCalledWith({
430
430
  platform: "ios",
431
431
  channel: "production",
432
- sinceMs: end - 24 * 60 * 60 * 1_000,
432
+ sinceMs: start,
433
433
  });
434
434
  });
435
435
 
@@ -442,7 +442,7 @@ describe("createInsightsProvider", () => {
442
442
  .mockResolvedValueOnce(7)
443
443
  .mockResolvedValueOnce(5)
444
444
  .mockResolvedValueOnce(3)
445
- .mockResolvedValueOnce(1);
445
+ .mockResolvedValueOnce(2);
446
446
  const provider = createInsightsProvider(fixture.model);
447
447
  const scope = { platform: "ios", channel: "production" } as const;
448
448
  const result = await provider.getReportingOverview({
@@ -469,15 +469,26 @@ describe("createInsightsProvider", () => {
469
469
  expect(result.bundle?.downloadedReports.count).toBe(7);
470
470
  expect(result.bundle?.appliedReports.count).toBe(5);
471
471
  expect(result.bundle?.recoveredReports.count).toBe(3);
472
- expect(result.bundle?.unchangedReports.count).toBe(1);
472
+ expect(result.bundle?.failedReports.count).toBe(2);
473
+ // UNCHANGED reports are kept as no events, so no count or list names them.
474
+ expect(result.bundle).not.toHaveProperty("unchangedReports");
473
475
  expect(
474
476
  fixture.countEvents.mock.calls.map(([input]) => input.filter),
475
477
  ).toEqual([
476
478
  { ...scope, type: "UPDATE_DOWNLOADED", toBundleId: "B" },
477
479
  { ...scope, type: "UPDATE_APPLIED", toBundleId: "B" },
478
480
  { ...scope, type: "RECOVERED", fromBundleId: "B" },
479
- { ...scope, type: "UNCHANGED", toBundleId: "B" },
481
+ { ...scope, type: "UPDATE_FAILED", toBundleId: "B" },
480
482
  ]);
483
+ expect(() =>
484
+ provider.listEvents({
485
+ bundle: {
486
+ ...scope,
487
+ bundleId: "B",
488
+ outcome: "unchanged" as "applied",
489
+ },
490
+ }),
491
+ ).toThrow(InsightsBadRequestError);
481
492
  await provider.listEvents({
482
493
  bundle: { ...scope, bundleId: "B", outcome: "recovered" },
483
494
  sinceMs: result.sinceMs,