Skip to content

Commit

Permalink
Merge branch 'main' into feat-latest-audit-entity
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 authored Dec 21, 2024
2 parents 5111972 + 815bf26 commit 71defc0
Show file tree
Hide file tree
Showing 13 changed files with 728 additions and 3,502 deletions.
4,049 changes: 550 additions & 3,499 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions packages/spacecat-shared-rum-api-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# [@adobe/spacecat-shared-rum-api-client-v2.16.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.16.0...@adobe/spacecat-shared-rum-api-client-v2.16.1) (2024-12-20)


### Bug Fixes

* add totalClicks ([df9a688](https://github.com/adobe/spacecat-shared/commit/df9a688c8d12e10ceb84da2da8a409a74aeddeea))

# [@adobe/spacecat-shared-rum-api-client-v2.16.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.15.7...@adobe/spacecat-shared-rum-api-client-v2.16.0) (2024-12-20)


### Features

* support site metrics overview ([99a7c66](https://github.com/adobe/spacecat-shared/commit/99a7c66f18ff1ba869ffdb07b17b9505d1f00069))

# [@adobe/spacecat-shared-rum-api-client-v2.15.7](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.15.6...@adobe/spacecat-shared-rum-api-client-v2.15.7) (2024-12-16)


Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-rum-api-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/spacecat-shared-rum-api-client",
"version": "2.15.7",
"version": "2.16.1",
"description": "Shared modules of the Spacecat Services - Rum API client",
"type": "module",
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.
*/

import { DataChunks, series } from '@adobe/rum-distiller';
import { loadBundles } from '../utils.js';

function handler(bundles) {
const dataChunks = new DataChunks();
loadBundles(bundles, dataChunks);
dataChunks.addSeries('traffic_domain', series.pageViews);
dataChunks.addSeries('clicks', (bundle) => (bundle.events.some((e) => e.checkpoint === 'click')
? bundle.weight
: 0));
const totalPageViews = dataChunks?.totals?.traffic_domain?.weight;
const sum = dataChunks?.totals?.clicks?.sum ?? 0;
const weight = dataChunks?.totals?.clicks?.weight ?? 0;
const totalCTR = weight !== 0 ? sum / weight : 0;
return {
totalPageViews,
totalCTR,
totalClicks: sum,
};
}

export default {
handler,
checkpoints: ['click'],
};
2 changes: 2 additions & 0 deletions packages/spacecat-shared-rum-api-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import cwv from './functions/cwv.js';
import formVitals from './functions/form-vitals.js';
import experiment from './functions/experiment.js';
import trafficAcquisition from './functions/traffic-acquisition.js';
import totalMetrics from './functions/total-metrics.js';
import variant from './functions/variant.js';
import rageclick from './functions/opportunities/rageclick.js';
import highInorganicHighBounceRate from './functions/opportunities/high-inorganic-high-bounce-rate.js';
Expand All @@ -30,6 +31,7 @@ const HANDLERS = {
'traffic-acquisition': trafficAcquisition,
variant,
rageclick,
totalMetrics,
'high-inorganic-high-bounce-rate': highInorganicHighBounceRate,
'high-organic-low-ctr': highOrganicLowCtr,
};
Expand Down
26 changes: 26 additions & 0 deletions packages/spacecat-shared-rum-api-client/test/total-metrics.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/
import { expect } from 'chai';
import totalMetrics from '../src/functions/total-metrics.js';
import bundlesForUrls from './fixtures/bundles.json' with { type: 'json' };
/* eslint-env mocha */

describe('Total Metrics Queries', () => {
it('crunches CWV data', async () => {
const result = totalMetrics.handler(bundlesForUrls.rumBundles);
expect(result).to.deep.equal({
totalCTR: 0.2027468663384768,
totalClicks: 4901,
totalPageViews: 24173,
});
});
});
7 changes: 7 additions & 0 deletions packages/spacecat-shared-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [@adobe/spacecat-shared-utils-v1.24.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.23.8...@adobe/spacecat-shared-utils-v1.24.0) (2024-12-20)


### Features

* support site metrics overview ([99a7c66](https://github.com/adobe/spacecat-shared/commit/99a7c66f18ff1ba869ffdb07b17b9505d1f00069))

# [@adobe/spacecat-shared-utils-v1.23.8](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.23.7...@adobe/spacecat-shared-utils-v1.23.8) (2024-12-14)


Expand Down
3 changes: 2 additions & 1 deletion packages/spacecat-shared-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/spacecat-shared-utils",
"version": "1.23.8",
"version": "1.24.0",
"description": "Shared modules of the Spacecat Services - utils",
"type": "module",
"engines": {
Expand Down Expand Up @@ -46,6 +46,7 @@
"dependencies": {
"@adobe/fetch": "4.1.11",
"@aws-sdk/client-s3": "3.712.0",
"@aws-sdk/client-secrets-manager": "3.699.0",
"@aws-sdk/client-sqs": "3.712.0",
"@json2csv/plainjs": "7.0.6",
"aws-xray-sdk": "3.10.2"
Expand Down
25 changes: 25 additions & 0 deletions packages/spacecat-shared-utils/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import { Parser } from '@json2csv/plainjs';
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
import { isString } from './functions.js';

/**
Expand Down Expand Up @@ -53,6 +54,30 @@ export function resolveCustomerSecretsName(baseURL, ctx) {
return resolveSecretsName({}, ctx, `${basePath}/${customer}`);
}

/**
* Retrieves the RUM domain key for the specified base URL from the customer secrets.
*
* @param {string} baseURL - The base URL for which the RUM domain key is to be retrieved.
* @param {UniversalContext} context - Helix Universal Context. See https://github.com/adobe/helix-universal/blob/main/src/adapter.d.ts#L120
* @returns {Promise<string>} - A promise that resolves to the RUM domain key.
* @throws {Error} Throws an error if no domain key is found for the specified base URL.
*/
export async function getRUMDomainKey(baseURL, context) {
const customerSecretName = resolveCustomerSecretsName(baseURL, context);
const { runtime } = context;

try {
const client = new SecretsManagerClient({ region: runtime.region });
const command = new GetSecretValueCommand({
SecretId: customerSecretName,
});
const response = await client.send(command);
return JSON.parse(response.SecretString)?.RUM_DOMAIN_KEY;
} catch (error) {
throw new Error(`Error retrieving the domain key for ${baseURL}. Error: ${error.message}`);
}
}

/**
* Generates a CSV file from the provided JSON data.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/spacecat-shared-utils/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ declare function resolveSecretsName(opts: object, ctx: object, defaultPath: stri
*/
declare function resolveCustomerSecretsName(baseURL: string, ctx: object): string;

/**
* Retrieves the RUM domain key for the specified base URL from the customer secrets.
*
* @param {string} baseURL - The base URL for which the RUM domain key is to be retrieved.
* @param {object} ctx - Helix Universal Context. See https://github.com/adobe/helix-universal/blob/main/src/adapter.d.ts#L120
* @returns {Promise<string>} - A promise that resolves to the RUM domain key.
* @throws {Error} Throws an error if no domain key is found for the specified base URL.
*/
declare function getRUMDomainKey(baseURL: string, ctx: object): Promise<string>;

/**
* Generates a CSV file from the provided JSON data.
*
Expand Down
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
export {
resolveSecretsName,
resolveCustomerSecretsName,
getRUMDomainKey,
generateCSVFile,
} from './helpers.js';

Expand Down
53 changes: 52 additions & 1 deletion packages/spacecat-shared-utils/test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

import { expect } from 'chai';

import nock from 'nock';
import {
generateCSVFile,
resolveSecretsName,
resolveCustomerSecretsName,
resolveCustomerSecretsName, getRUMDomainKey,
} from '../src/helpers.js';

describe('resolveSecretsName', () => {
Expand Down Expand Up @@ -90,6 +91,56 @@ describe('resolveCustomerSecretsName', () => {
});
});

describe('rum utils', () => {
let context;
let processEnvCopy;
beforeEach('setup', () => {
context = {
env: {
AWS_REGION: 'us-east-1',
AWS_ACCESS_KEY_ID: 'some-key-id',
AWS_SECRET_ACCESS_KEY: 'some-secret-key',
AWS_SESSION_TOKEN: 'some-secret-token',
},
runtime: { name: 'aws-lambda', region: 'us-east-1' },
func: { package: 'spacecat-services', version: 'ci', name: 'test' },
};
processEnvCopy = { ...process.env };
process.env = {
...process.env,
...context.env,
};
});

afterEach('clean up', () => {
process.env = processEnvCopy;
nock.cleanAll();
});

it('throws error when domain key does not exist', async () => {
const scope = nock('https://secretsmanager.us-east-1.amazonaws.com/')
.post('/', (body) => body.SecretId === '/helix-deploy/spacecat-services/customer-secrets/some_domain_com/ci')
.replyWithError('Some error');

await expect(getRUMDomainKey('https://some-domain.com', context)).to.be.rejectedWith('Error retrieving the domain key for https://some-domain.com. Error: Some error');
scope.done();
});

it('retrieves the domain key', async () => {
const scope = nock('https://secretsmanager.us-east-1.amazonaws.com/')
.post('/', (body) => body.SecretId === '/helix-deploy/spacecat-services/customer-secrets/some_domain_com/ci')
.reply(200, {
SecretString: JSON.stringify({
RUM_DOMAIN_KEY: '42',
}),
});

const rumDomainkey = await getRUMDomainKey('https://some-domain.com', context);
expect(rumDomainkey).to.equal('42');
scope.done();
});
});

describe('generateCSVFile', () => {
it('should convert the JSON data to CSV', () => {
const data = [
Expand Down
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('Index Exports', () => {
'logWrapper',
'prependSchema',
'resolveCustomerSecretsName',
'getRUMDomainKey',
'resolveSecretsName',
'sqsEventAdapter',
's3Wrapper',
Expand Down

0 comments on commit 71defc0

Please sign in to comment.