@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.
- package/LICENSE +33 -0
- package/README.md +24 -0
- package/client/package.json +7 -0
- package/dist/client/eventId.cjs +181 -0
- package/dist/client/eventId.mjs +179 -0
- package/dist/client/index.cjs +234 -0
- package/dist/client/index.d.cts +42 -0
- package/dist/client/index.d.mts +42 -0
- package/dist/client/index.mjs +233 -0
- package/dist/client/sender.cjs +103 -0
- package/dist/client/sender.mjs +103 -0
- package/dist/client/state.cjs +121 -0
- package/dist/client/state.mjs +119 -0
- package/dist/internal.cjs +9 -0
- package/dist/internal.d.cts +5 -0
- package/dist/internal.d.mts +5 -0
- package/dist/internal.mjs +5 -0
- package/dist/server/contract.cjs +193 -0
- package/dist/server/contract.mjs +189 -0
- package/dist/server/domain.d.cts +149 -0
- package/dist/server/domain.d.mts +149 -0
- package/dist/server/errors.cjs +15 -0
- package/dist/server/errors.d.cts +6 -0
- package/dist/server/errors.d.mts +6 -0
- package/dist/server/errors.mjs +14 -0
- package/dist/server/eventInput.cjs +284 -0
- package/dist/server/eventInput.d.cts +6 -0
- package/dist/server/eventInput.d.mts +6 -0
- package/dist/server/eventInput.mjs +282 -0
- package/dist/server/eventRow.cjs +49 -0
- package/dist/server/eventRow.d.cts +64 -0
- package/dist/server/eventRow.d.mts +64 -0
- package/dist/server/eventRow.mjs +47 -0
- package/dist/server/index.cjs +128 -0
- package/dist/server/index.d.cts +394 -0
- package/dist/server/index.d.mts +394 -0
- package/dist/server/index.mjs +121 -0
- package/dist/server/model.cjs +17 -0
- package/dist/server/model.d.cts +10 -0
- package/dist/server/model.d.mts +10 -0
- package/dist/server/model.mjs +17 -0
- package/dist/server/modelTypes.d.cts +176 -0
- package/dist/server/modelTypes.d.mts +176 -0
- package/dist/server/overview.cjs +109 -0
- package/dist/server/overview.d.cts +15 -0
- package/dist/server/overview.d.mts +15 -0
- package/dist/server/overview.mjs +107 -0
- package/dist/server/provider.cjs +375 -0
- package/dist/server/provider.d.cts +6 -0
- package/dist/server/provider.d.mts +6 -0
- package/dist/server/provider.mjs +374 -0
- package/dist/server/queryInput.cjs +93 -0
- package/dist/server/queryInput.mjs +91 -0
- package/dist/server/reads.cjs +681 -0
- package/dist/server/reads.d.cts +60 -0
- package/dist/server/reads.d.mts +60 -0
- package/dist/server/reads.mjs +674 -0
- package/dist/server/recordEvent.cjs +319 -0
- package/dist/server/recordEvent.d.cts +17 -0
- package/dist/server/recordEvent.d.mts +17 -0
- package/dist/server/recordEvent.mjs +316 -0
- package/dist/server/routes.cjs +96 -0
- package/dist/server/routes.d.cts +36 -0
- package/dist/server/routes.d.mts +36 -0
- package/dist/server/routes.mjs +95 -0
- package/dist/server/schema.cjs +489 -0
- package/dist/server/schema.d.cts +706 -0
- package/dist/server/schema.d.mts +706 -0
- package/dist/server/schema.mjs +483 -0
- package/dist/server/testing/eventRow.d.mts +64 -0
- package/dist/server/testing/expectInsightsIndex.mjs +18 -0
- package/dist/server/testing/fixtures.d.mts +8 -0
- package/dist/server/testing/fixtures.mjs +25 -0
- package/dist/server/testing/index.d.mts +24 -0
- package/dist/server/testing/index.mjs +25 -0
- package/dist/server/testing/insightsModelTests.mjs +714 -0
- package/dist/server/testing/modelTypes.d.mts +176 -0
- package/dist/server/testing/setupInsightsHttpTestSuite.d.mts +11 -0
- package/dist/server/testing/setupInsightsHttpTestSuite.mjs +107 -0
- package/dist/server/testing/setupInsightsModelTestSuite.d.mts +11 -0
- package/dist/server/testing/setupInsightsModelTestSuite.mjs +31 -0
- package/dist/server/testing/types.d.mts +26 -0
- package/dist/server/types.d.cts +33 -0
- package/dist/server/types.d.mts +33 -0
- package/package.json +68 -0
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
import "./provider.mjs";
|
|
2
|
+
import { DAILY_EVENTS, DAY_MS, HOUR_MS } from "./schema.mjs";
|
|
3
|
+
import { PAIR_FIELD, bundlePairKey, insightsIdentity } from "./recordEvent.mjs";
|
|
4
|
+
import { DatabaseAdapterInputError } from "@hot-updater/plugin-core";
|
|
5
|
+
import { compareUtf8, countDistinct, mergeDistinct } from "@hot-updater/plugin-core/internal";
|
|
6
|
+
//#region src/server/reads.ts
|
|
7
|
+
const PAGE = 500;
|
|
8
|
+
const hourFloor = (ms) => ms - ms % HOUR_MS;
|
|
9
|
+
const hourCeil = (ms) => hourFloor(ms + HOUR_MS - 1);
|
|
10
|
+
const dayFloor = (ms) => ms - ms % DAY_MS;
|
|
11
|
+
const dayCeil = (ms) => dayFloor(ms + DAY_MS - 1);
|
|
12
|
+
/** Every page of one read, in order. */
|
|
13
|
+
const drain = async (read) => {
|
|
14
|
+
const rows = [];
|
|
15
|
+
let cursor;
|
|
16
|
+
do {
|
|
17
|
+
const page = await read(cursor === void 0 ? {} : { cursor });
|
|
18
|
+
rows.push(...page.rows);
|
|
19
|
+
cursor = page.next;
|
|
20
|
+
} while (cursor !== void 0);
|
|
21
|
+
return rows;
|
|
22
|
+
};
|
|
23
|
+
const EVENT_FIELDS = [
|
|
24
|
+
"id",
|
|
25
|
+
"type",
|
|
26
|
+
"install_id",
|
|
27
|
+
"user_id",
|
|
28
|
+
"from_release_id",
|
|
29
|
+
"from_bundle_id",
|
|
30
|
+
"to_release_id",
|
|
31
|
+
"to_bundle_id",
|
|
32
|
+
"platform",
|
|
33
|
+
"app_version",
|
|
34
|
+
"channel",
|
|
35
|
+
"metadata",
|
|
36
|
+
"received_at_ms"
|
|
37
|
+
];
|
|
38
|
+
/** A stored event or head, as the event row it holds. */
|
|
39
|
+
const toEvent = (row) => Object.fromEntries(EVENT_FIELDS.map((field) => [field, row[field]]));
|
|
40
|
+
const bundleRef = (filter) => filter.type === "RECOVERED" ? `from:${filter.fromBundleId}` : `to:${filter.toBundleId}`;
|
|
41
|
+
const scopeOf = (filter) => ({
|
|
42
|
+
platform: filter.platform,
|
|
43
|
+
channel: filter.channel,
|
|
44
|
+
type: filter.type,
|
|
45
|
+
bundle_ref: bundleRef(filter)
|
|
46
|
+
});
|
|
47
|
+
/** One UTC day of a global or bundle list, newest first. */
|
|
48
|
+
const eventsOfDay = (db, filter, day, range, limit) => filter.kind === "all" ? db.findMany("bundle_events", {
|
|
49
|
+
index: "byDay",
|
|
50
|
+
where: { day },
|
|
51
|
+
range,
|
|
52
|
+
order: "desc",
|
|
53
|
+
limit
|
|
54
|
+
}) : db.findMany("bundle_events", {
|
|
55
|
+
index: "byBundle",
|
|
56
|
+
where: {
|
|
57
|
+
...scopeOf(filter),
|
|
58
|
+
day
|
|
59
|
+
},
|
|
60
|
+
range,
|
|
61
|
+
order: "desc",
|
|
62
|
+
limit
|
|
63
|
+
});
|
|
64
|
+
/**
|
|
65
|
+
* The newest UTC day before `day`, and not before `since`'s, that holds an
|
|
66
|
+
* event the list matches, from one outcome row: the global list's per-day
|
|
67
|
+
* count, or the bundle filter's own hourly one.
|
|
68
|
+
*/
|
|
69
|
+
const newestDayBelow = async (db, filter, day, since) => {
|
|
70
|
+
const counted = filter.kind === "all" ? {
|
|
71
|
+
where: DAILY_EVENTS,
|
|
72
|
+
from: dayFloor(since)
|
|
73
|
+
} : {
|
|
74
|
+
where: scopeOf(filter),
|
|
75
|
+
from: hourFloor(since)
|
|
76
|
+
};
|
|
77
|
+
const [newest] = (await db.findAggregates("insights_outcomes", {
|
|
78
|
+
index: "byRef",
|
|
79
|
+
where: counted.where,
|
|
80
|
+
range: {
|
|
81
|
+
gte: counted.from,
|
|
82
|
+
lt: day
|
|
83
|
+
},
|
|
84
|
+
order: "desc",
|
|
85
|
+
limit: 1
|
|
86
|
+
})).rows;
|
|
87
|
+
return newest === void 0 ? void 0 : dayFloor(newest.bucket_start_ms);
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Newest first over [since, before), after the cursor. The global and bundle
|
|
91
|
+
* lists cover at most 90 × 24 hours, however many UTC days that touches, and
|
|
92
|
+
* reject a longer range instead of cutting it short. They read one query per
|
|
93
|
+
* UTC day that holds a matching event: a day that holds none is read once,
|
|
94
|
+
* then one outcome row names the newest day below it that does, so a gap of
|
|
95
|
+
* any length costs two reads.
|
|
96
|
+
*/
|
|
97
|
+
const listEvents = async (db, input) => {
|
|
98
|
+
const since = input.sinceMs ?? 0;
|
|
99
|
+
const range = {
|
|
100
|
+
gte: since,
|
|
101
|
+
lt: input.after === void 0 ? input.beforeReceivedAtMs : [input.after.receivedAtMs, input.after.id]
|
|
102
|
+
};
|
|
103
|
+
const { filter, limit } = input;
|
|
104
|
+
if (filter.kind === "installationMovement") return (await db.findMany("bundle_events", {
|
|
105
|
+
index: "movementsByInstall",
|
|
106
|
+
where: { movement_install_id: filter.installId },
|
|
107
|
+
range,
|
|
108
|
+
order: "desc",
|
|
109
|
+
limit
|
|
110
|
+
})).rows.map(toEvent);
|
|
111
|
+
if (input.beforeReceivedAtMs - since > 7776e6) throw new DatabaseAdapterInputError("invalid-query");
|
|
112
|
+
const rows = [];
|
|
113
|
+
const bottom = dayFloor(since);
|
|
114
|
+
let day = dayFloor(input.after?.receivedAtMs ?? input.beforeReceivedAtMs - 1);
|
|
115
|
+
while (day !== void 0 && day >= bottom && rows.length < limit) {
|
|
116
|
+
const page = await eventsOfDay(db, filter, day, range, limit - rows.length);
|
|
117
|
+
rows.push(...page.rows.map(toEvent));
|
|
118
|
+
day = page.rows.length > 0 ? day - DAY_MS : day > bottom ? await newestDayBelow(db, filter, day, since) : void 0;
|
|
119
|
+
}
|
|
120
|
+
return rows;
|
|
121
|
+
};
|
|
122
|
+
/** One point read for an installation; a user's installations in binary install id order. */
|
|
123
|
+
const findLatestEvents = async (db, input) => {
|
|
124
|
+
if ("installId" in input) {
|
|
125
|
+
const head = await db.findOne("bundle_event_heads", { install_id: input.installId });
|
|
126
|
+
return head === null ? [] : [toEvent(head)];
|
|
127
|
+
}
|
|
128
|
+
return (await db.findMany("bundle_event_heads", {
|
|
129
|
+
index: "byUser",
|
|
130
|
+
where: { user_id: input.userId },
|
|
131
|
+
...input.afterInstallId === void 0 ? {} : { range: { gt: input.afterInstallId } },
|
|
132
|
+
order: "asc",
|
|
133
|
+
limit: input.limit
|
|
134
|
+
})).rows.map(toEvent);
|
|
135
|
+
};
|
|
136
|
+
/** Raw events of one bundle filter over [from, to), which spans at most two partial hours. */
|
|
137
|
+
const countRaw = async (db, filter, from, to) => {
|
|
138
|
+
let total = 0;
|
|
139
|
+
for (let day = dayFloor(from); from < to && day < to; day += DAY_MS) {
|
|
140
|
+
const rows = await drain((page) => db.findMany("bundle_events", {
|
|
141
|
+
index: "byBundle",
|
|
142
|
+
where: {
|
|
143
|
+
...scopeOf(filter),
|
|
144
|
+
day
|
|
145
|
+
},
|
|
146
|
+
range: {
|
|
147
|
+
gte: from,
|
|
148
|
+
lt: to
|
|
149
|
+
},
|
|
150
|
+
limit: PAGE,
|
|
151
|
+
...page
|
|
152
|
+
}));
|
|
153
|
+
total += rows.length;
|
|
154
|
+
}
|
|
155
|
+
return total;
|
|
156
|
+
};
|
|
157
|
+
/** Whole hours from outcome counters; the partial hours at either edge from raw events. */
|
|
158
|
+
const countEvents = async (db, input) => {
|
|
159
|
+
const { filter, sinceMs, beforeReceivedAtMs } = input;
|
|
160
|
+
const start = hourCeil(sinceMs);
|
|
161
|
+
const end = hourFloor(beforeReceivedAtMs);
|
|
162
|
+
if (start >= end) return countRaw(db, filter, sinceMs, beforeReceivedAtMs);
|
|
163
|
+
const hours = await drain((page) => db.findAggregates("insights_outcomes", {
|
|
164
|
+
index: "byRef",
|
|
165
|
+
where: scopeOf(filter),
|
|
166
|
+
range: {
|
|
167
|
+
gte: start,
|
|
168
|
+
lt: end
|
|
169
|
+
},
|
|
170
|
+
limit: PAGE,
|
|
171
|
+
...page
|
|
172
|
+
}));
|
|
173
|
+
return await countRaw(db, filter, sinceMs, start) + hours.reduce((sum, row) => sum + row.events, 0) + await countRaw(db, filter, end, beforeReceivedAtMs);
|
|
174
|
+
};
|
|
175
|
+
/** A bundle filter's distinct (field, value, type) predicates. */
|
|
176
|
+
const latestPredicates = (bundle) => {
|
|
177
|
+
if (bundle === void 0) return void 0;
|
|
178
|
+
return [...new Map(bundle.flatMap(({ field, value, types }) => types.map((type) => [JSON.stringify([
|
|
179
|
+
field,
|
|
180
|
+
value,
|
|
181
|
+
type
|
|
182
|
+
]), {
|
|
183
|
+
field,
|
|
184
|
+
value,
|
|
185
|
+
type
|
|
186
|
+
}]))).values()];
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Latest events at or after `sinceMs`, a UTC day's start: the gauges count
|
|
190
|
+
* each installation in the UTC day of its latest event, so they answer whole
|
|
191
|
+
* days only, and a later start is rejected rather than rounded.
|
|
192
|
+
*/
|
|
193
|
+
const countLatestEvents = async (db, input) => {
|
|
194
|
+
const { platform, channel, sinceMs: start } = input;
|
|
195
|
+
if (start % 864e5 !== 0) throw new DatabaseAdapterInputError("invalid-query");
|
|
196
|
+
const predicates = latestPredicates(input.bundle);
|
|
197
|
+
let total = 0;
|
|
198
|
+
if (predicates === void 0) {
|
|
199
|
+
const rows = await drain((page) => db.findAggregates("insights_distribution", {
|
|
200
|
+
index: "byScope",
|
|
201
|
+
where: {
|
|
202
|
+
channel,
|
|
203
|
+
platform
|
|
204
|
+
},
|
|
205
|
+
range: { gte: start },
|
|
206
|
+
limit: PAGE,
|
|
207
|
+
...page
|
|
208
|
+
}));
|
|
209
|
+
total += rows.reduce((sum, row) => sum + row.latest_installations, 0);
|
|
210
|
+
} else {
|
|
211
|
+
const gauge = async (bundleField, bundleId, type) => {
|
|
212
|
+
return (await drain((page) => db.findAggregates("insights_latest_by_bundle", {
|
|
213
|
+
index: "byBundle",
|
|
214
|
+
where: {
|
|
215
|
+
platform,
|
|
216
|
+
channel,
|
|
217
|
+
bundle_field: bundleField,
|
|
218
|
+
bundle_id: bundleId,
|
|
219
|
+
type
|
|
220
|
+
},
|
|
221
|
+
range: { gte: start },
|
|
222
|
+
limit: PAGE,
|
|
223
|
+
...page
|
|
224
|
+
}))).reduce((sum, row) => sum + row.installations, 0);
|
|
225
|
+
};
|
|
226
|
+
for (const { field, value, type } of predicates) total += await gauge(field, value, type);
|
|
227
|
+
for (const from of predicates) {
|
|
228
|
+
if (from.field !== "from_bundle_id") continue;
|
|
229
|
+
for (const to of predicates) {
|
|
230
|
+
if (to.field !== "to_bundle_id" || to.type !== from.type) continue;
|
|
231
|
+
total -= await gauge(PAIR_FIELD, bundlePairKey(from.value, to.value), from.type);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return Math.max(0, total);
|
|
236
|
+
};
|
|
237
|
+
/** The oldest whole hour and UTC day that hourly and daily rows still hold. */
|
|
238
|
+
const retained = (now, { rawDays, dailyDays }) => ({
|
|
239
|
+
hour: hourCeil(now - rawDays * DAY_MS),
|
|
240
|
+
day: dayCeil(now - dailyDays * DAY_MS)
|
|
241
|
+
});
|
|
242
|
+
/**
|
|
243
|
+
* Whether the rows a window reads still hold all of it; an older start is
|
|
244
|
+
* partial from `oldest`, the first bucket its rows keep.
|
|
245
|
+
*/
|
|
246
|
+
const coverageOf = (start, oldest) => start === void 0 || start >= oldest ? {
|
|
247
|
+
kind: "complete",
|
|
248
|
+
sinceMs: 0
|
|
249
|
+
} : {
|
|
250
|
+
kind: "partial",
|
|
251
|
+
sinceMs: oldest
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Periods covering [start, end): whole UTC days when `days` and the window is
|
|
255
|
+
* over 48 hours, hours elsewhere. Hourly rows start at `hour`, the raw
|
|
256
|
+
* period ago, so with `days` an edge older than that reads its whole UTC day
|
|
257
|
+
* from the daily rows.
|
|
258
|
+
*/
|
|
259
|
+
const periodsOf = (range, days, hour) => {
|
|
260
|
+
const start = days && range.start < hour ? dayFloor(range.start) : range.start;
|
|
261
|
+
const end = days && range.end < hour ? dayCeil(range.end) : range.end;
|
|
262
|
+
if (!days || end - start <= 1728e5 && start >= hour) return [{
|
|
263
|
+
periodKind: "hour",
|
|
264
|
+
start,
|
|
265
|
+
end
|
|
266
|
+
}];
|
|
267
|
+
const first = Math.min(end, dayCeil(start));
|
|
268
|
+
const last = Math.max(first, dayFloor(end));
|
|
269
|
+
return [
|
|
270
|
+
{
|
|
271
|
+
periodKind: "hour",
|
|
272
|
+
start,
|
|
273
|
+
end: first
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
periodKind: "day",
|
|
277
|
+
start: first,
|
|
278
|
+
end: last
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
periodKind: "hour",
|
|
282
|
+
start: last,
|
|
283
|
+
end
|
|
284
|
+
}
|
|
285
|
+
].filter((period) => period.start < period.end);
|
|
286
|
+
};
|
|
287
|
+
/** The hourly and daily aggregates of counters, and of sketches. */
|
|
288
|
+
const COUNTERS = {
|
|
289
|
+
hour: "insights_overview",
|
|
290
|
+
day: "insights_overview_daily"
|
|
291
|
+
};
|
|
292
|
+
const SKETCHES = {
|
|
293
|
+
hour: "insights_sketches",
|
|
294
|
+
day: "insights_sketches_daily"
|
|
295
|
+
};
|
|
296
|
+
/** Every counter or sketch row of one identity over [start, end), mixing day and hour periods. */
|
|
297
|
+
const windowRows = async (db, models, parts, range, days, hour) => {
|
|
298
|
+
const rows = [];
|
|
299
|
+
for (const period of periodsOf(range, days, hour)) {
|
|
300
|
+
const identity = insightsIdentity({
|
|
301
|
+
...parts,
|
|
302
|
+
periodKind: period.periodKind
|
|
303
|
+
});
|
|
304
|
+
rows.push(...await drain((page) => db.findAggregates(models[period.periodKind], {
|
|
305
|
+
index: "window",
|
|
306
|
+
where: { identity },
|
|
307
|
+
range: {
|
|
308
|
+
gte: period.start,
|
|
309
|
+
lt: period.end
|
|
310
|
+
},
|
|
311
|
+
limit: PAGE,
|
|
312
|
+
...page
|
|
313
|
+
})));
|
|
314
|
+
}
|
|
315
|
+
return rows;
|
|
316
|
+
};
|
|
317
|
+
const counterRows = (...[db, ...args]) => windowRows(db, COUNTERS, ...args);
|
|
318
|
+
const sketchRows = (...[db, ...args]) => windowRows(db, SKETCHES, ...args);
|
|
319
|
+
const releaseParts = (release) => ({
|
|
320
|
+
scopeKind: "release",
|
|
321
|
+
releaseKind: "specific",
|
|
322
|
+
releaseId: release.releaseId,
|
|
323
|
+
channel: release.channel,
|
|
324
|
+
platform: release.platform,
|
|
325
|
+
appVersionKind: "all",
|
|
326
|
+
appVersion: ""
|
|
327
|
+
});
|
|
328
|
+
/** A release's lifetime counters: one logical row at bucket 0. */
|
|
329
|
+
const lifetimeMetrics = async (db, release) => {
|
|
330
|
+
const identity = insightsIdentity({
|
|
331
|
+
...releaseParts(release),
|
|
332
|
+
periodKind: "lifetime"
|
|
333
|
+
});
|
|
334
|
+
const [lifetime] = (await db.findAggregates("insights_overview_lifetime", {
|
|
335
|
+
index: "window",
|
|
336
|
+
where: { identity },
|
|
337
|
+
range: {
|
|
338
|
+
gte: 0,
|
|
339
|
+
lte: 0
|
|
340
|
+
},
|
|
341
|
+
limit: PAGE
|
|
342
|
+
})).rows;
|
|
343
|
+
return {
|
|
344
|
+
downloads: lifetime?.downloads ?? 0,
|
|
345
|
+
launches: lifetime?.launches ?? 0,
|
|
346
|
+
failedLaunches: lifetime?.failed_launches ?? 0
|
|
347
|
+
};
|
|
348
|
+
};
|
|
349
|
+
/** Counters, unique users, and a per-UTC-day series over a window. */
|
|
350
|
+
const rangedMetrics = async (db, parts, users, range, days, hour) => {
|
|
351
|
+
const [counters, sketches] = await Promise.all([counterRows(db, parts, range, days, hour), sketchRows(db, users.parts, range, days, hour)]);
|
|
352
|
+
const series = /* @__PURE__ */ new Map();
|
|
353
|
+
for (const row of counters) {
|
|
354
|
+
const day = dayFloor(row.bucket_start_ms);
|
|
355
|
+
const point = series.get(day) ?? {
|
|
356
|
+
launches: 0,
|
|
357
|
+
failedLaunches: 0
|
|
358
|
+
};
|
|
359
|
+
point.launches += row.launches;
|
|
360
|
+
point.failedLaunches += row.failed_launches;
|
|
361
|
+
series.set(day, point);
|
|
362
|
+
}
|
|
363
|
+
const total = (field) => counters.reduce((sum, row) => sum + row[field], 0);
|
|
364
|
+
return {
|
|
365
|
+
downloads: total("downloads"),
|
|
366
|
+
launches: total("launches"),
|
|
367
|
+
failedLaunches: total("failed_launches"),
|
|
368
|
+
uniqueUsers: countDistinct(mergeDistinct(sketches.map((row) => row[users.field]))),
|
|
369
|
+
series: [...series].sort(([left], [right]) => left - right).map(([startMs, point]) => ({
|
|
370
|
+
startMs,
|
|
371
|
+
...point
|
|
372
|
+
}))
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
/** A channel and platform's usage rows, of every app version or of one. */
|
|
376
|
+
const usageParts = (channel, platform, appVersion) => ({
|
|
377
|
+
scopeKind: "usage",
|
|
378
|
+
releaseKind: "all",
|
|
379
|
+
releaseId: "",
|
|
380
|
+
channel,
|
|
381
|
+
platform,
|
|
382
|
+
appVersionKind: appVersion === void 0 ? "all" : "specific",
|
|
383
|
+
appVersion: appVersion ?? ""
|
|
384
|
+
});
|
|
385
|
+
const getReleaseActivity = async (db, input, now, retention) => {
|
|
386
|
+
const at = now();
|
|
387
|
+
const kept = retained(at, retention);
|
|
388
|
+
const data = input.scope !== void 0 ? [{
|
|
389
|
+
scope: input.scope,
|
|
390
|
+
metrics: await rangedMetrics(db, {
|
|
391
|
+
scopeKind: "channel",
|
|
392
|
+
releaseKind: "all",
|
|
393
|
+
releaseId: "",
|
|
394
|
+
channel: input.scope.channel,
|
|
395
|
+
platform: input.scope.platform,
|
|
396
|
+
appVersionKind: "all",
|
|
397
|
+
appVersion: ""
|
|
398
|
+
}, {
|
|
399
|
+
parts: usageParts(input.scope.channel, input.scope.platform),
|
|
400
|
+
field: "activity_users"
|
|
401
|
+
}, input.timeRange, true, kept.hour)
|
|
402
|
+
}] : await Promise.all(input.releases.map(async (release) => ({
|
|
403
|
+
release,
|
|
404
|
+
metrics: input.timeRange === void 0 ? await lifetimeMetrics(db, release) : await rangedMetrics(db, releaseParts(release), {
|
|
405
|
+
parts: releaseParts(release),
|
|
406
|
+
field: "launch_users"
|
|
407
|
+
}, input.timeRange, false, kept.hour)
|
|
408
|
+
})));
|
|
409
|
+
const oldest = kept[input.scope === void 0 ? "hour" : "day"];
|
|
410
|
+
return {
|
|
411
|
+
coverage: coverageOf(input.timeRange?.start, oldest),
|
|
412
|
+
data,
|
|
413
|
+
measuredAtMs: at
|
|
414
|
+
};
|
|
415
|
+
};
|
|
416
|
+
/**
|
|
417
|
+
* Entries with installations, most first. Batched gauges sum signed shard
|
|
418
|
+
* rows, so a version or platform everyone left can read 0, and is left out.
|
|
419
|
+
*/
|
|
420
|
+
const byInstallations = (values) => [...values].flatMap(([name, installations]) => installations > 0 ? [{
|
|
421
|
+
name,
|
|
422
|
+
installations
|
|
423
|
+
}] : []).sort((left, right) => right.installations - left.installations || left.name.localeCompare(right.name, "en", { numeric: true }));
|
|
424
|
+
const getAppUsage = async (db, input, now, retention) => {
|
|
425
|
+
const { timeRange, intervalMs } = input;
|
|
426
|
+
const at = now();
|
|
427
|
+
const kept = retained(at, retention);
|
|
428
|
+
const days = intervalMs % 864e5 === 0 && timeRange.start % 864e5 === 0;
|
|
429
|
+
const reported = input.platform === "all" ? ["ios", "android"] : [input.platform];
|
|
430
|
+
const usage = (await Promise.all(reported.map((platform) => sketchRows(db, usageParts(input.channel, platform, input.appVersion), timeRange, days, kept.hour)))).flat();
|
|
431
|
+
const range = {
|
|
432
|
+
gte: dayFloor(timeRange.start),
|
|
433
|
+
lt: timeRange.end
|
|
434
|
+
};
|
|
435
|
+
const distribution = [];
|
|
436
|
+
for (const platform of reported) distribution.push(...await drain((page) => input.appVersion === void 0 ? db.findAggregates("insights_distribution", {
|
|
437
|
+
index: "byScope",
|
|
438
|
+
where: {
|
|
439
|
+
channel: input.channel,
|
|
440
|
+
platform
|
|
441
|
+
},
|
|
442
|
+
range,
|
|
443
|
+
limit: PAGE,
|
|
444
|
+
...page
|
|
445
|
+
}) : db.findAggregates("insights_distribution", {
|
|
446
|
+
index: "byVersion",
|
|
447
|
+
where: {
|
|
448
|
+
channel: input.channel,
|
|
449
|
+
platform,
|
|
450
|
+
app_version: input.appVersion
|
|
451
|
+
},
|
|
452
|
+
range,
|
|
453
|
+
limit: PAGE,
|
|
454
|
+
...page
|
|
455
|
+
})));
|
|
456
|
+
const points = [];
|
|
457
|
+
for (let start = timeRange.start; start < timeRange.end; start += intervalMs) points.push({
|
|
458
|
+
startMs: start,
|
|
459
|
+
installations: countDistinct(mergeDistinct(usage.filter((row) => row.bucket_start_ms >= start && row.bucket_start_ms < start + intervalMs).map((row) => row.activity_users)))
|
|
460
|
+
});
|
|
461
|
+
const versions = /* @__PURE__ */ new Map();
|
|
462
|
+
const platforms = /* @__PURE__ */ new Map();
|
|
463
|
+
const bundles = /* @__PURE__ */ new Map();
|
|
464
|
+
for (const row of distribution) {
|
|
465
|
+
const count = row.latest_installations;
|
|
466
|
+
versions.set(row.app_version, (versions.get(row.app_version) ?? 0) + count);
|
|
467
|
+
platforms.set(row.platform, (platforms.get(row.platform) ?? 0) + count);
|
|
468
|
+
const releaseId = row.release_id === "" ? null : row.release_id;
|
|
469
|
+
const key = JSON.stringify([
|
|
470
|
+
row.app_version,
|
|
471
|
+
row.platform,
|
|
472
|
+
releaseId
|
|
473
|
+
]);
|
|
474
|
+
bundles.set(key, {
|
|
475
|
+
appVersion: row.app_version,
|
|
476
|
+
platform: row.platform,
|
|
477
|
+
releaseId,
|
|
478
|
+
installations: (bundles.get(key)?.installations ?? 0) + count
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
const sortedVersions = byInstallations(versions);
|
|
482
|
+
return {
|
|
483
|
+
coverage: coverageOf(timeRange.start, kept[days ? "day" : "hour"]),
|
|
484
|
+
activeInstallations: countDistinct(mergeDistinct(usage.map((row) => row.activity_users))),
|
|
485
|
+
points,
|
|
486
|
+
appVersions: sortedVersions.map(({ name }) => name),
|
|
487
|
+
versions: sortedVersions,
|
|
488
|
+
platforms: byInstallations(platforms),
|
|
489
|
+
bundleDistribution: [...bundles.values()].filter(({ installations }) => installations > 0),
|
|
490
|
+
measuredAtMs: at
|
|
491
|
+
};
|
|
492
|
+
};
|
|
493
|
+
/** The longest period the failures read covers: the console's longest, 30 days. */
|
|
494
|
+
const UPDATE_FAILURES_RANGE_MS = 30 * DAY_MS;
|
|
495
|
+
const isRange = (range) => range === void 0 || Number.isSafeInteger(range.start) && Number.isSafeInteger(range.end) && range.start >= 0 && range.start < range.end && range.end - range.start <= UPDATE_FAILURES_RANGE_MS;
|
|
496
|
+
const isText = (value, maxLength) => typeof value === "string" && value.length > 0 && value.length <= maxLength;
|
|
497
|
+
const failed = (...rows) => countDistinct(mergeDistinct(rows.flat().map((row) => row.failed_users)));
|
|
498
|
+
/** Entries of a count map, most first, then in key order. */
|
|
499
|
+
const mostFirst = (counts) => [...counts].sort(([leftKey, left], [rightKey, right]) => right.events - left.events || compareUtf8(leftKey, rightKey)).map(([, entry]) => entry);
|
|
500
|
+
/** Failure rows as their breakdown, and recovery rows as exit reasons. */
|
|
501
|
+
const breakdownOf = (rows) => {
|
|
502
|
+
const failures = /* @__PURE__ */ new Map();
|
|
503
|
+
const exits = /* @__PURE__ */ new Map();
|
|
504
|
+
for (const { stage, reason, detail, events } of rows) {
|
|
505
|
+
if (events <= 0) continue;
|
|
506
|
+
if (stage === "launch") {
|
|
507
|
+
const exit = exits.get(reason) ?? {
|
|
508
|
+
value: reason,
|
|
509
|
+
events: 0
|
|
510
|
+
};
|
|
511
|
+
exit.events += events;
|
|
512
|
+
exits.set(reason, exit);
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
const key = JSON.stringify([stage, reason]);
|
|
516
|
+
const entry = failures.get(key) ?? {
|
|
517
|
+
value: {
|
|
518
|
+
stage,
|
|
519
|
+
reason
|
|
520
|
+
},
|
|
521
|
+
events: 0,
|
|
522
|
+
details: /* @__PURE__ */ new Map()
|
|
523
|
+
};
|
|
524
|
+
entry.events += events;
|
|
525
|
+
const known = entry.details.get(detail) ?? {
|
|
526
|
+
value: detail,
|
|
527
|
+
events: 0
|
|
528
|
+
};
|
|
529
|
+
known.events += events;
|
|
530
|
+
entry.details.set(detail, known);
|
|
531
|
+
failures.set(key, entry);
|
|
532
|
+
}
|
|
533
|
+
return {
|
|
534
|
+
breakdown: mostFirst(failures).map(({ value, events }) => {
|
|
535
|
+
const { details } = failures.get(JSON.stringify([value.stage, value.reason]));
|
|
536
|
+
return {
|
|
537
|
+
...value,
|
|
538
|
+
events,
|
|
539
|
+
details: mostFirst(details).map(({ value: detail, events }) => {
|
|
540
|
+
const [resource, httpStatus, originCode, transport] = JSON.parse(detail);
|
|
541
|
+
return {
|
|
542
|
+
resource,
|
|
543
|
+
httpStatus,
|
|
544
|
+
originCode,
|
|
545
|
+
transport,
|
|
546
|
+
events
|
|
547
|
+
};
|
|
548
|
+
})
|
|
549
|
+
};
|
|
550
|
+
}),
|
|
551
|
+
byExitReason: mostFirst(exits).map(({ value, events }) => ({
|
|
552
|
+
exitReason: value,
|
|
553
|
+
events
|
|
554
|
+
}))
|
|
555
|
+
};
|
|
556
|
+
};
|
|
557
|
+
/**
|
|
558
|
+
* A release's or a channel's update failures. Since a release's first
|
|
559
|
+
* report: its lifetime counters and failed installations. Over a time range
|
|
560
|
+
* of at most 30 days: failures, failed checks, the breakdown by stage,
|
|
561
|
+
* reason, and detail, and recoveries by exit reason, all summed from the
|
|
562
|
+
* breakdown's hourly rows; downloads and failed launches from the counters;
|
|
563
|
+
* failed installations from their sketches. A release reads hourly rows, a
|
|
564
|
+
* channel daily rows past 48 hours like its activity; the breakdown's rows
|
|
565
|
+
* are hourly, so its coverage is the raw period's.
|
|
566
|
+
*/
|
|
567
|
+
const getUpdateFailures = async (db, input, now, retention) => {
|
|
568
|
+
const { platform, channel, releaseId, timeRange } = input;
|
|
569
|
+
if (platform !== "ios" && platform !== "android" || !isText(channel, 1024) || releaseId !== void 0 && !isText(releaseId, 36) || !isRange(timeRange) || releaseId === void 0 && timeRange === void 0) throw new DatabaseAdapterInputError("invalid-query");
|
|
570
|
+
const at = now();
|
|
571
|
+
const kept = retained(at, retention);
|
|
572
|
+
const channelParts = {
|
|
573
|
+
...usageParts(channel, platform),
|
|
574
|
+
scopeKind: "channel"
|
|
575
|
+
};
|
|
576
|
+
const parts = releaseId === void 0 ? channelParts : releaseParts({
|
|
577
|
+
releaseId,
|
|
578
|
+
platform,
|
|
579
|
+
channel
|
|
580
|
+
});
|
|
581
|
+
const failure = {
|
|
582
|
+
...parts,
|
|
583
|
+
scopeKind: "failure"
|
|
584
|
+
};
|
|
585
|
+
if (timeRange === void 0) {
|
|
586
|
+
const where = (from) => ({ identity: insightsIdentity({
|
|
587
|
+
...from,
|
|
588
|
+
periodKind: "lifetime"
|
|
589
|
+
}) });
|
|
590
|
+
const [counters, sketches] = await Promise.all([db.findAggregates("insights_overview_lifetime", {
|
|
591
|
+
index: "window",
|
|
592
|
+
where: where(parts),
|
|
593
|
+
range: {
|
|
594
|
+
gte: 0,
|
|
595
|
+
lte: 0
|
|
596
|
+
},
|
|
597
|
+
limit: PAGE
|
|
598
|
+
}), db.findAggregates("insights_sketches_lifetime", {
|
|
599
|
+
index: "window",
|
|
600
|
+
where: where(failure),
|
|
601
|
+
range: {
|
|
602
|
+
gte: 0,
|
|
603
|
+
lte: 0
|
|
604
|
+
},
|
|
605
|
+
limit: PAGE
|
|
606
|
+
})]);
|
|
607
|
+
const lifetime = counters.rows[0];
|
|
608
|
+
return {
|
|
609
|
+
coverage: {
|
|
610
|
+
kind: "complete",
|
|
611
|
+
sinceMs: 0
|
|
612
|
+
},
|
|
613
|
+
measuredAtMs: at,
|
|
614
|
+
failedUpdates: lifetime?.failed_updates ?? 0,
|
|
615
|
+
failedInstallations: countDistinct(mergeDistinct(sketches.rows.map((row) => row.failed_users))),
|
|
616
|
+
downloads: lifetime?.downloads ?? 0,
|
|
617
|
+
patchDownloads: lifetime?.patch_downloads ?? 0,
|
|
618
|
+
patchFallbacks: lifetime?.patch_fallbacks ?? 0
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
const days = releaseId === void 0;
|
|
622
|
+
const window = [
|
|
623
|
+
timeRange,
|
|
624
|
+
days,
|
|
625
|
+
kept.hour
|
|
626
|
+
];
|
|
627
|
+
const [counters, failures, checks, active, rows] = await Promise.all([
|
|
628
|
+
counterRows(db, parts, ...window),
|
|
629
|
+
sketchRows(db, failure, ...window),
|
|
630
|
+
days ? sketchRows(db, {
|
|
631
|
+
...channelParts,
|
|
632
|
+
scopeKind: "check"
|
|
633
|
+
}, ...window) : [],
|
|
634
|
+
days ? sketchRows(db, usageParts(channel, platform), ...window) : [],
|
|
635
|
+
drain((page) => db.findAggregates("insights_failures", {
|
|
636
|
+
index: "byScope",
|
|
637
|
+
where: {
|
|
638
|
+
platform,
|
|
639
|
+
channel
|
|
640
|
+
},
|
|
641
|
+
range: {
|
|
642
|
+
gte: timeRange.start,
|
|
643
|
+
lt: timeRange.end
|
|
644
|
+
},
|
|
645
|
+
limit: PAGE,
|
|
646
|
+
...page
|
|
647
|
+
}))
|
|
648
|
+
]);
|
|
649
|
+
const total = (field) => counters.reduce((sum, row) => sum + row[field], 0);
|
|
650
|
+
const scoped = releaseId === void 0 ? rows : rows.filter((row) => row.release_id === releaseId);
|
|
651
|
+
const failuresAt = (counted) => scoped.reduce((sum, row) => sum + (counted(row.stage) ? Math.max(0, row.events) : 0), 0);
|
|
652
|
+
const { breakdown, byExitReason } = breakdownOf(scoped);
|
|
653
|
+
return {
|
|
654
|
+
coverage: coverageOf(timeRange.start, kept.hour),
|
|
655
|
+
measuredAtMs: at,
|
|
656
|
+
failedUpdates: failuresAt((stage) => stage !== "check" && stage !== "launch"),
|
|
657
|
+
failedInstallations: failed(failures),
|
|
658
|
+
downloads: total("downloads"),
|
|
659
|
+
patchDownloads: total("patch_downloads"),
|
|
660
|
+
patchFallbacks: total("patch_fallbacks"),
|
|
661
|
+
...days ? { checks: {
|
|
662
|
+
failures: failuresAt((stage) => stage === "check"),
|
|
663
|
+
failedInstallations: failed(checks),
|
|
664
|
+
activeInstallations: countDistinct(mergeDistinct(active.map((row) => row.activity_users)))
|
|
665
|
+
} } : {},
|
|
666
|
+
breakdown,
|
|
667
|
+
recoveries: {
|
|
668
|
+
failedLaunches: total("failed_launches"),
|
|
669
|
+
byExitReason
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
};
|
|
673
|
+
//#endregion
|
|
674
|
+
export { UPDATE_FAILURES_RANGE_MS, countEvents, countLatestEvents, findLatestEvents, getAppUsage, getReleaseActivity, getUpdateFailures, listEvents };
|