Skip to content

Commit 1d3fe2d

Browse files
muodovclaude
andauthored
Import all autoconsent performance measurements into ClickHouse (#220)
The autoconsentPerformance rows were built from a hardcoded list of measurement names. Autoconsent no longer reports filterCMPs, so every frame got a 0/0 filterCMPs row, and the newer parseDeclarativeRules timing was never imported. Take the measurement names from each frame's performance entry instead (every array-valued key), so rows follow whatever the crawled autoconsent version reports. No schema change: measurement is a String column. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2da46b8 commit 1d3fe2d

3 files changed

Lines changed: 78 additions & 35 deletions

File tree

‎collectors/CookiePopupsCollector.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,15 +597,17 @@ class CookiePopupsCollector extends ContentScriptCollector {
597597
*/
598598

599599
/**
600+
* Durations in ms, one per call. The set of measurements depends on the autoconsent version.
600601
* @typedef AutoconsentPerformanceResult
601602
* @property {string} url
602603
* @property {boolean} isMainFrame
603-
* @property {number[]} filterCMPs
604+
* @property {number[]} [filterCMPs] only in older autoconsent versions
604605
* @property {number[]} detectHeuristics
605606
* @property {number[]} heuristicDetector
606607
* @property {number[]} findCmpSiteSpecific
607608
* @property {number[]} findCmpGeneric
608609
* @property {number[]} findCmpHeuristic
610+
* @property {number[]} [parseDeclarativeRules]
609611
*/
610612

611613
/**

‎reporters/ClickhouseReporter.js‎

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -164,28 +164,28 @@ const TABLE_DEFINITIONS = [
164164
/** @type {string[]} */
165165
const SCRAPED_FRAMES_TABLES = ['cookiePopupFrames', 'cookiePopupPopups', 'cookiePopupButtons'];
166166

167-
/** @type {readonly string[]} */
168-
const PERFORMANCE_MEASUREMENTS = [
169-
'filterCMPs',
170-
'detectHeuristics',
171-
'heuristicDetector',
172-
'findCmpSiteSpecific',
173-
'findCmpGeneric',
174-
'findCmpHeuristic',
175-
];
176-
177167
/**
178-
* @param {number[] | undefined} values
179-
* @returns {{ sum: number, count: number }}
168+
* One row per (frame, measurement) with the total and number of calls. Measurement names are taken from the
169+
* entry itself (every array-valued key), since they change between autoconsent versions.
170+
* @param {string} crawlId
171+
* @param {string} pageId
172+
* @param {import('../collectors/CookiePopupsCollector').AutoconsentPerformanceResult[]} performance
173+
* @param {string} fallbackUrl used for entries without a frame url
180174
*/
181-
function summarizeMeasurementArray(values) {
182-
if (!Array.isArray(values) || values.length === 0) {
183-
return { sum: 0, count: 0 };
184-
}
185-
return {
186-
sum: values.reduce((total, value) => total + value, 0),
187-
count: values.length,
188-
};
175+
function performanceToRows(crawlId, pageId, performance, fallbackUrl) {
176+
return performance.flatMap((entry) =>
177+
Object.entries(entry)
178+
.filter((/** @type {[string, unknown]} */ e) => Array.isArray(e[1]))
179+
.map(([measurement, values]) => [
180+
crawlId,
181+
pageId,
182+
entry.url || fallbackUrl,
183+
entry.isMainFrame ? 1 : 0,
184+
measurement,
185+
/** @type {number[]} */ (values).reduce((total, value) => total + value, 0),
186+
/** @type {number[]} */ (values).length,
187+
]),
188+
);
189189
}
190190

191191
/**
@@ -478,19 +478,12 @@ class ClickhouseReporter extends BaseReporter {
478478
]);
479479
this.queue.cmps = this.queue.cmps.concat(cmpRows);
480480

481-
const performanceRows = (data.data.cookiepopups.performance || []).flatMap((entry) =>
482-
PERFORMANCE_MEASUREMENTS.map((measurement) => {
483-
const { sum, count } = summarizeMeasurementArray(entry[measurement]);
484-
return [
485-
this.crawlId,
486-
pageId,
487-
entry.url || data.finalUrl || data.initialUrl,
488-
entry.isMainFrame ? 1 : 0,
489-
measurement,
490-
sum,
491-
count,
492-
];
493-
}),
481+
// older crawls predate performance logging
482+
const performanceRows = performanceToRows(
483+
this.crawlId,
484+
pageId,
485+
data.data.cookiepopups.performance || [],
486+
data.finalUrl || data.initialUrl,
494487
);
495488
this.queue.autoconsentPerformance = this.queue.autoconsentPerformance.concat(performanceRows);
496489

@@ -561,5 +554,6 @@ class ClickhouseReporter extends BaseReporter {
561554

562555
module.exports = ClickhouseReporter;
563556
module.exports.scrapedFramesToRows = scrapedFramesToRows;
557+
module.exports.performanceToRows = performanceToRows;
564558
module.exports.clickhouseCredentials = clickhouseCredentials;
565559
module.exports.SCRAPED_FRAMES_TABLES = SCRAPED_FRAMES_TABLES;

‎tests/reporters/ClickhouseReporter.mocha.js‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const assert = require('assert');
22
const ClickhouseReporter = require('../../reporters/ClickhouseReporter');
3-
const { scrapedFramesToRows, clickhouseCredentials, SCRAPED_FRAMES_TABLES } = ClickhouseReporter;
3+
const { scrapedFramesToRows, performanceToRows, clickhouseCredentials, SCRAPED_FRAMES_TABLES } = ClickhouseReporter;
44

55
describe('ClickhouseReporter scrapedFramesToRows', () => {
66
it('handles empty scrapedFrames', () => {
@@ -209,6 +209,41 @@ describe('ClickhouseReporter text sanitization', () => {
209209
});
210210
});
211211

212+
describe('ClickhouseReporter performanceToRows', () => {
213+
it('stores the total and call count of every measurement in the entry', () => {
214+
const rows = performanceToRows(
215+
'c',
216+
'p',
217+
[
218+
{
219+
url: 'https://example.com/',
220+
isMainFrame: true,
221+
detectHeuristics: [1.5, 2.5],
222+
heuristicDetector: [],
223+
findCmpSiteSpecific: [0.25],
224+
findCmpGeneric: [10, 20, 30],
225+
findCmpHeuristic: [4],
226+
parseDeclarativeRules: [1.75],
227+
},
228+
// entries from older autoconsent versions have a different set of measurements
229+
// @ts-expect-error missing parseDeclarativeRules and findCmp* measurements
230+
{ url: '', isMainFrame: false, filterCMPs: [3], detectHeuristics: [1] },
231+
],
232+
'https://fallback.example/',
233+
);
234+
assert.deepStrictEqual(rows, [
235+
['c', 'p', 'https://example.com/', 1, 'detectHeuristics', 4, 2],
236+
['c', 'p', 'https://example.com/', 1, 'heuristicDetector', 0, 0],
237+
['c', 'p', 'https://example.com/', 1, 'findCmpSiteSpecific', 0.25, 1],
238+
['c', 'p', 'https://example.com/', 1, 'findCmpGeneric', 60, 3],
239+
['c', 'p', 'https://example.com/', 1, 'findCmpHeuristic', 4, 1],
240+
['c', 'p', 'https://example.com/', 1, 'parseDeclarativeRules', 1.75, 1],
241+
['c', 'p', 'https://fallback.example/', 0, 'filterCMPs', 3, 1],
242+
['c', 'p', 'https://fallback.example/', 0, 'detectHeuristics', 1, 1],
243+
]);
244+
});
245+
});
246+
212247
/**
213248
* Create a reporter with a stubbed client, since there's no clickhouse server in tests.
214249
* @param {string[]} [tables]
@@ -256,6 +291,18 @@ describe('ClickhouseReporter processSite', () => {
256291
SCRAPED_FRAMES_TABLES.forEach((table) => assert.strictEqual(rowsByTable[table].length, 0));
257292
});
258293

294+
it('imports older crawls without performance data', async () => {
295+
const { ch, inserts } = createStubbedReporter();
296+
const data = siteData({});
297+
delete data.data.cookiepopups.performance;
298+
await ch.processSite(data);
299+
await ch.cleanup();
300+
301+
const rowsByTable = Object.fromEntries(inserts.map((i) => [i.table, i.values]));
302+
assert.strictEqual(rowsByTable.pages.length, 1);
303+
assert.strictEqual(rowsByTable.autoconsentPerformance.length, 0);
304+
});
305+
259306
it('only inserts into the selected tables', async () => {
260307
const { ch, inserts } = createStubbedReporter(SCRAPED_FRAMES_TABLES);
261308
await ch.processSite(

0 commit comments

Comments
 (0)