From 59e4046c2af40bb85891554ecf094b17b01d4f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominique=20J=C3=A4ggi?= Date: Mon, 11 Dec 2023 08:48:50 +0100 Subject: [PATCH] fix: sort order sites with latest audits --- .../src/service/sites/accessPatterns.js | 42 +++++++++++-------- .../test/it/db.test.js | 7 +--- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js b/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js index 34bfb45c9..42844bc5c 100644 --- a/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js +++ b/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js @@ -55,19 +55,18 @@ export const getSitesToAudit = async (dynamoClient, config) => { }; /** - * 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. - * + * Retrieves all sites with their latest audit. Sites without a latest audit will be included + * in the result, but will have an empty audits array. 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. If a site has no latest audit, it will be sorted at the end of + * the list. * @param {DynamoDbClient} dynamoClient - The DynamoDB client. * @param {DataAccessConfig} config - The data access config. * @param {Logger} log - The logger. - * @param {string} auditType - The type of the latest audits to retrieve for each site. - * @param {boolean} [sortAuditsAscending] - Optional. Determines if the audits - * should be sorted ascending or descending by scores. - * @returns {Promise[]>} A promise that resolves to an array of site objects, - * each with its latest audit of the specified type. + * @param {string} auditType - The type of audits to retrieve for the sites. + * @param {boolean} [sortAuditsAscending=true] - Determines if the audits should be sorted in + * @return {Promise[]>} A promise that resolves to an array of sites with their + * latest audit. */ export const getSitesWithLatestAudit = async ( dynamoClient, @@ -81,17 +80,26 @@ export const getSitesWithLatestAudit = async ( getLatestAudits(dynamoClient, config, log, auditType, sortAuditsAscending), ]); - const auditsMap = new Map(latestAudits.map((audit) => [audit.getSiteId(), audit])); + const sitesMap = new Map(sites.map((site) => [site.getId(), site])); + const orderedSites = []; - return sites.map((site) => { - const audit = auditsMap.get(site.getId()); - if (audit) { + // First, append sites with a latest audit in the sorted order + latestAudits.forEach((audit) => { + const site = sitesMap.get(audit.getSiteId()); + if (site) { site.setAudits([audit]); - } else { - site.setAudits([]); + orderedSites.push(site); + sitesMap.delete(site.getId()); // Remove the site from the map to avoid adding it again } - return site; }); + + // Then, append the remaining sites (without a latest audit) + sitesMap.forEach((site) => { + site.setAudits([]); + orderedSites.push(site); + }); + + return orderedSites; }; /** diff --git a/packages/spacecat-shared-data-access/test/it/db.test.js b/packages/spacecat-shared-data-access/test/it/db.test.js index 32252e8c7..10a8410fa 100644 --- a/packages/spacecat-shared-data-access/test/it/db.test.js +++ b/packages/spacecat-shared-data-access/test/it/db.test.js @@ -115,15 +115,10 @@ describe('DynamoDB Integration Test', async () => { expect(sites.length).to.equal(NUMBER_OF_SITES); - sites.forEach((site, index) => { + sites.forEach((site) => { checkSite(site); 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(