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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/LICENSE +33 -0
  2. package/README.md +24 -0
  3. package/client/package.json +7 -0
  4. package/dist/client/eventId.cjs +181 -0
  5. package/dist/client/eventId.mjs +179 -0
  6. package/dist/client/index.cjs +234 -0
  7. package/dist/client/index.d.cts +42 -0
  8. package/dist/client/index.d.mts +42 -0
  9. package/dist/client/index.mjs +233 -0
  10. package/dist/client/sender.cjs +103 -0
  11. package/dist/client/sender.mjs +103 -0
  12. package/dist/client/state.cjs +121 -0
  13. package/dist/client/state.mjs +119 -0
  14. package/dist/internal.cjs +9 -0
  15. package/dist/internal.d.cts +5 -0
  16. package/dist/internal.d.mts +5 -0
  17. package/dist/internal.mjs +5 -0
  18. package/dist/server/contract.cjs +193 -0
  19. package/dist/server/contract.mjs +189 -0
  20. package/dist/server/domain.d.cts +149 -0
  21. package/dist/server/domain.d.mts +149 -0
  22. package/dist/server/errors.cjs +15 -0
  23. package/dist/server/errors.d.cts +6 -0
  24. package/dist/server/errors.d.mts +6 -0
  25. package/dist/server/errors.mjs +14 -0
  26. package/dist/server/eventInput.cjs +284 -0
  27. package/dist/server/eventInput.d.cts +6 -0
  28. package/dist/server/eventInput.d.mts +6 -0
  29. package/dist/server/eventInput.mjs +282 -0
  30. package/dist/server/eventRow.cjs +49 -0
  31. package/dist/server/eventRow.d.cts +64 -0
  32. package/dist/server/eventRow.d.mts +64 -0
  33. package/dist/server/eventRow.mjs +47 -0
  34. package/dist/server/index.cjs +128 -0
  35. package/dist/server/index.d.cts +394 -0
  36. package/dist/server/index.d.mts +394 -0
  37. package/dist/server/index.mjs +121 -0
  38. package/dist/server/model.cjs +17 -0
  39. package/dist/server/model.d.cts +10 -0
  40. package/dist/server/model.d.mts +10 -0
  41. package/dist/server/model.mjs +17 -0
  42. package/dist/server/modelTypes.d.cts +176 -0
  43. package/dist/server/modelTypes.d.mts +176 -0
  44. package/dist/server/overview.cjs +109 -0
  45. package/dist/server/overview.d.cts +15 -0
  46. package/dist/server/overview.d.mts +15 -0
  47. package/dist/server/overview.mjs +107 -0
  48. package/dist/server/provider.cjs +375 -0
  49. package/dist/server/provider.d.cts +6 -0
  50. package/dist/server/provider.d.mts +6 -0
  51. package/dist/server/provider.mjs +374 -0
  52. package/dist/server/queryInput.cjs +93 -0
  53. package/dist/server/queryInput.mjs +91 -0
  54. package/dist/server/reads.cjs +681 -0
  55. package/dist/server/reads.d.cts +60 -0
  56. package/dist/server/reads.d.mts +60 -0
  57. package/dist/server/reads.mjs +674 -0
  58. package/dist/server/recordEvent.cjs +319 -0
  59. package/dist/server/recordEvent.d.cts +17 -0
  60. package/dist/server/recordEvent.d.mts +17 -0
  61. package/dist/server/recordEvent.mjs +316 -0
  62. package/dist/server/routes.cjs +96 -0
  63. package/dist/server/routes.d.cts +36 -0
  64. package/dist/server/routes.d.mts +36 -0
  65. package/dist/server/routes.mjs +95 -0
  66. package/dist/server/schema.cjs +489 -0
  67. package/dist/server/schema.d.cts +706 -0
  68. package/dist/server/schema.d.mts +706 -0
  69. package/dist/server/schema.mjs +483 -0
  70. package/dist/server/testing/eventRow.d.mts +64 -0
  71. package/dist/server/testing/expectInsightsIndex.mjs +18 -0
  72. package/dist/server/testing/fixtures.d.mts +8 -0
  73. package/dist/server/testing/fixtures.mjs +25 -0
  74. package/dist/server/testing/index.d.mts +24 -0
  75. package/dist/server/testing/index.mjs +25 -0
  76. package/dist/server/testing/insightsModelTests.mjs +714 -0
  77. package/dist/server/testing/modelTypes.d.mts +176 -0
  78. package/dist/server/testing/setupInsightsHttpTestSuite.d.mts +11 -0
  79. package/dist/server/testing/setupInsightsHttpTestSuite.mjs +107 -0
  80. package/dist/server/testing/setupInsightsModelTestSuite.d.mts +11 -0
  81. package/dist/server/testing/setupInsightsModelTestSuite.mjs +31 -0
  82. package/dist/server/testing/types.d.mts +26 -0
  83. package/dist/server/types.d.cts +33 -0
  84. package/dist/server/types.d.mts +33 -0
  85. package/package.json +68 -0
@@ -0,0 +1,176 @@
1
+ import { BundleEventRow } from "./eventRow.cjs";
2
+ //#region src/server/modelTypes.d.ts
3
+ interface InsightsEventCursor {
4
+ readonly receivedAtMs: number;
5
+ readonly id: string;
6
+ }
7
+ interface InsightsScope {
8
+ readonly platform: "ios" | "android";
9
+ readonly channel: string;
10
+ }
11
+ /**
12
+ * Raw event predicates; recovery is attributed to the source bundle. An
13
+ * UNCHANGED report is kept as no event, so no filter names it.
14
+ */
15
+ type InsightsBundleEventFilter = InsightsScope & ({
16
+ readonly type: "RECOVERED";
17
+ readonly fromBundleId: string;
18
+ } | {
19
+ readonly type: "UPDATE_DOWNLOADED" | "UPDATE_APPLIED" | "UPDATE_FAILED";
20
+ readonly toBundleId: string;
21
+ });
22
+ type InsightsEventFilter = {
23
+ readonly kind: "all";
24
+ } | {
25
+ readonly kind: "installationMovement";
26
+ readonly installId: string;
27
+ } | ({
28
+ readonly kind: "bundle";
29
+ } & InsightsBundleEventFilter);
30
+ interface InsightsRecordEventInput {
31
+ readonly event: BundleEventRow;
32
+ }
33
+ interface InsightsListEventsInput {
34
+ readonly filter: InsightsEventFilter;
35
+ readonly sinceMs?: number;
36
+ readonly beforeReceivedAtMs: number;
37
+ readonly after?: InsightsEventCursor;
38
+ readonly limit: number;
39
+ }
40
+ type InsightsFindLatestEventsInput = {
41
+ readonly installId: string;
42
+ } | {
43
+ readonly userId: string;
44
+ readonly afterInstallId?: string;
45
+ readonly limit: number;
46
+ };
47
+ interface InsightsCountLatestEventsInput extends InsightsScope {
48
+ /** A UTC day's start: latest events are counted by the day they fall in. */
49
+ readonly sinceMs: number;
50
+ /** Optional OR of one or two fixed predicates; count a matching event once. */
51
+ readonly bundle?: readonly {
52
+ readonly field: "from_bundle_id" | "to_bundle_id";
53
+ readonly value: string;
54
+ readonly types: readonly BundleEventRow["type"][];
55
+ }[];
56
+ }
57
+ interface InsightsCountEventsInput {
58
+ readonly filter: InsightsBundleEventFilter;
59
+ readonly sinceMs: number;
60
+ readonly beforeReceivedAtMs: number;
61
+ }
62
+ interface ReleaseReference {
63
+ readonly releaseId: string;
64
+ readonly platform: "ios" | "android";
65
+ readonly channel: string;
66
+ }
67
+ interface InsightsTimeRange {
68
+ /** Inclusive UTC Unix timestamp in milliseconds. */
69
+ readonly start: number;
70
+ /** Exclusive UTC Unix timestamp in milliseconds. */
71
+ readonly end: number;
72
+ }
73
+ type InsightsCoverage = {
74
+ readonly kind: "complete";
75
+ readonly sinceMs: number;
76
+ } | {
77
+ readonly kind: "partial";
78
+ readonly sinceMs: number | null;
79
+ };
80
+ interface ReleaseActivityMetrics {
81
+ readonly downloads: number;
82
+ readonly launches: number;
83
+ readonly failedLaunches: number;
84
+ /** Omitted for lifetime-only bundle-row reads. */
85
+ readonly uniqueUsers?: number;
86
+ /** Daily points, present only for period reads. */
87
+ readonly series?: readonly {
88
+ readonly startMs: number;
89
+ readonly launches: number;
90
+ readonly failedLaunches: number;
91
+ }[];
92
+ }
93
+ type InsightsGetReleaseActivityInput = {
94
+ readonly releases: readonly ReleaseReference[];
95
+ readonly timeRange?: InsightsTimeRange;
96
+ readonly scope?: never;
97
+ } | {
98
+ readonly scope: InsightsScope;
99
+ readonly timeRange: InsightsTimeRange;
100
+ readonly releases?: never;
101
+ };
102
+ interface InsightsGetReleaseActivityResult {
103
+ readonly coverage: InsightsCoverage;
104
+ readonly data: readonly {
105
+ readonly release?: ReleaseReference;
106
+ readonly scope?: InsightsScope;
107
+ readonly metrics: ReleaseActivityMetrics;
108
+ }[];
109
+ readonly measuredAtMs: number;
110
+ }
111
+ interface InsightsGetAppUsageInput {
112
+ readonly channel: string;
113
+ readonly platform: "all" | "ios" | "android";
114
+ readonly appVersion?: string;
115
+ readonly timeRange: InsightsTimeRange;
116
+ readonly intervalMs: number;
117
+ }
118
+ interface InsightsGetAppUsageResult {
119
+ readonly coverage: InsightsCoverage;
120
+ readonly activeInstallations: number;
121
+ readonly points: readonly {
122
+ readonly startMs: number;
123
+ readonly installations: number;
124
+ }[];
125
+ readonly appVersions: readonly string[];
126
+ readonly versions: readonly {
127
+ readonly name: string;
128
+ readonly installations: number;
129
+ }[];
130
+ readonly platforms: readonly {
131
+ readonly name: string;
132
+ readonly installations: number;
133
+ }[];
134
+ readonly bundleDistribution: readonly {
135
+ readonly appVersion: string;
136
+ readonly platform: "ios" | "android";
137
+ readonly releaseId: string | null;
138
+ readonly installations: number;
139
+ }[];
140
+ readonly measuredAtMs: number;
141
+ }
142
+ interface InsightsModel {
143
+ /**
144
+ * Persist the immutable event once. A private latest-event index, if used,
145
+ * advances atomically for a greater (received_at_ms, id). Duplicate event
146
+ * IDs are complete no-ops, whichever installation sends them.
147
+ * Retry the identical prepared input after an ambiguous commit outcome.
148
+ */
149
+ recordEvent(input: InsightsRecordEventInput): Promise<void>;
150
+ /**
151
+ * Descending (received_at_ms, id), in [sinceMs ?? 0, beforeReceivedAtMs).
152
+ * Apply filters and the exclusive cursor before limit (1..101). Return the
153
+ * complete matching prefix; native continuation pages must not truncate it.
154
+ * A global or bundle list may reject a range longer than 90 × 24 hours;
155
+ * the server asks for older events with an earlier range instead.
156
+ */
157
+ listEvents(input: InsightsListEventsInput): Promise<readonly BundleEventRow[]>;
158
+ /**
159
+ * Choose each installation's greatest (received_at_ms, id) before filtering.
160
+ * Exact installation lookup returns zero or one canonical event. User lookup
161
+ * returns current membership ordered by UTF-8 installation ID, exclusive
162
+ * afterInstallId and limit 1..101. Check lagging index candidates against
163
+ * canonical state; newly indexed associations may temporarily be absent.
164
+ */
165
+ findLatestEvents(input: InsightsFindLatestEventsInput): Promise<readonly BundleEventRow[]>;
166
+ /** Count latest events once per installation, then apply the supplied predicates. */
167
+ countLatestEvents(input: InsightsCountLatestEventsInput): Promise<number>;
168
+ /** Count accepted reports in [sinceMs, beforeReceivedAtMs), across all pages. */
169
+ countEvents(input: InsightsCountEventsInput): Promise<number>;
170
+ /** Read maintained release/scope summaries without scanning raw events. */
171
+ getReleaseActivity(input: InsightsGetReleaseActivityInput): Promise<InsightsGetReleaseActivityResult>;
172
+ /** Read maintained App usage summaries and latest-report distribution. */
173
+ getAppUsage(input: InsightsGetAppUsageInput): Promise<InsightsGetAppUsageResult>;
174
+ }
175
+ //#endregion
176
+ export { InsightsBundleEventFilter, InsightsCountEventsInput, InsightsCountLatestEventsInput, InsightsCoverage, InsightsEventCursor, InsightsEventFilter, InsightsFindLatestEventsInput, InsightsGetAppUsageInput, InsightsGetAppUsageResult, InsightsGetReleaseActivityInput, InsightsGetReleaseActivityResult, InsightsListEventsInput, InsightsModel, InsightsRecordEventInput, InsightsScope, InsightsTimeRange, ReleaseActivityMetrics, ReleaseReference };
@@ -0,0 +1,176 @@
1
+ import { BundleEventRow } from "./eventRow.mjs";
2
+ //#region src/server/modelTypes.d.ts
3
+ interface InsightsEventCursor {
4
+ readonly receivedAtMs: number;
5
+ readonly id: string;
6
+ }
7
+ interface InsightsScope {
8
+ readonly platform: "ios" | "android";
9
+ readonly channel: string;
10
+ }
11
+ /**
12
+ * Raw event predicates; recovery is attributed to the source bundle. An
13
+ * UNCHANGED report is kept as no event, so no filter names it.
14
+ */
15
+ type InsightsBundleEventFilter = InsightsScope & ({
16
+ readonly type: "RECOVERED";
17
+ readonly fromBundleId: string;
18
+ } | {
19
+ readonly type: "UPDATE_DOWNLOADED" | "UPDATE_APPLIED" | "UPDATE_FAILED";
20
+ readonly toBundleId: string;
21
+ });
22
+ type InsightsEventFilter = {
23
+ readonly kind: "all";
24
+ } | {
25
+ readonly kind: "installationMovement";
26
+ readonly installId: string;
27
+ } | ({
28
+ readonly kind: "bundle";
29
+ } & InsightsBundleEventFilter);
30
+ interface InsightsRecordEventInput {
31
+ readonly event: BundleEventRow;
32
+ }
33
+ interface InsightsListEventsInput {
34
+ readonly filter: InsightsEventFilter;
35
+ readonly sinceMs?: number;
36
+ readonly beforeReceivedAtMs: number;
37
+ readonly after?: InsightsEventCursor;
38
+ readonly limit: number;
39
+ }
40
+ type InsightsFindLatestEventsInput = {
41
+ readonly installId: string;
42
+ } | {
43
+ readonly userId: string;
44
+ readonly afterInstallId?: string;
45
+ readonly limit: number;
46
+ };
47
+ interface InsightsCountLatestEventsInput extends InsightsScope {
48
+ /** A UTC day's start: latest events are counted by the day they fall in. */
49
+ readonly sinceMs: number;
50
+ /** Optional OR of one or two fixed predicates; count a matching event once. */
51
+ readonly bundle?: readonly {
52
+ readonly field: "from_bundle_id" | "to_bundle_id";
53
+ readonly value: string;
54
+ readonly types: readonly BundleEventRow["type"][];
55
+ }[];
56
+ }
57
+ interface InsightsCountEventsInput {
58
+ readonly filter: InsightsBundleEventFilter;
59
+ readonly sinceMs: number;
60
+ readonly beforeReceivedAtMs: number;
61
+ }
62
+ interface ReleaseReference {
63
+ readonly releaseId: string;
64
+ readonly platform: "ios" | "android";
65
+ readonly channel: string;
66
+ }
67
+ interface InsightsTimeRange {
68
+ /** Inclusive UTC Unix timestamp in milliseconds. */
69
+ readonly start: number;
70
+ /** Exclusive UTC Unix timestamp in milliseconds. */
71
+ readonly end: number;
72
+ }
73
+ type InsightsCoverage = {
74
+ readonly kind: "complete";
75
+ readonly sinceMs: number;
76
+ } | {
77
+ readonly kind: "partial";
78
+ readonly sinceMs: number | null;
79
+ };
80
+ interface ReleaseActivityMetrics {
81
+ readonly downloads: number;
82
+ readonly launches: number;
83
+ readonly failedLaunches: number;
84
+ /** Omitted for lifetime-only bundle-row reads. */
85
+ readonly uniqueUsers?: number;
86
+ /** Daily points, present only for period reads. */
87
+ readonly series?: readonly {
88
+ readonly startMs: number;
89
+ readonly launches: number;
90
+ readonly failedLaunches: number;
91
+ }[];
92
+ }
93
+ type InsightsGetReleaseActivityInput = {
94
+ readonly releases: readonly ReleaseReference[];
95
+ readonly timeRange?: InsightsTimeRange;
96
+ readonly scope?: never;
97
+ } | {
98
+ readonly scope: InsightsScope;
99
+ readonly timeRange: InsightsTimeRange;
100
+ readonly releases?: never;
101
+ };
102
+ interface InsightsGetReleaseActivityResult {
103
+ readonly coverage: InsightsCoverage;
104
+ readonly data: readonly {
105
+ readonly release?: ReleaseReference;
106
+ readonly scope?: InsightsScope;
107
+ readonly metrics: ReleaseActivityMetrics;
108
+ }[];
109
+ readonly measuredAtMs: number;
110
+ }
111
+ interface InsightsGetAppUsageInput {
112
+ readonly channel: string;
113
+ readonly platform: "all" | "ios" | "android";
114
+ readonly appVersion?: string;
115
+ readonly timeRange: InsightsTimeRange;
116
+ readonly intervalMs: number;
117
+ }
118
+ interface InsightsGetAppUsageResult {
119
+ readonly coverage: InsightsCoverage;
120
+ readonly activeInstallations: number;
121
+ readonly points: readonly {
122
+ readonly startMs: number;
123
+ readonly installations: number;
124
+ }[];
125
+ readonly appVersions: readonly string[];
126
+ readonly versions: readonly {
127
+ readonly name: string;
128
+ readonly installations: number;
129
+ }[];
130
+ readonly platforms: readonly {
131
+ readonly name: string;
132
+ readonly installations: number;
133
+ }[];
134
+ readonly bundleDistribution: readonly {
135
+ readonly appVersion: string;
136
+ readonly platform: "ios" | "android";
137
+ readonly releaseId: string | null;
138
+ readonly installations: number;
139
+ }[];
140
+ readonly measuredAtMs: number;
141
+ }
142
+ interface InsightsModel {
143
+ /**
144
+ * Persist the immutable event once. A private latest-event index, if used,
145
+ * advances atomically for a greater (received_at_ms, id). Duplicate event
146
+ * IDs are complete no-ops, whichever installation sends them.
147
+ * Retry the identical prepared input after an ambiguous commit outcome.
148
+ */
149
+ recordEvent(input: InsightsRecordEventInput): Promise<void>;
150
+ /**
151
+ * Descending (received_at_ms, id), in [sinceMs ?? 0, beforeReceivedAtMs).
152
+ * Apply filters and the exclusive cursor before limit (1..101). Return the
153
+ * complete matching prefix; native continuation pages must not truncate it.
154
+ * A global or bundle list may reject a range longer than 90 × 24 hours;
155
+ * the server asks for older events with an earlier range instead.
156
+ */
157
+ listEvents(input: InsightsListEventsInput): Promise<readonly BundleEventRow[]>;
158
+ /**
159
+ * Choose each installation's greatest (received_at_ms, id) before filtering.
160
+ * Exact installation lookup returns zero or one canonical event. User lookup
161
+ * returns current membership ordered by UTF-8 installation ID, exclusive
162
+ * afterInstallId and limit 1..101. Check lagging index candidates against
163
+ * canonical state; newly indexed associations may temporarily be absent.
164
+ */
165
+ findLatestEvents(input: InsightsFindLatestEventsInput): Promise<readonly BundleEventRow[]>;
166
+ /** Count latest events once per installation, then apply the supplied predicates. */
167
+ countLatestEvents(input: InsightsCountLatestEventsInput): Promise<number>;
168
+ /** Count accepted reports in [sinceMs, beforeReceivedAtMs), across all pages. */
169
+ countEvents(input: InsightsCountEventsInput): Promise<number>;
170
+ /** Read maintained release/scope summaries without scanning raw events. */
171
+ getReleaseActivity(input: InsightsGetReleaseActivityInput): Promise<InsightsGetReleaseActivityResult>;
172
+ /** Read maintained App usage summaries and latest-report distribution. */
173
+ getAppUsage(input: InsightsGetAppUsageInput): Promise<InsightsGetAppUsageResult>;
174
+ }
175
+ //#endregion
176
+ export { InsightsBundleEventFilter, InsightsCountEventsInput, InsightsCountLatestEventsInput, InsightsCoverage, InsightsEventCursor, InsightsEventFilter, InsightsFindLatestEventsInput, InsightsGetAppUsageInput, InsightsGetAppUsageResult, InsightsGetReleaseActivityInput, InsightsGetReleaseActivityResult, InsightsListEventsInput, InsightsModel, InsightsRecordEventInput, InsightsScope, InsightsTimeRange, ReleaseActivityMetrics, ReleaseReference };
@@ -0,0 +1,109 @@
1
+ //#region src/server/overview.ts
2
+ const encodeIdentity = (identity) => [
3
+ identity.scopeKind,
4
+ identity.releaseKind,
5
+ identity.releaseId,
6
+ identity.channel,
7
+ identity.platform,
8
+ identity.appVersionKind,
9
+ identity.appVersion,
10
+ identity.periodKind,
11
+ String(identity.bucketStartMs)
12
+ ].map((value) => `${new TextEncoder().encode(value).length}:${value}`).join("");
13
+ const fnv = (value, seed) => {
14
+ let result = seed;
15
+ for (const byte of new TextEncoder().encode(value)) {
16
+ result ^= BigInt(byte);
17
+ result = BigInt.asUintN(64, result * 1099511628211n);
18
+ }
19
+ return result.toString(16).padStart(16, "0");
20
+ };
21
+ /** A 128-bit hex key for a string: two FNV-1a 64 hashes with different seeds. */
22
+ const insightsKey = (value) => `${fnv(value, 14695981039346656037n)}${fnv(value, 9521211207457086692n)}`;
23
+ const insightsOverviewId = (identity) => insightsKey(encodeIdentity(identity));
24
+ const hour = (value) => Math.floor(value / 36e5) * 36e5;
25
+ const insightsOverviewDeltas = (event) => {
26
+ const bucketStartMs = hour(event.received_at_ms);
27
+ const counters = /* @__PURE__ */ new Map();
28
+ const append = (identity, values) => {
29
+ const key = encodeIdentity(identity);
30
+ const previous = counters.get(key);
31
+ counters.set(key, {
32
+ identity,
33
+ downloads: (previous?.downloads ?? 0) + values.downloads,
34
+ launches: (previous?.launches ?? 0) + values.launches,
35
+ failedLaunches: (previous?.failedLaunches ?? 0) + values.failedLaunches,
36
+ launchIdentity: previous?.launchIdentity ?? values.launchIdentity,
37
+ activityIdentity: previous?.activityIdentity ?? values.activityIdentity
38
+ });
39
+ };
40
+ const metric = (releaseId, values) => {
41
+ const launchIdentity = values.launches > 0 ? event.install_id : void 0;
42
+ if (releaseId !== null) for (const [periodKind, start] of [["lifetime", 0], ["hour", bucketStartMs]]) append({
43
+ scopeKind: "release",
44
+ releaseKind: "specific",
45
+ releaseId,
46
+ channel: event.channel,
47
+ platform: event.platform,
48
+ appVersionKind: "all",
49
+ appVersion: "",
50
+ periodKind,
51
+ bucketStartMs: start
52
+ }, {
53
+ ...values,
54
+ ...periodKind === "hour" ? { launchIdentity } : {}
55
+ });
56
+ append({
57
+ scopeKind: "channel",
58
+ releaseKind: "all",
59
+ releaseId: "",
60
+ channel: event.channel,
61
+ platform: event.platform,
62
+ appVersionKind: "all",
63
+ appVersion: "",
64
+ periodKind: "hour",
65
+ bucketStartMs
66
+ }, {
67
+ ...values,
68
+ launchIdentity
69
+ });
70
+ };
71
+ if (event.type === "UPDATE_DOWNLOADED") metric(event.to_release_id, {
72
+ downloads: 1,
73
+ launches: 0,
74
+ failedLaunches: 0
75
+ });
76
+ else if (event.type !== "UPDATE_FAILED") {
77
+ metric(event.to_release_id, {
78
+ downloads: 0,
79
+ launches: 1,
80
+ failedLaunches: 0
81
+ });
82
+ if (event.type === "RECOVERED") metric(event.from_release_id, {
83
+ downloads: 0,
84
+ launches: 0,
85
+ failedLaunches: 1
86
+ });
87
+ }
88
+ for (const platform of [event.platform, "all"]) for (const appVersionKind of ["specific", "all"]) append({
89
+ scopeKind: "usage",
90
+ releaseKind: "all",
91
+ releaseId: "",
92
+ channel: event.channel,
93
+ platform,
94
+ appVersionKind,
95
+ appVersion: appVersionKind === "specific" ? event.app_version : "",
96
+ periodKind: "hour",
97
+ bucketStartMs
98
+ }, {
99
+ downloads: 0,
100
+ launches: 0,
101
+ failedLaunches: 0,
102
+ activityIdentity: event.install_id
103
+ });
104
+ return [...counters.values()];
105
+ };
106
+ //#endregion
107
+ exports.insightsKey = insightsKey;
108
+ exports.insightsOverviewDeltas = insightsOverviewDeltas;
109
+ exports.insightsOverviewId = insightsOverviewId;
@@ -0,0 +1,15 @@
1
+ import "./eventRow.cjs";
2
+ //#region src/server/overview.d.ts
3
+ 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
+ //#endregion
15
+ export { InsightsOverviewIdentity };
@@ -0,0 +1,15 @@
1
+ import "./eventRow.mjs";
2
+ //#region src/server/overview.d.ts
3
+ 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
+ //#endregion
15
+ export { InsightsOverviewIdentity };
@@ -0,0 +1,107 @@
1
+ //#region src/server/overview.ts
2
+ const encodeIdentity = (identity) => [
3
+ identity.scopeKind,
4
+ identity.releaseKind,
5
+ identity.releaseId,
6
+ identity.channel,
7
+ identity.platform,
8
+ identity.appVersionKind,
9
+ identity.appVersion,
10
+ identity.periodKind,
11
+ String(identity.bucketStartMs)
12
+ ].map((value) => `${new TextEncoder().encode(value).length}:${value}`).join("");
13
+ const fnv = (value, seed) => {
14
+ let result = seed;
15
+ for (const byte of new TextEncoder().encode(value)) {
16
+ result ^= BigInt(byte);
17
+ result = BigInt.asUintN(64, result * 1099511628211n);
18
+ }
19
+ return result.toString(16).padStart(16, "0");
20
+ };
21
+ /** A 128-bit hex key for a string: two FNV-1a 64 hashes with different seeds. */
22
+ const insightsKey = (value) => `${fnv(value, 14695981039346656037n)}${fnv(value, 9521211207457086692n)}`;
23
+ const insightsOverviewId = (identity) => insightsKey(encodeIdentity(identity));
24
+ const hour = (value) => Math.floor(value / 36e5) * 36e5;
25
+ const insightsOverviewDeltas = (event) => {
26
+ const bucketStartMs = hour(event.received_at_ms);
27
+ const counters = /* @__PURE__ */ new Map();
28
+ const append = (identity, values) => {
29
+ const key = encodeIdentity(identity);
30
+ const previous = counters.get(key);
31
+ counters.set(key, {
32
+ identity,
33
+ downloads: (previous?.downloads ?? 0) + values.downloads,
34
+ launches: (previous?.launches ?? 0) + values.launches,
35
+ failedLaunches: (previous?.failedLaunches ?? 0) + values.failedLaunches,
36
+ launchIdentity: previous?.launchIdentity ?? values.launchIdentity,
37
+ activityIdentity: previous?.activityIdentity ?? values.activityIdentity
38
+ });
39
+ };
40
+ const metric = (releaseId, values) => {
41
+ const launchIdentity = values.launches > 0 ? event.install_id : void 0;
42
+ if (releaseId !== null) for (const [periodKind, start] of [["lifetime", 0], ["hour", bucketStartMs]]) append({
43
+ scopeKind: "release",
44
+ releaseKind: "specific",
45
+ releaseId,
46
+ channel: event.channel,
47
+ platform: event.platform,
48
+ appVersionKind: "all",
49
+ appVersion: "",
50
+ periodKind,
51
+ bucketStartMs: start
52
+ }, {
53
+ ...values,
54
+ ...periodKind === "hour" ? { launchIdentity } : {}
55
+ });
56
+ append({
57
+ scopeKind: "channel",
58
+ releaseKind: "all",
59
+ releaseId: "",
60
+ channel: event.channel,
61
+ platform: event.platform,
62
+ appVersionKind: "all",
63
+ appVersion: "",
64
+ periodKind: "hour",
65
+ bucketStartMs
66
+ }, {
67
+ ...values,
68
+ launchIdentity
69
+ });
70
+ };
71
+ if (event.type === "UPDATE_DOWNLOADED") metric(event.to_release_id, {
72
+ downloads: 1,
73
+ launches: 0,
74
+ failedLaunches: 0
75
+ });
76
+ else if (event.type !== "UPDATE_FAILED") {
77
+ metric(event.to_release_id, {
78
+ downloads: 0,
79
+ launches: 1,
80
+ failedLaunches: 0
81
+ });
82
+ if (event.type === "RECOVERED") metric(event.from_release_id, {
83
+ downloads: 0,
84
+ launches: 0,
85
+ failedLaunches: 1
86
+ });
87
+ }
88
+ for (const platform of [event.platform, "all"]) for (const appVersionKind of ["specific", "all"]) append({
89
+ scopeKind: "usage",
90
+ releaseKind: "all",
91
+ releaseId: "",
92
+ channel: event.channel,
93
+ platform,
94
+ appVersionKind,
95
+ appVersion: appVersionKind === "specific" ? event.app_version : "",
96
+ periodKind: "hour",
97
+ bucketStartMs
98
+ }, {
99
+ downloads: 0,
100
+ launches: 0,
101
+ failedLaunches: 0,
102
+ activityIdentity: event.install_id
103
+ });
104
+ return [...counters.values()];
105
+ };
106
+ //#endregion
107
+ export { insightsKey, insightsOverviewDeltas, insightsOverviewId };