Skip to content
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

feat: migrate to v2 data access #552

Merged
merged 24 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1a32361
fix: migrate to v2 data access
solaris007 Dec 28, 2024
d32e56f
Merge branch 'main' into migrate-to-v2-data-access
solaris007 Dec 29, 2024
4445079
fix: update deps
solaris007 Dec 30, 2024
19321ed
Merge branch 'main' into migrate-to-v2-data-access
solaris007 Dec 30, 2024
97547f5
Merge branch 'main' into migrate-to-v2-data-access
solaris007 Dec 31, 2024
10f191b
chore: set engine & npm versions
solaris007 Dec 31, 2024
6cd3737
fix: getUrl
solaris007 Dec 31, 2024
c7a877f
chore: update deps
solaris007 Dec 31, 2024
f3f1b9a
chore: update deps
solaris007 Dec 31, 2024
8caadc0
Merge branch 'main' into migrate-to-v2-data-access
solaris007 Dec 31, 2024
4a28e17
Merge branch 'main' into migrate-to-v2-data-access
solaris007 Dec 31, 2024
06251af
chore: update deps
solaris007 Dec 31, 2024
7468495
chore: update deps
solaris007 Dec 31, 2024
9e369ce
Merge branch 'main' into migrate-to-v2-data-access
solaris007 Dec 31, 2024
fb5659f
Merge branch 'main' into migrate-to-v2-data-access
solaris007 Dec 31, 2024
3c16bdd
chore: update deps
solaris007 Dec 31, 2024
4936ce6
Merge branch 'main' into migrate-to-v2-data-access
solaris007 Dec 31, 2024
6369356
fix: test debug experimentation and coversion audit data
dzehnder Jan 3, 2025
f1dfc1a
fix: remove debugging logs
dzehnder Jan 3, 2025
8ff8641
chore: update data-access
dzehnder Jan 3, 2025
7eb26a5
Merge branch 'refs/heads/main' into migrate-to-v2-data-access
dzehnder Jan 3, 2025
b61cbe0
chore: update data-access
dzehnder Jan 3, 2025
54e0965
Merge branch 'refs/heads/main' into migrate-to-v2-data-access
dzehnder Jan 3, 2025
32e6aed
fix: package-lock
dzehnder Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/backlinks/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export async function filterOutValidBacklinks(backlinks, log) {
export default async function auditBrokenBacklinks(message, context) {
const { type, auditContext = {} } = message;
const { dataAccess, log, sqs } = context;
const {
Audit, Configuration, Opportunity, SiteTopPage,
} = dataAccess;
const {
AUDIT_RESULTS_QUEUE_URL: queueUrl,
} = context.env;
Expand All @@ -73,11 +76,11 @@ export default async function auditBrokenBacklinks(message, context) {
if (!site) {
return notFound('Site not found');
}
if (!site.isLive()) {
if (!site.getIsLive()) {
log.info(`Site ${siteId} is not live`);
return ok();
}
const configuration = await dataAccess.getConfiguration();
const configuration = await Configuration.findLatest();
if (!configuration.isHandlerEnabledForSite(type, site)) {
log.info(`Audit type ${type} disabled for site ${siteId}`);
return ok();
Expand All @@ -104,10 +107,10 @@ export default async function auditBrokenBacklinks(message, context) {

if (configuration.isHandlerEnabledForSite(`${type}-auto-suggest`, site)) {
try {
const topPages = await dataAccess.getTopPagesForSite(siteId, 'ahrefs', 'global');
const topPages = await SiteTopPage.allBySiteIdAndSourceAndGeo(siteId, 'ahrefs', 'global');
const keywords = topPages.map(
(page) => ({
url: page.getURL(),
url: page.getUrl(),
keyword: page.getTopKeyword(),
traffic: page.getTraffic(),
}),
Expand All @@ -132,14 +135,14 @@ export default async function auditBrokenBacklinks(message, context) {
}
const auditData = {
siteId: site.getId(),
isLive: site.isLive(),
isLive: site.getIsLive(),
auditedAt: new Date().toISOString(),
auditType: type,
fullAuditRef: auditResult?.fullAuditRef,
auditResult,
};

const audit = await dataAccess.addAudit(auditData);
const audit = await Audit.create(auditData);
const result = {
type,
url: site.getBaseURL(),
Expand All @@ -150,7 +153,7 @@ export default async function auditBrokenBacklinks(message, context) {
let brokenBacklinksOppty;

try {
const opportunities = await dataAccess.Opportunity.allBySiteIdAndStatus(siteId, 'NEW');
const opportunities = await Opportunity.allBySiteIdAndStatus(siteId, 'NEW');
brokenBacklinksOppty = opportunities.find((oppty) => oppty.getType() === 'broken-backlinks');
} catch (e) {
log.error(`Fetching opportunities for siteId ${siteId} failed with error: ${e.message}`);
Expand Down Expand Up @@ -179,7 +182,7 @@ export default async function auditBrokenBacklinks(message, context) {
tags: ['Traffic acquisition'],
};

brokenBacklinksOppty = await dataAccess.Opportunity.create(opportunityData);
brokenBacklinksOppty = await Opportunity.create(opportunityData);
} else {
brokenBacklinksOppty.setAuditId(audit.getId());
await brokenBacklinksOppty.save();
Expand Down
3 changes: 2 additions & 1 deletion src/canonical/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export const CANONICAL_CHECKS = Object.freeze({
*/
export async function getTopPagesForSiteId(dataAccess, siteId, context, log) {
try {
const result = await dataAccess.getTopPagesForSite(siteId, 'ahrefs', 'global');
const { SiteTopPage } = dataAccess;
const result = await SiteTopPage.allBySiteIdAndSourceAndGeo(siteId, 'ahrefs', 'global');
log.info('Received top pages response:', JSON.stringify(result, null, 2));

const topPages = result || [];
Expand Down
16 changes: 9 additions & 7 deletions src/common/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
import { composeAuditURL, hasText } from '@adobe/spacecat-shared-utils';
import { ok } from '@adobe/spacecat-shared-http-utils';
import URI from 'urijs';
import { createAudit } from '@adobe/spacecat-shared-data-access/src/models/audit.js';
import { retrieveSiteBySiteId } from '../utils/data-access.js';

// eslint-disable-next-line no-empty-function
export async function defaultMessageSender() {}

export async function defaultPersister(auditData, context) {
const { dataAccess } = context;
const audit = await dataAccess.addAudit(auditData);
return audit;
const { Audit } = dataAccess;

return Audit.create(auditData);
}

export async function noopPersister(auditData) {
return createAudit(auditData);
return { getId: () => auditData.id || 'noop' };
}

export async function defaultSiteProvider(siteId, context) {
Expand All @@ -42,8 +42,9 @@ export async function defaultSiteProvider(siteId, context) {

export async function defaultOrgProvider(orgId, context) {
const { dataAccess } = context;
const { Organization } = dataAccess;

const org = await dataAccess.getOrganizationByID(orgId);
const org = await Organization.findById(orgId);
if (!org) {
throw new Error(`Org with id ${orgId} not found`);
}
Expand Down Expand Up @@ -88,6 +89,7 @@ export class Audit {

async run(message, context) {
const { log, dataAccess } = context;
const { Configuration } = dataAccess;
const {
type,
auditContext = {},
Expand All @@ -96,7 +98,7 @@ export class Audit {

try {
const site = await this.siteProvider(siteId, context);
const configuration = await dataAccess.getConfiguration();
const configuration = await Configuration.findLatest();
if (!configuration.isHandlerEnabledForSite(type, site)) {
log.warn(`${type} audits disabled for site ${siteId}, skipping...`);
return ok();
Expand All @@ -110,7 +112,7 @@ export class Audit {
} = await this.runner(finalUrl, context, site);
const auditData = {
siteId: site.getId(),
isLive: site.isLive(),
isLive: site.getIsLive(),
auditedAt: new Date().toISOString(),
auditType: type,
auditResult,
Expand Down
5 changes: 3 additions & 2 deletions src/cwv/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ export async function convertToOppty(auditUrl, auditData, context, site) {
dataAccess,
log,
} = context;
const { Opportunity } = dataAccess;
const groupedURLs = site.getConfig().getGroupedURLs(AUDIT_TYPE);

log.info(`auditUrl: ${auditUrl}`);
log.info(`auditData: ${JSON.stringify(auditData)}`);

const opportunities = await dataAccess.Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
const opportunities = await Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
log.info(`opportunities: ${JSON.stringify(opportunities)}`);

let opportunity = opportunities.find((oppty) => oppty.getType() === AUDIT_TYPE);
Expand Down Expand Up @@ -89,7 +90,7 @@ export async function convertToOppty(auditUrl, auditData, context, site) {
},
};
try {
opportunity = await dataAccess.Opportunity.create(opportunityData);
opportunity = await Opportunity.create(opportunityData);
} catch (e) {
log.error(`Failed to create new opportunity for siteId ${auditData.siteId} and auditId ${auditData.id}: ${e.message}`);
throw e;
Expand Down
8 changes: 5 additions & 3 deletions src/experimentation-ess/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ async function persistOnlyMetadata(auditData, context) {
// persists only the audit metadata, as the
// whole audit result will be bigger than the allowed size in dynamo
const { dataAccess } = context;
await dataAccess.addAudit({
const { Audit } = dataAccess;
await Audit.create({
...auditData,
auditResult: [], // deliberately overrides the result
});
Expand All @@ -32,12 +33,13 @@ async function persistOnlyMetadata(auditData, context) {
export async function essExperimentationAllAuditRunner(auditUrl, context, site) {
log = context.log;
const { dataAccess } = context;
const { Experiment, LatestAudit } = dataAccess;
const siteId = site.getId();
log.info(`Received ESS Experimentation All audit request for ${auditUrl}`);
const startTime = process.hrtime();

const latestAudit = await dataAccess.getLatestAuditForSite(siteId, 'experimentation-ess-all');
const experiments = await dataAccess.getExperiments(siteId);
const latestAudit = await LatestAudit.findBySiteIdAndAuditType(siteId, 'experimentation-ess-all');
const experiments = await Experiment.allBySiteId(siteId);
const activeExperiments = experiments.filter((experiment) => (
experiment.getStatus() && experiment.getStatus().toLowerCase() === 'active' && experiment.getStartDate() !== null));
let days;
Expand Down
10 changes: 7 additions & 3 deletions src/experimentation-ess/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,13 @@ export async function processAudit(auditURL, context, site, days) {

export async function postProcessor(auditUrl, auditData, context) {
const { dataAccess } = context;
const { Experiment } = dataAccess;
log = context.log;
// iterate array auditData.auditResult
for (const experiment of auditData.auditResult) {
const experimentData = {
siteId: auditData.siteId,
experimentId: experiment.id,
expId: experiment.id,
name: experiment.label,
url: experiment.url,
startDate: experiment.startDate,
Expand All @@ -683,7 +684,10 @@ export async function postProcessor(auditUrl, auditData, context) {
conversionEventValue: experiment.conversionEventValue,
};
// eslint-disable-next-line no-await-in-loop
const existingExperiments = await dataAccess.getExperiments(auditData.siteId, experiment.id);
const existingExperiments = await Experiment.allBySiteIdAndExpId(
auditData.siteId,
experiment.id,
);
let existingExperiment;
if (existingExperiments) {
if (existingExperiments.length === 1) {
Expand Down Expand Up @@ -720,7 +724,7 @@ export async function postProcessor(auditUrl, auditData, context) {
}
}
// eslint-disable-next-line no-await-in-loop
await dataAccess.upsertExperiment(experimentData);
await Experiment.create(experimentData);
}
log.info(`Experiments data for site ${auditData.siteId} has been upserted`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,10 @@ function convertToOpportunityEntity(oppty, auditData) {
export async function postProcessor(auditUrl, auditData, context) {
const { log } = context;
const { dataAccess } = context;
const { Opportunity } = dataAccess;
let updatedEntities = 0;
log.info(`Experimentation Opportunities post processing started for ${auditUrl} from audit ${auditData.id}`);
const existingOpportunities = await dataAccess.Opportunity.allBySiteId(auditData.siteId);
const existingOpportunities = await Opportunity.allBySiteId(auditData.siteId);

// Get opportunities with recommendations
const opportunities = auditData.auditResult.experimentationOpportunities
Expand Down
5 changes: 3 additions & 2 deletions src/internal-links/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ export async function convertToOpportunity(auditUrl, auditData, context) {
dataAccess,
log,
} = context;
const { Opportunity } = dataAccess;

let opportunity;
try {
const opportunities = await dataAccess.Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
const opportunities = await Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
opportunity = opportunities.find((oppty) => oppty.getType() === AUDIT_TYPE);
} catch (e) {
log.error(`Fetching opportunities for siteId ${auditData.siteId} failed with error: ${e.message}`);
Expand Down Expand Up @@ -133,7 +134,7 @@ export async function convertToOpportunity(auditUrl, auditData, context) {
'Engagement',
],
};
opportunity = await dataAccess.Opportunity.create(opportunityData);
opportunity = await Opportunity.create(opportunityData);
} else {
opportunity.setAuditId(auditData.id);
await opportunity.save();
Expand Down
9 changes: 5 additions & 4 deletions src/metatags/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,19 @@ export default async function auditMetaTags(message, context) {
const {
dataAccess, log, s3Client,
} = context;
const { Audit, Configuration } = dataAccess;

try {
log.info(`Received ${type} audit request for siteId: ${siteId}`);
const site = await retrieveSiteBySiteId(dataAccess, siteId, log);
if (!site) {
return notFound('Site not found');
}
if (!site.isLive()) {
if (!site.getIsLive()) {
log.info(`Site ${siteId} is not live`);
return ok();
}
const configuration = await dataAccess.getConfiguration();
const configuration = await Configuration.findLatest();
if (!configuration.isHandlerEnabledForSite(type, site)) {
log.info(`Audit type ${type} disabled for site ${siteId}`);
return ok();
Expand Down Expand Up @@ -98,14 +99,14 @@ export default async function auditMetaTags(message, context) {
};
const auditData = {
siteId: site.getId(),
isLive: site.isLive(),
isLive: site.getIsLive(),
auditedAt: new Date().toISOString(),
auditType: type,
fullAuditRef: auditResult?.fullAuditRef,
auditResult,
};
// Persist Audit result
const audit = await dataAccess.addAudit(auditData);
const audit = await Audit.create(auditData);
log.info(`Successfully audited ${siteId} for ${type} type audit`);
await syncOpportunityAndSuggestions(
siteId,
Expand Down
5 changes: 3 additions & 2 deletions src/metatags/opportunityHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ export default async function syncOpportunityAndSuggestions(
dataAccess,
log,
) {
const { Opportunity } = dataAccess;
log.info(`Syncing opportunity and suggestions for ${siteId}`);
let metatagsOppty;
try {
// Get all opportunities by site-id and new status
const opportunities = await dataAccess.Opportunity.allBySiteIdAndStatus(siteId, 'NEW');
const opportunities = await Opportunity.allBySiteIdAndStatus(siteId, 'NEW');
// Find existing opportunity for meta-tags
metatagsOppty = opportunities.find((oppty) => oppty.getType() === 'meta-tags');
} catch (e) {
Expand Down Expand Up @@ -188,7 +189,7 @@ export default async function syncOpportunityAndSuggestions(
},
tags: ['Traffic acquisition'],
};
metatagsOppty = await dataAccess.Opportunity.create(opportunityData);
metatagsOppty = await Opportunity.create(opportunityData);
log.debug('Meta-tags Opportunity created');
} else {
metatagsOppty.setAuditId(auditId);
Expand Down
5 changes: 3 additions & 2 deletions src/site-detection/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export async function siteDetectionRunner(_, context) {
env,
log,
} = context;
const { Site, SiteCandidate } = dataAccess;
const {
CORALOGIX_API_KEY: authorization,
SITE_DETECTION_WEBHOOK: siteDetectionWebHook,
Expand All @@ -173,8 +174,8 @@ export async function siteDetectionRunner(_, context) {
ignoredSubdomains,
};

const sites = await dataAccess.getSites();
const siteCandidates = await dataAccess.getSiteCandidates();
const sites = await Site.all();
const siteCandidates = await SiteCandidate.all();

const knownHosts = new Set([...sites, ...siteCandidates]
.map((s) => s.getBaseURL())
Expand Down
5 changes: 3 additions & 2 deletions src/sitemap/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ export function classifySuggestions(auditUrl, auditData, log) {

export async function convertToOpportunity(auditUrl, auditData, context) {
const { dataAccess, log } = context;
const { Opportunity } = dataAccess;

log.info('Converting SITEMAP audit to opportunity...');

Expand All @@ -481,7 +482,7 @@ export async function convertToOpportunity(auditUrl, auditData, context) {
return;
}

const opportunities = await dataAccess.Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
const opportunities = await Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
log.info(`opportunities: ${JSON.stringify(opportunities)}`);

let opportunity = opportunities.find((oppty) => oppty.getType() === AUDIT_TYPE);
Expand All @@ -504,7 +505,7 @@ export async function convertToOpportunity(auditUrl, auditData, context) {
tags: ['Traffic Acquisition'],
};
try {
opportunity = await dataAccess.Opportunity.create(opportunityData);
opportunity = await Opportunity.create(opportunityData);
} catch (e) {
log.error(`Failed to create new opportunity for siteId ${auditData.siteId} and auditId ${auditData.id}: ${e.message}`, e);
throw e;
Expand Down
5 changes: 3 additions & 2 deletions src/structured-data/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ export async function processStructuredData(baseURL, context, pages) {

export async function convertToOpportunity(auditUrl, auditData, context) {
const { dataAccess, log } = context;
const { Opportunity } = dataAccess;

const opportunities = await dataAccess.Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
const opportunities = await Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
let opportunity = opportunities.find((oppty) => oppty.getType() === 'structured-data');

if (!opportunity) {
Expand All @@ -124,7 +125,7 @@ export async function convertToOpportunity(auditUrl, auditData, context) {
tags: ['Traffic acquisition'],
};
try {
opportunity = await dataAccess.Opportunity.create(opportunityData);
opportunity = await Opportunity.create(opportunityData);
} catch (e) {
log.error(`Failed to create new opportunity for siteId ${auditData.siteId} and auditId ${auditData.id}: ${e.message}`);
throw e;
Expand Down
Loading
Loading