Skip to content

Commit

Permalink
feat: modifying cwv handler for accommodating forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Kumar Singh committed Nov 18, 2024
1 parent a2f6293 commit c47d650
Show file tree
Hide file tree
Showing 6 changed files with 509 additions and 391 deletions.
8 changes: 4 additions & 4 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
Expand Up @@ -60,7 +60,7 @@
"@adobe/spacecat-shared-data-access": "1.53.1",
"@adobe/spacecat-shared-google-client": "1.2.22",
"@adobe/spacecat-shared-http-utils": "1.7.0",
"@adobe/spacecat-shared-rum-api-client": "2.9.10",
"@adobe/spacecat-shared-rum-api-client": "2.10.0",
"@adobe/spacecat-shared-rum-api-client-v1": "npm:@adobe/[email protected]",
"@aws-sdk/client-s3": "3.693.0",
"@aws-sdk/client-lambda": "3.693.0",
Expand Down
28 changes: 26 additions & 2 deletions src/cwv/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ import { wwwUrlResolver } from '../common/audit.js';

const DAILY_THRESHOLD = 1000;
const INTERVAL = 7; // days
const CWV_QUERIES = [
'cwv',
'form-vitals',
];

function checkHasForm(matchingFormVital) {
const { formview, formsubmit, formengagement } = matchingFormVital;

// Check if 'formview', 'formsubmit' or 'formengagement' is undefined or empty
const isFormViewPresent = formview && Object.keys(formview).length > 0;
const isFormSubmitPresent = formsubmit && Object.keys(formsubmit).length > 0;
const isFormEngagementPresent = formengagement && Object.keys(formengagement).length > 0;
// Return the boolean value based on presence of formview, formsubmit or formengegement
return isFormViewPresent || isFormSubmitPresent || isFormEngagementPresent;
}

export async function CWVRunner(auditUrl, context, site) {
const rumAPIClient = RUMAPIClient.createFrom(context);
Expand All @@ -27,9 +42,18 @@ export async function CWVRunner(auditUrl, context, site) {
interval: INTERVAL,
granularity: 'hourly',
};
const cwvData = await rumAPIClient.query('cwv', options);
const cwvData = await rumAPIClient.queryMulti(CWV_QUERIES, options);
const auditResult = {
cwv: cwvData.filter((data) => data.pageviews >= DAILY_THRESHOLD * INTERVAL),
cwv: cwvData.cwv.filter((data) => data.pageviews >= DAILY_THRESHOLD * INTERVAL)
.map((cwvItem) => {
// Find a matching formVital by URL
const matchingFormVital = cwvData['form-vitals'].find(
(formVital) => formVital.url === cwvItem.url,
);

const hasForm = matchingFormVital ? checkHasForm(matchingFormVital) : false;
return { ...cwvItem, hasForm };
}),
auditContext: {
interval: INTERVAL,
},
Expand Down
13 changes: 3 additions & 10 deletions test/audits/cwv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import nock from 'nock';
import { createSite } from '@adobe/spacecat-shared-data-access/src/models/site.js';
import { CWVRunner } from '../../src/cwv/handler.js';
import { rumData } from '../fixtures/rum-data.js';
import { expectedCWVData } from '../fixtures/expected-cwv-data.js';

use(sinonChai);

Expand All @@ -39,7 +40,7 @@ describe('Index Tests', () => {
runtime: { name: 'aws-lambda', region: 'us-east-1' },
func: { package: 'spacecat-services', version: 'ci', name: 'test' },
rumApiClient: {
query: sandbox.stub().withArgs('variable-1', sinon.match(DOMAIN_REQUEST_DEFAULT_PARAMS)).resolves(rumData),
queryMulti: sandbox.stub().withArgs('variable-1', sinon.match(DOMAIN_REQUEST_DEFAULT_PARAMS)).resolves(rumData),
},
};

Expand All @@ -60,14 +61,6 @@ describe('Index Tests', () => {

it('cwv audit runs rum api client cwv query', async () => {
const result = await CWVRunner('www.spacecat.com', context, site);
expect(result).to.deep.equal({
auditResult: {
cwv: rumData.filter((data) => data.pageviews >= 7000),
auditContext: {
interval: 7,
},
},
fullAuditRef: auditUrl,
});
expect(result).to.deep.equal(expectedCWVData);
});
});
73 changes: 73 additions & 0 deletions test/fixtures/expected-cwv-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
export const expectedCWVData = {
auditResult: {
cwv: [
{
url: 'https://www.aem.live/home',
lcp: 2099.699999988079,
lcpCount: 9,
cls: 0.020660136604802475,
clsCount: 7,
inp: 12,
inpCount: 3,
ttfb: 520.4500000476837,
ttfbCount: 18,
pageviews: 9620,
hasForm: true,
},
{
url: 'https://www.aem.live/developer/block-collection',
lcp: 512.1249999403954,
lcpCount: 4,
cls: 0.0005409526209424976,
clsCount: 4,
inp: 20,
inpCount: 2,
ttfb: 122.90000003576279,
ttfbCount: 4,
pageviews: 8000,
hasForm: true,
},
{
url: 'https://www.aem.live/docs/',
lcp: 711.3499999996275,
lcpCount: 15,
cls: 0.011139588793585503,
clsCount: 10,
inp: 36,
inpCount: 8,
ttfb: 96.97500000149012,
ttfbCount: 16,
pageviews: 7910,
hasForm: true,
},
{
url: 'https://www.aem.live/tools/rum/explorer.html',
lcp: 1111.8000000715256,
lcpCount: 9,
cls: 0.000996222305285266,
clsCount: 7,
inp: 184,
inpCount: 5,
ttfb: 312.69999980926514,
ttfbCount: 9,
pageviews: 7600,
hasForm: false,
},
],
auditContext: {
interval: 7,
},
},
fullAuditRef: 'www.spacecat.com',
};
Loading

0 comments on commit c47d650

Please sign in to comment.