@hot-updater/server 1.0.0-rc.0 → 1.0.0-rc.2

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 (210) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +1 -1
  2. package/dist/adapters/databaseConstraintErrors.cjs +0 -1
  3. package/dist/adapters/databasePluginUtils.cjs +0 -1
  4. package/dist/adapters/drizzle.cjs +11 -4
  5. package/dist/adapters/drizzle.d.cts +0 -1
  6. package/dist/adapters/drizzle.d.mts +0 -1
  7. package/dist/adapters/drizzle.mjs +12 -4
  8. package/dist/adapters/drizzleCrud.cjs +62 -1
  9. package/dist/adapters/drizzleCrud.mjs +61 -2
  10. package/dist/adapters/drizzleLazyDB.cjs +6 -1
  11. package/dist/adapters/drizzleLazyDB.mjs +6 -1
  12. package/dist/adapters/drizzleQuery.cjs +3 -2
  13. package/dist/adapters/drizzleQuery.mjs +3 -1
  14. package/dist/adapters/insightsTransaction.cjs +22 -0
  15. package/dist/adapters/insightsTransaction.mjs +22 -0
  16. package/dist/adapters/kysely.cjs +2 -1
  17. package/dist/adapters/kysely.d.cts +0 -1
  18. package/dist/adapters/kysely.d.mts +0 -1
  19. package/dist/adapters/kysely.mjs +3 -1
  20. package/dist/adapters/kyselyCrud.cjs +54 -3
  21. package/dist/adapters/kyselyCrud.mjs +54 -3
  22. package/dist/adapters/kyselyQuery.cjs +2 -2
  23. package/dist/adapters/kyselyQuery.mjs +2 -1
  24. package/dist/adapters/mongodb.cjs +27 -1
  25. package/dist/adapters/mongodb.d.cts +1 -1
  26. package/dist/adapters/mongodb.d.mts +1 -1
  27. package/dist/adapters/mongodb.mjs +28 -1
  28. package/dist/adapters/mongodbCollections.cjs +2 -0
  29. package/dist/adapters/mongodbCollections.mjs +2 -0
  30. package/dist/adapters/mongodbQuery.cjs +21 -4
  31. package/dist/adapters/mongodbQuery.mjs +21 -5
  32. package/dist/adapters/mongodbReads.cjs +91 -29
  33. package/dist/adapters/mongodbReads.mjs +92 -29
  34. package/dist/adapters/mongodbWrites.cjs +16 -2
  35. package/dist/adapters/mongodbWrites.mjs +18 -3
  36. package/dist/adapters/prisma.cjs +55 -7
  37. package/dist/adapters/prisma.d.cts +0 -1
  38. package/dist/adapters/prisma.d.mts +0 -1
  39. package/dist/adapters/prisma.mjs +56 -7
  40. package/dist/adapters/prismaQuery.cjs +0 -1
  41. package/dist/adapters/prismaRows.cjs +22 -1
  42. package/dist/adapters/prismaRows.mjs +22 -1
  43. package/dist/apiKeys.cjs +6 -2
  44. package/dist/apiKeys.d.cts +0 -1
  45. package/dist/apiKeys.d.mts +0 -1
  46. package/dist/apiKeys.mjs +3 -3
  47. package/dist/createHotUpdaterCore.cjs +27 -12
  48. package/dist/createHotUpdaterCore.d.cts +8 -8
  49. package/dist/createHotUpdaterCore.d.mts +8 -8
  50. package/dist/createHotUpdaterCore.mjs +26 -12
  51. package/dist/db/createBundleDiff.cjs +2 -2
  52. package/dist/db/createBundleDiff.d.cts +1 -5
  53. package/dist/db/createBundleDiff.d.mts +1 -5
  54. package/dist/db/databasePluginCore.cjs +0 -1
  55. package/dist/db/fixedMigrator.cjs +2 -2
  56. package/dist/db/fixedMigrator.mjs +2 -2
  57. package/dist/db/fixedMigratorMongo.cjs +6 -4
  58. package/dist/db/fixedMigratorMongo.mjs +6 -4
  59. package/dist/db/index.d.cts +0 -1
  60. package/dist/db/index.d.mts +0 -1
  61. package/dist/db/releaseCatalog.cjs +0 -1
  62. package/dist/db/releaseCatalog.d.cts +0 -1
  63. package/dist/db/releaseCatalog.d.mts +0 -1
  64. package/dist/db/schema/insightsCollation.cjs +14 -0
  65. package/dist/db/schema/insightsCollation.mjs +14 -0
  66. package/dist/db/schema/sql.cjs +11 -7
  67. package/dist/db/schema/sql.mjs +8 -8
  68. package/dist/db/schemaGenerators.cjs +28 -9
  69. package/dist/db/schemaGenerators.mjs +28 -9
  70. package/dist/db/schemaReadiness.cjs +2 -0
  71. package/dist/db/schemaReadiness.d.cts +1 -0
  72. package/dist/db/schemaReadiness.d.mts +1 -0
  73. package/dist/db/schemaReadiness.mjs +2 -0
  74. package/dist/db/sqlMigrationExecution.cjs +1 -1
  75. package/dist/db/sqlMigrationExecution.mjs +1 -0
  76. package/dist/db/types.cjs +3 -1
  77. package/dist/db/types.d.cts +3 -6
  78. package/dist/db/types.d.mts +3 -6
  79. package/dist/db/types.mjs +2 -2
  80. package/dist/db/updateArtifacts.cjs +0 -1
  81. package/dist/handler.cjs +5 -5
  82. package/dist/handler.d.cts +1 -2
  83. package/dist/handler.d.mts +1 -2
  84. package/dist/handler.mjs +5 -5
  85. package/dist/handlerReleaseCatalogRoutes.cjs +0 -1
  86. package/dist/handlerReleaseManagementRoutes.cjs +0 -1
  87. package/dist/handlerTypes.d.cts +0 -1
  88. package/dist/handlerTypes.d.mts +0 -1
  89. package/dist/handlerVersionRoutes.d.cts +1 -0
  90. package/dist/handlerVersionRoutes.d.mts +1 -0
  91. package/dist/index.cjs +2 -2
  92. package/dist/index.d.cts +4 -4
  93. package/dist/index.d.mts +4 -4
  94. package/dist/index.mjs +2 -2
  95. package/dist/insights/domain.d.cts +89 -0
  96. package/dist/insights/domain.d.mts +89 -0
  97. package/dist/insights/errors.cjs +15 -0
  98. package/dist/insights/errors.mjs +14 -0
  99. package/dist/{analytics → insights}/eventInput.cjs +58 -14
  100. package/dist/{analytics → insights}/eventInput.mjs +58 -16
  101. package/dist/insights/provider.cjs +314 -0
  102. package/dist/insights/provider.d.cts +6 -0
  103. package/dist/insights/provider.d.mts +6 -0
  104. package/dist/insights/provider.mjs +314 -0
  105. package/dist/insights/queryInput.cjs +93 -0
  106. package/dist/insights/queryInput.mjs +91 -0
  107. package/dist/insights/routes.cjs +58 -0
  108. package/dist/insights/routes.mjs +56 -0
  109. package/dist/insights/types.d.cts +32 -0
  110. package/dist/insights/types.d.mts +32 -0
  111. package/dist/node.d.cts +0 -1
  112. package/dist/node.d.mts +0 -1
  113. package/dist/package.cjs +1 -1
  114. package/dist/package.mjs +1 -1
  115. package/dist/schema/index.cjs +17 -3
  116. package/dist/schema/index.mjs +4 -4
  117. package/dist/schema/v1_0_0.cjs +72 -30
  118. package/dist/schema/v1_0_0.mjs +72 -31
  119. package/dist/storageAccess.cjs +0 -1
  120. package/dist/types/index.d.cts +0 -1
  121. package/dist/types/index.d.mts +0 -1
  122. package/package.json +7 -7
  123. package/src/adapters/databasePluginTestDatabase.ts +14 -0
  124. package/src/adapters/drizzle.spec.ts +99 -0
  125. package/src/adapters/drizzle.ts +22 -4
  126. package/src/adapters/drizzleCrud.ts +154 -2
  127. package/src/adapters/drizzleLazyDB.ts +17 -3
  128. package/src/adapters/insightsTransaction.ts +32 -0
  129. package/src/adapters/kysely.spec.ts +29 -0
  130. package/src/adapters/kysely.ts +10 -1
  131. package/src/adapters/kyselyCrud.ts +121 -0
  132. package/src/adapters/mongodb.integration.spec.ts +210 -0
  133. package/src/adapters/mongodb.spec.ts +69 -1
  134. package/src/adapters/mongodb.ts +66 -2
  135. package/src/adapters/mongodbCollections.ts +5 -0
  136. package/src/adapters/mongodbQuery.ts +32 -3
  137. package/src/adapters/mongodbReads.ts +69 -0
  138. package/src/adapters/mongodbTestClient.ts +41 -6
  139. package/src/adapters/mongodbTestFilter.ts +4 -1
  140. package/src/adapters/mongodbWrites.ts +27 -0
  141. package/src/adapters/prisma.spec.ts +97 -0
  142. package/src/adapters/prisma.ts +103 -3
  143. package/src/adapters/prismaRows.ts +37 -1
  144. package/src/adapters/prismaTestClient.ts +22 -5
  145. package/src/apiKeys.integration.spec.ts +3 -3
  146. package/src/createHotUpdaterCore.ts +30 -16
  147. package/src/db/databasePluginUpdateCheck.bench.ts +3 -0
  148. package/src/db/fixedMigrator.spec.ts +65 -0
  149. package/src/db/fixedMigratorMongo.spec.ts +56 -9
  150. package/src/db/fixedMigratorMongo.ts +10 -10
  151. package/src/db/index.spec.ts +8 -1
  152. package/src/db/schema/insightsCollation.ts +21 -0
  153. package/src/db/schema/sql.ts +8 -5
  154. package/src/db/schemaGenerators.ts +41 -13
  155. package/src/db/types.spec.ts +11 -3
  156. package/src/db/types.ts +13 -7
  157. package/src/handler-standalone.integration.spec.ts +6 -8
  158. package/src/handler.ts +12 -12
  159. package/src/index.ts +3 -3
  160. package/src/insights/domain.ts +103 -0
  161. package/src/insights/errors.ts +11 -0
  162. package/src/{analytics → insights}/eventInput.ts +71 -14
  163. package/src/insights/insights.integration.spec.ts +381 -0
  164. package/src/insights/provider.spec.ts +385 -0
  165. package/src/insights/provider.ts +591 -0
  166. package/src/insights/queryInput.ts +141 -0
  167. package/src/insights/routes.ts +104 -0
  168. package/src/insights/types.ts +55 -0
  169. package/src/schema/dsl-all-versions.spec.ts +3 -1
  170. package/src/schema/v1_0_0.spec.ts +77 -3
  171. package/src/schema/v1_0_0.ts +94 -30
  172. package/dist/analytics/bounded/activeOverview.cjs +0 -87
  173. package/dist/analytics/bounded/activeOverview.mjs +0 -85
  174. package/dist/analytics/bounded/installationSearch.cjs +0 -42
  175. package/dist/analytics/bounded/installationSearch.mjs +0 -42
  176. package/dist/analytics/bounded/persistence.cjs +0 -39
  177. package/dist/analytics/bounded/persistence.mjs +0 -38
  178. package/dist/analytics/bounded/provider.cjs +0 -170
  179. package/dist/analytics/bounded/provider.d.cts +0 -7
  180. package/dist/analytics/bounded/provider.d.mts +0 -7
  181. package/dist/analytics/bounded/provider.mjs +0 -170
  182. package/dist/analytics/bounded/scan.cjs +0 -144
  183. package/dist/analytics/bounded/scan.mjs +0 -138
  184. package/dist/analytics/domain.d.cts +0 -121
  185. package/dist/analytics/domain.d.mts +0 -121
  186. package/dist/analytics/errors.cjs +0 -22
  187. package/dist/analytics/errors.mjs +0 -20
  188. package/dist/analytics/persistence.d.cts +0 -6
  189. package/dist/analytics/persistence.d.mts +0 -6
  190. package/dist/analytics/queryInput.cjs +0 -58
  191. package/dist/analytics/queryInput.mjs +0 -55
  192. package/dist/analytics/routes.cjs +0 -63
  193. package/dist/analytics/routes.mjs +0 -61
  194. package/dist/analytics/types.d.cts +0 -17
  195. package/dist/analytics/types.d.mts +0 -17
  196. package/src/analytics/analytics.integration.spec.ts +0 -144
  197. package/src/analytics/bounded/activeOverview.spec.ts +0 -189
  198. package/src/analytics/bounded/activeOverview.ts +0 -148
  199. package/src/analytics/bounded/installationSearch.spec.ts +0 -130
  200. package/src/analytics/bounded/installationSearch.ts +0 -69
  201. package/src/analytics/bounded/persistence.ts +0 -43
  202. package/src/analytics/bounded/provider.spec.ts +0 -219
  203. package/src/analytics/bounded/provider.ts +0 -229
  204. package/src/analytics/bounded/scan.ts +0 -235
  205. package/src/analytics/domain.ts +0 -139
  206. package/src/analytics/errors.ts +0 -18
  207. package/src/analytics/persistence.ts +0 -10
  208. package/src/analytics/queryInput.ts +0 -102
  209. package/src/analytics/routes.ts +0 -123
  210. package/src/analytics/types.ts +0 -44
@@ -1,229 +0,0 @@
1
- import type {
2
- BundleEventAnalyticsWindow,
3
- InstallationHistoryRow,
4
- } from "../domain";
5
- import type {
6
- AnalyticsPersistence,
7
- BundleEventPersistenceRow,
8
- } from "../persistence";
9
- import type { AnalyticsProvider } from "../types";
10
- import { collectActiveInstallationOverview } from "./activeOverview";
11
- import { searchEventInstallations } from "./installationSearch";
12
- import { createBundleEventRow } from "./persistence";
13
- import {
14
- ANALYTICS_SCAN_MAX_ROWS,
15
- collectEventActivity,
16
- compareCodePoints,
17
- compareEventNewest,
18
- materializeActiveRows,
19
- materializeEventRows,
20
- materializeRowsForWindow,
21
- } from "./scan";
22
-
23
- type TransitionEventRow = BundleEventPersistenceRow & {
24
- readonly type: "UPDATE_APPLIED" | "RECOVERED";
25
- };
26
-
27
- const isTransitionEventRow = (
28
- row: BundleEventPersistenceRow,
29
- ): row is TransitionEventRow =>
30
- row.type === "UPDATE_APPLIED" || row.type === "RECOVERED";
31
-
32
- const toHistoryRow = (row: TransitionEventRow): InstallationHistoryRow => ({
33
- id: row.id,
34
- type: row.type,
35
- fromBundleId: row.from_bundle_id,
36
- toBundleId: row.to_bundle_id,
37
- username: row.username,
38
- userId: row.user_id,
39
- platform: row.platform,
40
- appVersion: row.app_version,
41
- channel: row.channel,
42
- cohort: row.cohort,
43
- receivedAtMs: row.received_at_ms,
44
- });
45
-
46
- const isInstalledForBundle = (
47
- row: TransitionEventRow,
48
- bundleId: string,
49
- ): boolean => row.type === "UPDATE_APPLIED" && row.to_bundle_id === bundleId;
50
-
51
- const isRecoveredFromBundle = (
52
- row: TransitionEventRow,
53
- bundleId: string,
54
- ): boolean => row.type === "RECOVERED" && row.from_bundle_id === bundleId;
55
-
56
- const countDistinctInstallations = (
57
- rows: readonly BundleEventPersistenceRow[],
58
- ): number => new Set(rows.map(({ install_id }) => install_id)).size;
59
-
60
- const getAnalyticsResult = async (
61
- persistence: AnalyticsPersistence,
62
- bundleId: string,
63
- window: BundleEventAnalyticsWindow,
64
- limit: number,
65
- offset: number,
66
- ) => {
67
- const scope = { persistence, cutoffMs: Date.now() };
68
- const rows = (await materializeRowsForWindow(scope, window)).filter(
69
- isTransitionEventRow,
70
- );
71
- const installedRows = rows.filter((row) =>
72
- isInstalledForBundle(row, bundleId),
73
- );
74
- const recoveredRows = rows.filter((row) =>
75
- isRecoveredFromBundle(row, bundleId),
76
- );
77
- const installed = collectEventActivity({
78
- rows: installedRows,
79
- window,
80
- cutoffMs: scope.cutoffMs,
81
- });
82
- const recovered = collectEventActivity({
83
- rows: recoveredRows,
84
- window,
85
- cutoffMs: scope.cutoffMs,
86
- });
87
- const recentRows = [...installedRows, ...recoveredRows].sort(
88
- compareEventNewest,
89
- );
90
- return {
91
- summary: { installed: installed.summary, recovered: recovered.summary },
92
- series: { installed: installed.series, recovered: recovered.series },
93
- cohorts: { installed: installed.cohorts, recovered: recovered.cohorts },
94
- recentEvents: {
95
- data: recentRows.slice(offset, offset + limit).map(toHistoryRow),
96
- pagination: { total: recentRows.length, limit, offset },
97
- },
98
- };
99
- };
100
-
101
- const getAnalyticsSummaries = async (
102
- persistence: AnalyticsPersistence,
103
- bundleIds: readonly string[],
104
- window: BundleEventAnalyticsWindow,
105
- ) => {
106
- const normalizedBundleIds = [...new Set(bundleIds)];
107
- if (normalizedBundleIds.length === 0) return [];
108
-
109
- const requestedBundleIds = new Set(normalizedBundleIds);
110
- const installedByBundleId = new Map<string, Set<string>>();
111
- const recoveredByBundleId = new Map<string, Set<string>>();
112
- const rows = (
113
- await materializeRowsForWindow(
114
- { persistence, cutoffMs: Date.now() },
115
- window,
116
- )
117
- ).filter(isTransitionEventRow);
118
-
119
- for (const row of rows) {
120
- const bundleId =
121
- row.type === "UPDATE_APPLIED" ? row.to_bundle_id : row.from_bundle_id;
122
- if (!requestedBundleIds.has(bundleId)) continue;
123
-
124
- const counts =
125
- row.type === "UPDATE_APPLIED" ? installedByBundleId : recoveredByBundleId;
126
- const installIds = counts.get(bundleId) ?? new Set<string>();
127
- installIds.add(row.install_id);
128
- counts.set(bundleId, installIds);
129
- }
130
-
131
- return normalizedBundleIds.map((bundleId) => ({
132
- bundleId,
133
- installed: installedByBundleId.get(bundleId)?.size ?? 0,
134
- recovered: recoveredByBundleId.get(bundleId)?.size ?? 0,
135
- }));
136
- };
137
-
138
- export const createAnalyticsProvider = (
139
- persistence: AnalyticsPersistence,
140
- ): AnalyticsProvider =>
141
- Object.freeze({
142
- mode: "bounded",
143
- maxMatchingRows: ANALYTICS_SCAN_MAX_ROWS,
144
- async appendBundleEvent(input) {
145
- await persistence.append(createBundleEventRow(input));
146
- },
147
- async getBundleEventSummary(bundleId) {
148
- const rows = (
149
- await materializeEventRows({ persistence, cutoffMs: Date.now() })
150
- ).filter(isTransitionEventRow);
151
- return {
152
- installed: countDistinctInstallations(
153
- rows.filter((row) => isInstalledForBundle(row, bundleId)),
154
- ),
155
- recovered: countDistinctInstallations(
156
- rows.filter((row) => isRecoveredFromBundle(row, bundleId)),
157
- ),
158
- };
159
- },
160
- getBundleEventSummaries(bundleIds, window) {
161
- return getAnalyticsSummaries(persistence, bundleIds, window);
162
- },
163
- getBundleEventAnalytics(bundleId, window, limit, offset) {
164
- return getAnalyticsResult(persistence, bundleId, window, limit, offset);
165
- },
166
- async getBundleEventOverview() {
167
- const rows = await materializeEventRows({
168
- persistence,
169
- cutoffMs: Date.now(),
170
- });
171
- const latestByInstall = new Map<string, BundleEventPersistenceRow>();
172
- for (const row of rows) {
173
- const current = latestByInstall.get(row.install_id);
174
- if (current === undefined || compareEventNewest(row, current) < 0) {
175
- latestByInstall.set(row.install_id, row);
176
- }
177
- }
178
- const counts = new Map<string, number>();
179
- for (const row of latestByInstall.values()) {
180
- counts.set(row.to_bundle_id, (counts.get(row.to_bundle_id) ?? 0) + 1);
181
- }
182
- return {
183
- trackedInstallations: latestByInstall.size,
184
- bundles: [...counts]
185
- .map(([bundleId, installations]) => ({ bundleId, installations }))
186
- .sort(
187
- (left, right) =>
188
- right.installations - left.installations ||
189
- compareCodePoints(left.bundleId, right.bundleId),
190
- ),
191
- };
192
- },
193
- async getActiveInstallationOverview(input) {
194
- const asOfMs = Date.now();
195
- const rows = await materializeActiveRows(
196
- { persistence, cutoffMs: asOfMs },
197
- input.window,
198
- );
199
- return collectActiveInstallationOverview({
200
- rows,
201
- asOfMs,
202
- window: input.window,
203
- ...(input.userId === undefined ? {} : { userId: input.userId }),
204
- });
205
- },
206
- async searchInstallations(query, limit, offset) {
207
- const rows = await materializeEventRows({
208
- persistence,
209
- cutoffMs: Date.now(),
210
- });
211
- return searchEventInstallations({ rows, query, limit, offset });
212
- },
213
- async getInstallationHistory(installId, limit, offset) {
214
- const rows = await materializeEventRows({
215
- persistence,
216
- cutoffMs: Date.now(),
217
- });
218
- const ordered = rows
219
- .filter(
220
- (row): row is TransitionEventRow =>
221
- row.install_id === installId && isTransitionEventRow(row),
222
- )
223
- .toSorted(compareEventNewest);
224
- return {
225
- data: ordered.slice(offset, offset + limit).map(toHistoryRow),
226
- pagination: { total: ordered.length, limit, offset },
227
- };
228
- },
229
- } satisfies AnalyticsProvider);
@@ -1,235 +0,0 @@
1
- import type {
2
- ActiveInstallationWindow,
3
- BundleEventAnalyticsWindow,
4
- } from "../domain";
5
- import { AnalyticsScanLimitExceededError } from "../errors";
6
- import type {
7
- AnalyticsPersistence,
8
- BundleEventPersistenceRow,
9
- } from "../persistence";
10
- import {
11
- ACTIVE_BUNDLE_EVENT_TYPES,
12
- getActiveWindowDefinition,
13
- } from "./activeOverview";
14
-
15
- export const ANALYTICS_SCAN_MAX_ROWS = 50_000;
16
- export const ANALYTICS_MATERIALIZATION_LIMIT = ANALYTICS_SCAN_MAX_ROWS + 1;
17
- export const ANALYTICS_SCAN_PAGE_SIZE = 1_000;
18
- const ANALYTICS_LOWER_BOUND_ID = "00000000-0000-0000-0000-000000000000";
19
-
20
- export type AnalyticsScanScope = {
21
- readonly persistence: AnalyticsPersistence;
22
- readonly cutoffMs: number;
23
- readonly lowerBoundMs?: number;
24
- };
25
-
26
- type EventActivityRequest = {
27
- readonly rows: readonly BundleEventPersistenceRow[];
28
- readonly window: BundleEventAnalyticsWindow;
29
- readonly cutoffMs: number;
30
- };
31
-
32
- export const compareCodePoints = (left: string, right: string): number => {
33
- if (left < right) return -1;
34
- if (left > right) return 1;
35
- return 0;
36
- };
37
-
38
- export const compareEventNewest = (
39
- left: BundleEventPersistenceRow,
40
- right: BundleEventPersistenceRow,
41
- ): number =>
42
- right.received_at_ms - left.received_at_ms ||
43
- compareCodePoints(right.id, left.id);
44
-
45
- const compareEventOldest = (
46
- left: { readonly received_at_ms: number; readonly id: string },
47
- right: { readonly received_at_ms: number; readonly id: string },
48
- ): number =>
49
- left.received_at_ms - right.received_at_ms ||
50
- compareCodePoints(left.id, right.id);
51
-
52
- export const materializeEventRows = async (
53
- scope: AnalyticsScanScope,
54
- ): Promise<readonly BundleEventPersistenceRow[]> => {
55
- const rows: BundleEventPersistenceRow[] = [];
56
- const seenIds = new Set<string>();
57
- let after =
58
- scope.lowerBoundMs === undefined
59
- ? undefined
60
- : { receivedAtMs: scope.lowerBoundMs, id: ANALYTICS_LOWER_BOUND_ID };
61
- while (rows.length < ANALYTICS_MATERIALIZATION_LIMIT) {
62
- const limit = Math.min(
63
- ANALYTICS_SCAN_PAGE_SIZE,
64
- ANALYTICS_MATERIALIZATION_LIMIT - rows.length,
65
- );
66
- const page = await scope.persistence.scan({
67
- beforeReceivedAtMs: scope.cutoffMs,
68
- ...(after === undefined ? {} : { after }),
69
- limit,
70
- });
71
- if (page.length === 0) break;
72
- if (page.length > limit) throw new AnalyticsPersistenceOrderError();
73
- let previous =
74
- after === undefined
75
- ? undefined
76
- : { received_at_ms: after.receivedAtMs, id: after.id };
77
- for (const row of page) {
78
- if (
79
- row.received_at_ms >= scope.cutoffMs ||
80
- (previous !== undefined && compareEventOldest(previous, row) >= 0) ||
81
- seenIds.has(row.id)
82
- ) {
83
- throw new AnalyticsPersistenceOrderError();
84
- }
85
- seenIds.add(row.id);
86
- rows.push(row);
87
- previous = row;
88
- }
89
- const last = page.at(-1);
90
- if (last !== undefined) {
91
- after = { receivedAtMs: last.received_at_ms, id: last.id };
92
- }
93
- if (rows.length > ANALYTICS_SCAN_MAX_ROWS) {
94
- throw new AnalyticsScanLimitExceededError(ANALYTICS_SCAN_MAX_ROWS);
95
- }
96
- }
97
- return rows;
98
- };
99
-
100
- export class AnalyticsPersistenceOrderError extends Error {
101
- readonly name = "AnalyticsPersistenceOrderError";
102
-
103
- constructor() {
104
- super("Analytics persistence did not advance its scan cursor.");
105
- }
106
- }
107
-
108
- export const materializeActiveRows = async (
109
- scope: AnalyticsScanScope,
110
- window: ActiveInstallationWindow,
111
- ): Promise<readonly BundleEventPersistenceRow[]> => {
112
- const definition = getActiveWindowDefinition(window);
113
- const durationMs = definition.bucketCount * definition.bucketSizeMs;
114
- return (
115
- await materializeEventRows({
116
- ...scope,
117
- lowerBoundMs: scope.cutoffMs - durationMs,
118
- })
119
- ).filter(
120
- (row) =>
121
- row.received_at_ms >= scope.cutoffMs - durationMs &&
122
- ACTIVE_BUNDLE_EVENT_TYPES.includes(row.type),
123
- );
124
- };
125
-
126
- const startOfUtcHour = (value: number): number => {
127
- const date = new Date(value);
128
- return Date.UTC(
129
- date.getUTCFullYear(),
130
- date.getUTCMonth(),
131
- date.getUTCDate(),
132
- date.getUTCHours(),
133
- );
134
- };
135
-
136
- const startOfUtcDay = (value: number): number => {
137
- const date = new Date(value);
138
- return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
139
- };
140
-
141
- export const getWindowRange = (
142
- window: Exclude<BundleEventAnalyticsWindow, "all">,
143
- now: number,
144
- ): { readonly sizeMs: number; readonly rangeStart: number } => {
145
- if (window === "24h") {
146
- return {
147
- sizeMs: 60 * 60 * 1_000,
148
- rangeStart: startOfUtcHour(now) - 23 * 60 * 60 * 1_000,
149
- };
150
- }
151
- const days = window === "7d" ? 7 : 30;
152
- return {
153
- sizeMs: 24 * 60 * 60 * 1_000,
154
- rangeStart: startOfUtcDay(now) - (days - 1) * 24 * 60 * 60 * 1_000,
155
- };
156
- };
157
-
158
- export const materializeRowsForWindow = async (
159
- scope: AnalyticsScanScope,
160
- window: BundleEventAnalyticsWindow,
161
- ): Promise<readonly BundleEventPersistenceRow[]> => {
162
- if (window === "all") return materializeEventRows(scope);
163
- const range = getWindowRange(window, scope.cutoffMs);
164
- const rows = await materializeEventRows({
165
- ...scope,
166
- lowerBoundMs: range.rangeStart,
167
- });
168
- return rows.filter(
169
- ({ received_at_ms }) => received_at_ms >= range.rangeStart,
170
- );
171
- };
172
-
173
- const bucketStart = (receivedAtMs: number, sizeMs: number): number =>
174
- sizeMs === 60 * 60 * 1_000
175
- ? startOfUtcHour(receivedAtMs)
176
- : startOfUtcDay(receivedAtMs);
177
-
178
- const createSeries = (request: EventActivityRequest) => {
179
- const range =
180
- request.window === "all"
181
- ? undefined
182
- : getWindowRange(request.window, request.cutoffMs);
183
- const sizeMs = range?.sizeMs ?? 24 * 60 * 60 * 1_000;
184
- const installIdsByBucket = new Map<number, Set<string>>();
185
- let oldestMs = request.cutoffMs;
186
- for (const row of request.rows) {
187
- oldestMs = Math.min(oldestMs, row.received_at_ms);
188
- const start = bucketStart(row.received_at_ms, sizeMs);
189
- const installIds = installIdsByBucket.get(start) ?? new Set<string>();
190
- installIds.add(row.install_id);
191
- installIdsByBucket.set(start, installIds);
192
- }
193
- const first = range?.rangeStart ?? startOfUtcDay(oldestMs);
194
- const last = bucketStart(request.cutoffMs, sizeMs);
195
- return Array.from(
196
- { length: Math.floor((last - first) / sizeMs) + 1 },
197
- (_, index) => {
198
- const start = first + index * sizeMs;
199
- return {
200
- bucketStartMs: start,
201
- value: installIdsByBucket.get(start)?.size ?? 0,
202
- };
203
- },
204
- );
205
- };
206
-
207
- export const collectEventActivity = (request: EventActivityRequest) => {
208
- const range =
209
- request.window === "all"
210
- ? undefined
211
- : getWindowRange(request.window, request.cutoffMs);
212
- const rows =
213
- range === undefined
214
- ? request.rows
215
- : request.rows.filter(
216
- ({ received_at_ms }) =>
217
- received_at_ms >= range.rangeStart &&
218
- received_at_ms < request.cutoffMs,
219
- );
220
- const installs = new Set<string>();
221
- const installsByCohort = new Map<string, Set<string>>();
222
- for (const row of rows) {
223
- installs.add(row.install_id);
224
- const cohort = installsByCohort.get(row.cohort) ?? new Set<string>();
225
- cohort.add(row.install_id);
226
- installsByCohort.set(row.cohort, cohort);
227
- }
228
- return {
229
- summary: installs.size,
230
- cohorts: [...installsByCohort]
231
- .sort(([left], [right]) => compareCodePoints(left, right))
232
- .map(([cohort, installIds]) => ({ cohort, value: installIds.size })),
233
- series: createSeries({ ...request, rows }),
234
- };
235
- };
@@ -1,139 +0,0 @@
1
- export type CreateBundleEventRequestBase = {
2
- readonly installId: string;
3
- readonly toBundleId: string;
4
- readonly userId?: string;
5
- readonly username?: string;
6
- readonly platform: "ios" | "android";
7
- readonly appVersion: string;
8
- readonly channel: string;
9
- readonly cohort: string;
10
- readonly fingerprintHash: string | null;
11
- readonly sdkVersion?: string | null;
12
- readonly fromReleaseId: string | null;
13
- readonly toReleaseId: string | null;
14
- };
15
-
16
- export type CreateBundleEventRequest =
17
- | (CreateBundleEventRequestBase & {
18
- readonly type: "UPDATE_APPLIED";
19
- readonly fromBundleId: string;
20
- readonly updateStrategy: "fingerprint" | "appVersion";
21
- })
22
- | (CreateBundleEventRequestBase & {
23
- readonly type: "RECOVERED";
24
- readonly fromBundleId: string;
25
- readonly updateStrategy: "fingerprint" | "appVersion";
26
- })
27
- | (CreateBundleEventRequestBase & {
28
- readonly type: "RELEASE_ADOPTED";
29
- readonly fromBundleId: string;
30
- readonly updateStrategy: "fingerprint" | "appVersion";
31
- })
32
- | (CreateBundleEventRequestBase & {
33
- readonly type: "UNCHANGED";
34
- readonly fromBundleId: null;
35
- readonly updateStrategy: null;
36
- });
37
-
38
- export type BundleEventSummary = {
39
- readonly installed: number;
40
- readonly recovered: number;
41
- };
42
-
43
- export type BundleEventSummaryByBundle = BundleEventSummary & {
44
- readonly bundleId: string;
45
- };
46
-
47
- export type BundleEventAnalyticsWindow = "24h" | "7d" | "30d" | "all";
48
- export type ActiveInstallationWindow = "24h" | "7d" | "30d";
49
-
50
- export type InstallationSearchRow = {
51
- readonly installId: string;
52
- readonly username: string | null;
53
- readonly userId: string | null;
54
- readonly lastKnownBundleId: string;
55
- readonly latestStatus:
56
- | "UPDATE_APPLIED"
57
- | "RECOVERED"
58
- | "RELEASE_ADOPTED"
59
- | "UNCHANGED";
60
- readonly platform: "ios" | "android";
61
- readonly appVersion: string;
62
- readonly channel: string;
63
- readonly cohort: string;
64
- readonly receivedAtMs: number;
65
- };
66
-
67
- export type InstallationHistoryRow = {
68
- readonly id: string;
69
- readonly type: "UPDATE_APPLIED" | "RECOVERED";
70
- readonly fromBundleId: string;
71
- readonly toBundleId: string;
72
- readonly username: string | null;
73
- readonly userId: string | null;
74
- readonly platform: "ios" | "android";
75
- readonly appVersion: string;
76
- readonly channel: string;
77
- readonly cohort: string;
78
- readonly receivedAtMs: number;
79
- };
80
-
81
- export type OffsetPaginationResult<TData> = {
82
- readonly data: readonly TData[];
83
- readonly pagination: {
84
- readonly total: number;
85
- readonly limit: number;
86
- readonly offset: number;
87
- };
88
- };
89
-
90
- export type AnalyticsSeriesPoint = {
91
- readonly bucketStartMs: number;
92
- readonly value: number;
93
- };
94
-
95
- export type AnalyticsCohortPoint = {
96
- readonly cohort: string;
97
- readonly value: number;
98
- };
99
-
100
- export type BundleEventAnalyticsResult = {
101
- readonly summary: BundleEventSummary;
102
- readonly series: {
103
- readonly installed: readonly AnalyticsSeriesPoint[];
104
- readonly recovered: readonly AnalyticsSeriesPoint[];
105
- };
106
- readonly cohorts: {
107
- readonly installed: readonly AnalyticsCohortPoint[];
108
- readonly recovered: readonly AnalyticsCohortPoint[];
109
- };
110
- readonly recentEvents: OffsetPaginationResult<InstallationHistoryRow>;
111
- };
112
-
113
- export type BundleEventOverview = {
114
- readonly trackedInstallations: number;
115
- readonly bundles: readonly {
116
- readonly bundleId: string;
117
- readonly installations: number;
118
- }[];
119
- };
120
-
121
- export type ActiveInstallationOverview = {
122
- readonly asOfMs: number;
123
- readonly window: ActiveInstallationWindow;
124
- readonly activeInstallations: number;
125
- readonly series: readonly AnalyticsSeriesPoint[];
126
- readonly bundleSeries: readonly {
127
- readonly bundleId: string;
128
- readonly series: readonly AnalyticsSeriesPoint[];
129
- }[];
130
- readonly bundles: readonly {
131
- readonly bundleId: string;
132
- readonly installations: number;
133
- }[];
134
- };
135
-
136
- export type ActiveInstallationInput = {
137
- readonly window: ActiveInstallationWindow;
138
- readonly userId?: string;
139
- };
@@ -1,18 +0,0 @@
1
- export class AnalyticsScanLimitExceededError extends Error {
2
- constructor(readonly limit: number) {
3
- super(`Analytics event scan exceeded ${limit} rows.`);
4
- this.name = "AnalyticsScanLimitExceededError";
5
- }
6
- }
7
-
8
- export class AnalyticsBadRequestError extends Error {
9
- readonly name = "AnalyticsBadRequestError";
10
- }
11
-
12
- export class AnalyticsPayloadTooLargeError extends Error {
13
- readonly name = "AnalyticsPayloadTooLargeError";
14
-
15
- constructor(readonly maximumBytes: number) {
16
- super(`Event payload exceeds ${maximumBytes} bytes`);
17
- }
18
- }
@@ -1,10 +0,0 @@
1
- import type {
2
- AnalyticsScanCursor,
3
- AnalyticsScanInput,
4
- AnalyticsModel,
5
- BundleEventRow,
6
- } from "@hot-updater/plugin-core";
7
-
8
- export type BundleEventPersistenceRow = BundleEventRow;
9
- export type AnalyticsPersistence = AnalyticsModel;
10
- export type { AnalyticsScanCursor, AnalyticsScanInput };