@hot-updater/cloudflare 1.2.0-rc.56 → 1.2.0-rc.58

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.
@@ -539,14 +539,14 @@ function modularInverse(value, modulus) {
539
539
  return positiveMod(t, modulus);
540
540
  }
541
541
  __name(modularInverse, "modularInverse");
542
- function getRolloutShuffleParameters(bundleId) {
543
- let multiplier = positiveMod(hashString(`${bundleId}:multiplier`), 997);
542
+ function getRolloutShuffleParameters(releaseId) {
543
+ let multiplier = positiveMod(hashString(`${releaseId}:multiplier`), 997);
544
544
  if (multiplier === 0) multiplier = 1;
545
545
  while (gcd(multiplier, NUMERIC_COHORT_SIZE) !== 1) {
546
546
  multiplier = positiveMod(multiplier + 1, NUMERIC_COHORT_SIZE);
547
547
  if (multiplier === 0) multiplier = 1;
548
548
  }
549
- const offset = positiveMod(hashString(`${bundleId}:offset`), NUMERIC_COHORT_SIZE);
549
+ const offset = positiveMod(hashString(`${releaseId}:offset`), NUMERIC_COHORT_SIZE);
550
550
  return {
551
551
  multiplier,
552
552
  offset,
@@ -586,17 +586,17 @@ function isValidCohort(cohort) {
586
586
  return isNumericCohort(normalized) || isCustomCohort(normalized);
587
587
  }
588
588
  __name(isValidCohort, "isValidCohort");
589
- function getNumericCohortRolloutPosition(bundleId, cohortValue) {
589
+ function getNumericCohortRolloutPosition(releaseId, cohortValue) {
590
590
  if (cohortValue < 1 || cohortValue > 1e3) throw new Error(`Invalid numeric cohort: ${cohortValue}`);
591
- const { offset, inverseMultiplier } = getRolloutShuffleParameters(bundleId);
591
+ const { offset, inverseMultiplier } = getRolloutShuffleParameters(releaseId);
592
592
  return positiveMod(inverseMultiplier * (cohortValue - 1 - offset), NUMERIC_COHORT_SIZE);
593
593
  }
594
594
  __name(getNumericCohortRolloutPosition, "getNumericCohortRolloutPosition");
595
- function getRolledOutNumericCohorts(bundleId, rolloutCohortCount) {
595
+ function getRolledOutNumericCohorts(releaseId, rolloutCohortCount) {
596
596
  const normalizedRolloutCount = normalizeRolloutCohortCount(rolloutCohortCount);
597
597
  if (normalizedRolloutCount <= 0) return [];
598
598
  if (normalizedRolloutCount >= 1e3) return Array.from({ length: NUMERIC_COHORT_SIZE }, (_, index) => index + 1);
599
- const { multiplier, offset } = getRolloutShuffleParameters(bundleId);
599
+ const { multiplier, offset } = getRolloutShuffleParameters(releaseId);
600
600
  return Array.from({ length: normalizedRolloutCount }, (_, position) => positiveMod(multiplier * position + offset, NUMERIC_COHORT_SIZE)).map((zeroBasedCohort) => zeroBasedCohort + 1).sort((left, right) => left - right);
601
601
  }
602
602
  __name(getRolledOutNumericCohorts, "getRolledOutNumericCohorts");
@@ -5021,7 +5021,7 @@ var createSqlAdapter = /* @__PURE__ */ __name(({ executor, tablePrefix = "", max
5021
5021
  }, "createSqlAdapter");
5022
5022
 
5023
5023
  // ../../packages/server/dist/package.mjs
5024
- var version = "1.2.0-rc.56";
5024
+ var version = "1.2.0-rc.58";
5025
5025
 
5026
5026
  // ../../packages/server/dist/version.mjs
5027
5027
  var HOT_UPDATER_SERVER_VERSION = version;
@@ -9005,7 +9005,8 @@ var createRequestHandler = /* @__PURE__ */ __name(({ api, clientPolicy, privateR
9005
9005
  let authenticated;
9006
9006
  try {
9007
9007
  authenticated = await clientPolicy.authenticate(request);
9008
- } catch {
9008
+ } catch (error) {
9009
+ console.error("Hot Updater client authentication failed:", error);
9009
9010
  return errorResponse("Service unavailable", 503);
9010
9011
  }
9011
9012
  if (!authenticated) return errorResponse("Unauthorized", 401);
@@ -9512,13 +9513,15 @@ var isLaunch = /* @__PURE__ */ __name((row) => {
9512
9513
  const kinds = metadataOf(row).change?.kinds ?? [];
9513
9514
  return kinds.includes("bundle") && !kinds.includes("first_seen") && row.to_release_id !== null;
9514
9515
  }, "isLaunch");
9515
- var bundleRefsOf = /* @__PURE__ */ __name((row) => {
9516
+ var sideRef = /* @__PURE__ */ __name((key, releaseId, bundleId) => key === "release" ? releaseId ?? bundleId : bundleId, "sideRef");
9517
+ var bundleRefsOf = /* @__PURE__ */ __name((row, key = "release") => {
9516
9518
  if (isFailedCheck(row) || isLateEvent(row)) return [];
9517
- if (row.type === "RECOVERED") return [`from:${row.from_bundle_id}`];
9518
- if (row.type === "UNCHANGED") return isLaunch(row) ? [`on:${row.to_bundle_id}`] : [];
9519
- return [`to:${row.to_bundle_id}`];
9519
+ if (row.type === "RECOVERED") return [`from:${sideRef(key, row.from_release_id, row.from_bundle_id)}`];
9520
+ if (row.type === "UNCHANGED") return isLaunch(row) ? [`on:${sideRef(key, row.to_release_id, row.to_bundle_id)}`] : [];
9521
+ return [`to:${sideRef(key, row.to_release_id, row.to_bundle_id)}`];
9520
9522
  }, "bundleRefsOf");
9521
- var bundleRefOfFilter = /* @__PURE__ */ __name((filter) => filter.type === "RECOVERED" ? `from:${filter.fromBundleId}` : filter.type === "UNCHANGED" ? `on:${filter.toBundleId}` : `to:${filter.toBundleId}`, "bundleRefOfFilter");
9523
+ var outcomeRefsOf = /* @__PURE__ */ __name((row, key = "release") => [...bundleRefsOf(row, key).map((ref) => [row.type, ref]), ...metadataOf(row).implied_download === true ? [["UPDATE_DOWNLOADED", row.type === "RECOVERED" ? `to:${sideRef(key, row.from_release_id, row.from_bundle_id)}` : `to:${sideRef(key, row.to_release_id, row.to_bundle_id)}`]] : []], "outcomeRefsOf");
9524
+ var bundleRefOfFilter = /* @__PURE__ */ __name((filter) => filter.type === "RECOVERED" ? `from:${filter.fromReleaseId}` : filter.type === "UNCHANGED" ? `on:${filter.toReleaseId}` : `to:${filter.toReleaseId}`, "bundleRefOfFilter");
9522
9525
  var bundleEvents = /* @__PURE__ */ __name((days) => defineTable({
9523
9526
  id: {
9524
9527
  type: "string",
@@ -9577,7 +9580,11 @@ var bundleEvents = /* @__PURE__ */ __name((days) => defineTable({
9577
9580
  type: "string",
9578
9581
  compute: /* @__PURE__ */ __name((row) => MOVEMENTS.has(row.type) ? row.install_id : null, "compute")
9579
9582
  },
9580
- /** The bundle a bundle filter matches (`bundleRefsOf`). */
9583
+ /**
9584
+ * The bundle a bundle filter matches (`bundleRefsOf`), by release.
9585
+ * Rows a 1.1 server wrote name the artifact until
9586
+ * `backfillReleaseRefs` rewrites them.
9587
+ */
9581
9588
  bundle_ref: {
9582
9589
  type: "string",
9583
9590
  multi: true,
@@ -9955,6 +9962,70 @@ var createInsightsSchema = /* @__PURE__ */ __name(({ rawDays, dailyDays } = DEFA
9955
9962
  }, "createInsightsSchema");
9956
9963
  var insightsSchema = createInsightsSchema();
9957
9964
 
9965
+ // ../insights/dist/server/backfill.mjs
9966
+ var PAGE5 = 500;
9967
+ var BATCH = 25;
9968
+ var sameRefs = /* @__PURE__ */ __name((stored2, refs) => {
9969
+ const values = Array.isArray(stored2) ? stored2 : [];
9970
+ return values.length === refs.length && values.every((value) => refs.includes(value));
9971
+ }, "sameRefs");
9972
+ var namesArtifact = /* @__PURE__ */ __name((row) => {
9973
+ const byKey = /* @__PURE__ */ __name((key) => bundleRefsOf(row, key), "byKey");
9974
+ return sameRefs(row.bundle_ref, byKey("artifact")) && !sameRefs(row.bundle_ref, byKey("release"));
9975
+ }, "namesArtifact");
9976
+ var backfillReleaseRefs = /* @__PURE__ */ __name(async (db, now, { rawDays }, { dryRun: dryRun2 = false } = {}) => {
9977
+ const today = now() - now() % DAY_MS2;
9978
+ let events = 0;
9979
+ const move = /* @__PURE__ */ __name(async (candidates) => {
9980
+ if (dryRun2) {
9981
+ events += candidates.length;
9982
+ return;
9983
+ }
9984
+ const moved = await rewrite3(candidates).catch(async (error) => {
9985
+ if (!(error instanceof DatabaseConstraintError) || error.reason !== "too_large" || candidates.length === 1) throw error;
9986
+ const half = Math.ceil(candidates.length / 2);
9987
+ await move(candidates.slice(0, half));
9988
+ await move(candidates.slice(half));
9989
+ return 0;
9990
+ });
9991
+ events += moved;
9992
+ }, "move");
9993
+ const rewrite3 = /* @__PURE__ */ __name((candidates) => db.transaction(async (tx) => {
9994
+ const rows = await Promise.all(candidates.map(({ id }) => tx.findOne("bundle_events", { id })));
9995
+ let moved = 0;
9996
+ for (const row of rows) {
9997
+ if (row === null || !namesArtifact(row)) continue;
9998
+ tx.update("bundle_events", row, {});
9999
+ const outcome = /* @__PURE__ */ __name((ref, delta) => tx.aggregate("insights_outcomes", {
10000
+ platform: row.platform,
10001
+ channel: row.channel,
10002
+ type: ref[0],
10003
+ bundle_ref: ref[1],
10004
+ bucket_start_ms: row.received_at_ms - row.received_at_ms % HOUR_MS2
10005
+ }, { events: delta }, { shardBy: row.install_id }), "outcome");
10006
+ for (const ref of outcomeRefsOf(row, "artifact")) outcome(ref, -1);
10007
+ for (const ref of outcomeRefsOf(row, "release")) outcome(ref, 1);
10008
+ moved += 1;
10009
+ }
10010
+ return moved;
10011
+ }), "rewrite");
10012
+ for (let day = today - rawDays * DAY_MS2; day <= today; day += DAY_MS2) {
10013
+ let cursor;
10014
+ do {
10015
+ const page2 = await db.findMany("bundle_events", {
10016
+ index: "byDay",
10017
+ where: { day },
10018
+ limit: PAGE5,
10019
+ ...cursor === void 0 ? {} : { cursor }
10020
+ });
10021
+ const stale = page2.rows.filter(namesArtifact);
10022
+ for (let start = 0; start < stale.length; start += BATCH) await move(stale.slice(start, start + BATCH));
10023
+ cursor = page2.next;
10024
+ } while (cursor !== void 0);
10025
+ }
10026
+ return { events };
10027
+ }, "backfillReleaseRefs");
10028
+
9958
10029
  // ../insights/dist/server/eventRow.mjs
9959
10030
  var CHANGE_KINDS = /* @__PURE__ */ new Set([
9960
10031
  "first_seen",
@@ -10032,7 +10103,7 @@ var isBundleFilter = /* @__PURE__ */ __name((value, withKind = false) => {
10032
10103
  "type",
10033
10104
  ...withKind ? ["kind"] : []
10034
10105
  ];
10035
- return value.type === "RECOVERED" ? isText2(value.fromBundleId) && hasOnlyKeys(value, [...keys, "fromBundleId"]) : (value.type === "UPDATE_DOWNLOADED" || value.type === "UPDATE_APPLIED" || value.type === "UPDATE_FAILED" || value.type === "UNCHANGED") && isText2(value.toBundleId) && hasOnlyKeys(value, [...keys, "toBundleId"]);
10106
+ return value.type === "RECOVERED" ? isText2(value.fromReleaseId) && hasOnlyKeys(value, [...keys, "fromReleaseId"]) : (value.type === "UPDATE_DOWNLOADED" || value.type === "UPDATE_APPLIED" || value.type === "UPDATE_FAILED" || value.type === "UNCHANGED") && isText2(value.toReleaseId) && hasOnlyKeys(value, [...keys, "toReleaseId"]);
10036
10107
  }, "isBundleFilter");
10037
10108
  var isEventFilter = /* @__PURE__ */ __name((value) => {
10038
10109
  if (!isRecord5(value)) return false;
@@ -10127,20 +10198,24 @@ var createValidatedInsightsModel = /* @__PURE__ */ __name((model) => ({
10127
10198
  if (!isRecord5(input) || !hasOnlyKeys(input, [
10128
10199
  "platform",
10129
10200
  "channel",
10130
- "sinceMs",
10131
- "bundle"
10132
- ]) || !hasScope(input) || !isTimestamp(input.sinceMs) || input.bundle !== void 0 && (!Array.isArray(input.bundle) || input.bundle.length < 1 || input.bundle.length > 2 || input.bundle.some((bundle) => !isRecord5(bundle) || !hasOnlyKeys(bundle, [
10133
- "field",
10134
- "value",
10135
- "types"
10136
- ]) || bundle.field !== "from_bundle_id" && bundle.field !== "to_bundle_id" || !isText2(bundle.value) || !Array.isArray(bundle.types) || bundle.types.length === 0 || bundle.types.some((type) => ![
10137
- "UNCHANGED",
10138
- "UPDATE_DOWNLOADED",
10139
- "UPDATE_APPLIED",
10140
- "RECOVERED"
10141
- ].includes(type))))) invalidQuery();
10201
+ "sinceMs"
10202
+ ]) || !hasScope(input) || !isTimestamp(input.sinceMs)) invalidQuery();
10142
10203
  return validateCount(await model.countLatestEvents(input));
10143
10204
  },
10205
+ async countReleaseInstallations(input) {
10206
+ if (!isRecord5(input) || !hasOnlyKeys(input, [
10207
+ "platform",
10208
+ "channel",
10209
+ "sinceMs",
10210
+ "releaseId"
10211
+ ]) || !hasScope(input) || !isTimestamp(input.sinceMs) || !isText2(input.releaseId)) invalidQuery();
10212
+ const counts = await model.countReleaseInstallations(input);
10213
+ if (!isRecord5(counts) || !hasOnlyKeys(counts, ["installations", "release"])) invalidResult();
10214
+ return {
10215
+ installations: validateCount(counts.installations),
10216
+ release: validateCount(counts.release)
10217
+ };
10218
+ },
10144
10219
  async countEvents(input) {
10145
10220
  if (!isRecord5(input) || !hasOnlyKeys(input, [
10146
10221
  "filter",
@@ -10193,6 +10268,7 @@ var createInsightsModel = /* @__PURE__ */ __name((api) => createValidatedInsight
10193
10268
  listEvents: api.listEvents,
10194
10269
  findLatestEvents: api.findLatestEvents,
10195
10270
  countLatestEvents: api.countLatestEvents,
10271
+ countReleaseInstallations: api.countReleaseInstallations,
10196
10272
  countEvents: api.countEvents,
10197
10273
  countEventSeries: api.countEventSeries,
10198
10274
  getReleaseActivity: api.getReleaseActivity,
@@ -10584,31 +10660,31 @@ var readScope = /* @__PURE__ */ __name((input) => {
10584
10660
  }, "readScope");
10585
10661
  var bundleFilter = /* @__PURE__ */ __name((input) => {
10586
10662
  const scope = readScope(input);
10587
- const bundleId = requireString(input.bundleId, "bundle ID", MAX_EVENT_ID_LENGTH);
10663
+ const releaseId = requireString(input.releaseId, "bundle ID", MAX_EVENT_ID_LENGTH);
10588
10664
  switch (input.outcome) {
10589
10665
  case "downloaded":
10590
10666
  return {
10591
10667
  ...scope,
10592
10668
  type: "UPDATE_DOWNLOADED",
10593
- toBundleId: bundleId
10669
+ toReleaseId: releaseId
10594
10670
  };
10595
10671
  case "applied":
10596
10672
  return {
10597
10673
  ...scope,
10598
10674
  type: "UPDATE_APPLIED",
10599
- toBundleId: bundleId
10675
+ toReleaseId: releaseId
10600
10676
  };
10601
10677
  case "recovered":
10602
10678
  return {
10603
10679
  ...scope,
10604
10680
  type: "RECOVERED",
10605
- fromBundleId: bundleId
10681
+ fromReleaseId: releaseId
10606
10682
  };
10607
10683
  case "failed":
10608
10684
  return {
10609
10685
  ...scope,
10610
10686
  type: "UPDATE_FAILED",
10611
- toBundleId: bundleId
10687
+ toReleaseId: releaseId
10612
10688
  };
10613
10689
  default:
10614
10690
  throw new InsightsBadRequestError("Invalid Insights outcome.");
@@ -10622,7 +10698,7 @@ var sameFilter = /* @__PURE__ */ __name((left, right) => {
10622
10698
  case "installationMovement":
10623
10699
  return left.installId === right.installId;
10624
10700
  case "bundle":
10625
- return left.platform === right.platform && left.channel === right.channel && left.type === right.type && (right.type === "RECOVERED" ? left.fromBundleId === right.fromBundleId : left.toBundleId === right.toBundleId);
10701
+ return left.platform === right.platform && left.channel === right.channel && left.type === right.type && (right.type === "RECOVERED" ? left.fromReleaseId === right.fromReleaseId : left.toReleaseId === right.toReleaseId);
10626
10702
  }
10627
10703
  }, "sameFilter");
10628
10704
  var readEventCursor = /* @__PURE__ */ __name((value, filter) => {
@@ -10659,7 +10735,7 @@ var assertEventRows = /* @__PURE__ */ __name((rows, input) => {
10659
10735
  for (let index = 0; index < rows.length; index += 1) {
10660
10736
  const row = rows[index];
10661
10737
  const previous = rows[index - 1];
10662
- if (!row || typeof row.id !== "string" || !isUUIDv7(row.id) || !Number.isSafeInteger(row.received_at_ms) || row.received_at_ms >= input.beforeReceivedAtMs || row.received_at_ms < input.sinceMs || previous !== void 0 && compareEventNewest(previous, row) >= 0 || input.after !== void 0 && !isAfterEventCursor(row, input.after) || input.filter.kind === "installationMovement" && (row.install_id !== input.filter.installId || !isInsightsMovementEvent(row)) || input.filter.kind === "bundle" && (row.platform !== input.filter.platform || row.channel !== input.filter.channel || row.type !== input.filter.type || (input.filter.type === "RECOVERED" ? row.from_bundle_id !== input.filter.fromBundleId : row.to_bundle_id !== input.filter.toBundleId))) throw new Error("Insights database returned invalid event rows.");
10738
+ if (!row || typeof row.id !== "string" || !isUUIDv7(row.id) || !Number.isSafeInteger(row.received_at_ms) || row.received_at_ms >= input.beforeReceivedAtMs || row.received_at_ms < input.sinceMs || previous !== void 0 && compareEventNewest(previous, row) >= 0 || input.after !== void 0 && !isAfterEventCursor(row, input.after) || input.filter.kind === "installationMovement" && (row.install_id !== input.filter.installId || !isInsightsMovementEvent(row)) || input.filter.kind === "bundle" && (row.platform !== input.filter.platform || row.channel !== input.filter.channel || row.type !== input.filter.type || (input.filter.type === "RECOVERED" ? (row.from_release_id ?? row.from_bundle_id) !== input.filter.fromReleaseId : (row.to_release_id ?? row.to_bundle_id) !== input.filter.toReleaseId))) throw new Error("Insights database returned invalid event rows.");
10663
10739
  }
10664
10740
  }, "assertEventRows");
10665
10741
  var toFailure = /* @__PURE__ */ __name((failure) => ({
@@ -10698,6 +10774,7 @@ var eventDetails = /* @__PURE__ */ __name(({ type, metadata }) => ({
10698
10774
  ...metadata.implied_download === true ? { impliedDownload: true } : {}
10699
10775
  }), "eventDetails");
10700
10776
  var toEventHistoryRow = /* @__PURE__ */ __name((row) => ({
10777
+ ...row.from_release_id == null ? {} : { fromReleaseId: row.from_release_id },
10701
10778
  ...row.to_release_id == null ? {} : { toReleaseId: row.to_release_id },
10702
10779
  ...row.metadata.sdk_version == null ? {} : { sdkVersion: row.metadata.sdk_version },
10703
10780
  ...row.metadata.min_bundle_id === void 0 ? {} : { minBundleId: row.metadata.min_bundle_id },
@@ -10722,6 +10799,7 @@ var toInstallationRow = /* @__PURE__ */ __name((row) => ({
10722
10799
  cohort: row.metadata.cohort,
10723
10800
  installId: row.install_id,
10724
10801
  lastKnownBundleId: row.type === "UPDATE_DOWNLOADED" ? row.from_bundle_id : row.to_bundle_id,
10802
+ lastKnownReleaseId: row.type === "UPDATE_DOWNLOADED" ? row.from_release_id : row.to_release_id,
10725
10803
  pendingBundleId: row.type === "UPDATE_DOWNLOADED" ? row.to_bundle_id : null,
10726
10804
  pendingReleaseId: row.type === "UPDATE_DOWNLOADED" ? row.to_release_id : null,
10727
10805
  latestStatus: row.type,
@@ -10844,46 +10922,33 @@ var createInsightsProvider = /* @__PURE__ */ __name((model) => Object.freeze({
10844
10922
  measuredAtMs: Date.now()
10845
10923
  };
10846
10924
  }, "measure");
10847
- const bundleId = input.bundleId === void 0 ? void 0 : requireString(input.bundleId, "bundle ID", MAX_EVENT_ID_LENGTH);
10848
- const reporting = measure(model.countLatestEvents({
10849
- ...scope,
10850
- sinceMs
10851
- }));
10852
- if (bundleId === void 0) return {
10925
+ const releaseId = input.releaseId === void 0 ? void 0 : requireString(input.releaseId, "bundle ID", MAX_EVENT_ID_LENGTH);
10926
+ if (releaseId === void 0) return {
10853
10927
  ...scope,
10854
10928
  window: window2,
10855
10929
  sinceMs,
10856
10930
  beforeReceivedAtMs,
10857
- reportingInstallations: await reporting
10931
+ reportingInstallations: await measure(model.countLatestEvents({
10932
+ ...scope,
10933
+ sinceMs
10934
+ }))
10858
10935
  };
10936
+ const installations2 = model.countReleaseInstallations({
10937
+ ...scope,
10938
+ sinceMs,
10939
+ releaseId
10940
+ }).then(async (counts) => [await measure(Promise.resolve(counts.installations)), await measure(Promise.resolve(counts.release))]);
10859
10941
  const countOutcome2 = /* @__PURE__ */ __name((outcome) => measure(model.countEvents({
10860
10942
  filter: bundleFilter({
10861
10943
  ...scope,
10862
- bundleId,
10944
+ releaseId,
10863
10945
  outcome
10864
10946
  }),
10865
10947
  sinceMs,
10866
10948
  beforeReceivedAtMs
10867
10949
  })), "countOutcome");
10868
- const [reportingInstallations, bundleInstallations, downloadedReports, appliedReports, recoveredReports, failedReports] = await Promise.all([
10869
- reporting,
10870
- measure(model.countLatestEvents({
10871
- ...scope,
10872
- sinceMs,
10873
- bundle: [{
10874
- field: "from_bundle_id",
10875
- value: bundleId,
10876
- types: ["UPDATE_DOWNLOADED"]
10877
- }, {
10878
- field: "to_bundle_id",
10879
- value: bundleId,
10880
- types: [
10881
- "UNCHANGED",
10882
- "UPDATE_APPLIED",
10883
- "RECOVERED"
10884
- ]
10885
- }]
10886
- })),
10950
+ const [[reportingInstallations, bundleInstallations], downloadedReports, appliedReports, recoveredReports, failedReports] = await Promise.all([
10951
+ installations2,
10887
10952
  countOutcome2("downloaded"),
10888
10953
  countOutcome2("applied"),
10889
10954
  countOutcome2("recovered"),
@@ -10896,7 +10961,7 @@ var createInsightsProvider = /* @__PURE__ */ __name((model) => Object.freeze({
10896
10961
  beforeReceivedAtMs,
10897
10962
  reportingInstallations,
10898
10963
  bundle: {
10899
- bundleId,
10964
+ releaseId,
10900
10965
  reportingInstallations: bundleInstallations,
10901
10966
  downloadedReports,
10902
10967
  appliedReports,
@@ -11021,8 +11086,6 @@ var gaugeSlot = /* @__PURE__ */ __name((head) => ({
11021
11086
  releaseId: head.type === "UPDATE_DOWNLOADED" ? head.from_release_id : head.to_release_id,
11022
11087
  builtinBundleId: runningBuiltinBundleId(head)
11023
11088
  }), "gaugeSlot");
11024
- var PAIR_FIELD = "from_to";
11025
- var bundlePairKey = /* @__PURE__ */ __name((from, to) => insightsKey(`${from.length}:${from}${to.length}:${to}`), "bundlePairKey");
11026
11089
  var countHead = /* @__PURE__ */ __name((tx, head, delta) => {
11027
11090
  const { bucket, releaseId, builtinBundleId } = gaugeSlot(head);
11028
11091
  const shardBy = head.install_id;
@@ -11041,26 +11104,6 @@ var countHead = /* @__PURE__ */ __name((tx, head, delta) => {
11041
11104
  builtin_bundle_id: builtinBundleId,
11042
11105
  bucket_start_ms: bucket
11043
11106
  }, { latest_installations: delta }, { shardBy });
11044
- for (const field of ["from_bundle_id", "to_bundle_id"]) {
11045
- const bundleId = head[field];
11046
- if (bundleId === null) continue;
11047
- tx.aggregate("insights_latest_by_bundle", {
11048
- platform: head.platform,
11049
- channel: head.channel,
11050
- bundle_field: field,
11051
- bundle_id: bundleId,
11052
- type: head.type,
11053
- bucket_start_ms: bucket
11054
- }, { installations: delta }, { shardBy });
11055
- }
11056
- if (head.from_bundle_id !== null) tx.aggregate("insights_latest_by_bundle", {
11057
- platform: head.platform,
11058
- channel: head.channel,
11059
- bundle_field: PAIR_FIELD,
11060
- bundle_id: bundlePairKey(head.from_bundle_id, head.to_bundle_id),
11061
- type: head.type,
11062
- bucket_start_ms: bucket
11063
- }, { installations: delta }, { shardBy });
11064
11107
  }, "countHead");
11065
11108
  var PERIOD_MODELS = {
11066
11109
  hour: {
@@ -11110,18 +11153,13 @@ var countEvent = /* @__PURE__ */ __name((tx, event) => {
11110
11153
  }, "countEvent");
11111
11154
  var countOutcome = /* @__PURE__ */ __name((tx, event) => {
11112
11155
  const shardBy = event.install_id;
11113
- const hour2 = /* @__PURE__ */ __name((type, bundleRef) => tx.aggregate("insights_outcomes", {
11156
+ for (const [type, bundleRef] of outcomeRefsOf(event)) tx.aggregate("insights_outcomes", {
11114
11157
  platform: event.platform,
11115
11158
  channel: event.channel,
11116
11159
  type,
11117
11160
  bundle_ref: bundleRef,
11118
11161
  bucket_start_ms: hourOf(event.received_at_ms)
11119
- }, { events: 1 }, { shardBy }), "hour");
11120
- for (const bundleRef of bundleRefsOf(event)) hour2(event.type, bundleRef);
11121
- if (event.metadata.implied_download === true) hour2("UPDATE_DOWNLOADED", bundleRefOfFilter({
11122
- type: "UPDATE_DOWNLOADED",
11123
- toBundleId: event.type === "RECOVERED" ? event.from_bundle_id : event.to_bundle_id
11124
- }));
11162
+ }, { events: 1 }, { shardBy });
11125
11163
  tx.aggregate("insights_outcomes", {
11126
11164
  ...DAILY_EVENTS,
11127
11165
  bucket_start_ms: dayOf(event.received_at_ms)
@@ -11339,7 +11377,7 @@ var recordEvent = /* @__PURE__ */ __name((db, event) => {
11339
11377
  }, "recordEvent");
11340
11378
 
11341
11379
  // ../insights/dist/server/reads.mjs
11342
- var PAGE5 = 500;
11380
+ var PAGE6 = 500;
11343
11381
  var hourFloor = /* @__PURE__ */ __name((ms) => ms - ms % HOUR_MS2, "hourFloor");
11344
11382
  var hourCeil = /* @__PURE__ */ __name((ms) => hourFloor(ms + HOUR_MS2 - 1), "hourCeil");
11345
11383
  var dayFloor = /* @__PURE__ */ __name((ms) => ms - ms % DAY_MS2, "dayFloor");
@@ -11463,7 +11501,7 @@ var countRaw = /* @__PURE__ */ __name(async (db, filter, from, to) => {
11463
11501
  gte: from,
11464
11502
  lt: to
11465
11503
  },
11466
- limit: PAGE5,
11504
+ limit: PAGE6,
11467
11505
  ...page2
11468
11506
  }));
11469
11507
  total += rows.length;
@@ -11482,7 +11520,7 @@ var countEvents = /* @__PURE__ */ __name(async (db, input) => {
11482
11520
  gte: start,
11483
11521
  lt: end
11484
11522
  },
11485
- limit: PAGE5,
11523
+ limit: PAGE6,
11486
11524
  ...page2
11487
11525
  }));
11488
11526
  return await countRaw(db, filter, sinceMs, start) + hours.reduce((sum, row) => sum + row.events, 0) + await countRaw(db, filter, end, beforeReceivedAtMs);
@@ -11498,7 +11536,7 @@ var countEventSeries = /* @__PURE__ */ __name(async (db, input) => {
11498
11536
  gte: start,
11499
11537
  lt: end
11500
11538
  },
11501
- limit: PAGE5,
11539
+ limit: PAGE6,
11502
11540
  ...page2
11503
11541
  }));
11504
11542
  for (const row of hours) events[Math.floor((row.bucket_start_ms - start) / intervalMs)] += row.events;
@@ -11507,62 +11545,50 @@ var countEventSeries = /* @__PURE__ */ __name(async (db, input) => {
11507
11545
  events: count
11508
11546
  }));
11509
11547
  }, "countEventSeries");
11510
- var latestPredicates = /* @__PURE__ */ __name((bundle) => {
11511
- if (bundle === void 0) return void 0;
11512
- return [...new Map(bundle.flatMap(({ field, value, types }) => types.map((type) => [JSON.stringify([
11513
- field,
11514
- value,
11515
- type
11516
- ]), {
11517
- field,
11518
- value,
11519
- type
11520
- }]))).values()];
11521
- }, "latestPredicates");
11522
- var countLatestEvents = /* @__PURE__ */ __name(async (db, input) => {
11523
- const { platform, channel, sinceMs: start } = input;
11548
+ var distributionRows = /* @__PURE__ */ __name((db, { platform, channel, sinceMs: start }) => {
11524
11549
  if (start % 864e5 !== 0) throw new DatabaseAdapterInputError("invalid-query");
11525
- const predicates = latestPredicates(input.bundle);
11550
+ return drain2((page2) => db.findAggregates("insights_distribution", {
11551
+ index: "byScope",
11552
+ where: {
11553
+ channel,
11554
+ platform
11555
+ },
11556
+ range: { gte: start },
11557
+ limit: PAGE6,
11558
+ ...page2
11559
+ }));
11560
+ }, "distributionRows");
11561
+ var installations = /* @__PURE__ */ __name((count) => Math.max(0, count), "installations");
11562
+ var countLatestEvents = /* @__PURE__ */ __name(async (db, input) => installations((await distributionRows(db, input)).reduce((sum, row) => sum + row.latest_installations, 0)), "countLatestEvents");
11563
+ var countReleaseInstallations = /* @__PURE__ */ __name(async (db, input) => {
11564
+ const { platform, channel, sinceMs: start, releaseId } = input;
11526
11565
  let total = 0;
11527
- if (predicates === void 0) {
11528
- const rows = await drain2((page2) => db.findAggregates("insights_distribution", {
11566
+ let release = 0;
11567
+ let named = false;
11568
+ for (const row of await distributionRows(db, input)) {
11569
+ total += row.latest_installations;
11570
+ if (row.release_id !== releaseId) continue;
11571
+ named = true;
11572
+ release += row.latest_installations;
11573
+ }
11574
+ if (!named) {
11575
+ const builtin = await drain2((page2) => db.findAggregates("insights_builtin_distribution", {
11529
11576
  index: "byScope",
11530
11577
  where: {
11531
11578
  channel,
11532
11579
  platform
11533
11580
  },
11534
11581
  range: { gte: start },
11535
- limit: PAGE5,
11582
+ limit: PAGE6,
11536
11583
  ...page2
11537
11584
  }));
11538
- total += rows.reduce((sum, row) => sum + row.latest_installations, 0);
11539
- } else {
11540
- const gauge = /* @__PURE__ */ __name(async (bundleField, bundleId, type) => {
11541
- return (await drain2((page2) => db.findAggregates("insights_latest_by_bundle", {
11542
- index: "byBundle",
11543
- where: {
11544
- platform,
11545
- channel,
11546
- bundle_field: bundleField,
11547
- bundle_id: bundleId,
11548
- type
11549
- },
11550
- range: { gte: start },
11551
- limit: PAGE5,
11552
- ...page2
11553
- }))).reduce((sum, row) => sum + row.installations, 0);
11554
- }, "gauge");
11555
- for (const { field, value, type } of predicates) total += await gauge(field, value, type);
11556
- for (const from of predicates) {
11557
- if (from.field !== "from_bundle_id") continue;
11558
- for (const to of predicates) {
11559
- if (to.field !== "to_bundle_id" || to.type !== from.type) continue;
11560
- total -= await gauge(PAIR_FIELD, bundlePairKey(from.value, to.value), from.type);
11561
- }
11562
- }
11585
+ for (const row of builtin) if (row.release_id === "" && row.builtin_bundle_id === releaseId) release += row.latest_installations;
11563
11586
  }
11564
- return Math.max(0, total);
11565
- }, "countLatestEvents");
11587
+ return {
11588
+ installations: installations(total),
11589
+ release: installations(release)
11590
+ };
11591
+ }, "countReleaseInstallations");
11566
11592
  var retained = /* @__PURE__ */ __name((now, { rawDays, dailyDays }) => ({
11567
11593
  hour: hourCeil(now - rawDays * DAY_MS2),
11568
11594
  day: dayCeil(now - dailyDays * DAY_MS2)
@@ -11624,7 +11650,7 @@ var windowRows = /* @__PURE__ */ __name(async (db, models, parts, range, days, h
11624
11650
  gte: period.start,
11625
11651
  lt: period.end
11626
11652
  },
11627
- limit: PAGE5,
11653
+ limit: PAGE6,
11628
11654
  ...page2
11629
11655
  })));
11630
11656
  }
@@ -11653,7 +11679,7 @@ var lifetimeMetrics = /* @__PURE__ */ __name(async (db, release) => {
11653
11679
  gte: 0,
11654
11680
  lte: 0
11655
11681
  },
11656
- limit: PAGE5
11682
+ limit: PAGE6
11657
11683
  })).rows;
11658
11684
  return {
11659
11685
  downloads: lifetime?.downloads ?? 0,
@@ -11680,9 +11706,9 @@ var getReleaseActivity = /* @__PURE__ */ __name(async (db, input, now) => {
11680
11706
  measuredAtMs
11681
11707
  };
11682
11708
  }, "getReleaseActivity");
11683
- var byInstallations = /* @__PURE__ */ __name((values) => [...values].flatMap(([name, installations]) => installations > 0 ? [{
11709
+ var byInstallations = /* @__PURE__ */ __name((values) => [...values].flatMap(([name, installations2]) => installations2 > 0 ? [{
11684
11710
  name,
11685
- installations
11711
+ installations: installations2
11686
11712
  }] : []).sort((left, right) => right.installations - left.installations || left.name.localeCompare(right.name, "en", { numeric: true })), "byInstallations");
11687
11713
  var getAppUsage = /* @__PURE__ */ __name(async (db, input, now, retention) => {
11688
11714
  const { timeRange, intervalMs } = input;
@@ -11705,7 +11731,7 @@ var getAppUsage = /* @__PURE__ */ __name(async (db, input, now, retention) => {
11705
11731
  platform
11706
11732
  },
11707
11733
  range,
11708
- limit: PAGE5,
11734
+ limit: PAGE6,
11709
11735
  ...page2
11710
11736
  }) : db.findAggregates("insights_distribution", {
11711
11737
  index: "byVersion",
@@ -11715,7 +11741,7 @@ var getAppUsage = /* @__PURE__ */ __name(async (db, input, now, retention) => {
11715
11741
  app_version: input.appVersion
11716
11742
  },
11717
11743
  range,
11718
- limit: PAGE5,
11744
+ limit: PAGE6,
11719
11745
  ...page2
11720
11746
  })));
11721
11747
  builtinDistribution.push(...await drain2((page2) => input.appVersion === void 0 ? db.findAggregates("insights_builtin_distribution", {
@@ -11725,7 +11751,7 @@ var getAppUsage = /* @__PURE__ */ __name(async (db, input, now, retention) => {
11725
11751
  platform
11726
11752
  },
11727
11753
  range,
11728
- limit: PAGE5,
11754
+ limit: PAGE6,
11729
11755
  ...page2
11730
11756
  }) : db.findAggregates("insights_builtin_distribution", {
11731
11757
  index: "byVersion",
@@ -11735,7 +11761,7 @@ var getAppUsage = /* @__PURE__ */ __name(async (db, input, now, retention) => {
11735
11761
  app_version: input.appVersion
11736
11762
  },
11737
11763
  range,
11738
- limit: PAGE5,
11764
+ limit: PAGE6,
11739
11765
  ...page2
11740
11766
  })));
11741
11767
  }
@@ -11781,7 +11807,7 @@ var getAppUsage = /* @__PURE__ */ __name(async (db, input, now, retention) => {
11781
11807
  appVersions: sortedVersions.map(({ name }) => name),
11782
11808
  versions: sortedVersions,
11783
11809
  platforms: byInstallations(platforms),
11784
- bundleDistribution: [...bundles2.values()].filter(({ installations }) => installations > 0),
11810
+ bundleDistribution: [...bundles2.values()].filter(({ installations: installations2 }) => installations2 > 0),
11785
11811
  measuredAtMs: at
11786
11812
  };
11787
11813
  }, "getAppUsage");
@@ -11837,7 +11863,7 @@ var breakdownOf = /* @__PURE__ */ __name((rows) => {
11837
11863
  });
11838
11864
  }, "breakdownOf");
11839
11865
  var FAILURE_ROWS_PER_HOUR = 20;
11840
- var FAILURE_QUERY_LIMIT = Math.min(PAGE5, 168);
11866
+ var FAILURE_QUERY_LIMIT = Math.min(PAGE6, 168);
11841
11867
  var failureRowsByHour = /* @__PURE__ */ __name(async (db, platform, channel, timeRange) => {
11842
11868
  const read = /* @__PURE__ */ __name((end2, cursor2) => db.findAggregates("insights_failures", {
11843
11869
  index: "byScope",
@@ -11913,7 +11939,7 @@ var getUpdateFailures = /* @__PURE__ */ __name(async (db, input, now, retention)
11913
11939
  gte: 0,
11914
11940
  lte: 0
11915
11941
  },
11916
- limit: PAGE5
11942
+ limit: PAGE6
11917
11943
  }), db.findAggregates("insights_sketches_lifetime", {
11918
11944
  index: "window",
11919
11945
  where: where(failure),
@@ -11921,7 +11947,7 @@ var getUpdateFailures = /* @__PURE__ */ __name(async (db, input, now, retention)
11921
11947
  gte: 0,
11922
11948
  lte: 0
11923
11949
  },
11924
- limit: PAGE5
11950
+ limit: PAGE6
11925
11951
  })]);
11926
11952
  const lifetime = counters3.rows[0];
11927
11953
  return {
@@ -11978,6 +12004,20 @@ var getUpdateFailures = /* @__PURE__ */ __name(async (db, input, now, retention)
11978
12004
  };
11979
12005
  }, "getUpdateFailures");
11980
12006
 
12007
+ // ../insights/dist/server/wire.mjs
12008
+ var RELEASE_ID_PARAM = "bundleId";
12009
+ var toWireOverview = /* @__PURE__ */ __name(({ bundle, ...overview }) => {
12010
+ if (bundle === void 0) return overview;
12011
+ const { releaseId, ...counts } = bundle;
12012
+ return {
12013
+ ...overview,
12014
+ bundle: {
12015
+ ...counts,
12016
+ bundleId: releaseId
12017
+ }
12018
+ };
12019
+ }, "toWireOverview");
12020
+
11981
12021
  // ../insights/dist/server/queryInput.mjs
11982
12022
  var MAX_PAGE_LIMIT2 = 100;
11983
12023
  var MAX_IDENTITY_LENGTH3 = 255;
@@ -12022,7 +12062,7 @@ var readScope2 = /* @__PURE__ */ __name((url) => {
12022
12062
  var parseEventPageInput = /* @__PURE__ */ __name((request) => {
12023
12063
  const url = new URL(request.url);
12024
12064
  const bundleFields = [
12025
- "bundleId",
12065
+ RELEASE_ID_PARAM,
12026
12066
  "outcome",
12027
12067
  "platform",
12028
12068
  "channel"
@@ -12033,7 +12073,7 @@ var parseEventPageInput = /* @__PURE__ */ __name((request) => {
12033
12073
  if (outcome !== "downloaded" && outcome !== "applied" && outcome !== "recovered" && outcome !== "failed") throw new InsightsBadRequestError("Invalid 'outcome' query parameter.");
12034
12074
  bundle = {
12035
12075
  ...readScope2(url),
12036
- bundleId: readId(url, "bundleId", 1024),
12076
+ releaseId: readId(url, RELEASE_ID_PARAM, 1024),
12037
12077
  outcome
12038
12078
  };
12039
12079
  }
@@ -12059,11 +12099,10 @@ var parseReportingOverviewInput = /* @__PURE__ */ __name((request) => {
12059
12099
  const url = new URL(request.url);
12060
12100
  const window2 = readSingle(url, "window") ?? "30d";
12061
12101
  if (window2 !== "24h" && window2 !== "7d" && window2 !== "30d") throw new InsightsBadRequestError("Invalid 'window' query parameter.");
12062
- const bundleId = readSingle(url, "bundleId");
12063
12102
  return {
12064
12103
  ...readScope2(url),
12065
12104
  window: window2,
12066
- ...bundleId === void 0 ? {} : { bundleId: readId(url, "bundleId", 1024) }
12105
+ ...readSingle(url, "bundleId") === void 0 ? {} : { releaseId: readId(url, RELEASE_ID_PARAM, 1024) }
12067
12106
  };
12068
12107
  }, "parseReportingOverviewInput");
12069
12108
 
@@ -12142,7 +12181,7 @@ var createInsightsRouteHandlers = /* @__PURE__ */ __name((provider) => ({
12142
12181
  await provider.appendBundleEvent(await parseBundleEventRequest(request));
12143
12182
  return new Response(null, { status: 204 });
12144
12183
  }), "appendBundleEvent"),
12145
- getReportingOverview: /* @__PURE__ */ __name((_params, request) => query(() => provider.getReportingOverview(parseReportingOverviewInput(request))), "getReportingOverview"),
12184
+ getReportingOverview: /* @__PURE__ */ __name((_params, request) => query(async () => toWireOverview(await provider.getReportingOverview(parseReportingOverviewInput(request)))), "getReportingOverview"),
12146
12185
  getInstallation: /* @__PURE__ */ __name((params) => run(async () => {
12147
12186
  const installation = await provider.getInstallation({ installId: requireParam(params, "installId") });
12148
12187
  return installation === null ? json2({ error: "Installation not found" }, 404) : json2(installation, 200);
@@ -12164,12 +12203,20 @@ var createInsightsApi = /* @__PURE__ */ __name((db, now, retention) => ({
12164
12203
  listEvents: /* @__PURE__ */ __name((input) => listEvents(db, input), "listEvents"),
12165
12204
  findLatestEvents: /* @__PURE__ */ __name((input) => findLatestEvents(db, input), "findLatestEvents"),
12166
12205
  countLatestEvents: /* @__PURE__ */ __name((input) => countLatestEvents(db, input), "countLatestEvents"),
12206
+ countReleaseInstallations: /* @__PURE__ */ __name((input) => countReleaseInstallations(db, input), "countReleaseInstallations"),
12167
12207
  countEvents: /* @__PURE__ */ __name((input) => countEvents(db, input), "countEvents"),
12168
12208
  countEventSeries: /* @__PURE__ */ __name((input) => countEventSeries(db, input), "countEventSeries"),
12169
12209
  getReleaseActivity: /* @__PURE__ */ __name((input) => getReleaseActivity(db, input, now), "getReleaseActivity"),
12170
12210
  getAppUsage: /* @__PURE__ */ __name((input) => getAppUsage(db, input, now, retention), "getAppUsage"),
12171
12211
  /** A release's or a channel's update failures, and their breakdown over a time range. */
12172
- getUpdateFailures: /* @__PURE__ */ __name((input) => getUpdateFailures(db, input, now, retention), "getUpdateFailures")
12212
+ getUpdateFailures: /* @__PURE__ */ __name((input) => getUpdateFailures(db, input, now, retention), "getUpdateFailures"),
12213
+ /**
12214
+ * Moves the stored events a 1.1 server keyed by artifact, and their
12215
+ * outcome counts, to their release, reading every stored event of the
12216
+ * raw period: `hot-updater db migrate` and `hot-updater init` run it once
12217
+ * a database, and an interrupted run can run again.
12218
+ */
12219
+ backfillReleaseRefs: /* @__PURE__ */ __name((options) => backfillReleaseRefs(db, now, retention, options), "backfillReleaseRefs")
12173
12220
  }), "createInsightsApi");
12174
12221
  var readFailuresQuery = /* @__PURE__ */ __name((url) => {
12175
12222
  const query2 = url.searchParams;