Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export const getSitesToAudit = async (dynamoClient, config) => {
};

/**
* Retrieves sites with their latest audit of a specified type.
* Retrieves sites with their latest audit of a specified type. If there is
* no audit of the specified type for a site, the site will be returned with
* an empty audits array.
* The audits are sorted ascending or descending by scores.
*
* @param {DynamoDbClient} dynamoClient - The DynamoDB client.
* @param {DataAccessConfig} config - The data access config.
Expand All @@ -78,16 +81,17 @@ export const getSitesWithLatestAudit = async (
getLatestAudits(dynamoClient, config, log, auditType, sortAuditsAscending),
]);

const sitesMap = new Map(sites.map((site) => [site.getId(), site]));
const auditsMap = new Map(latestAudits.map((audit) => [audit.getSiteId(), audit]));

return latestAudits.reduce((result, audit) => {
const site = sitesMap.get(audit.getSiteId());
if (site) {
return sites.map((site) => {
const audit = auditsMap.get(site.getId());
if (audit) {
site.setAudits([audit]);
result.push(site);
} else {
site.setAudits([]);
}
return result;
}, []);
return site;
});
};

/**
Expand Down
13 changes: 9 additions & 4 deletions packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ describe('DynamoDB Integration Test', async () => {
it('gets sites with latest audit', async () => {
const sites = await dataAccess.getSitesWithLatestAudit(AUDIT_TYPE_LHS_MOBILE);

// Every tenth site will not have any audits
expect(sites.length).to.equal(NUMBER_OF_SITES - 1);
expect(sites.length).to.equal(NUMBER_OF_SITES);

sites.forEach((site) => {
sites.forEach((site, index) => {
checkSite(site);
expect(site.getAudits()).to.be.an('array').that.has.lengthOf(1);
expect(site.getAudits()).to.be.an('array');

// Every tenth site will not have any audits
if (index % 10 === 0) {
expect(site.getAudits()).to.be.an('array').that.is.empty;
}

site.getAudits().forEach((audit) => {
expect(audit.getAuditType()).to.equal(AUDIT_TYPE_LHS_MOBILE);
expect(Object.keys(audit.getScores())).to.have.members(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ describe('Site Access Pattern Tests', () => {
mockDynamoClient.query.onSecondCall().resolves(mockAuditData);

const result = await exportedFunctions.getSitesWithLatestAudit('auditType');
expect(result).to.be.an('array').that.is.empty;
expect(result).to.be.an('array').that.has.lengthOf(1);
expect(result[0].getAudits()).to.be.an('array').that.is.empty;
});

it('calls getSiteByBaseURL and returns null', async () => {
Expand Down