Skip to content

Commit

Permalink
Merge branch 'main' into site-competitors
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea authored Jan 24, 2025
2 parents d4ed2a6 + a3f193b commit 4996666
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 29 deletions.
94 changes: 94 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"version": "0.2.0",
"compounds": [
{
"name": "All Packages: Unit Tests",
"configurations": [
"Data Access: Unit Tests",
"Utils: Unit Tests",
"RUM API: Unit Tests"
]
}
],
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Data Access: Unit Tests",
"cwd": "${workspaceFolder}/packages/spacecat-shared-data-access",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"test/unit/**/*.test.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Utils: Unit Tests",
"cwd": "${workspaceFolder}/packages/spacecat-shared-utils",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"test/unit/**/*.test.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "RUM API: Unit Tests",
"cwd": "${workspaceFolder}/packages/spacecat-shared-rum-api-client",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"test/unit/**/*.test.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Current Test File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${file}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"<node_internals>/**"
]
},
{
"type": "node",
"request": "launch",
"name": "Current Test Suite",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${fileDirname}/**/*.test.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"<node_internals>/**"
]
}
]
}
21 changes: 21 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"eslint.workingDirectories": [
"./packages/spacecat-shared-data-access",
"./packages/spacecat-shared-utils",
"./packages/spacecat-shared-rum-api-client"
],
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.formatOnSave": true,
"javascript.format.enable": false,
"eslint.alwaysShowStatus": true,
"eslint.debug": true,
"files.eol": "\n"
}
23 changes: 16 additions & 7 deletions packages/spacecat-shared-data-access/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ The module provides the following DAOs:
## Integrating Data Access in AWS Lambda Functions

Our `spacecat-shared-data-access` module includes a wrapper that can be easily integrated into AWS Lambda functions using `@adobe/helix-shared-wrap`.
This integration allows your Lambda functions to access and manipulate data seamlessly.
This integration allows your Lambda functions to access and manipulate data.

### Steps for Integration

Expand All @@ -111,10 +111,19 @@ This integration allows your Lambda functions to access and manipulate data seam
Along with other wrappers and utilities, import the `dataAccessWrapper`.

```javascript
import dataAccessWrapper from '@adobe/spacecat-shared-data-access/wrapper';
import dataAccessWrapper from '@adobe/spacecat-shared-data-access';
```

2. **Modify Your Lambda Wrapper Script**
2. **Provide Required Environment Variables**

The `dataAccessWrapper` requires the `DYNAMO_TABLE_NAME_DATA` environment variable to be set via AWS
secret assigned to your Lambda function.

```javascript
const { DYNAMO_TABLE_NAME_DATA } = context.env;
```

3. **Modify Your Lambda Wrapper Script**

Include `dataAccessWrapper` in the chain of wrappers when defining your Lambda handler.

Expand All @@ -127,7 +136,7 @@ This integration allows your Lambda functions to access and manipulate data seam
.with(helixStatus);
```

3. **Access Data in Your Lambda Function**
4. **Access Data in Your Lambda Function**

Use the `dataAccess` object from the context to interact with your data layer.

Expand All @@ -136,7 +145,7 @@ This integration allows your Lambda functions to access and manipulate data seam
const { dataAccess } = context;

// Example: Retrieve all sites
const sites = await dataAccess.getSites();
const sites = await dataAccess.Site.getSites();
// ... more logic ...
}
```
Expand All @@ -147,7 +156,7 @@ Here's a complete example of a Lambda function utilizing the data access wrapper

```javascript
import wrap from '@adobe/helix-shared-wrap';
import dataAccessWrapper from '@adobe/spacecat-shared-data-access/wrapper';
import dataAccessWrapper from '@adobe/spacecat-shared-data-access';
import sqsEventAdapter from './sqsEventAdapter';
import sqs from './sqs';
import secrets from '@adobe/helix-shared-secrets';
Expand All @@ -156,7 +165,7 @@ import helixStatus from '@adobe/helix-status';
async function run(message, context) {
const { dataAccess } = context;
try {
const sites = await dataAccess.getSites();
const sites = await dataAccess.Site.getSites();
// Function logic here
} catch (error) {
// Error handling
Expand Down
14 changes: 14 additions & 0 deletions packages/spacecat-shared-data-access/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ export * from './service/index.js';

const TABLE_NAME_DATA = 'spacecat-services-data-dev';

/**
* Wrapper for data access layer
* @param {function} fn - The function to wrap
* @returns {function} - The wrapped function
*/
export default function dataAccessWrapper(fn) {
/**
* Wrapper for data access layer. This wrapper will create a data access layer if it is not
* already created. It requires the context to have a log object. It will also use the
* DYNAMO_TABLE_NAME_DATA environment variable to create the data access layer.
*
* @param {object} request - The request object
* @param {object} context - The context object
* @returns {Promise<object>} - The wrapped function
*/
return async (request, context) => {
if (!context.dataAccess) {
const { log } = context;
Expand Down
16 changes: 16 additions & 0 deletions packages/spacecat-shared-data-access/src/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,19 @@ await user.save();
## Consideration for Indexes

Indexes cost money and complexity. Do not add indexes lightly. Determine which query patterns you truly need and only then introduce additional indexes.

## Data Access Service

You can use the data layer by obtaining a service instance through the `createDataAccess` function:

```javascript
const { createDataAccess } = require('@adobe/spacecat-shared-data-access');

const dataAccess = createDataAccess({
tableNameData: 'spacecat-services-data-dev',
});

// You can now use the dataAccess object to interact with the data layer
const sites = await dataAccess.Site.getSites();
```

16 changes: 5 additions & 11 deletions packages/spacecat-shared-data-access/src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,12 @@ const createElectroService = (client, config, log) => {
};

/**
* Creates a data access object.
* Creates a data access layer for interacting with DynamoDB using ElectroDB.
*
* @param {{pkAllSites: string, pkAllLatestAudits: string, indexNameAllLatestAuditScores: string,
* tableNameAudits: string,tableNameLatestAudits: string, indexNameAllSitesOrganizations: string,
* tableNameSites: string, tableNameOrganizations: string, tableNameExperiments: string,
* indexNameAllSites: string,
* tableNameImportJobs: string, pkAllImportJobs: string, indexNameAllImportJobs: string,
* tableNameSiteTopPages: string, indexNameAllOrganizations: string,
* indexNameAllOrganizationsByImsOrgId: string, pkAllOrganizations: string}} config configuration
* @param {Logger} log log
* @param client custom dynamo client
* @returns {object} data access object
* @param {{tableNameData: string}} config - Configuration object containing table name
* @param {object} log - Logger instance, defaults to console
* @param {DynamoDB} [client] - Optional custom DynamoDB client instance
* @returns {object} Data access collections for interacting with entities
*/
export const createDataAccess = (config, log = console, client = undefined) => {
const rawClient = createRawClient(client);
Expand Down
7 changes: 7 additions & 0 deletions packages/spacecat-shared-rum-api-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [@adobe/spacecat-shared-rum-api-client-v2.18.7](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.18.6...@adobe/spacecat-shared-rum-api-client-v2.18.7) (2025-01-23)


### Bug Fixes

* **rum-api-client:** log RUM Bundler requests ([#555](https://github.com/adobe/spacecat-shared/issues/555)) ([9b3ed87](https://github.com/adobe/spacecat-shared/commit/9b3ed87d12d0838ef922d6f373a287b4782dd197))

# [@adobe/spacecat-shared-rum-api-client-v2.18.6](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-rum-api-client-v2.18.5...@adobe/spacecat-shared-rum-api-client-v2.18.6) (2025-01-20)


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.18.6",
"version": "2.18.7",
"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
Expand Up @@ -157,7 +157,7 @@ async function mergeBundlesWithSameId(bundles) {
}
/* c8 ignore end */

async function fetchBundles(opts = {}) {
async function fetchBundles(opts, log) {
const {
domain,
domainkey,
Expand Down Expand Up @@ -188,7 +188,10 @@ async function fetchBundles(opts = {}) {

const result = [];
for (const chunk of chunks) {
const responses = await Promise.all(chunk.map((url) => fetch(url)));
const responses = await Promise.all(chunk.map((url) => {
log.info(`Retrieving RUM bundles. Granularity: ${granularity}. Domain: ${domain}`);
return fetch(url);
}));
const bundles = await Promise.all(responses.map((response) => response.json()));

bundles.forEach((b) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/spacecat-shared-rum-api-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class RUMAPIClient {
const bundles = await fetchBundles({
...opts,
checkpoints,
});
}, this.log);

this.log.info(`Query "${query}" fetched ${bundles.length} bundles`);

Expand Down Expand Up @@ -91,7 +91,7 @@ export default class RUMAPIClient {
const bundles = await fetchBundles({
...opts,
checkpoints: [...allCheckpoints],
});
}, this.log);

const results = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Rum bundler client', () => {
});

it('should throw an error if required parameters are missing', async () => {
await expect(fetchBundles({})).to.be.rejectedWith('Missing required parameters');
await expect(fetchBundles({}, console)).to.be.rejectedWith('Missing required parameters');
});

it('should fetch correct hourly bundles and filter by checkpoint', async () => {
Expand All @@ -67,7 +67,7 @@ describe('Rum bundler client', () => {
granularity,
interval,
checkpoints: ['good'],
});
}, console);

const containsCorrectData = result.every((item) => item.events.length === 1 && item.events[0].checkpoint === 'good');

Expand Down Expand Up @@ -100,7 +100,7 @@ describe('Rum bundler client', () => {
granularity,
interval,
checkpoints: ['good'],
});
}, console);

expect(result.length).to.equal(dates.length - 1);
});
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('Rum bundler client', () => {
interval,
checkpoints: ['good'],
filterBotTraffic: false,
});
}, console);

expect(result.length).to.equal(dates.length);
});
Expand All @@ -157,7 +157,7 @@ describe('Rum bundler client', () => {
granularity,
interval,
checkpoints: ['good'],
});
}, console);

const containsCorrectData = result.every((item) => item.events.length === 1 && item.events[0].checkpoint === 'good');

Expand Down Expand Up @@ -188,7 +188,7 @@ describe('Rum bundler client', () => {
granularity,
interval,
// no checkpoints param
});
}, console);

const containsCorrectData = result.every((item) => item.events.length === 2);

Expand Down

0 comments on commit 4996666

Please sign in to comment.