@adobe/spacecat-shared-data-access 1.37.0 → 1.38.1

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.38.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.38.0...@adobe/spacecat-shared-data-access-v1.38.1) (2024-07-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * naming issues ([#300](https://github.com/adobe/spacecat-shared/issues/300)) ([f0a7468](https://github.com/adobe/spacecat-shared/commit/f0a7468526a5f71c4bfed5ec0b88c3c889316cd6))
7
+
8
+ # [@adobe/spacecat-shared-data-access-v1.38.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.37.0...@adobe/spacecat-shared-data-access-v1.38.0) (2024-07-23)
9
+
10
+
11
+ ### Features
12
+
13
+ * Experimentation entity ([#288](https://github.com/adobe/spacecat-shared/issues/288)) ([774e2c7](https://github.com/adobe/spacecat-shared/commit/774e2c7013c9e617c745c494e20e1cdd8cce71e7))
14
+
1
15
  # [@adobe/spacecat-shared-data-access-v1.37.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.36.0...@adobe/spacecat-shared-data-access-v1.37.0) (2024-07-22)
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.37.0",
3
+ "version": "1.38.1",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,66 @@
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 { createExperiment } from '../models/experiment.js';
14
+
15
+ /**
16
+ * Data transfer object for Experiment.
17
+ */
18
+ export const ExperimentDto = {
19
+ /**
20
+ * Converts an Experiment object into a DynamoDB item.
21
+ * @param {Readonly<Experiment>} experiment - Experiment object.
22
+ * @returns {{siteId, id, name, url, status, type, startDate, endDate,
23
+ * variants, updatedAt, updatedBy, conversionEventName, conversionEventValue}}
24
+ */
25
+ toDynamoItem: (experiment) => ({
26
+ siteId: experiment.getSiteId(),
27
+ experimentId: experiment.getExperimentId(),
28
+ name: experiment.getName(),
29
+ url: experiment.getUrl(),
30
+ status: experiment.getStatus(),
31
+ type: experiment.getType(),
32
+ startDate: experiment.getStartDate(),
33
+ endDate: experiment.getEndDate(),
34
+ variants: experiment.getVariants(),
35
+ updatedAt: experiment.getUpdatedAt(),
36
+ updatedBy: experiment.getUpdatedBy(),
37
+ conversionEventName: experiment.getConversionEventName(),
38
+ conversionEventValue: experiment.getConversionEventValue(),
39
+ SK: `${experiment.getExperimentId()}#${experiment.getUrl()}`,
40
+ }),
41
+
42
+ /**
43
+ * Converts a DynamoDB item into Experiment object;
44
+ * @param {object } dynamoItem - DynamoDB item.
45
+ * @returns {Readonly<Experiment>} Experiment object.
46
+ */
47
+ fromDynamoItem: (dynamoItem) => {
48
+ const experiment = {
49
+ siteId: dynamoItem.siteId,
50
+ experimentId: dynamoItem.experimentId,
51
+ name: dynamoItem.name,
52
+ url: dynamoItem.url,
53
+ status: dynamoItem.status,
54
+ type: dynamoItem.type,
55
+ startDate: dynamoItem.startDate,
56
+ endDate: dynamoItem.endDate,
57
+ variants: dynamoItem.variants,
58
+ updatedAt: dynamoItem.updatedAt,
59
+ updatedBy: dynamoItem.updatedBy,
60
+ conversionEventName: dynamoItem.conversionEventName,
61
+ conversionEventValue: dynamoItem.conversionEventValue,
62
+ };
63
+
64
+ return createExperiment(experiment);
65
+ },
66
+ };
package/src/index.d.ts CHANGED
@@ -534,6 +534,76 @@ export interface ImportUrl {
534
534
 
535
535
  }
536
536
 
537
+ /**
538
+ * Represents an experiment entity.
539
+ */
540
+ export interface Experiment {
541
+ /**
542
+ * Retrieves the ID of the experiment.
543
+ */
544
+ getExperimentId: () => string;
545
+
546
+ /**
547
+ * Retrieves the site ID of the experiment.
548
+ */
549
+ getSiteId: () => string;
550
+
551
+ /**
552
+ * Retrieves the Control URL of the experiment.
553
+ */
554
+ getUrl: () => string;
555
+
556
+ /**
557
+ * Retrieves the experiment name.
558
+ */
559
+ getName: () => string;
560
+
561
+ /**
562
+ * Retrieves the experiment type.
563
+ */
564
+ getType: () => string;
565
+
566
+ /**
567
+ * Retrieves the experiment status.
568
+ */
569
+ getStatus: () => string;
570
+
571
+ /**
572
+ * Retrieves the experiment variants.
573
+ */
574
+ getVariants: () => Array<object>;
575
+
576
+ /**
577
+ * Retrieves the experiment start date.
578
+ */
579
+ getStartDate: () => string;
580
+
581
+ /**
582
+ * Retrieves the experiment end date.
583
+ */
584
+ getEndDate: () => string;
585
+
586
+ /**
587
+ * Retrieves the conversion event name.
588
+ */
589
+ getConversionEventName: () => string;
590
+
591
+ /**
592
+ * Retrieves the conversion event value
593
+ */
594
+ getConversionEventValue: () => string;
595
+
596
+ /**
597
+ * Retrieves the last update timestamp of the experiment entity in persistent store.
598
+ */
599
+ getUpdatedAt: () => string;
600
+
601
+ /**
602
+ * Retrieves the updated by of the experiment entity in persistent store.
603
+ */
604
+ getUpdatedBy: () => string;
605
+ }
606
+
537
607
  export interface DataAccess {
538
608
  getAuditForSite: (
539
609
  sitedId: string,
@@ -675,6 +745,11 @@ export interface DataAccess {
675
745
  createKeyEvent: (keyEventData: object) => Promise<KeyEvent>;
676
746
  getKeyEventsForSite: (siteId: string) => Promise<KeyEvent[]>
677
747
  removeKeyEvent: (keyEventId: string) => Promise<void>;
748
+
749
+ // experiment functions
750
+ getExperiments: (siteId: string, experimentId?: string) => Promise<Experiment[]>;
751
+ getExperiment: (siteId: string, experimentId: string, url: string) => Promise<Experiment | null>;
752
+ upsertExperiment: (experimentData: object) => Promise<Experiment>;
678
753
  }
679
754
 
680
755
  interface DataAccessConfig {
@@ -688,6 +763,7 @@ interface DataAccessConfig {
688
763
  tableNameSiteTopPages: string;
689
764
  tableNameImportJobs: string;
690
765
  tableNameImportUrls: string;
766
+ tableNameExperiments: string;
691
767
  indexNameAllKeyEventsBySiteId: string,
692
768
  indexNameAllSites: string;
693
769
  indexNameAllSitesOrganizations: string,
package/src/index.js CHANGED
@@ -24,6 +24,7 @@ const TABLE_NAME_CONFIGURATIONS = 'spacecat-services-configurations-dev';
24
24
  const TABLE_NAME_SITE_TOP_PAGES = 'spacecat-services-site-top-pages-dev';
25
25
  const TABLE_NAME_IMPORT_JOBS = 'spacecat-services-import-jobs-dev';
26
26
  const TABLE_NAME_IMPORT_URLS = 'spacecat-services-import-urls-dev';
27
+ const TABLE_NAME_EXPERIMENTS = 'spacecat-services-experiments-dev';
27
28
 
28
29
  const INDEX_NAME_ALL_KEY_EVENTS_BY_SITE_ID = 'spacecat-services-key-events-by-site-id';
29
30
  const INDEX_NAME_ALL_SITES = 'spacecat-services-all-sites-dev';
@@ -58,6 +59,7 @@ export default function dataAccessWrapper(fn) {
58
59
  DYNAMO_TABLE_NAME_SITE_TOP_PAGES = TABLE_NAME_SITE_TOP_PAGES,
59
60
  DYNAMO_TABLE_NAME_IMPORT_JOBS = TABLE_NAME_IMPORT_JOBS,
60
61
  DYNAMO_TABLE_NAME_IMPORT_URLS = TABLE_NAME_IMPORT_URLS,
62
+ DYNAMO_TABLE_NAME_EXPERIMENTS = TABLE_NAME_EXPERIMENTS,
61
63
  DYNAMO_INDEX_NAME_ALL_KEY_EVENTS_BY_SITE_ID = INDEX_NAME_ALL_KEY_EVENTS_BY_SITE_ID,
62
64
  DYNAMO_INDEX_NAME_ALL_SITES = INDEX_NAME_ALL_SITES,
63
65
  DYNAMO_INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE = INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE,
@@ -83,6 +85,7 @@ export default function dataAccessWrapper(fn) {
83
85
  tableNameSiteTopPages: DYNAMO_TABLE_NAME_SITE_TOP_PAGES,
84
86
  tableNameImportJobs: DYNAMO_TABLE_NAME_IMPORT_JOBS,
85
87
  tableNameImportUrls: DYNAMO_TABLE_NAME_IMPORT_URLS,
88
+ tableNameExperiments: DYNAMO_TABLE_NAME_EXPERIMENTS,
86
89
  indexNameAllKeyEventsBySiteId: DYNAMO_INDEX_NAME_ALL_KEY_EVENTS_BY_SITE_ID,
87
90
  indexNameAllSites: DYNAMO_INDEX_NAME_ALL_SITES,
88
91
  indexNameAllOrganizations: DYNAMO_INDEX_NAME_ALL_ORGANIZATIONS,
@@ -0,0 +1,67 @@
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 } from '@adobe/spacecat-shared-utils';
14
+
15
+ import { Base } from './base.js';
16
+
17
+ export const DEFAULT_UPDATED_BY = 'spacecat';
18
+
19
+ /**
20
+ * Creates a new Experiment.
21
+ *
22
+ * @param {object} data - experiment data
23
+ * @returns {Readonly<SiteCandidate>} new experiment
24
+ */
25
+ const Experiment = (data = {}) => {
26
+ const self = Base(data);
27
+
28
+ self.getSiteId = () => self.state.siteId;
29
+ self.getExperimentId = () => self.state.experimentId;
30
+ self.getName = () => self.state.name;
31
+ self.getUrl = () => self.state.url;
32
+ self.getStatus = () => self.state.status;
33
+ self.getType = () => self.state.type;
34
+ self.getStartDate = () => self.state.startDate;
35
+ self.getEndDate = () => self.state.endDate;
36
+ self.getVariants = () => self.state.variants;
37
+ self.getUpdatedAt = () => self.state.updatedAt;
38
+ self.getUpdatedBy = () => self.state.updatedBy;
39
+ self.getConversionEventName = () => self.state.conversionEventName;
40
+ self.getConversionEventValue = () => self.state.conversionEventValue;
41
+
42
+ return Object.freeze(self);
43
+ };
44
+
45
+ /**
46
+ * Creates a new Experiment.
47
+ *
48
+ * @param {object} data - experiment data
49
+ * @returns {Readonly<SiteCandidate>} new experiment
50
+ */
51
+ export const createExperiment = (data) => {
52
+ const newState = { ...data };
53
+
54
+ if (!hasText(newState.siteId)) {
55
+ throw new Error('Site ID must be provided');
56
+ }
57
+
58
+ if (!hasText(newState.experimentId)) {
59
+ throw new Error('Experiment ID must be provided');
60
+ }
61
+
62
+ if (!hasText(newState.updatedBy)) {
63
+ newState.updatedBy = DEFAULT_UPDATED_BY;
64
+ }
65
+
66
+ return Experiment(newState);
67
+ };
@@ -61,7 +61,7 @@ export const Config = (data = {}) => {
61
61
  self.getHandlers = () => state.handlers;
62
62
  self.getImports = () => state.imports;
63
63
  self.getExcludedURLs = (type) => state?.handlers[type]?.excludedURLs;
64
- self.getManualOverrides = (type) => state?.handlers[type]?.manualOverwrites;
64
+ self.getManualOverwrites = (type) => state?.handlers[type]?.manualOverwrites;
65
65
  self.getFixedURLs = (type) => state?.handlers[type]?.fixedURLs;
66
66
 
67
67
  self.updateSlackConfig = (channel, workspace, invitedUserCount) => {
@@ -79,13 +79,13 @@ export const Config = (data = {}) => {
79
79
  state.handlers[type].mentions.slack = mentions;
80
80
  };
81
81
 
82
- self.updateExcludeURLs = (type, excludedURLs) => {
82
+ self.updateExcludedURLs = (type, excludedURLs) => {
83
83
  state.handlers = state.handlers || {};
84
84
  state.handlers[type] = state.handlers[type] || {};
85
85
  state.handlers[type].excludedURLs = excludedURLs;
86
86
  };
87
87
 
88
- self.updateManualOverrides = (type, manualOverwrites) => {
88
+ self.updateManualOverwrites = (type, manualOverwrites) => {
89
89
  state.handlers = state.handlers || {};
90
90
  state.handlers[type] = state.handlers[type] || {};
91
91
  state.handlers[type].manualOverwrites = manualOverwrites;
@@ -0,0 +1,98 @@
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 { isObject } from '@adobe/spacecat-shared-utils';
14
+ import { createExperiment } from '../../models/experiment.js';
15
+ import { ExperimentDto } from '../../dto/experiment.js';
16
+
17
+ /**
18
+ * Returns the experiment if exists in experiments table using siteId, experimentId and url
19
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
20
+ * @param {DataAccessConfig} config - The data access config.
21
+ * @param {string} siteId - siteId of the experiment.
22
+ * @param {string} id - id of the experiment.
23
+ * @return the experiment if exists, null otherwise
24
+ */
25
+ export const getExperiment = async (dynamoClient, config, siteId, experimentId, url) => {
26
+ const queryParams = {
27
+ TableName: config.tableNameExperiments,
28
+ KeyConditionExpression: 'siteId = :siteId AND SK = :SK',
29
+ ExpressionAttributeValues: {
30
+ ':siteId': siteId,
31
+ ':SK': `${experimentId}#${url}`,
32
+ },
33
+ ScanIndexForward: false,
34
+ };
35
+ const dynamoItems = await dynamoClient.query(queryParams);
36
+ if (dynamoItems != null && dynamoItems.length > 0 && isObject(dynamoItems[0])) {
37
+ return ExperimentDto.fromDynamoItem(dynamoItems[0]);
38
+ }
39
+
40
+ return null;
41
+ };
42
+
43
+ /**
44
+ * Retrieves all experiments for a given siteId.
45
+ * @param {*} dynamoClient - The DynamoDB client.
46
+ * @param {*} config - The data access config.
47
+ * @param {*} log - the logger object
48
+ * @param {*} siteId - siteId of the experiment.
49
+ * @param {*} experimentId - experiment id.
50
+ * @returns {Promise<ReadonlyArray<SiteCandidate>>} A promise that resolves to
51
+ * an array of experiments
52
+ */
53
+ export const getExperiments = async (
54
+ dynamoClient,
55
+ config,
56
+ log,
57
+ siteId,
58
+ experimentId,
59
+ ) => {
60
+ const queryParams = {
61
+ TableName: config.tableNameExperiments,
62
+ KeyConditionExpression: 'siteId = :siteId',
63
+ ExpressionAttributeValues: { ':siteId': siteId },
64
+ ScanIndexForward: false,
65
+ };
66
+ if (experimentId) {
67
+ queryParams.KeyConditionExpression += ' AND begins_with(SK, :experimentId)';
68
+ queryParams.ExpressionAttributeValues[':experimentId'] = experimentId;
69
+ }
70
+ const dynamoItems = await dynamoClient.query(queryParams);
71
+
72
+ return dynamoItems.map((item) => ExperimentDto.fromDynamoItem(item));
73
+ };
74
+
75
+ /**
76
+ * Upserts an experiment.
77
+ *
78
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
79
+ * @param {DataAccessConfig} config - The data access config.
80
+ * @param {object} log - the logger object
81
+ * @param {object} experimentData - The experiment data.
82
+ * @returns {Promise<Readonly<SiteCandidate>>} A promise that resolves to newly created/updated
83
+ * experiment
84
+ */
85
+ export const upsertExperiment = async (
86
+ dynamoClient,
87
+ config,
88
+ log,
89
+ experimentData,
90
+ ) => {
91
+ const experiment = createExperiment(experimentData);
92
+ await dynamoClient.putItem(
93
+ config.tableNameExperiments,
94
+ ExperimentDto.toDynamoItem(experiment),
95
+ );
96
+
97
+ return experiment;
98
+ };
@@ -0,0 +1,40 @@
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
+ getExperiments,
15
+ upsertExperiment,
16
+ getExperiment,
17
+ } from './accessPatterns.js';
18
+
19
+ export const experimentFunctions = (dynamoClient, config, log) => ({
20
+ getExperiments: (siteId, experimentId) => getExperiments(
21
+ dynamoClient,
22
+ config,
23
+ log,
24
+ siteId,
25
+ experimentId,
26
+ ),
27
+ upsertExperiment: (experimentData) => upsertExperiment(
28
+ dynamoClient,
29
+ config,
30
+ log,
31
+ experimentData,
32
+ ),
33
+ getExperiment: (siteId, experimentId, url) => getExperiment(
34
+ dynamoClient,
35
+ config,
36
+ siteId,
37
+ experimentId,
38
+ url,
39
+ ),
40
+ });
@@ -20,13 +20,15 @@ import { configurationFunctions } from './configurations/index.js';
20
20
  import { siteTopPagesFunctions } from './site-top-pages/index.js';
21
21
  import { importJobFunctions } from './import-job/index.js';
22
22
  import { importUrlFunctions } from './import-url/index.js';
23
+ import { experimentFunctions } from './experiments/index.js';
23
24
 
24
25
  /**
25
26
  * Creates a data access object.
26
27
  *
27
28
  * @param {{pkAllSites: string, pkAllLatestAudits: string, indexNameAllLatestAuditScores: string,
28
29
  * tableNameAudits: string,tableNameLatestAudits: string, indexNameAllSitesOrganizations: string,
29
- * tableNameSites: string, tableNameOrganizations: string, indexNameAllSites: string,
30
+ * tableNameSites: string, tableNameOrganizations: string, tableNameExperiments: string,
31
+ * indexNameAllSites: string,
30
32
  * tableNameImportJobs: string, pkAllImportJobs: string, indexNameAllImportJobs: string,
31
33
  * tableNameSiteTopPages: string, indexNameAllOrganizations: string,
32
34
  * indexNameAllOrganizationsByImsOrgId: string, pkAllOrganizations: string}} config configuration
@@ -45,6 +47,7 @@ export const createDataAccess = (config, log = console) => {
45
47
  const siteTopPagesFuncs = siteTopPagesFunctions(dynamoClient, config);
46
48
  const importJobFuncs = importJobFunctions(dynamoClient, config, log);
47
49
  const importUrlFuncs = importUrlFunctions(dynamoClient, config, log);
50
+ const experimentFuncs = experimentFunctions(dynamoClient, config, log);
48
51
 
49
52
  return {
50
53
  ...auditFuncs,
@@ -56,5 +59,6 @@ export const createDataAccess = (config, log = console) => {
56
59
  ...siteTopPagesFuncs,
57
60
  ...importJobFuncs,
58
61
  ...importUrlFuncs,
62
+ ...experimentFuncs,
59
63
  };
60
64
  };