|
| 1 | +import { env } from "cloudflare:workers"; |
| 2 | +import { fetchWithRetries } from "./fetchWithRetries.js"; |
| 3 | +import { LazyExpirableValue } from "./LazyExpirableValue.js"; |
| 4 | + |
| 5 | +// download counts come from the Analytics Engine dataset that each plugin |
| 6 | +// download writes a data point to (see trackPluginDownload in handleRequest.ts). |
| 7 | +// the dataset only retains roughly the last 90 days, so these are downloads over |
| 8 | +// the trailing 30 days ("monthly") rather than all-time totals. |
| 9 | + |
| 10 | +export interface PluginDownloadCounts { |
| 11 | + // downloads over the last 30 days across every version |
| 12 | + allVersions: number; |
| 13 | + // downloads over the last 30 days keyed by the tag that appeared in the request |
| 14 | + // url (a version like "0.1.0" or the "latest" alias) |
| 15 | + byTag: Map<string, number>; |
| 16 | +} |
| 17 | + |
| 18 | +const DATASET = "dprint-plugin-downloads"; |
| 19 | +const WINDOW_DAYS = 30; |
| 20 | + |
| 21 | +const downloadCounts = new LazyExpirableValue<Map<string, PluginDownloadCounts>>({ |
| 22 | + expiryMs: 5 * 60 * 1_000, // keep for 5 minutes |
| 23 | + createValue: queryDownloadCounts, |
| 24 | +}); |
| 25 | + |
| 26 | +/** Gets the download counts per plugin keyed by `username/repo`. */ |
| 27 | +export async function getDownloadCounts(): Promise<Map<string, PluginDownloadCounts>> { |
| 28 | + try { |
| 29 | + return await downloadCounts.getValue(); |
| 30 | + } catch (err) { |
| 31 | + // don't let analytics being unavailable break the info file build |
| 32 | + console.error("Failed to get download counts from analytics.", err); |
| 33 | + return new Map(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +async function queryDownloadCounts(): Promise<Map<string, PluginDownloadCounts>> { |
| 38 | + const accountId = env.CLOUDFLARE_ACCOUNT_ID; |
| 39 | + const token = env.DPRINT_PLUGINS_ANALYTICS_TOKEN; |
| 40 | + const result = new Map<string, PluginDownloadCounts>(); |
| 41 | + if (accountId == null || token == null) { |
| 42 | + console.warn("Analytics credentials not configured. Skipping download counts."); |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + // index1 is `username/repo`, blob1 is the tag, double1 is 1 per download |
| 47 | + const query = `SELECT index1 AS plugin, blob1 AS tag, sum(_sample_interval * double1) AS downloads` |
| 48 | + + ` FROM "${DATASET}"` |
| 49 | + + ` WHERE timestamp > NOW() - INTERVAL '${WINDOW_DAYS}' DAY` |
| 50 | + + ` GROUP BY plugin, tag LIMIT 10000`; |
| 51 | + const response = await fetchWithRetries( |
| 52 | + `https://api.cloudflare.com/client/v4/accounts/${accountId}/analytics_engine/sql`, |
| 53 | + { |
| 54 | + method: "POST", |
| 55 | + headers: { |
| 56 | + "authorization": `Bearer ${token}`, |
| 57 | + "content-type": "text/plain", |
| 58 | + }, |
| 59 | + body: query, |
| 60 | + }, |
| 61 | + ); |
| 62 | + if (!response.ok) { |
| 63 | + const text = await response.text(); |
| 64 | + throw new Error(`Analytics query failed: ${response.status}\n\n${text}`); |
| 65 | + } |
| 66 | + |
| 67 | + const body = await response.json() as { |
| 68 | + data: { plugin: string; tag: string; downloads: number | string }[]; |
| 69 | + }; |
| 70 | + for (const row of body.data) { |
| 71 | + const downloads = Number(row.downloads) || 0; |
| 72 | + const counts = getOrCreateCounts(result, row.plugin); |
| 73 | + counts.allVersions += downloads; |
| 74 | + counts.byTag.set(row.tag, (counts.byTag.get(row.tag) ?? 0) + downloads); |
| 75 | + } |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +function getOrCreateCounts(map: Map<string, PluginDownloadCounts>, plugin: string) { |
| 80 | + let counts = map.get(plugin); |
| 81 | + if (counts == null) { |
| 82 | + counts = { allVersions: 0, byTag: new Map() }; |
| 83 | + map.set(plugin, counts); |
| 84 | + } |
| 85 | + return counts; |
| 86 | +} |
0 commit comments