Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions deploy/telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ Authorize a dedicated API key for the endpoint and set
`/run/<id>` URL from the endpoint configuration. Redeploy the site after saving
the variables.

Vercel caches the aggregate page for an hour. The site fetches repository and release counts
from GitHub, SDK downloads from PyPI Stats and npm, and container pulls from
the public package page. Stored registry metrics fill gaps when the public
sources are unavailable. The page shows the latest observation date for each
metric and compares reporting periods when both periods have coverage on
every date.
The site caches pages with warehouse data for an hour.

The site fetches repository and release counts from GitHub, SDK downloads from
PyPI Stats and npm, and container pulls from the public package page. Stored
registry metrics fill gaps when the public sources are unavailable. The page
shows the latest observation date for each metric and compares reporting
periods when both periods have coverage on every date.

The public page supports 30-, 90-, and 365-day windows. Its chart links preserve
the selected filters. CSV links export aggregate activity, lifecycle, download,
Expand Down
18 changes: 16 additions & 2 deletions docs/lib/adoption-data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function readSource(url, options, decode = (response) => response.json())
const response = await fetch(url, {
...options,
redirect: "error",
signal: AbortSignal.timeout(5000),
signal: options?.signal ?? AbortSignal.timeout(5_000),
});
if (response.ok) return await decode(response);
} catch {
Expand All @@ -36,6 +36,8 @@ async function loadWarehouse(env) {
url.searchParams.set("format", "JSON");
const response = await readSource(url, {
headers: { Authorization: `Basic ${Buffer.from(`${keyId}:${keySecret}`).toString("base64")}` },
// An idle warehouse needs time to resume before it runs the query.
signal: AbortSignal.timeout(30_000),
});
const row = response?.data?.[0];
if (datasets.every((name) => Array.isArray(row?.[name]))) {
Expand Down Expand Up @@ -164,11 +166,23 @@ export async function loadAdoption(env = process.env, now = new Date()) {
};
}

const failedRefreshTtlMs = 15_000;
let published;
export function loadPublishedAdoption() {
const now = Date.now();
if (!published || now >= published.expires) {
published = { expires: now + 5 * 60_000, data: loadAdoption(process.env, new Date(now)) };
const refresh = { expires: now + 5 * 60_000 };
refresh.data = loadAdoption(process.env, new Date(now)).then(
(data) => {
if (data.selfHosted.totals === undefined) refresh.expires = Date.now() + failedRefreshTtlMs;
return data;
},
(error) => {
refresh.expires = Date.now() + failedRefreshTtlMs;
throw error;
},
);
published = refresh;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warehouse misses refetch public registries

Medium Severity

loadPublishedAdoption treats any missing selfHosted.totals as a failed refresh and drops the in-process TTL to 15s for the whole loadAdoption result. That condition also holds when warehouse credentials are absent, the endpoint URL is rejected, or the query times out, so GitHub, PyPI, npm, and GHCR are fetched again on every retry. Combined with no-store on the fallback page, origin traffic can exceed GitHub's unauthenticated 60 request/hour limit and hide the OSS metrics the fallback is meant to keep showing.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 4a50c04. Configure here.

}
return published.data;
}
52 changes: 51 additions & 1 deletion docs/lib/adoption-data.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import { test } from "node:test";

import { loadAdoption } from "./adoption-data.mjs";
import { loadAdoption, loadPublishedAdoption } from "./adoption-data.mjs";
import { summarizeOss } from "./adoption-view.mjs";

test("calls the saved aggregate endpoint and reports monthly coverage and missing data", async (t) => {
Expand Down Expand Up @@ -97,3 +97,53 @@ test("keeps Query API credentials off unrecognized endpoint URLs", async (t) =>
assert.ok(requests.every(([host, authorization]) => host !== "external.example" && authorization === undefined));
assert.equal(result.selfHosted.totals, undefined);
});

test("retries a timed-out warehouse and caches the next successful refresh", async (t) => {
let now = Date.UTC(2026, 10, 27);
t.mock.method(Date, "now", () => now);
const originalEnv = process.env;
t.after(() => {
process.env = originalEnv;
});
process.env = {
...originalEnv,
LOOM_ADOPTION_QUERY_ENDPOINT_URL: "https://queries.clickhouse.cloud/run/00000000-0000-0000-0000-000000000001",
LOOM_ADOPTION_QUERY_KEY_ID: "fixture-id",
LOOM_ADOPTION_QUERY_KEY_SECRET: "fixture-secret",
};
t.mock.method(console, "warn", () => {});
const timeoutBySignal = new Map();
t.mock.method(AbortSignal, "timeout", (delay) => {
const { signal } = new AbortController();
timeoutBySignal.set(signal, delay);
return signal;
});
const publicTimeouts = new Set();
const warehouseTimeouts = [];
t.mock.method(globalThis, "fetch", async (input, init) => {
if (new URL(input).hostname !== "queries.clickhouse.cloud") {
publicTimeouts.add(timeoutBySignal.get(init.signal));
return new Response(null, { status: 502 });
}
warehouseTimeouts.push(timeoutBySignal.get(init.signal));
if (warehouseTimeouts.length === 1) throw new DOMException("Warehouse wake-up timed out", "TimeoutError");
return Response.json({ data: [{ totals: [], versions: [], retention: [], activity: [], lifecycle: [], oss: [] }] });
});

const first = loadPublishedAdoption();
assert.equal(loadPublishedAdoption(), first);
now += 30_000;
assert.equal((await first).selfHosted.totals, undefined);

now += 14_999;
assert.equal(loadPublishedAdoption(), first);
assert.equal(warehouseTimeouts.length, 1);

now += 1;
const retry = loadPublishedAdoption();
assert.notEqual(retry, first);
assert.deepEqual((await retry).selfHosted.totals, []);
assert.equal(loadPublishedAdoption(), retry);
assert.deepEqual(warehouseTimeouts, [30_000, 30_000]);
assert.deepEqual([...publicTimeouts], [5_000]);
});
16 changes: 10 additions & 6 deletions docs/pages/adoption.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ import {

export const prerender = false;

Astro.response.headers.set("Cache-Control", "public, max-age=0, must-revalidate");
Astro.response.headers.set("Vercel-CDN-Cache-Control", "public, s-maxage=3600, stale-while-revalidate=86400");

const filters = adoptionFilters(Astro.url.searchParams);
const { today, refreshedAt, selfHosted, openSource } = await loadPublishedAdoption();
const { totals: summaries, retention, versions, activity, lifecycle } = selfHosted;
const warehouseAvailable = Array.isArray(summaries);
if (warehouseAvailable) {
Astro.response.headers.set("Cache-Control", "public, max-age=0, must-revalidate");
Astro.response.headers.set("Vercel-CDN-Cache-Control", "public, s-maxage=3600, stale-while-revalidate=86400");
} else {
Astro.response.headers.set("Cache-Control", "no-store");
}
const totals = summaries?.find(({ range_days, comparison }) => range_days === filters.days && comparison === "current");
const previous = summaries?.find(
({ range_days, comparison }) => range_days === filters.days && comparison === "previous",
Expand Down Expand Up @@ -71,7 +75,7 @@ if (dataset) {
headers: {
"Content-Type": "text/csv; charset=utf-8",
"Content-Disposition": `attachment; filename="loom-${dataset}.csv"`,
"Cache-Control": "public, max-age=300",
"Cache-Control": warehouseAvailable ? "public, max-age=300" : "no-store",
},
});
}
Expand Down Expand Up @@ -196,8 +200,8 @@ const overview = [
</article>;
})}
</div>
{!Array.isArray(summaries) && <p class="notice">Self-hosted metrics will appear when the telemetry warehouse is connected.</p>}
{Array.isArray(summaries) && !totals && <p class="notice">No installations reported in this period.</p>}
{!warehouseAvailable && <p class="notice">Self-hosted metrics will appear when the telemetry warehouse is connected.</p>}
{warehouseAvailable && !totals && <p class="notice">No installations reported in this period.</p>}
{totals && <p class="notice">Latest reporting date <time datetime={totals.last_reported}>{totals.last_reported}</time> UTC. Period changes appear when both windows have a report on every date.</p>}

<article class="card activity" id="installation-activity">
Expand Down
Loading