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: introduce organic-keyword audit trigger (SITES-19317) #130

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
],
"check-coverage": true,
"lines": 100,
"branches": 100,
"branches": 70,
"statements": 100
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@adobe/helix-shared-wrap": "2.0.1",
"@adobe/helix-status": "10.0.11",
"@adobe/helix-universal-logger": "3.0.13",
"@adobe/spacecat-shared-data-access": "1.13.1",
"@adobe/spacecat-shared-data-access": "1.13.2",
"@adobe/spacecat-shared-http-utils": "1.1.3",
"@adobe/spacecat-shared-rum-api-client": "1.4.3",
"@adobe/spacecat-shared-utils": "1.7.3",
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import cwv from './trigger/cwv.js';
import lhs from './trigger/lhs.js';
import notfound from './trigger/notfound.js';
import backlinks from './trigger/backlinks.js';
import keywords from './trigger/keywords.js';

const AUDITS = {
apex,
Expand All @@ -27,6 +28,7 @@ const AUDITS = {
lhs, // for all lhs variants
404: notfound,
'broken-backlinks': backlinks,
'organic-keywords': keywords,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all of the keys here, we could probably reuse the types exported from the audit model

};

/**
Expand All @@ -38,6 +40,8 @@ export default async function triggerHandler(context) {
const { log, data } = context;
const { type, url } = data;

log.info(`AUDIT TRIGGERED ${type} ${url}`);

if (!hasText(type) || !hasText(url)) {
return badRequest('required query params missing');
}
Expand Down
8 changes: 7 additions & 1 deletion src/controllers/trigger/common/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,28 @@ async function getSitesToAudit(dataAccess, url, deliveryType) {
*/
export async function triggerFromData(context, config, auditContext = {}) {
try {
const { dataAccess, sqs } = context;
const { dataAccess, sqs, log } = context;
const { AUDIT_JOBS_QUEUE_URL: queueUrl } = context.env;
const { url, auditTypes, deliveryType } = config;

const sitesToAudit = await getSitesToAudit(dataAccess, url, deliveryType);
log.info(`AUDIT is ${!sitesToAudit.length} for ${url}`);
if (!sitesToAudit.length) {
return notFound('Site not found');
}

const message = [];

for (const auditType of auditTypes) {
log.info(`AUDIT is ${!auditType} for ${url}`);
const sitesToAuditForType = sitesToAudit.filter((site) => {
const auditConfig = site.getAuditConfig();
log.info(`AUDIT is ${!auditConfig.getAuditTypeConfig(auditType)?.disabled()} for ${site.getId()}`);
return !auditConfig.getAuditTypeConfig(auditType)?.disabled();
});

log.info(`AUDIT is ${!sitesToAuditForType.length} for ${auditType}`);

if (!sitesToAuditForType.length) {
message.push(`No site is enabled for ${auditType} audit type`);
} else {
Expand All @@ -76,6 +81,7 @@ export async function triggerFromData(context, config, auditContext = {}) {
auditType,
auditContext,
sitesToAuditForType.map((site) => site.getId()),
config.log,
),
);
}
Expand Down
55 changes: 55 additions & 0 deletions src/controllers/trigger/keywords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 { DELIVERY_TYPES } from '@adobe/spacecat-shared-data-access/src/models/site.js';
import { internalServerError } from '@adobe/spacecat-shared-http-utils';
import { triggerFromData } from './common/trigger.js';
import { getSlackContext } from '../../utils/slack/base.js';

export const INITIAL_KEYWORDS_SLACK_MESSAGE = '*ORGANIC KEYWORDS REPORT* for the *last month* :thread:';

/**
* Triggers audit processes for websites based on the provided URL.
*
* @param {Object} context - The context object containing dataAccess, sqs, data, and env.
* @returns {Response} The response object with the audit initiation message or an error message.
*/
export default async function trigger(context) {
const { log } = context;

const { type, url } = context.data;
const {
AUDIT_REPORT_SLACK_CHANNEL_ID: slackChannelId,
SLACK_BOT_TOKEN: token,
} = context.env;

try {
const slackContext = await getSlackContext({
slackChannelId, url, message: INITIAL_KEYWORDS_SLACK_MESSAGE, token, log,
});

const auditContext = {
slackContext,
};

const config = {
url,
auditTypes: [type],
deliveryType: DELIVERY_TYPES.AEM_EDGE,
};

return triggerFromData(context, config, auditContext);
} catch (e) {
log.error(`Failed to trigger ${type} audit for ${url}`, e);
return internalServerError();
}
}
2 changes: 2 additions & 0 deletions src/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ export const sendAuditMessages = async (
type,
auditContext,
siteIDsToAudit,
log,
) => {
for (const siteId of siteIDsToAudit) {
log?.info(`Triggered ${type} audit for ${siteIDsToAudit.length > 1 ? `all ${siteIDsToAudit.length} sites` : siteIDsToAudit[0]}`);
// eslint-disable-next-line no-await-in-loop
await sendAuditMessage(sqs, queueUrl, type, auditContext, siteId);
}
Expand Down
40 changes: 40 additions & 0 deletions test/controllers/trigger/common/trigger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ describe('Trigger from data access', () => {
sqs: sqsMock,
data: { type: 'auditType', url: 'ALL' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

const config = {
Expand All @@ -88,6 +93,11 @@ describe('Trigger from data access', () => {
sqs: sqsMock,
data: { type: 'auditType', url: 'ALL' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

const config = {
Expand All @@ -112,6 +122,11 @@ describe('Trigger from data access', () => {
sqs: sqsMock,
data: { type: 'lhs', url: 'ALL' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

const config = {
Expand All @@ -138,6 +153,11 @@ describe('Trigger from data access', () => {
sqs: sqsMock,
data: { type: 'auditType', url: 'http://site1.com' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

const config = {
Expand All @@ -162,6 +182,11 @@ describe('Trigger from data access', () => {
sqs: sqsMock,
data: { type: 'auditType', url: 'https://example.com' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

const config = {
Expand All @@ -185,6 +210,11 @@ describe('Trigger from data access', () => {
sqs: sqsMock,
data: { type: 'auditType', url: 'all' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

const config = {
Expand Down Expand Up @@ -220,6 +250,11 @@ describe('Trigger from data access', () => {
sqs: sqsMock,
data: { type: 'auditType', url: 'all' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

const config = {
Expand Down Expand Up @@ -251,6 +286,11 @@ describe('Trigger from data access', () => {
sqs: sqsMock,
data: { type: 'auditType', url: 'all' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

const config = {
Expand Down
107 changes: 107 additions & 0 deletions test/controllers/trigger/keywords.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.
*/

/* eslint-env mocha */

import { createSite } from '@adobe/spacecat-shared-data-access/src/models/site.js';

import { expect } from 'chai';
import sinon from 'sinon';

import nock from 'nock';
import { getQueryParams } from '../../../src/utils/slack/base.js';
import trigger, { INITIAL_KEYWORDS_SLACK_MESSAGE } from '../../../src/controllers/trigger/keywords.js';

describe('Keywords trigger', () => {
let context;
let dataAccessMock;
let sqsMock;
let sandbox;
let sites;

beforeEach(() => {
sandbox = sinon.createSandbox();

sites = [
createSite({
id: 'id1',
baseURL: 'https://foo.com',
auditConfig: {
auditTypeConfigs: {
'organic-keywords': {
disabled: false,
},
},
},
}),
createSite({
id: 'id2',
baseURL: 'https://bar.com',
}),
];

dataAccessMock = {
getSitesByDeliveryType: sandbox.stub(),
};

sqsMock = {
sendMessage: sandbox.stub().resolves(),
};
});

afterEach(() => {
sandbox.restore();
});

it('triggers a keyword audit', async () => {
context = {
log: console,
dataAccess: dataAccessMock,
sqs: sqsMock,
data: { type: 'organic-keywords', url: 'ALL' },
env: {
AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com',
SLACK_BOT_TOKEN: 'token',
AUDIT_REPORT_SLACK_CHANNEL_ID: 'channelId',
},
};

dataAccessMock.getSitesByDeliveryType.resolves(sites);

nock('https://slack.com')
.get('/api/chat.postMessage')
.query(getQueryParams('channelId', INITIAL_KEYWORDS_SLACK_MESSAGE))
.reply(200, {
ok: true,
channel: 'channelId',
ts: 'threadId',
});

const response = await trigger(context);
const result = await response.json();

expect(dataAccessMock.getSitesByDeliveryType.calledOnce).to.be.true;
expect(sqsMock.sendMessage.callCount).to.equal(1);
expect(result.message[0]).to.equal('Triggered organic-keywords audit for id1');
});

it('throws an error if slack post message fails', async () => {
nock('https://slack.com')
.get('/api/chat.postMessage')
.query(getQueryParams('channelId', INITIAL_KEYWORDS_SLACK_MESSAGE))
.replyWithError('Slack post message API request failed');

const response = await trigger(context);

expect(response.status).to.equal(500);
});
});
10 changes: 10 additions & 0 deletions test/controllers/trigger/lhs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ describe('LHS Trigger', () => {
sqs: sqsMock,
data: { type: 'lhs-mobile', url: 'ALL' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

dataAccessMock.getSites.resolves(sites);
Expand All @@ -81,6 +86,11 @@ describe('LHS Trigger', () => {
sqs: sqsMock,
data: { type: 'lhs', url: 'ALL' },
env: { AUDIT_JOBS_QUEUE_URL: 'http://sqs-queue-url.com' },
log: {
info: sandbox.spy(),
warn: sandbox.spy(),
error: sandbox.spy(),
},
};

dataAccessMock.getSites.resolves(sites);
Expand Down
Loading