Skip to content

Commit

Permalink
Merge branch 'main' into feat-slack-channel-management
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Feb 1, 2024
2 parents d7bfda0 + fb7785e commit aed28b0
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 26 deletions.
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.

35 changes: 35 additions & 0 deletions packages/spacecat-shared-data-access/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
# [@adobe/spacecat-shared-data-access-v1.13.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.13.1...@adobe/spacecat-shared-data-access-v1.13.2) (2024-01-30)


### Bug Fixes

* config validation ([edc1276](https://github.com/adobe/spacecat-shared/commit/edc127681e26c3283e5ecc94cde7c5fec78529dd))

# [@adobe/spacecat-shared-data-access-v1.13.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.13.0...@adobe/spacecat-shared-data-access-v1.13.1) (2024-01-30)


### Bug Fixes

* allow update config for site ([ad9e677](https://github.com/adobe/spacecat-shared/commit/ad9e677c7c721aa70e37b97373a4b9545f2f21fd))

# [@adobe/spacecat-shared-data-access-v1.13.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.12.0...@adobe/spacecat-shared-data-access-v1.13.0) (2024-01-30)


### Features

* introduce default config for organic-keywords report (SITES-19317) ([#118](https://github.com/adobe/spacecat-shared/issues/118)) ([4301556](https://github.com/adobe/spacecat-shared/commit/430155645e1452760bf2818d70a0faae2fb8db12))

# [@adobe/spacecat-shared-data-access-v1.12.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.11.0...@adobe/spacecat-shared-data-access-v1.12.0) (2024-01-29)


### Features

* add getSitesByOrganizationIDWithLatestAudits query ([d3a8f8c](https://github.com/adobe/spacecat-shared/commit/d3a8f8cc2148229aecec3df277e298a3f0746865))

# [@adobe/spacecat-shared-data-access-v1.11.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.10.5...@adobe/spacecat-shared-data-access-v1.11.0) (2024-01-29)


### Features

* default value for disabled property of audit config ([#114](https://github.com/adobe/spacecat-shared/issues/114)) ([daf9b80](https://github.com/adobe/spacecat-shared/commit/daf9b8039a6fe567e283dc07eef156bfd13edc4f))

# [@adobe/spacecat-shared-data-access-v1.10.5](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.10.4...@adobe/spacecat-shared-data-access-v1.10.5) (2024-01-27)


Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-data-access/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/spacecat-shared-data-access",
"version": "1.10.5",
"version": "1.13.2",
"description": "Shared modules of the Spacecat Services - Data Access",
"type": "module",
"main": "src/index.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/spacecat-shared-data-access/src/models/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Base } from './base.js';

export const AUDIT_TYPE_404 = '404';
export const AUDIT_TYPE_BROKEN_BACKLINKS = 'broken-backlinks';
export const AUDIT_TYPE_ORGANIC_KEYWORDS = 'organic-keywords';
export const AUDIT_TYPE_CWV = 'cwv';
export const AUDIT_TYPE_LHS_DESKTOP = 'lhs-desktop';
export const AUDIT_TYPE_LHS_MOBILE = 'lhs-mobile';
Expand All @@ -24,6 +25,7 @@ const EXPIRES_IN_DAYS = 30;
const AUDIT_TYPE_PROPERTIES = {
[AUDIT_TYPE_404]: [],
[AUDIT_TYPE_BROKEN_BACKLINKS]: [],
[AUDIT_TYPE_ORGANIC_KEYWORDS]: [],
[AUDIT_TYPE_CWV]: [],
[AUDIT_TYPE_LHS_DESKTOP]: ['performance', 'seo', 'accessibility', 'best-practices'],
[AUDIT_TYPE_LHS_MOBILE]: ['performance', 'seo', 'accessibility', 'best-practices'],
Expand Down
16 changes: 16 additions & 0 deletions packages/spacecat-shared-data-access/src/models/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ const Site = (data = {}) => {
return self;
};

/**
* Updates the site config.
* @param {string} config - The Site config.
* @return {Base} The updated site.
*/
self.updateConfig = (config) => {
if (!isObject(config)) {
throw new Error('Config must be provided');
}

self.state.config = Config.toDynamoItem(config);
self.touch();

return self;
};

self.updateDeliveryType = (deliveryType) => {
if (!Object.values(DELIVERY_TYPES).includes(deliveryType)) {
throw new Error(`Invalid delivery type: ${deliveryType}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,32 @@
*/

import AuditConfigType from './audit-config-type.js';
import {
AUDIT_TYPE_BROKEN_BACKLINKS,
AUDIT_TYPE_ORGANIC_KEYWORDS,
} from '../audit.js';

const AUDIT_TYPE_DISABLED_DEFAULTS = {
[AUDIT_TYPE_BROKEN_BACKLINKS]: true,
[AUDIT_TYPE_ORGANIC_KEYWORDS]: true,
};

function getAuditTypeConfigs(auditTypeConfigs, auditsDisabled) {
if (!auditTypeConfigs || Object.keys(auditTypeConfigs).length === 0) {
return {
[AUDIT_TYPE_BROKEN_BACKLINKS]: AuditConfigType({ disabled: true }),
[AUDIT_TYPE_ORGANIC_KEYWORDS]: AuditConfigType({ disabled: true }),
};
}
return Object.entries(auditTypeConfigs || {}).reduce((acc, [key, value]) => {
acc[key] = AuditConfigType(value, auditsDisabled);
const disabled = value.disabled !== undefined
? value.disabled : (AUDIT_TYPE_DISABLED_DEFAULTS[key] || auditsDisabled || false);
acc[key] = AuditConfigType(
{
...value,
disabled,
},
);
return acc;
}, {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export const configSchema = Joi.object({
channel: Joi.string(),
}),
alerts: Joi.array().items(Joi.object({
type: Joi.string(),
type: Joi.string().required(),
byOrg: Joi.boolean(),
mentions: Joi.array().items(Joi.object({ slack: Joi.array().items(Joi.string()) })),
})),
});
}).unknown(true)),
}).unknown(true);

export const DEFAULT_CONFIG = {
slack: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,51 @@ export const getSitesByOrganizationID = async (
return dynamoItems.map((dynamoItem) => SiteDto.fromDynamoItem(dynamoItem));
};

/**
* Retrieves sites by organizationId with their latest audit. Sites without a latest
* audit will not be included in the result.
* The sites are sorted by their latest audit scores in ascending order by default.
* The sortAuditsAscending parameter can be used to change the sort order.
* @param {DynamoDbClient} dynamoClient - The DynamoDB client.
* @param {DataAccessConfig} config - The data access config.
* @param {Logger} log - The logger.
* @param {string} auditType - The type of audits to retrieve for the sites.
* @param {string} organizationId - The organizationId to retrieve the sites.
* @param {boolean} [sortAuditsAscending=true] - Determines if the audits should be sorted in
* ascending order.
* to retrieve.
* @return {Promise<Readonly<Site>[]>} A promise that resolves to an array of sites with their
* latest audit.
*/
export const getSitesByOrganizationIDWithLatestAudits = async (
dynamoClient,
config,
log,
organizationId,
auditType,
sortAuditsAscending = true,
) => {
const [sites, latestAudits] = await Promise.all([
getSitesByOrganizationID(dynamoClient, config, organizationId),
getLatestAudits(dynamoClient, config, log, auditType, sortAuditsAscending),
]);

const sitesMap = new Map(sites.map((site) => [site.getId(), site]));
const orderedSites = [];

// Append just sites with a latest audit in the sorted order
latestAudits.forEach((audit) => {
const site = sitesMap.get(audit.getSiteId());
if (site) {
site.setAudits([audit]);
orderedSites.push(site);
sitesMap.delete(site.getId()); // Remove the site from the map to avoid adding it again
}
});

return orderedSites;
};

/**
* Retrieves a site by its ID.
*
Expand Down
14 changes: 13 additions & 1 deletion packages/spacecat-shared-data-access/src/service/sites/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
getSiteByID,
getSites,
getSitesByDeliveryType,
getSitesByOrganizationID,
getSitesByOrganizationID, getSitesByOrganizationIDWithLatestAudits,
getSitesToAudit,
getSitesWithLatestAudit,
removeSite,
Expand Down Expand Up @@ -58,6 +58,18 @@ export const siteFunctions = (dynamoClient, config, log) => ({
sortAuditsAscending,
deliveryType,
),
getSitesByOrganizationIDWithLatestAudits: (
organizationId,
auditType,
sortAuditsAscending,
) => getSitesByOrganizationIDWithLatestAudits(
dynamoClient,
config,
log,
organizationId,
auditType,
sortAuditsAscending,
),
getSiteByBaseURL: (baseUrl) => getSiteByBaseURL(
dynamoClient,
config,
Expand Down
20 changes: 20 additions & 0 deletions packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,26 @@ describe('DynamoDB Integration Test', async () => {
});
});

it('gets sites by organization ID with latest audit', async () => {
const organizations = await dataAccess.getOrganizations();
const sites = await dataAccess.getSitesByOrganizationIDWithLatestAudits(
organizations[0].getId(),
AUDIT_TYPE_LHS_MOBILE,
);

sites.forEach((site) => {
checkSite(site);
expect(site.getAudits()).to.be.an('array');

site.getAudits().forEach((audit) => {
expect(audit.getAuditType()).to.equal(AUDIT_TYPE_LHS_MOBILE);
expect(Object.keys(audit.getScores())).to.have.members(
['performance', 'seo', 'accessibility', 'best-practices'],
);
});
});
});

it('gets sites with latest audit of delivery type', async () => {
const sites = await dataAccess.getSitesWithLatestAudit(AUDIT_TYPE_LHS_MOBILE, true, 'aem_cs');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ export default async function generateSampleData(
type: '404',
byOrg: true,
mentions: [{ slack: [`${i}-slackId`] }],
},
{
type: 'organic-keywords',
country: 'RO',
}],
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Base Model Tests', () => {

it('correctly returns the updatedAt date if provided', () => {
const updatedAt = new Date().toISOString();
const baseEntity = Base({ updatedAt });
const baseEntity = Base({ id: 'test-id', updatedAt });
expect(baseEntity.getUpdatedAt()).to.equal(updatedAt);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('Organization Model Tests', () => {

await sleep(20);

organization.updateName('Name123');
organization.updateConfig({});

expect(organization.getUpdatedAt()).to.not.equal(initialUpdatedAt);
});
Expand Down
39 changes: 37 additions & 2 deletions packages/spacecat-shared-data-access/test/unit/models/site.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ describe('Site Model Tests', () => {
expect(auditConfig).to.be.an('object');
expect(auditConfig.auditsDisabled()).to.be.true;
expect(auditConfig.getAuditTypeConfig('type1')).to.be.an('object');
expect(auditConfig.getAuditTypeConfig('type1').disabled()).to.be.false;
expect(auditConfig.getAuditTypeConfig('type1').disabled()).to.be.true;
expect(auditConfig.getAuditTypeConfig('type2')).to.be.an('object');
expect(auditConfig.getAuditTypeConfig('type2').disabled()).to.be.false;
expect(auditConfig.getAuditTypeConfig('type2').disabled()).to.be.true;
});
});

Expand Down Expand Up @@ -111,6 +111,27 @@ describe('Site Model Tests', () => {
expect(site.getOrganizationId()).to.equal(organizationId);
});

it('updates config correctly', () => {
const conf = {
slack: {
workspace: 'workspace',
channel: 'channel',
},
alerts: [{
type: '404',
byOrg: false,
mentions: [{ slack: ['slackId'] }],
}],
};
site.updateConfig(conf);
const updatedConf = site.getConfig();
expect(updatedConf.slack).to.be.an('object');
expect(updatedConf.alerts).to.be.an('array');
expect(updatedConf.slack.workspace).to.equal('workspace');
expect(updatedConf.slack.channel).to.equal('channel');
expect(updatedConf.alerts[0].mentions[0].slack[0]).to.equal('slackId');
});

it('updates gitHubURL correctly', () => {
const newGitHubURL = 'https://gibhub.com/example/example';
site.updateGitHubURL(newGitHubURL);
Expand All @@ -125,6 +146,10 @@ describe('Site Model Tests', () => {
expect(() => site.updateGitHubURL('')).to.throw('GitHub URL must be a valid URL');
});

it('throws an error when updating with an invalid config', () => {
expect(() => site.updateConfig('abcd')).to.throw('Config must be provided');
});

it('sets audits correctly', () => {
const audits = [{ id: 'audit1' }, { id: 'audit2' }];
site.setAudits(audits);
Expand Down Expand Up @@ -153,6 +178,16 @@ describe('Site Model Tests', () => {
expect(site.getUpdatedAt()).to.not.equal(initialUpdatedAt);
});

it('updates updatedAt when config is updated', async () => {
const initialUpdatedAt = site.getUpdatedAt();

await sleep(20);

site.updateConfig({});

expect(site.getUpdatedAt()).to.not.equal(initialUpdatedAt);
});

it('updates updatedAt when gitHubURL is updated', async () => {
const initialUpdatedAt = site.getUpdatedAt();

Expand Down
Loading

0 comments on commit aed28b0

Please sign in to comment.