From 21fab97322de1e36816031201ca526d19fe14042 Mon Sep 17 00:00:00 2001 From: Abdel Fane <32418586+thebenignhacker@users.noreply.github.com> Date: Tue, 3 Nov 2026 03:39:11 -0700 Subject: [PATCH] fix: serve telemetry growth as null beside a withheld count /api/telemetry served wauGrowth7d, mauGrowth30d and engagedGrowth30d as the difference of the stored counts. A withheld count is stored as NULL, which the subtraction took as 0, so a change figure was served beside a count shown as withheld: a latest wau of NULL beside a prior of 25 gave -25, and a count of 9 left in an older store beside 25 gave -16. With no earlier snapshot, each figure was served as 0. Each figure is now computed from the floored counts on both sides (measuredChange in lib/telemetry-withheld.js) and is null when either side is withheld or no earlier snapshot is stored: never 0 and never the raw difference. Two measured counts still give their difference, so 138 beside 93 gives 45. The dashboard already leaves such a change out, and no other served field changes. --- lib/telemetry-withheld.js | 14 ++++++++ pages/api/telemetry.js | 8 ++--- test/telemetry-withheld.test.js | 59 +++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/lib/telemetry-withheld.js b/lib/telemetry-withheld.js index 1d21740b..fa548ab3 100644 --- a/lib/telemetry-withheld.js +++ b/lib/telemetry-withheld.js @@ -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() : ''; @@ -84,6 +97,7 @@ module.exports = { byMeasuredDesc, floorCount, formatTelemetryCount, + measuredChange, topMeasuredVersion, withWithheldNote, }; diff --git a/pages/api/telemetry.js b/pages/api/telemetry.js index 3912b405..ba144dbd 100644 --- a/pages/api/telemetry.js +++ b/pages/api/telemetry.js @@ -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'); /** @@ -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, diff --git a/test/telemetry-withheld.test.js b/test/telemetry-withheld.test.js index 84d3e7d9..cc5a3eca 100644 --- a/test/telemetry-withheld.test.js +++ b/test/telemetry-withheld.test.js @@ -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 {