@adobe/spacecat-shared-data-access 1.22.1 → 1.23.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,10 @@
1
+ # [@adobe/spacecat-shared-data-access-v1.23.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.22.1...@adobe/spacecat-shared-data-access-v1.23.0) (2024-05-03)
2
+
3
+
4
+ ### Features
5
+
6
+ * key events data access layer ([#223](https://github.com/adobe/spacecat-shared/issues/223)) ([83228cc](https://github.com/adobe/spacecat-shared/commit/83228ccb3011868da54460b7fb2788e27ee33454))
7
+
1
8
  # [@adobe/spacecat-shared-data-access-v1.22.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.22.0...@adobe/spacecat-shared-data-access-v1.22.1) (2024-04-30)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-data-access",
3
- "version": "1.22.1",
3
+ "version": "1.23.0",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,52 @@
1
+ /*
2
+ * Copyright 2024 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
+ import { createKeyEvent } from '../models/key-event.js';
14
+
15
+ /**
16
+ * Data transfer object for Key Event.
17
+ */
18
+ export const KeyEventDto = {
19
+ /**
20
+ * Converts a DynamoDB item into a KeyEvent object.
21
+ * @param {object } dynamoItem - DynamoDB item.
22
+ * @returns {Readonly<KeyEvent>} KeyEvent object.
23
+ */
24
+ fromDynamoItem: (dynamoItem) => {
25
+ const keyEventData = {
26
+ id: dynamoItem.id,
27
+ siteId: dynamoItem.siteId,
28
+ name: dynamoItem.name,
29
+ type: dynamoItem.type,
30
+ time: dynamoItem.time,
31
+ createdAt: dynamoItem.createdAt,
32
+ updatedAt: dynamoItem.updatedAt,
33
+ };
34
+
35
+ return createKeyEvent(keyEventData);
36
+ },
37
+
38
+ /**
39
+ * Converts a KeyEvent object into a DynamoDB item.
40
+ * @param {Readonly<KeyEvent>} keyEvent - KeyEvent object.
41
+ * @returns {object} DynamoDB item.
42
+ */
43
+ toDynamoItem: (keyEvent) => ({
44
+ id: keyEvent.getId(),
45
+ siteId: keyEvent.getSiteId(),
46
+ name: keyEvent.getName(),
47
+ type: keyEvent.getType(),
48
+ time: keyEvent.getTime(),
49
+ createdAt: keyEvent.getCreatedAt(),
50
+ updatedAt: keyEvent.getUpdatedAt(),
51
+ }),
52
+ };
package/src/index.d.ts CHANGED
@@ -129,6 +129,53 @@ export interface AuditConfig {
129
129
  getAuditTypeConfigs: () => object;
130
130
  }
131
131
 
132
+ /**
133
+ * Represents a key event.
134
+ */
135
+ export interface KeyEvent {
136
+ /**
137
+ * Retrieves the ID of the key event.
138
+ * @returns {string} The Key Event ID.
139
+ */
140
+ getId: () => string;
141
+
142
+ /**
143
+ * Retrieves the site id of the key event.
144
+ * @returns {string} site id
145
+ */
146
+ getSiteId: () => string;
147
+
148
+ /**
149
+ * Retrieves the name of the key event.
150
+ * @returns {string} The name
151
+ */
152
+ getName: () => string;
153
+
154
+ /**
155
+ * Retrieves the type of the key event.
156
+ * @returns {string} The type
157
+ */
158
+ getType: () => string;
159
+
160
+ /**
161
+ * Retrieves the time of the key event.
162
+ * @returns {string} The time
163
+ */
164
+ getTime: () => string;
165
+
166
+ /**
167
+ * Retrieves the creation timestamp of the key event.
168
+ * @returns {string} The creation timestamp.
169
+ */
170
+ getCreatedAt: () => string;
171
+
172
+ /**
173
+ * Retrieves the last update timestamp of the key event.
174
+ * @returns {string} The last update timestamp.
175
+ */
176
+ getUpdatedAt: () => string;
177
+ }
178
+
132
179
  /**
133
180
  * Represents a site with associated audit and configuration data.
134
181
  */
@@ -491,16 +538,23 @@ export interface DataAccess {
491
538
  getConfigurations: () => Promise<Readonly<Configuration>[]>
492
539
  getConfigurationByVersion: (version: string) => Promise<Readonly<Configuration>>
493
540
  updateConfiguration: (configurationData: object) => Promise<Readonly<Configuration>>
541
+
542
+ // key events functions
543
+ createKeyEvent: (keyEventData: object) => Promise<KeyEvent>;
544
+ getKeyEventsForSite: (siteId: string) => Promise<KeyEvent[]>
545
+ removeKeyEvent: (keyEventId: string) => Promise<void>;
494
546
  }
495
547
 
496
548
  interface DataAccessConfig {
497
549
  tableNameAudits: string;
550
+ tableNameKeyEvents: string;
498
551
  tableNameLatestAudits: string;
499
552
  tableNameOrganizations: string,
500
553
  tableNameSites: string;
501
554
  tableNameSiteCandidates: string;
502
555
  tableNameConfigurations: string;
503
556
  tableNameSiteTopPages: string;
557
+ indexNameAllKeyEventsBySiteId: string,
504
558
  indexNameAllSites: string;
505
559
  indexNameAllSitesOrganizations: string,
506
560
  indexNameAllSitesByDeliveryType: string;
package/src/index.js CHANGED
@@ -13,6 +13,7 @@
13
13
  import { createDataAccess } from './service/index.js';
14
14
 
15
15
  const TABLE_NAME_AUDITS = 'spacecat-services-audits-dev';
16
+ const TABLE_NAME_KEY_EVENTS = 'spacecat-services-key-events';
16
17
  const TABLE_NAME_LATEST_AUDITS = 'spacecat-services-latest-audits-dev';
17
18
  const TABLE_NAME_SITES = 'spacecat-services-sites-dev';
18
19
  const TABLE_NAME_SITE_CANDIDATES = 'spacecat-services-site-candidates-dev';
@@ -20,6 +21,7 @@ const TABLE_NAME_ORGANIZATIONS = 'spacecat-services-organizations-dev';
20
21
  const TABLE_NAME_CONFIGURATIONS = 'spacecat-services-configurations-dev';
21
22
  const TABLE_NAME_SITE_TOP_PAGES = 'spacecat-services-site-top-pages-dev';
22
23
 
24
+ const INDEX_NAME_ALL_KEY_EVENTS_BY_SITE_ID = 'spacecat-services-key-events-by-site-id';
23
25
  const INDEX_NAME_ALL_SITES = 'spacecat-services-all-sites-dev';
24
26
  const INDEX_NAME_ALL_ORGANIZATIONS = 'spacecat-services-all-organizations-dev';
25
27
  const INDEX_NAME_ALL_ORGANIZATIONS_BY_IMS_ORG_ID = 'spacecat-services-all-organizations-by-ims-org-id-dev';
@@ -39,12 +41,14 @@ export default function dataAccessWrapper(fn) {
39
41
 
40
42
  const {
41
43
  DYNAMO_TABLE_NAME_AUDITS = TABLE_NAME_AUDITS,
44
+ DYNAMO_TABLE_NAME_KEY_EVENTS = TABLE_NAME_KEY_EVENTS,
42
45
  DYNAMO_TABLE_NAME_LATEST_AUDITS = TABLE_NAME_LATEST_AUDITS,
43
46
  DYNAMO_TABLE_NAME_SITES = TABLE_NAME_SITES,
44
47
  DYNAMO_TABLE_NAME_SITE_CANDIDATES = TABLE_NAME_SITE_CANDIDATES,
45
48
  DYNAMO_TABLE_NAME_ORGANIZATIONS = TABLE_NAME_ORGANIZATIONS,
46
49
  DYNAMO_TABLE_NAME_CONFIGURATIONS = TABLE_NAME_CONFIGURATIONS,
47
50
  DYNAMO_TABLE_NAME_SITE_TOP_PAGES = TABLE_NAME_SITE_TOP_PAGES,
51
+ DYNAMO_INDEX_NAME_ALL_KEY_EVENTS_BY_SITE_ID = INDEX_NAME_ALL_KEY_EVENTS_BY_SITE_ID,
48
52
  DYNAMO_INDEX_NAME_ALL_SITES = INDEX_NAME_ALL_SITES,
49
53
  DYNAMO_INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE = INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE,
50
54
  DYNAMO_INDEX_NAME_ALL_LATEST_AUDIT_SCORES = INDEX_NAME_ALL_LATEST_AUDIT_SCORES,
@@ -56,12 +60,14 @@ export default function dataAccessWrapper(fn) {
56
60
 
57
61
  context.dataAccess = createDataAccess({
58
62
  tableNameAudits: DYNAMO_TABLE_NAME_AUDITS,
63
+ tableNameKeyEvents: DYNAMO_TABLE_NAME_KEY_EVENTS,
59
64
  tableNameLatestAudits: DYNAMO_TABLE_NAME_LATEST_AUDITS,
60
65
  tableNameOrganizations: DYNAMO_TABLE_NAME_ORGANIZATIONS,
61
66
  tableNameSites: DYNAMO_TABLE_NAME_SITES,
62
67
  tableNameSiteCandidates: DYNAMO_TABLE_NAME_SITE_CANDIDATES,
63
68
  tableNameConfigurations: DYNAMO_TABLE_NAME_CONFIGURATIONS,
64
69
  tableNameSiteTopPages: DYNAMO_TABLE_NAME_SITE_TOP_PAGES,
70
+ indexNameAllKeyEventsBySiteId: DYNAMO_INDEX_NAME_ALL_KEY_EVENTS_BY_SITE_ID,
65
71
  indexNameAllSites: DYNAMO_INDEX_NAME_ALL_SITES,
66
72
  indexNameAllOrganizations: DYNAMO_INDEX_NAME_ALL_ORGANIZATIONS,
67
73
  indexNameAllOrganizationsByImsOrgId: DYNAMO_INDEX_NAME_ALL_ORGANIZATIONS_BY_IMS_ORG_ID,
@@ -0,0 +1,80 @@
1
+ /*
2
+ * Copyright 2024 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
+ import { hasText, isIsoDate } from '@adobe/spacecat-shared-utils';
14
+
15
+ import { Base } from './base.js';
16
+
17
+ export const KEY_EVENT_TYPES = {
18
+ PERFORMANCE: 'PERFORMANCE',
19
+ SEO: 'SEO',
20
+ CONTENT: 'CONTENT',
21
+ CODE: 'CODE',
22
+ THIRD_PARTY: 'THIRD PARTY',
23
+ EXPERIMENTATION: 'EXPERIMENTATION',
24
+ NETWORK: 'NETWORK',
25
+ };
26
+
27
+ /**
28
+ * Creates a new Key Event.
29
+ *
30
+ * @param {object} data - key event data
31
+ * @returns {Readonly<KeyEvent>} new key event
32
+ */
33
+ const KeyEvent = (data = {}) => {
34
+ const self = Base(data);
35
+
36
+ self.getName = () => self.state.name;
37
+ self.getSiteId = () => self.state.siteId;
38
+ self.getType = () => self.state.type;
39
+ self.getTime = () => self.state.time;
40
+
41
+ // no set functions since all values are required at creation time
42
+
43
+ return Object.freeze(self);
44
+ };
45
+
46
+ /**
47
+ * Creates a new Key Event.
48
+ *
49
+ * @param {object} data - key event data
50
+ * @returns {Readonly<KeyEvent>} new key event
51
+ */
52
+ export const createKeyEvent = (data) => {
53
+ const newState = { ...data };
54
+
55
+ if (!hasText(newState.siteId)) {
56
+ throw new Error('Required field "siteId" is missing');
57
+ }
58
+
59
+ if (!hasText(newState.name)) {
60
+ throw new Error('Required field "name" is missing');
61
+ }
62
+
63
+ if (!hasText(newState.type)) {
64
+ throw new Error('Required field "type" is missing');
65
+ }
66
+
67
+ if (!Object.values(KEY_EVENT_TYPES).includes(newState.type.toUpperCase())) {
68
+ throw new Error(`Unknown value for "type": ${newState.type}`);
69
+ }
70
+
71
+ if (hasText(newState.time) && !isIsoDate(newState.time)) {
72
+ throw new Error('"Time" should be a valid ISO string');
73
+ }
74
+
75
+ if (!hasText(newState.time)) {
76
+ newState.time = new Date().toISOString();
77
+ }
78
+
79
+ return KeyEvent(newState);
80
+ };
@@ -12,6 +12,7 @@
12
12
 
13
13
  import { createClient } from '@adobe/spacecat-shared-dynamo';
14
14
  import { auditFunctions } from './audits/index.js';
15
+ import { keyEventFunctions } from './key-events/index.js';
15
16
  import { siteFunctions } from './sites/index.js';
16
17
  import { siteCandidateFunctions } from './site-candidates/index.js';
17
18
  import { organizationFunctions } from './organizations/index.js';
@@ -33,6 +34,7 @@ export const createDataAccess = (config, log = console) => {
33
34
  const dynamoClient = createClient(log);
34
35
 
35
36
  const auditFuncs = auditFunctions(dynamoClient, config, log);
37
+ const keyEventFuncs = keyEventFunctions(dynamoClient, config, log);
36
38
  const siteFuncs = siteFunctions(dynamoClient, config, log);
37
39
  const siteCandidateFuncs = siteCandidateFunctions(dynamoClient, config, log);
38
40
  const organizationFuncs = organizationFunctions(dynamoClient, config, log);
@@ -41,6 +43,7 @@ export const createDataAccess = (config, log = console) => {
41
43
 
42
44
  return {
43
45
  ...auditFuncs,
46
+ ...keyEventFuncs,
44
47
  ...siteFuncs,
45
48
  ...siteCandidateFuncs,
46
49
  ...organizationFuncs,
@@ -0,0 +1,92 @@
1
+ /*
2
+ * Copyright 2024 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
+ import { createKeyEvent } from '../../models/key-event.js';
14
+ import { KeyEventDto } from '../../dto/key-event.js';
15
+
16
+ /**
17
+ * Adds a new key event for a site.
18
+ *
19
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
20
+ * @param {DataAccessConfig} config - The data access config.
21
+ * @param {object} log - the logger object
22
+ * @param {object} keyEventData - The key event data.
23
+ * @returns {Promise<Readonly<KeyEvent>>} newly created key event
24
+ */
25
+ export const addKeyEvent = async (
26
+ dynamoClient,
27
+ config,
28
+ log,
29
+ keyEventData,
30
+ ) => {
31
+ const keyEvent = createKeyEvent(keyEventData);
32
+
33
+ await dynamoClient.putItem(
34
+ config.tableNameKeyEvents,
35
+ KeyEventDto.toDynamoItem(keyEvent),
36
+ );
37
+
38
+ return keyEvent;
39
+ };
40
+
41
+ /**
42
+ * Retrieves key events for a specified site.
43
+ *
44
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
45
+ * @param {DataAccessConfig} config - The data access config.
46
+ * @param {Logger} log - The logger.
47
+ * @param {string} siteId - The ID of the site for which key events are being retrieved.
48
+ * @param {boolean} ascending - Determines if the key events should be sorted ascending
49
+ * or descending by createdAt.
50
+ * @returns {Promise<Readonly<KeyEvent>[]>} A promise that resolves to an array of key events
51
+ * for the specified site.
52
+ */
53
+ export const getKeyEventsForSite = async (
54
+ dynamoClient,
55
+ config,
56
+ log,
57
+ siteId,
58
+ ascending = false,
59
+ ) => {
60
+ const dynamoItems = await dynamoClient.query({
61
+ TableName: config.tableNameKeyEvents,
62
+ IndexName: config.indexNameAllKeyEventsBySiteId,
63
+ KeyConditionExpression: 'siteId = :siteId',
64
+ ExpressionAttributeValues: { ':siteId': siteId },
65
+ ScanIndexForward: ascending, // Sorts ascending if true, descending if false
66
+ });
67
+
68
+ return dynamoItems.map((item) => KeyEventDto.fromDynamoItem(item));
69
+ };
70
+
71
+ /**
72
+ * Removes a key event.
73
+ *
74
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
75
+ * @param {DataAccessConfig} config - The data access config.
76
+ * @param {Logger} log - The logger.
77
+ * @param {string} keyEventId - The ID of the key event to remove.
78
+ * @returns {Promise<void>}
79
+ */
80
+ export const removeKeyEvent = async (
81
+ dynamoClient,
82
+ config,
83
+ log,
84
+ keyEventId,
85
+ ) => {
86
+ try {
87
+ await dynamoClient.removeItem(config.tableNameKeyEvents, { id: keyEventId });
88
+ } catch (error) {
89
+ log.error(`Error removing key event: ${error.message}`);
90
+ throw error;
91
+ }
92
+ };
@@ -0,0 +1,38 @@
1
+ /*
2
+ * Copyright 2024 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
+ import {
14
+ addKeyEvent,
15
+ getKeyEventsForSite,
16
+ removeKeyEvent,
17
+ } from './accessPatterns.js';
18
+
19
+ export const keyEventFunctions = (dynamoClient, config, log) => ({
20
+ createKeyEvent: (keyEventData) => addKeyEvent(
21
+ dynamoClient,
22
+ config,
23
+ log,
24
+ keyEventData,
25
+ ),
26
+ getKeyEventsForSite: (siteId) => getKeyEventsForSite(
27
+ dynamoClient,
28
+ config,
29
+ log,
30
+ siteId,
31
+ ),
32
+ removeKeyEvent: (keyEventId) => removeKeyEvent(
33
+ dynamoClient,
34
+ config,
35
+ log,
36
+ keyEventId,
37
+ ),
38
+ });