Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into url-inspect
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/index.js
  • Loading branch information
dzehnder committed Aug 26, 2024
2 parents 3e09702 + c24553a commit 6971fcf
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 716 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [1.27.0](https://github.com/adobe/spacecat-audit-worker/compare/v1.26.5...v1.27.0) (2024-08-26)


### Features

* cwv audit revival ([#378](https://github.com/adobe/spacecat-audit-worker/issues/378)) ([3a27bde](https://github.com/adobe/spacecat-audit-worker/commit/3a27bded17a6efe7354b5533a217e47fc14c2285))

## [1.26.5](https://github.com/adobe/spacecat-audit-worker/compare/v1.26.4...v1.26.5) (2024-08-24)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/spacecat-audit-worker",
"version": "1.26.5",
"version": "1.27.0",
"description": "SpaceCat Audit Worker",
"main": "src/index.js",
"type": "module",
Expand Down
108 changes: 29 additions & 79 deletions src/cwv/handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -10,83 +10,33 @@
* governing permissions and limitations under the License.
*/

import RUMAPIClient, { createRUMURL } from '@adobe/spacecat-shared-rum-api-client-v1';
import { internalServerError, noContent } from '@adobe/spacecat-shared-http-utils';
import { composeAuditURL } from '@adobe/spacecat-shared-utils';
import { retrieveSiteBySiteId } from '../utils/data-access.js';

const PAGEVIEW_THRESHOLD = 35000;

export function filterRUMData(data) {
return data.pageviews > PAGEVIEW_THRESHOLD // ignore the pages with low pageviews
&& data.url.toLowerCase() !== 'other'; // ignore the combined result
import RUMAPIClient from '@adobe/spacecat-shared-rum-api-client';
import { getRUMDomainkey } from '../support/utils.js';
import { AuditBuilder } from '../common/audit-builder.js';

const DAILY_THRESHOLD = 1000;
const INTERVAL = 7; // days

export async function CWVRunner(auditUrl, context, site) {
const rumAPIClient = RUMAPIClient.createFrom(context);
const domainkey = await getRUMDomainkey(site.getBaseURL(), context);
const options = {
domain: auditUrl,
domainkey,
interval: INTERVAL,
granularity: 'hourly',
};
const cwvData = await rumAPIClient.query('cwv', options);
const auditResult = {
cwv: cwvData.filter((data) => data.pageviews >= DAILY_THRESHOLD * INTERVAL),
};

return {
auditResult,
fullAuditRef: auditUrl,
};
}

/**
* url param in run-query@v3/rum-dashboard works in a 'startsWith' fashion. url=domain.com returns
* an empty result whereas url=www.domain.com/ returns the desired result. To catch the redirects
* to subdomains we issue a GET call to the domain, then use the final url after redirects
* @param url
* @returns finalUrl {Promise<string>}
*/

function processRUMResponse(data) {
return data
.filter(filterRUMData)
.map((row) => ({
url: row.url,
pageviews: row.pageviews,
CLS: row.avgcls,
INP: row.avginp,
LCP: row.avglcp,
}));
}
export default async function auditCWV(message, context) {
const { type, url: siteId, auditContext = {} } = message;
const { dataAccess, log, sqs } = context;
const {
AUDIT_RESULTS_QUEUE_URL: queueUrl,
} = context.env;
try {
const site = await retrieveSiteBySiteId(dataAccess, siteId, log);
const url = site.getBaseURL();

log.info(`Received audit req for domain: ${url}`);

const rumAPIClient = RUMAPIClient.createFrom(context);
const finalUrl = await composeAuditURL(url);
auditContext.finalUrl = finalUrl;

const params = {
url: finalUrl,
};

const data = await rumAPIClient.getRUMDashboard(params);
const auditResult = processRUMResponse(data);
const fullAuditRef = createRUMURL({ ...params, domainkey: '' });

const auditData = {
siteId: site.getId(),
isLive: site.isLive(),
auditedAt: new Date().toISOString(),
auditType: type,
fullAuditRef,
auditResult,
};

await dataAccess.addAudit(auditData);

await sqs.sendMessage(queueUrl, {
type,
url,
auditContext,
auditResult,
});

log.info(`Successfully audited ${url} for ${type} type audit`);
return noContent();
} catch (e) {
log.info(`CWV audit failed for ${siteId} failed due to ${e.message}`);
return internalServerError(`Internal server error: ${e.message}`);
}
}
export default new AuditBuilder()
.withRunner(CWVRunner)
.build();
12 changes: 4 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
*/
import wrap from '@adobe/helix-shared-wrap';
import { helixStatus } from '@adobe/helix-status';
import { Response } from '@adobe/fetch';
import secrets from '@adobe/helix-shared-secrets';
import dataAccess from '@adobe/spacecat-shared-data-access';
import { resolveSecretsName, sqsEventAdapter } from '@adobe/spacecat-shared-utils';
import { internalServerError, notFound, ok } from '@adobe/spacecat-shared-http-utils';

import sqs from './support/sqs.js';
import apex from './apex/handler.js';
Expand Down Expand Up @@ -47,6 +47,7 @@ const HANDLERS = {
'experimentation-ess-all': essExperimentationAll,
costs,
'structured-data': structuredData,
dummy: (message) => ok(message),
};

function getElapsedSeconds(startTime) {
Expand All @@ -71,7 +72,7 @@ async function run(message, context) {
if (!handler) {
const msg = `no such audit type: ${type}`;
log.error(msg);
return new Response('', { status: 404 });
return notFound();
}

const startTime = process.hrtime();
Expand All @@ -84,12 +85,7 @@ async function run(message, context) {
return result;
} catch (e) {
log.error(`Audit failed after ${getElapsedSeconds(startTime)} seconds`, e);
return new Response('', {
status: e.statusCode || 500,
headers: {
'x-error': 'internal server error',
},
});
return internalServerError();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import { context as h2, h1 } from '@adobe/fetch';
import { hasText, resolveCustomerSecretsName } from '@adobe/spacecat-shared-utils';
import { hasText, prependSchema, resolveCustomerSecretsName } from '@adobe/spacecat-shared-utils';
import URI from 'urijs';
import { JSDOM } from 'jsdom';
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
Expand All @@ -26,7 +26,7 @@ export const { fetch } = process.env.HELIX_FETCH_FORCE_HTTP1
// weekly pageview threshold to eliminate urls with lack of samples

export async function getRUMUrl(url) {
const urlWithScheme = url.startsWith('http') ? url : `https://${url}`;
const urlWithScheme = prependSchema(url);
const resp = await fetch(urlWithScheme);
const finalUrl = resp.url.split('://')[1];
return finalUrl.endsWith('/') ? finalUrl.slice(0, -1) : /* c8 ignore next */ finalUrl;
Expand Down
Loading

0 comments on commit 6971fcf

Please sign in to comment.