-
Notifications
You must be signed in to change notification settings - Fork 14
URL Store #1050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HollywoodTonight
wants to merge
1
commit into
adobe:main
Choose a base branch
from
HollywoodTonight:urlStore_Tud
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
URL Store #1050
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
packages/spacecat-shared-data-access/src/models/audit-url/audit-url.collection.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * 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 } from '@adobe/spacecat-shared-utils'; | ||
|
|
||
| import BaseCollection from '../base/base.collection.js'; | ||
|
|
||
| /** | ||
| * AuditUrlCollection - A collection class responsible for managing AuditUrl entities. | ||
| * Extends the BaseCollection to provide specific methods for interacting with AuditUrl records. | ||
| * | ||
| * @class AuditUrlCollection | ||
| * @extends BaseCollection | ||
| */ | ||
| class AuditUrlCollection extends BaseCollection { | ||
| /** | ||
| * Finds an audit URL by site ID and URL. | ||
| * This is a convenience method for looking up a specific URL. | ||
| * | ||
| * @param {string} siteId - The site ID. | ||
| * @param {string} url - The URL to find. | ||
| * @returns {Promise<AuditUrl|null>} The found AuditUrl or null. | ||
| */ | ||
| async findBySiteIdAndUrl(siteId, url) { | ||
| if (!hasText(siteId) || !hasText(url)) { | ||
| throw new Error('Both siteId and url are required'); | ||
| } | ||
|
|
||
| const results = await this.allBySiteIdAndUrl(siteId, url); | ||
| return results.length > 0 ? results[0] : null; | ||
| } | ||
|
|
||
| /** | ||
| * Gets all audit URLs for a site that have a specific audit type enabled. | ||
| * Note: This performs filtering after retrieval since audits is an array. | ||
| * | ||
| * @param {string} siteId - The site ID. | ||
| * @param {string} auditType - The audit type to filter by. | ||
| * @param {object} [options={}] - Query options (limit, cursor). | ||
| * @returns {Promise<{items: AuditUrl[], cursor?: string}>} Paginated results. | ||
| */ | ||
| async allBySiteIdAndAuditType(siteId, auditType, options = {}) { | ||
| if (!hasText(siteId) || !hasText(auditType)) { | ||
| throw new Error('Both siteId and auditType are required'); | ||
| } | ||
|
|
||
| // Get all URLs for the site | ||
| const allUrls = await this.allBySiteId(siteId, options); | ||
|
|
||
| // Filter by audit type | ||
| const filtered = allUrls.filter((auditUrl) => auditUrl.isAuditEnabled(auditType)); | ||
|
|
||
| return filtered; | ||
| } | ||
|
|
||
| /** | ||
| * Removes all audit URLs for a specific site. | ||
| * Useful for cleanup operations. | ||
| * | ||
| * @param {string} siteId - The site ID. | ||
| * @returns {Promise<void>} | ||
| */ | ||
| async removeForSiteId(siteId) { | ||
| if (!hasText(siteId)) { | ||
| throw new Error('SiteId is required'); | ||
| } | ||
|
|
||
| const urlsToRemove = await this.allBySiteId(siteId); | ||
| const idsToRemove = urlsToRemove.map((auditUrl) => auditUrl.getId()); | ||
|
|
||
| if (idsToRemove.length > 0) { | ||
| await this.removeByIds(idsToRemove); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes audit URLs by source for a specific site. | ||
| * For example, remove all 'sitemap' sourced URLs. | ||
| * | ||
| * @param {string} siteId - The site ID. | ||
| * @param {string} source - The source to filter by. | ||
| * @returns {Promise<void>} | ||
| */ | ||
| async removeForSiteIdAndSource(siteId, source) { | ||
| if (!hasText(siteId) || !hasText(source)) { | ||
| throw new Error('Both siteId and source are required'); | ||
| } | ||
|
|
||
| const urlsToRemove = await this.allBySiteIdAndSource(siteId, source); | ||
| const idsToRemove = urlsToRemove.map((auditUrl) => auditUrl.getId()); | ||
|
|
||
| if (idsToRemove.length > 0) { | ||
| await this.removeByIds(idsToRemove); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default AuditUrlCollection; | ||
|
|
82 changes: 82 additions & 0 deletions
82
packages/spacecat-shared-data-access/src/models/audit-url/audit-url.model.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * 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 BaseModel from '../base/base.model.js'; | ||
|
|
||
| /** | ||
| * AuditUrl - A class representing an AuditUrl entity. | ||
| * Provides methods to access and manipulate AuditUrl-specific data. | ||
| * | ||
| * @class AuditUrl | ||
| * @extends BaseModel | ||
| */ | ||
| class AuditUrl extends BaseModel { | ||
| static DEFAULT_SOURCE = 'manual'; | ||
|
|
||
| /** | ||
| * Checks if this URL is enabled for a specific audit type. | ||
| * @param {string} auditType - The audit type to check. | ||
| * @returns {boolean} True if the audit is enabled for this URL. | ||
| */ | ||
| isAuditEnabled(auditType) { | ||
| const audits = (this.getAudits ? this.getAudits() : this.audits) || []; | ||
| return audits.includes(auditType); | ||
| } | ||
|
|
||
| /** | ||
| * Adds an audit type to the audits array if not already present. | ||
| * @param {string} auditType - The audit type to add. | ||
| * @returns {this} The current instance for chaining. | ||
| */ | ||
| enableAudit(auditType) { | ||
| const audits = (this.getAudits ? this.getAudits() : this.audits) || []; | ||
| if (!audits.includes(auditType)) { | ||
| // Create a new array instead of mutating the existing one | ||
| const updatedAudits = [...audits, auditType]; | ||
| if (this.setAudits) { | ||
| this.setAudits(updatedAudits); | ||
| } else { | ||
| this.audits = updatedAudits; | ||
| } | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Removes an audit type from the audits array. | ||
| * @param {string} auditType - The audit type to remove. | ||
| * @returns {this} The current instance for chaining. | ||
| */ | ||
| disableAudit(auditType) { | ||
| const audits = (this.getAudits ? this.getAudits() : this.audits) || []; | ||
| // filter() already creates a new array | ||
| const filtered = audits.filter((a) => a !== auditType); | ||
| if (this.setAudits) { | ||
| this.setAudits(filtered); | ||
| } else { | ||
| this.audits = filtered; | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if this URL was manually created by a user. | ||
| * @returns {boolean} True if the source is manual. | ||
| */ | ||
| isManualSource() { | ||
| const source = this.getSource ? this.getSource() : this.source; | ||
| return source === AuditUrl.DEFAULT_SOURCE; | ||
| } | ||
| } | ||
|
|
||
| export default AuditUrl; | ||
|
|
97 changes: 97 additions & 0 deletions
97
packages/spacecat-shared-data-access/src/models/audit-url/audit-url.schema.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /* c8 ignore start */ | ||
|
|
||
| import { | ||
| isIsoDate, | ||
| isValidUrl, | ||
| isValidUUID, | ||
| } from '@adobe/spacecat-shared-utils'; | ||
|
|
||
| import SchemaBuilder from '../base/schema.builder.js'; | ||
| import AuditUrl from './audit-url.model.js'; | ||
| import AuditUrlCollection from './audit-url.collection.js'; | ||
|
|
||
| /* | ||
| Schema Doc: https://electrodb.dev/en/modeling/schema/ | ||
| Attribute Doc: https://electrodb.dev/en/modeling/attributes/ | ||
| Indexes Doc: https://electrodb.dev/en/modeling/indexes/ | ||
|
|
||
| Data Access Patterns: | ||
| 1. Get all URLs for a site: allBySiteId(siteId) | ||
| 2. Get all URLs for a site by source: allBySiteIdAndSource(siteId, source) | ||
| 3. Get a specific URL: allBySiteIdAndUrl(siteId, url) | ||
| 4. Get URLs by audit type: allBySiteIdAndAuditType(siteId, auditType) - filtered in code | ||
|
|
||
| Indexes: | ||
| - Primary: siteId (PK) + url (SK) - for unique identification | ||
| - bySiteIdAndSource: siteId + source (GSI) - for querying by source | ||
| */ | ||
|
|
||
| const schema = new SchemaBuilder(AuditUrl, AuditUrlCollection) | ||
| .addReference('belongs_to', 'Site', ['url']) | ||
| .addAttribute('url', { | ||
| type: 'string', | ||
| required: true, | ||
| validate: (value) => isValidUrl(value), | ||
| }) | ||
| .addAttribute('source', { | ||
| type: 'string', | ||
| required: true, | ||
| default: AuditUrl.DEFAULT_SOURCE, | ||
| }) | ||
| .addAttribute('audits', { | ||
| type: 'list', | ||
| items: { | ||
| type: 'string', | ||
| }, | ||
| required: true, | ||
| default: [], | ||
| }) | ||
| .addAttribute('createdAt', { | ||
| type: 'string', | ||
| required: true, | ||
| readOnly: true, | ||
| default: () => new Date().toISOString(), | ||
| validate: (value) => isIsoDate(value), | ||
| }) | ||
| .addAttribute('updatedAt', { | ||
| type: 'string', | ||
| required: true, | ||
| readOnly: true, | ||
| watch: '*', | ||
| default: () => new Date().toISOString(), | ||
| set: () => new Date().toISOString(), | ||
| validate: (value) => isIsoDate(value), | ||
| }) | ||
| .addAttribute('createdBy', { | ||
| type: 'string', | ||
| required: true, | ||
| readOnly: true, | ||
| default: 'system', | ||
| }) | ||
| .addAttribute('updatedBy', { | ||
| type: 'string', | ||
| required: true, | ||
| watch: '*', | ||
| default: 'system', | ||
| set: (value) => value, | ||
| }) | ||
| // Add a second GSI for querying by siteId and source | ||
| .addIndex( | ||
| { composite: ['siteId'] }, | ||
| { composite: ['source'] }, | ||
| ); | ||
|
|
||
| export default schema.build(); | ||
|
|
||
47 changes: 47 additions & 0 deletions
47
packages/spacecat-shared-data-access/src/models/audit-url/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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 type { BaseCollection, BaseModel, Site } from '../index'; | ||
|
|
||
| export interface AuditUrl extends BaseModel { | ||
| getAudits(): string[]; | ||
| getCreatedAt(): string; | ||
| getCreatedBy(): string; | ||
| getSite(): Promise<Site>; | ||
| getSiteId(): string; | ||
| getSource(): string; | ||
| getUrl(): string; | ||
| setAudits(audits: string[]): AuditUrl; | ||
| setCreatedBy(createdBy: string): AuditUrl; | ||
| setSiteId(siteId: string): AuditUrl; | ||
| setSource(source: string): AuditUrl; | ||
| setUrl(url: string): AuditUrl; | ||
| isAuditEnabled(auditType: string): boolean; | ||
| enableAudit(auditType: string): AuditUrl; | ||
| disableAudit(auditType: string): AuditUrl; | ||
| isManualSource(): boolean; | ||
| } | ||
|
|
||
| export interface AuditUrlCollection extends BaseCollection<AuditUrl> { | ||
| allBySiteId(siteId: string): Promise<AuditUrl[]>; | ||
| allBySiteIdAndSource(siteId: string, source: string): Promise<AuditUrl[]>; | ||
| allBySiteIdAndSourceAndUrl(siteId: string, source: string, url: string): Promise<AuditUrl[]>; | ||
| allBySiteIdAndUrl(siteId: string, url: string): Promise<AuditUrl[]>; | ||
| findBySiteId(siteId: string): Promise<AuditUrl | null>; | ||
| findBySiteIdAndSource(siteId: string, source: string): Promise<AuditUrl | null>; | ||
| findBySiteIdAndSourceAndUrl(siteId: string, source: string, url: string): Promise<AuditUrl | null>; | ||
| findBySiteIdAndUrl(siteId: string, url: string): Promise<AuditUrl | null>; | ||
| allBySiteIdAndAuditType(siteId: string, auditType: string, options?: object): Promise<AuditUrl[]>; | ||
| removeForSiteId(siteId: string): Promise<void>; | ||
| removeForSiteIdAndSource(siteId: string, source: string): Promise<void>; | ||
| } | ||
|
|
20 changes: 20 additions & 0 deletions
20
packages/spacecat-shared-data-access/src/models/audit-url/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * 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 AuditUrl from './audit-url.model.js'; | ||
| import AuditUrlCollection from './audit-url.collection.js'; | ||
|
|
||
| export { | ||
| AuditUrl, | ||
| AuditUrlCollection, | ||
| }; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note