Skip to content

Commit

Permalink
feat: site candidate data model
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney committed Feb 1, 2024
1 parent fb7785e commit 5c9d708
Show file tree
Hide file tree
Showing 14 changed files with 589 additions and 4 deletions.
12 changes: 12 additions & 0 deletions packages/spacecat-shared-data-access/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ npm install @adobe/spacecat-shared-data-access
- **updatedAt** (String): Timestamp of the last update.
- **GSI1PK** (String): Partition key for the Global Secondary Index.

### SiteCandidates
- **baseURL** (String): Base URL of the site candidate.
- **status** (String): Status of the site candidate (DISCOVERED, PENDING, IGNORED, APPROVED)
- **createdAt** (String): Timestamp of creation.
- **updatedAt** (String): Timestamp of the last update.
- **updatedBy** (String): Slack id of the last person updated the site candidate.

### Audits
- **siteId** (String): Identifier of the site being audited.
- **SK** (String): Sort key, typically a composite of audit type and timestamp.
Expand Down Expand Up @@ -65,6 +72,11 @@ The module provides two main DAOs:
- `updateSite`
- `removeSite`

### Site Candidate Functions
- `addSiteCandidate`
- `siteCandidateExists`
- `updateSiteCandidate`

### Audit Functions
- `getAuditsForSite`
- `getAuditForSite`
Expand Down
87 changes: 85 additions & 2 deletions packages/spacecat-shared-data-access/docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"ModelMetadata": {
"Author": "Dominique Jäggi",
"DateCreated": "Nov 23, 2023, 07:00 AM",
"DateLastModified": "Nov 23, 2023, 07:00 AM",
"DateLastModified": "Feb 01, 2024, 03:27 PM",
"Description": "",
"AWSService": "Amazon DynamoDB",
"Version": "3.0"
Expand Down Expand Up @@ -430,6 +430,89 @@
}
}
}
},
{
"TableName": "spacecat-services-site-candidates",
"KeyAttributes": {
"PartitionKey": {
"AttributeName": "baseUrl",
"AttributeType": "S"
}
},
"NonKeyAttributes": [
{
"AttributeName": "status",
"AttributeType": "S"
},
{
"AttributeName": "createdAt",
"AttributeType": "S"
},
{
"AttributeName": "updatedAt",
"AttributeType": "S"
},
{
"AttributeName": "updatedBy",
"AttributeType": "S"
}
],
"DataAccess": {
"MySql": {}
},
"SampleDataFormats": {
"lastModifiedAt": [
"date",
"ISO 8601 date and time"
],
"discoveredAt": [
"date",
"ISO 8601 date and time"
],
"createdAt": [
"date",
"ISO 8601 date and time"
],
"updatedBy": [
"identifiers",
"Full name"
],
"updatedAt": [
"date",
"ISO 8601 date and time"
],
"baseUrl": [
"identifiers",
"URL"
]
},
"BillingMode": "PROVISIONED",
"ProvisionedCapacitySettings": {
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"AutoScalingRead": {
"ScalableTargetRequest": {
"MinCapacity": 1,
"MaxCapacity": 10,
"ServiceRole": "AWSServiceRoleForApplicationAutoScaling_DynamoDBTable"
},
"ScalingPolicyConfiguration": {
"TargetValue": 70
}
},
"AutoScalingWrite": {
"ScalableTargetRequest": {
"MinCapacity": 1,
"MaxCapacity": 10,
"ServiceRole": "AWSServiceRoleForApplicationAutoScaling_DynamoDBTable"
},
"ScalingPolicyConfiguration": {
"TargetValue": 70
}
}
}
}
]
}
}
29 changes: 29 additions & 0 deletions packages/spacecat-shared-data-access/src/dto/site-candidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/**
* Data transfer object for Site Candidate.
*/
export const SiteCandidateDto = {
/**
* Converts a Site Candidate object into a DynamoDB item.
* @param {Readonly<SiteCandidate>} siteCandidate - Site Candidate object.
* @returns {{baseURL, status, createdAt, updatedAt, updatedBy}}
*/
toDynamoItem: (siteCandidate) => ({
baseURL: siteCandidate.getBaseURL(),
status: siteCandidate.getStatus(),
createdAt: siteCandidate.getCreatedAt(),
updatedAt: siteCandidate.getUpdatedAt(),
updatedBy: siteCandidate.getUpdatedBy(),
}),
};
36 changes: 36 additions & 0 deletions packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,41 @@ export interface Site {
updateOrganizationId: (organizationId: string) => Site;
}

/**
* Represents a site candidate.
*/
export interface SiteCandidate {
/**
* Retrieves the base URL of the site candidate.
* @returns {string} The base URL.
*/
getBaseURL: () => string;

/**
* Retrieves the status of the site candidate.
* @returns {string} The delivery type.
*/
getStatus: () => string;

/**
* Retrieves the creation timestamp of the site candidate.
* @returns {string} The creation timestamp.
*/
getCreatedAt: () => string;

/**
* Retrieves the last update timestamp of the site candidate.
* @returns {string} The last update timestamp.
*/
getUpdatedAt: () => string;

/**
* Retrieves the slack id of the person who last updated the site candidate.
* @returns {string} The last update timestamp.
*/
getUpdatedBy: () => string;
}

export interface Organization {
/**
* Retrieves the ID of the site.
Expand Down Expand Up @@ -360,6 +395,7 @@ interface DataAccessConfig {
tableNameLatestAudits: string;
tableNameOrganizations: string,
tableNameSites: string;
tableNameSiteCandidates: string;
indexNameAllSites: string;
indexNameAllSitesOrganizations: string,
indexNameAllSitesByDeliveryType: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/spacecat-shared-data-access/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { createDataAccess } from './service/index.js';
const TABLE_NAME_AUDITS = 'spacecat-services-audits-dev';
const TABLE_NAME_LATEST_AUDITS = 'spacecat-services-latest-audits-dev';
const TABLE_NAME_SITES = 'spacecat-services-sites-dev';
const TABLE_NAME_SITE_CANDIDATES = 'spacecat-services-site-candidates-dev';
const TABLE_NAME_ORGANIZATIONS = 'spacecat-services-organizations-dev';

const INDEX_NAME_ALL_SITES = 'spacecat-services-all-sites-dev';
Expand All @@ -36,6 +37,7 @@ export default function dataAccessWrapper(fn) {
DYNAMO_TABLE_NAME_AUDITS = TABLE_NAME_AUDITS,
DYNAMO_TABLE_NAME_LATEST_AUDITS = TABLE_NAME_LATEST_AUDITS,
DYNAMO_TABLE_NAME_SITES = TABLE_NAME_SITES,
DYNAMO_TABLE_NAME_SITE_CANDIDATES = TABLE_NAME_SITE_CANDIDATES,
DYNAMO_TABLE_NAME_ORGANIZATIONS = TABLE_NAME_ORGANIZATIONS,
DYNAMO_INDEX_NAME_ALL_SITES = INDEX_NAME_ALL_SITES,
DYNAMO_INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE = INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE,
Expand All @@ -49,6 +51,7 @@ export default function dataAccessWrapper(fn) {
tableNameLatestAudits: DYNAMO_TABLE_NAME_LATEST_AUDITS,
tableNameOrganizations: DYNAMO_TABLE_NAME_ORGANIZATIONS,
tableNameSites: DYNAMO_TABLE_NAME_SITES,
tableNameSiteCandidates: DYNAMO_TABLE_NAME_SITE_CANDIDATES,
indexNameAllSites: DYNAMO_INDEX_NAME_ALL_SITES,
indexNameAllOrganizations: DYNAMO_INDEX_NAME_ALL_ORGANIZATIONS,
indexNameAllSitesByDeliveryType: DYNAMO_INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE,
Expand Down
68 changes: 68 additions & 0 deletions packages/spacecat-shared-data-access/src/models/site-candidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import { hasText, isValidUrl } from '@adobe/spacecat-shared-utils';

import { Base } from './base.js';

export const DEFAULT_UPDATED_BY = 'spacecat';

export const SITE_CANDIDATE_STATUS = {
DISCOVERED: 'DISCOVERED', // site candidate is discovered
PENDING: 'PENDING', // site candidate notification sent and waiting for human input
IGNORED: 'IGNORED', // site candidate discarded: not to be added to star catalogue
APPROVED: 'APPROVED', // site candidate is added to star catalogue
};

/**
* Creates a new Site Candidate.
*
* @param {object} data - site candidate data
* @returns {Readonly<SiteCandidate>} new site candidate
*/
const SiteCandidate = (data = {}) => {
const self = Base(data);
delete self.id; // no id property used in SiteCandidate modal

self.getBaseURL = () => self.state.baseURL;
self.getStatus = () => self.state.status;
self.getUpdatedBy = () => self.state.updatedBy;

self.setStatus = (status, updatedBy = DEFAULT_UPDATED_BY) => {
self.state.status = status;
self.state.updatedBy = updatedBy;
self.touch();
return self;
};

return Object.freeze(self);
};

/**
* Creates a new Site Candidate.
*
* @param {object} data - site candidate data
* @returns {Readonly<SiteCandidate>} new site candidate
*/
export const createSiteCandidate = (data) => {
const newState = { ...data };

if (!isValidUrl(newState.baseURL)) {
throw new Error('Base URL must be a valid URL');
}

if (!hasText(newState.updatedBy)) {
newState.updatedBy = DEFAULT_UPDATED_BY;
}

return SiteCandidate(newState);
};
3 changes: 3 additions & 0 deletions packages/spacecat-shared-data-access/src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { createClient } from '@adobe/spacecat-shared-dynamo';
import { auditFunctions } from './audits/index.js';
import { siteFunctions } from './sites/index.js';
import { siteCandidateFunctions } from './site-candidates/index.js';
import { organizationFunctions } from './organizations/index.js';

/**
Expand All @@ -30,11 +31,13 @@ export const createDataAccess = (config, log = console) => {

const auditFuncs = auditFunctions(dynamoClient, config, log);
const siteFuncs = siteFunctions(dynamoClient, config, log);
const siteCandidateFuncs = siteCandidateFunctions(dynamoClient, config);
const organizationFuncs = organizationFunctions(dynamoClient, config, log);

return {
...auditFuncs,
...siteFuncs,
...siteCandidateFuncs,
...organizationFuncs,
};
};
Loading

0 comments on commit 5c9d708

Please sign in to comment.