@adobe/spacecat-shared-data-access 1.2.6 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-data-access-v1.3.0](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.2.7...@adobe/spacecat-shared-data-access-v1.3.0) (2023-12-16)
2
+
3
+
4
+ ### Features
5
+
6
+ * add audit config ([#59](https://github.com/adobe-rnd/spacecat-shared/issues/59)) ([f851862](https://github.com/adobe-rnd/spacecat-shared/commit/f8518623a8b8e4e337b3c44a16b65724a953596a))
7
+
8
+ # [@adobe/spacecat-shared-data-access-v1.2.7](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.2.6...@adobe/spacecat-shared-data-access-v1.2.7) (2023-12-11)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * sort order sites with latest audits ([#46](https://github.com/adobe-rnd/spacecat-shared/issues/46)) ([8da2bf1](https://github.com/adobe-rnd/spacecat-shared/commit/8da2bf16b904ced6787d8c56ba2b4b72678687a0))
14
+
1
15
  # [@adobe/spacecat-shared-data-access-v1.2.6](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.2.5...@adobe/spacecat-shared-data-access-v1.2.6) (2023-12-11)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-data-access",
3
- "version": "1.2.6",
3
+ "version": "1.3.0",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/dto/site.js CHANGED
@@ -11,6 +11,7 @@
11
11
  */
12
12
 
13
13
  import { createSite } from '../models/site.js';
14
+ import AuditConfig from '../models/site/audit-config.js';
14
15
 
15
16
  /**
16
17
  * Data transfer object for Site.
@@ -30,6 +31,7 @@ export const SiteDto = {
30
31
  createdAt: site.getCreatedAt(),
31
32
  updatedAt: site.getUpdatedAt(),
32
33
  GSI1PK: 'ALL_SITES',
34
+ auditConfig: AuditConfig.toDynamoItem(site.getAuditConfig()),
33
35
  }),
34
36
 
35
37
  /**
@@ -46,6 +48,7 @@ export const SiteDto = {
46
48
  isLive: dynamoItem.isLive,
47
49
  createdAt: dynamoItem.createdAt,
48
50
  updatedAt: dynamoItem.updatedAt,
51
+ auditConfig: dynamoItem.auditConfig,
49
52
  };
50
53
 
51
54
  return createSite(siteData);
package/src/index.d.ts CHANGED
@@ -20,9 +20,21 @@ export interface Audit {
20
20
  getExpiresAt: () => Date;
21
21
  getFullAuditRef: () => string;
22
22
  isLive: () => boolean;
23
+ isError: () => boolean;
23
24
  getScores: () => object;
24
25
  }
25
26
 
27
+ // AuditConfigType defines the structure for specific audit type configurations
28
+ export interface AuditConfigType {
29
+ disabled(): boolean;
30
+ }
31
+
32
+ // AuditConfig defines the structure for the overall audit configuration of a site
33
+ export interface AuditConfig {
34
+ auditsDisabled: () => boolean;
35
+ getAuditConfigForType: (auditType: string) => AuditConfigType;
36
+ }
37
+
26
38
  export interface Site {
27
39
  getId: () => string;
28
40
  getBaseURL: () => string;
@@ -30,6 +42,7 @@ export interface Site {
30
42
  getImsOrgId: () => string;
31
43
  getCreatedAt: () => string;
32
44
  getUpdatedAt: () => string;
45
+ getAuditConfig: () => AuditConfig;
33
46
  getAudits: () => Audit[];
34
47
  isLive: () => boolean;
35
48
  setAudits: (audits: Audit[]) => Site;
@@ -61,6 +61,7 @@ const Audit = (data = {}) => {
61
61
  self.getExpiresAt = () => self.state.expiresAt;
62
62
  self.getFullAuditRef = () => self.state.fullAuditRef;
63
63
  self.isLive = () => self.state.isLive;
64
+ self.isError = () => hasText(self.getAuditResult().runtimeError?.code);
64
65
  self.getScores = () => self.getAuditResult().scores;
65
66
 
66
67
  return Object.freeze(self);
@@ -0,0 +1,28 @@
1
+ /*
2
+ * Copyright 2023 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ const AuditConfigType = (data = {}, allAuditsDisabled = false) => ({
14
+ disabled: () => allAuditsDisabled || data.disabled || false,
15
+ });
16
+
17
+ AuditConfigType.fromDynamoItem = (dynamoItem) => {
18
+ const auditConfigTypeData = {
19
+ disabled: dynamoItem.disabled,
20
+ };
21
+ return AuditConfigType(auditConfigTypeData);
22
+ };
23
+
24
+ AuditConfigType.toDynamoItem = (auditConfigType) => ({
25
+ disabled: auditConfigType.disabled(),
26
+ });
27
+
28
+ export default AuditConfigType;
@@ -0,0 +1,41 @@
1
+ /*
2
+ * Copyright 2023 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import AuditConfigType from './audit-config-type.js';
13
+
14
+ function getAuditTypeConfigs(auditTypeConfigs, auditsDisabled) {
15
+ return Object.entries(auditTypeConfigs || {}).reduce((acc, [key, value]) => {
16
+ acc[key] = AuditConfigType(value, auditsDisabled);
17
+ return acc;
18
+ }, {});
19
+ }
20
+
21
+ const AuditConfig = (data = {}) => {
22
+ const auditTypeConfigs = getAuditTypeConfigs(data.auditTypeConfigs, data.auditsDisabled);
23
+ return {
24
+ auditsDisabled: () => data.auditsDisabled || false,
25
+ getAuditTypeConfigs: () => auditTypeConfigs,
26
+ getAuditTypeConfig: (type) => auditTypeConfigs[type],
27
+ };
28
+ };
29
+
30
+ AuditConfig.fromDynamoItem = (dynamoItem) => AuditConfig(dynamoItem);
31
+
32
+ AuditConfig.toDynamoItem = (auditConfig) => ({
33
+ auditsDisabled: auditConfig.auditsDisabled(),
34
+ auditTypeConfigs: Object.entries(auditConfig.getAuditTypeConfigs())
35
+ .reduce((acc, [key, value]) => {
36
+ acc[key] = AuditConfigType.toDynamoItem(value);
37
+ return acc;
38
+ }, {}),
39
+ });
40
+
41
+ export default AuditConfig;
@@ -10,8 +10,10 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import { hasText, isValidUrl } from '@adobe/spacecat-shared-utils';
13
+ import { hasText, isObject, isValidUrl } from '@adobe/spacecat-shared-utils';
14
+
14
15
  import { Base } from './base.js';
16
+ import AuditConfig from './site/audit-config.js';
15
17
 
16
18
  /**
17
19
  * Creates a new Site.
@@ -22,6 +24,7 @@ import { Base } from './base.js';
22
24
  const Site = (data = {}) => {
23
25
  const self = Base(data);
24
26
 
27
+ self.getAuditConfig = () => self.state.auditConfig;
25
28
  self.getAudits = () => self.state.audits;
26
29
  self.getBaseURL = () => self.state.baseURL;
27
30
  self.getGitHubURL = () => self.state.gitHubURL;
@@ -135,5 +138,14 @@ export const createSite = (data) => {
135
138
  newState.audits = [];
136
139
  }
137
140
 
141
+ if (!isObject(newState.auditConfig)) {
142
+ newState.auditConfig = {
143
+ auditsDisabled: false,
144
+ auditTypeConfigs: {},
145
+ };
146
+ }
147
+
148
+ newState.auditConfig = AuditConfig(newState.auditConfig);
149
+
138
150
  return Site(newState);
139
151
  };
@@ -55,19 +55,18 @@ export const getSitesToAudit = async (dynamoClient, config) => {
55
55
  };
56
56
 
57
57
  /**
58
- * Retrieves sites with their latest audit of a specified type. If there is
59
- * no audit of the specified type for a site, the site will be returned with
60
- * an empty audits array.
61
- * The audits are sorted ascending or descending by scores.
62
- *
58
+ * Retrieves all sites with their latest audit. Sites without a latest audit will be included
59
+ * in the result, but will have an empty audits array. The sites are sorted by their latest
60
+ * audit scores in ascending order by default. The sortAuditsAscending parameter can be used
61
+ * to change the sort order. If a site has no latest audit, it will be sorted at the end of
62
+ * the list.
63
63
  * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
64
64
  * @param {DataAccessConfig} config - The data access config.
65
65
  * @param {Logger} log - The logger.
66
- * @param {string} auditType - The type of the latest audits to retrieve for each site.
67
- * @param {boolean} [sortAuditsAscending] - Optional. Determines if the audits
68
- * should be sorted ascending or descending by scores.
69
- * @returns {Promise<Readonly<Site>[]>} A promise that resolves to an array of site objects,
70
- * each with its latest audit of the specified type.
66
+ * @param {string} auditType - The type of audits to retrieve for the sites.
67
+ * @param {boolean} [sortAuditsAscending=true] - Determines if the audits should be sorted in
68
+ * @return {Promise<Readonly<Site>[]>} A promise that resolves to an array of sites with their
69
+ * latest audit.
71
70
  */
72
71
  export const getSitesWithLatestAudit = async (
73
72
  dynamoClient,
@@ -81,17 +80,26 @@ export const getSitesWithLatestAudit = async (
81
80
  getLatestAudits(dynamoClient, config, log, auditType, sortAuditsAscending),
82
81
  ]);
83
82
 
84
- const auditsMap = new Map(latestAudits.map((audit) => [audit.getSiteId(), audit]));
83
+ const sitesMap = new Map(sites.map((site) => [site.getId(), site]));
84
+ const orderedSites = [];
85
85
 
86
- return sites.map((site) => {
87
- const audit = auditsMap.get(site.getId());
88
- if (audit) {
86
+ // First, append sites with a latest audit in the sorted order
87
+ latestAudits.forEach((audit) => {
88
+ const site = sitesMap.get(audit.getSiteId());
89
+ if (site) {
89
90
  site.setAudits([audit]);
90
- } else {
91
- site.setAudits([]);
91
+ orderedSites.push(site);
92
+ sitesMap.delete(site.getId()); // Remove the site from the map to avoid adding it again
92
93
  }
93
- return site;
94
94
  });
95
+
96
+ // Then, append the remaining sites (without a latest audit)
97
+ sitesMap.forEach((site) => {
98
+ site.setAudits([]);
99
+ orderedSites.push(site);
100
+ });
101
+
102
+ return orderedSites;
95
103
  };
96
104
 
97
105
  /**