Skip to content
Draft
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
42 changes: 25 additions & 17 deletions src/controllers/reportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ const REPORT_CONFIGS = {

/**
* Generic report data controller factory
* Creates controllers for adoption, pageWeight, lighthouse, and cwv data
* Creates controllers for adoption, pageWeight, lighthouse, and cwv data.
* Pass { crossGeo: true } to get a cross-geography snapshot (omits geo filter,
* includes geo in projection, returns a single month of data).
*/
const createReportController = (reportType) => {
const createReportController = (reportType, { crossGeo = false } = {}) => {
const config = REPORT_CONFIGS[reportType];
if (!config) {
throw new Error(`Unknown report type: ${reportType}`);
Expand Down Expand Up @@ -79,20 +81,10 @@ const createReportController = (reportType) => {
// Validate and process technology array
const techArray = validateArrayParameter(technologyParam, 'technology');

// Handle 'latest' date substitution
let startDate = params.start;
if (startDate === 'latest') {
startDate = await getLatestDate(firestore, config.table);
}

// Build Firestore query
let query = firestore.collection(config.table);

// Apply required filters
query = query.where('geo', '==', geoParam);
query = query.where('rank', '==', rankParam);

// Apply technology filter with batch processing
query = query.where('technology', 'in', techArray);

// Apply version filter with special handling for 'ALL' case
Expand All @@ -102,12 +94,27 @@ const createReportController = (reportType) => {
//query = query.where('version', '==', 'ALL');
}

// Apply date filters
if (startDate) query = query.where('date', '>=', startDate);
if (params.end) query = query.where('date', '<=', params.end);
if (crossGeo) {
// Cross-geo: single-month snapshot, all geographies included.
// Use 'end' param if provided, otherwise default to latest available date.
const snapshotDate = params.end || await getLatestDate(firestore, config.table);
query = query.where('date', '==', snapshotDate);
query = query.select('date', 'technology', 'geo', config.dataField);
} else {
// Normal time-series: filter by geo, apply date range, no geo in projection.
query = query.where('geo', '==', geoParam);

// Apply field projection to optimize query
query = query.select('date', 'technology', config.dataField);
// Handle 'latest' date substitution
let startDate = params.start;
if (startDate === 'latest') {
startDate = await getLatestDate(firestore, config.table);
}

if (startDate) query = query.where('date', '>=', startDate);
if (params.end) query = query.where('date', '<=', params.end);

query = query.select('date', 'technology', config.dataField);
}

// Execute query
const snapshot = await query.get();
Expand All @@ -132,5 +139,6 @@ export const listAdoptionData = createReportController('adoption');
export const listCWVTechData = createReportController('cwv');
export const listLighthouseData = createReportController('lighthouse');
export const listPageWeightData = createReportController('pageWeight');
export const listGeoBreakdownData = createReportController('cwv', { crossGeo: true });


7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const controllers = {
ranks: null,
geos: null,
versions: null,
geoBreakdown: null,
static: null
};

Expand Down Expand Up @@ -42,6 +43,9 @@ const getController = async (name) => {
case 'versions':
controllers[name] = await import('./controllers/versionsController.js');
break;
case 'geoBreakdown':
controllers[name] = await import('./controllers/reportController.js');
break;
case 'static':
controllers[name] = await import('./controllers/cdnController.js');
break;
Expand Down Expand Up @@ -140,6 +144,9 @@ const handleRequest = async (req, res) => {
} else if (pathname === '/v1/versions' && req.method === 'GET') {
const { listVersions } = await getController('versions');
await listVersions(req, res);
} else if (pathname === '/v1/geo-breakdown' && req.method === 'GET') {
const { listGeoBreakdownData } = await getController('geoBreakdown');
await listGeoBreakdownData(req, res);
} else if (pathname.startsWith('/v1/static/') && req.method === 'GET') {
// GCS proxy endpoint for reports files
const filePath = decodeURIComponent(pathname.replace('/v1/static/', ''));
Expand Down