Skip to content
Merged
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
14 changes: 14 additions & 0 deletions lib/telemetry-withheld.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ function topMeasuredVersion(versions) {
return top;
}

/**
* The change from an earlier count to a later one, computed from the two
* floored counts. When either count is withheld, or missing because no
* earlier snapshot is stored, there is no change to state: it is null, never
* 0 and never the difference of the raw counts. A change against a withheld
* count would read as a real rise or fall, or give back the count it hides.
*/
function measuredChange(latest, prior) {
const now = floorCount(latest);
const before = floorCount(prior);
return now === null || before === null ? null : now - before;
}

/** The provenance text with the withheld note added once. */
function withWithheldNote(text) {
const base = typeof text === 'string' ? text.trim() : '';
Expand All @@ -84,6 +97,7 @@ module.exports = {
byMeasuredDesc,
floorCount,
formatTelemetryCount,
measuredChange,
topMeasuredVersion,
withWithheldNote,
};
8 changes: 4 additions & 4 deletions pages/api/telemetry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Database = require('better-sqlite3');
const path = require('path');
const { byMeasuredDesc, floorCount, WITHHELD } = require('../../lib/telemetry-withheld');
const { byMeasuredDesc, floorCount, measuredChange, WITHHELD } = require('../../lib/telemetry-withheld');
const { toolDisplayName } = require('../../lib/telemetry');

/**
Expand Down Expand Up @@ -89,9 +89,9 @@ export default async function handler(req, res) {
mau: floorCount(latest.mau),
engagedMau: floorCount(latest.engaged_mau),
engagedMinDays: latest.engaged_min_days ?? 0,
wauGrowth7d: prior7 ? latest.wau - prior7.wau : 0,
mauGrowth30d: prior30 ? latest.mau - prior30.mau : 0,
engagedGrowth30d: prior30 && prior30.engaged_mau != null ? (latest.engaged_mau ?? 0) - prior30.engaged_mau : 0,
wauGrowth7d: measuredChange(latest.wau, prior7?.wau),
mauGrowth30d: measuredChange(latest.mau, prior30?.mau),
engagedGrowth30d: measuredChange(latest.engaged_mau, prior30?.engaged_mau),
},
tools,
byCountry,
Expand Down
59 changes: 59 additions & 0 deletions test/telemetry-withheld.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,65 @@ test('/api/telemetry serves only floored counts, keeps every row, and orders a w
}
});

// The growth figures /api/telemetry serves from a store holding these
// telemetry snapshots. A count a row leaves out is stored as NULL, as a
// floored store holds a withheld one.
async function servedGrowth(rows) {
const dir = tmp('growth');
try {
mkdirSync(join(dir, 'data'));
storeAt(join(dir, 'data', 'analytics.db'), NULLABLE_SCHEMA, (db) => {
const insert = db.prepare('INSERT INTO telemetry_snapshots (date, wau, mau, engaged_mau) VALUES (@date, @wau, @mau, @engaged_mau)');
for (const row of rows) insert.run({ wau: null, mau: null, engaged_mau: null, ...row });
});
const { wauGrowth7d, mauGrowth30d, engagedGrowth30d } = (await serveTelemetry(dir)).snapshot;
return { wauGrowth7d, mauGrowth30d, engagedGrowth30d };
} finally {
rmSync(dir, { recursive: true, force: true });
}
}

// Each growth figure is taken against the snapshot on or before the latest
// one less its window: wau over 7 days, mau and engaged_mau over 30. Every
// date follows 2026-10-11, the last day before the feed's counts changed
// basis, so no pair here spans that change.
const LATEST = '2026-12-02';
const GROWTH = [
{ field: 'wauGrowth7d', column: 'wau', prior: '2026-11-25' },
{ field: 'mauGrowth30d', column: 'mau', prior: '2026-11-02' },
{ field: 'engagedGrowth30d', column: 'engaged_mau', prior: '2026-11-02' },
];
const shown = (v) => (v === null ? 'NULL' : String(v));

for (const { field, column, prior } of GROWTH) {
// NULL is a withheld count as a floored store holds it, and 9 a count under
// ten as an unfloored store holds it: on either side, the change is null.
for (const [now, before] of [[null, 25], [9, 25], [138, null], [138, 9]]) {
test(`/api/telemetry serves ${field} as null, never 0 and never the raw difference, for a latest ${column} of ${shown(now)} beside a prior of ${shown(before)}`, async () => {
const served = await servedGrowth([{ date: prior, [column]: before }, { date: LATEST, [column]: now }]);
assert.strictEqual(served[field], null);
});
}

test(`/api/telemetry serves ${field} as the difference of two measured counts: a latest ${column} of 138 beside a prior of 93 gives 45`, async () => {
const served = await servedGrowth([{ date: prior, [column]: 93 }, { date: LATEST, [column]: 138 }]);
assert.strictEqual(served[field], 45);
});
}

test('/api/telemetry serves every growth figure as null, never 0, when no earlier snapshot is stored', async () => {
const served = await servedGrowth([{ date: LATEST, wau: 138, mau: 138, engaged_mau: 138 }]);
assert.deepStrictEqual(served, { wauGrowth7d: null, mauGrowth30d: null, engagedGrowth30d: null });
});

test('/api/telemetry serves the 30-day growth figures as null beside a measured 7-day one when no snapshot is stored 30 days back', async () => {
const served = await servedGrowth([
{ date: '2026-11-25', wau: 93, mau: 93, engaged_mau: 93 },
{ date: LATEST, wau: 138, mau: 138, engaged_mau: 138 },
]);
assert.deepStrictEqual(served, { wauGrowth7d: 45, mauGrowth30d: null, engagedGrowth30d: null });
});

test('the collector stores NULL for a withheld figure and for a count from 0 to 9, stores 10 or more as is, and keeps every row', async () => {
const dir = tmp('collect');
try {
Expand Down
Loading