Skip to content

Commit 2aaabb1

Browse files
Merge branch 'main' into messagesize
2 parents d984caf + c24553a commit 2aaabb1

File tree

9 files changed

+1647
-2225
lines changed

9 files changed

+1647
-2225
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# [1.27.0](https://github.com/adobe/spacecat-audit-worker/compare/v1.26.5...v1.27.0) (2024-08-26)
2+
3+
4+
### Features
5+
6+
* cwv audit revival ([#378](https://github.com/adobe/spacecat-audit-worker/issues/378)) ([3a27bde](https://github.com/adobe/spacecat-audit-worker/commit/3a27bded17a6efe7354b5533a217e47fc14c2285))
7+
8+
## [1.26.5](https://github.com/adobe/spacecat-audit-worker/compare/v1.26.4...v1.26.5) (2024-08-24)
9+
10+
11+
### Bug Fixes
12+
13+
* **deps:** update external fixes ([#376](https://github.com/adobe/spacecat-audit-worker/issues/376)) ([923ad80](https://github.com/adobe/spacecat-audit-worker/commit/923ad80f45b2ea2462857c997c377260349629f7))
14+
115
## [1.26.4](https://github.com/adobe/spacecat-audit-worker/compare/v1.26.3...v1.26.4) (2024-08-23)
216

317

package-lock.json

Lines changed: 1184 additions & 1498 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adobe/spacecat-audit-worker",
3-
"version": "1.26.4",
3+
"version": "1.27.0",
44
"description": "SpaceCat Audit Worker",
55
"main": "src/index.js",
66
"type": "module",
@@ -51,32 +51,32 @@
5151
},
5252
"dependencies": {
5353
"@adobe/fetch": "4.1.8",
54-
"@adobe/helix-shared-secrets": "2.2.8",
54+
"@adobe/helix-shared-secrets": "2.2.9",
5555
"@adobe/helix-shared-wrap": "2.0.2",
5656
"@adobe/helix-status": "10.1.2",
5757
"@adobe/helix-universal": "5.0.5",
5858
"@adobe/helix-universal-logger": "3.0.18",
59-
"@adobe/spacecat-shared-ahrefs-client": "1.5.0",
60-
"@adobe/spacecat-shared-data-access": "1.43.2",
59+
"@adobe/spacecat-shared-ahrefs-client": "1.5.1",
60+
"@adobe/spacecat-shared-data-access": "1.43.3",
6161
"@adobe/spacecat-shared-http-utils": "1.6.8",
62-
"@adobe/spacecat-shared-rum-api-client": "2.7.3",
62+
"@adobe/spacecat-shared-rum-api-client": "2.7.4",
6363
"@adobe/spacecat-shared-rum-api-client-v1": "npm:@adobe/[email protected]",
64-
"@aws-sdk/client-lambda": "3.632.0",
65-
"@aws-sdk/credential-provider-node": "3.632.0",
66-
"@adobe/spacecat-shared-utils": "1.19.5",
67-
"@aws-sdk/client-secrets-manager": "3.632.0",
68-
"@aws-sdk/client-sqs": "3.632.0",
64+
"@aws-sdk/client-lambda": "3.637.0",
65+
"@aws-sdk/credential-provider-node": "3.637.0",
66+
"@adobe/spacecat-shared-utils": "1.19.6",
67+
"@aws-sdk/client-secrets-manager": "3.637.0",
68+
"@aws-sdk/client-sqs": "3.637.0",
6969
"diff": "5.2.0",
7070
"jsdom": "24.1.1",
7171
"urijs": "1.19.11"
7272
},
7373
"devDependencies": {
7474
"@adobe/eslint-config-helix": "2.0.6",
75-
"@adobe/helix-deploy": "12.0.9",
75+
"@adobe/helix-deploy": "12.0.10",
7676
"@adobe/helix-universal": "5.0.5",
7777
"@adobe/semantic-release-coralogix": "1.1.31",
7878
"@adobe/semantic-release-skms-cmr": "1.1.2",
79-
"@redocly/cli": "1.19.0",
79+
"@redocly/cli": "1.21.0",
8080
"@semantic-release/changelog": "6.0.3",
8181
"@semantic-release/exec": "6.0.3",
8282
"@semantic-release/git": "10.0.1",
@@ -88,13 +88,13 @@
8888
"chai-as-promised": "8.0.0",
8989
"dotenv": "16.4.5",
9090
"eslint": "8.57.0",
91-
"husky": "9.1.4",
91+
"husky": "9.1.5",
9292
"junit-report-builder": "5.0.0",
9393
"lint-staged": "15.2.9",
9494
"mocha": "10.7.3",
9595
"mocha-multi-reporters": "1.5.1",
9696
"mocha-suppress-logs": "0.5.1",
97-
"nock": "13.5.4",
97+
"nock": "13.5.5",
9898
"nodemon": "3.1.4",
9999
"semantic-release": "24.1.0",
100100
"sinon": "18.0.0",

src/cwv/handler.js

Lines changed: 29 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 Adobe. All rights reserved.
2+
* Copyright 2024 Adobe. All rights reserved.
33
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License. You may obtain a copy
55
* of the License at http://www.apache.org/licenses/LICENSE-2.0
@@ -10,83 +10,33 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import RUMAPIClient, { createRUMURL } from '@adobe/spacecat-shared-rum-api-client-v1';
14-
import { internalServerError, noContent } from '@adobe/spacecat-shared-http-utils';
15-
import { composeAuditURL } from '@adobe/spacecat-shared-utils';
16-
import { retrieveSiteBySiteId } from '../utils/data-access.js';
17-
18-
const PAGEVIEW_THRESHOLD = 35000;
19-
20-
export function filterRUMData(data) {
21-
return data.pageviews > PAGEVIEW_THRESHOLD // ignore the pages with low pageviews
22-
&& data.url.toLowerCase() !== 'other'; // ignore the combined result
13+
import RUMAPIClient from '@adobe/spacecat-shared-rum-api-client';
14+
import { getRUMDomainkey } from '../support/utils.js';
15+
import { AuditBuilder } from '../common/audit-builder.js';
16+
17+
const DAILY_THRESHOLD = 1000;
18+
const INTERVAL = 7; // days
19+
20+
export async function CWVRunner(auditUrl, context, site) {
21+
const rumAPIClient = RUMAPIClient.createFrom(context);
22+
const domainkey = await getRUMDomainkey(site.getBaseURL(), context);
23+
const options = {
24+
domain: auditUrl,
25+
domainkey,
26+
interval: INTERVAL,
27+
granularity: 'hourly',
28+
};
29+
const cwvData = await rumAPIClient.query('cwv', options);
30+
const auditResult = {
31+
cwv: cwvData.filter((data) => data.pageviews >= DAILY_THRESHOLD * INTERVAL),
32+
};
33+
34+
return {
35+
auditResult,
36+
fullAuditRef: auditUrl,
37+
};
2338
}
2439

25-
/**
26-
* url param in run-query@v3/rum-dashboard works in a 'startsWith' fashion. url=domain.com returns
27-
* an empty result whereas url=www.domain.com/ returns the desired result. To catch the redirects
28-
* to subdomains we issue a GET call to the domain, then use the final url after redirects
29-
* @param url
30-
* @returns finalUrl {Promise<string>}
31-
*/
32-
33-
function processRUMResponse(data) {
34-
return data
35-
.filter(filterRUMData)
36-
.map((row) => ({
37-
url: row.url,
38-
pageviews: row.pageviews,
39-
CLS: row.avgcls,
40-
INP: row.avginp,
41-
LCP: row.avglcp,
42-
}));
43-
}
44-
export default async function auditCWV(message, context) {
45-
const { type, url: siteId, auditContext = {} } = message;
46-
const { dataAccess, log, sqs } = context;
47-
const {
48-
AUDIT_RESULTS_QUEUE_URL: queueUrl,
49-
} = context.env;
50-
try {
51-
const site = await retrieveSiteBySiteId(dataAccess, siteId, log);
52-
const url = site.getBaseURL();
53-
54-
log.info(`Received audit req for domain: ${url}`);
55-
56-
const rumAPIClient = RUMAPIClient.createFrom(context);
57-
const finalUrl = await composeAuditURL(url);
58-
auditContext.finalUrl = finalUrl;
59-
60-
const params = {
61-
url: finalUrl,
62-
};
63-
64-
const data = await rumAPIClient.getRUMDashboard(params);
65-
const auditResult = processRUMResponse(data);
66-
const fullAuditRef = createRUMURL({ ...params, domainkey: '' });
67-
68-
const auditData = {
69-
siteId: site.getId(),
70-
isLive: site.isLive(),
71-
auditedAt: new Date().toISOString(),
72-
auditType: type,
73-
fullAuditRef,
74-
auditResult,
75-
};
76-
77-
await dataAccess.addAudit(auditData);
78-
79-
await sqs.sendMessage(queueUrl, {
80-
type,
81-
url,
82-
auditContext,
83-
auditResult,
84-
});
85-
86-
log.info(`Successfully audited ${url} for ${type} type audit`);
87-
return noContent();
88-
} catch (e) {
89-
log.info(`CWV audit failed for ${siteId} failed due to ${e.message}`);
90-
return internalServerError(`Internal server error: ${e.message}`);
91-
}
92-
}
40+
export default new AuditBuilder()
41+
.withRunner(CWVRunner)
42+
.build();

src/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
*/
1212
import wrap from '@adobe/helix-shared-wrap';
1313
import { helixStatus } from '@adobe/helix-status';
14-
import { Response } from '@adobe/fetch';
1514
import secrets from '@adobe/helix-shared-secrets';
1615
import dataAccess from '@adobe/spacecat-shared-data-access';
1716
import { resolveSecretsName, sqsEventAdapter } from '@adobe/spacecat-shared-utils';
17+
import { internalServerError, notFound, ok } from '@adobe/spacecat-shared-http-utils';
1818

1919
import sqs from './support/sqs.js';
2020
import apex from './apex/handler.js';
@@ -45,6 +45,7 @@ const HANDLERS = {
4545
'experimentation-ess-daily': essExperimentationDaily,
4646
'experimentation-ess-all': essExperimentationAll,
4747
costs,
48+
dummy: (message) => ok(message),
4849
};
4950

5051
function getElapsedSeconds(startTime) {
@@ -69,7 +70,7 @@ async function run(message, context) {
6970
if (!handler) {
7071
const msg = `no such audit type: ${type}`;
7172
log.error(msg);
72-
return new Response('', { status: 404 });
73+
return notFound();
7374
}
7475

7576
const startTime = process.hrtime();
@@ -82,12 +83,7 @@ async function run(message, context) {
8283
return result;
8384
} catch (e) {
8485
log.error(`Audit failed after ${getElapsedSeconds(startTime)} seconds`, e);
85-
return new Response('', {
86-
status: e.statusCode || 500,
87-
headers: {
88-
'x-error': 'internal server error',
89-
},
90-
});
86+
return internalServerError();
9187
}
9288
}
9389

src/support/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

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

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

0 commit comments

Comments
 (0)