Skip to content

Commit 82b5121

Browse files
committed
move warming to after import
1 parent 2b755e4 commit 82b5121

File tree

2 files changed

+45
-64
lines changed

2 files changed

+45
-64
lines changed

src/paid-traffic-analysis/handler.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function buildMystiqueMessage(site, auditId, baseUrl, auditResult) {
4343
* @returns {Object} Audit result and reference
4444
*/
4545
export async function prepareTrafficAnalysisRequest(auditUrl, context, site, period) {
46-
const { log, env } = context;
46+
const { log } = context;
4747
const siteId = site.getSiteId();
4848

4949
log.info(`[traffic-analysis-audit-${period}] Preparing mystique traffic-analysis-audit request parameters for [siteId: ${siteId}] and baseUrl: ${auditUrl}`);
@@ -72,36 +72,35 @@ export async function prepareTrafficAnalysisRequest(auditUrl, context, site, per
7272
temporalCondition,
7373
};
7474
}
75-
76-
// Warm cache for this site and period
77-
78-
const temporalParams = {
79-
yearInt: auditResult.year,
80-
weekInt: auditResult.week || 0,
81-
monthInt: auditResult.month,
82-
};
83-
84-
log.info(`[cache-warming-${period}] Starting cache warming for site: ${siteId}`);
85-
await warmCacheForSite(context, log, env, site, temporalParams);
86-
log.info(`[cache-warming-${period}] Completed cache warming for site: ${siteId}`);
8775
log.info(`[traffic-analysis-audit-${period}] Request parameters: ${JSON.stringify(auditResult)} set for [siteId: ${siteId}] and baseUrl: ${auditUrl}`);
88-
8976
return {
9077
auditResult,
9178
fullAuditRef: auditUrl,
79+
period,
9280
};
9381
}
9482

9583
export async function sendRequestToMystique(auditUrl, auditData, context, site) {
96-
const { id, auditResult } = auditData;
84+
const { id, auditResult, period } = auditData;
9785
const {
9886
log, sqs, env, siteId,
9987
} = context;
88+
89+
const temporalParams = {
90+
yearInt: auditResult.year,
91+
weekInt: auditResult.week || 0,
92+
monthInt: auditResult.month,
93+
};
94+
95+
log.info(`[traffic-analysis-audit] cache-warming-${period} Starting cache warming for site: ${siteId}`);
96+
await warmCacheForSite(context, log, env, site, temporalParams);
97+
log.info(`[traffic-analysis-audit] cache-warming-${period} Completed cache warming for site: ${siteId}`);
98+
10099
const mystiqueMessage = buildMystiqueMessage(site, id, auditUrl, auditResult);
101100

102101
log.info(`[traffic-analysis-audit] [siteId: ${siteId}] and [baseUrl:${auditUrl}] with message ${JSON.stringify(mystiqueMessage, 2)} evaluation to mystique`);
103102
await sqs.sendMessage(env.QUEUE_SPACECAT_TO_MYSTIQUE, mystiqueMessage);
104-
log.info(`[traffic-analysis-audit] [siteId: ${auditUrl}] [baseUrl:${auditUrl}] Completed mystique evaluation step`);
103+
log.info(`[traffic-analysis-audit] [siteId: ${siteId}] [baseUrl:${siteId}] Completed mystique evaluation step`);
105104
}
106105

107106
async function importDataStep(context, period) {
@@ -110,7 +109,6 @@ async function importDataStep(context, period) {
110109
const allowOverwrite = false;
111110
log.info(`[traffic-analysis-import-${period}] Starting import data step for siteId: ${siteId}, url: ${finalUrl}`);
112111

113-
// First prepare and save the traffic analysis request like we did before
114112
const analysisResult = await prepareTrafficAnalysisRequest(
115113
finalUrl,
116114
context,

test/audits/paid-traffic-analysis/handler.test.js

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
weeklyImportDataStep,
2323
weeklyProcessAnalysisStep,
2424
} from '../../../src/paid-traffic-analysis/handler.js';
25-
25+
import { AWSAthenaClient } from '@adobe/spacecat-shared-athena-client';
2626
use(sinonChai);
2727
use(chaiAsPromised);
2828

@@ -40,12 +40,20 @@ describe('Paid Traffic Analysis Handler', () => {
4040
beforeEach(async () => {
4141
sandbox = sinon.createSandbox();
4242

43+
// Mock AWSAthenaClient.fromContext to return our mock client
44+
const mockAthenaClientInstance = {
45+
query: sandbox.stub().resolves([
46+
{ page_views: 1000, sessions: 500, conversion_rate: 0.05 },
47+
]),
48+
};
49+
sandbox.stub(AWSAthenaClient, 'fromContext').returns(mockAthenaClientInstance);
50+
4351
site = {
4452
getSiteId: sandbox.stub().returns(siteId),
4553
getId: sandbox.stub().returns(siteId),
4654
getDeliveryType: sandbox.stub().returns('aem_edge'),
47-
getBaseURL: sandbox.stub().returns(auditUrl),
48-
getPageTypes: sandbox.stub().returns(null),
55+
getBaseURL: sandbox.stub().resolves(auditUrl),
56+
getPageTypes: sandbox.stub().resolves(null),
4957
};
5058

5159
mockSqs = {
@@ -64,12 +72,13 @@ describe('Paid Traffic Analysis Handler', () => {
6472
};
6573

6674
const mockAthenaClient = {
67-
query: sandbox.stub().resolves([]),
75+
query: sandbox.stub().resolves([
76+
{ page_views: 1000, sessions: 500, conversion_rate: 0.05 },
77+
]),
6878
};
6979

7080
const mockS3Client = {
7181
send: sandbox.stub().resolves({
72-
// Simulate cache files exist, so no warming needed
7382
ContentLength: 1024,
7483
LastModified: new Date(),
7584
}),
@@ -95,6 +104,9 @@ describe('Paid Traffic Analysis Handler', () => {
95104
S3_IMPORTER_BUCKET_NAME: 'test-bucket',
96105
PAID_DATA_THRESHOLD: 2000,
97106
MAX_CONCURRENT_REQUESTS: 5,
107+
ATHENA_TEMP: 's3://test-athena-temp/',
108+
CACHE_LOCATION: 's3://test-cache-bucket/',
109+
PAGE_VIEW_THRESHOLD: 100,
98110
},
99111
siteId,
100112
})
@@ -110,7 +122,7 @@ describe('Paid Traffic Analysis Handler', () => {
110122
});
111123

112124
describe('prepareTrafficAnalysisRequest', () => {
113-
it('should prepare weekly analysis request correctly and warm cache', async () => {
125+
it('should prepare weekly analysis request correctly', async () => {
114126
const result = await prepareTrafficAnalysisRequest(
115127
auditUrl,
116128
context,
@@ -131,12 +143,9 @@ describe('Paid Traffic Analysis Handler', () => {
131143
fullAuditRef: auditUrl,
132144
});
133145
expect(result.auditResult.temporalCondition).to.include('week=2');
134-
135-
// Verify cache warming checked S3 for existing cache files
136-
expect(context.s3Client.send).to.have.been.called;
137146
});
138147

139-
it('should prepare monthly analysis request correctly and warm cache', async () => {
148+
it('should prepare monthly analysis request correctly', async () => {
140149
const result = await prepareTrafficAnalysisRequest(
141150
auditUrl,
142151
context,
@@ -151,44 +160,17 @@ describe('Paid Traffic Analysis Handler', () => {
151160
temporalCondition: '(year=2024 AND month=12)',
152161
};
153162

154-
expect(result).to.deep.equal({
163+
expect(result).to.deep.include({
155164
auditResult: expectedAuditResult,
156165
fullAuditRef: auditUrl,
157166
});
158-
159-
// Verify cache warming checked S3 for existing cache files
160-
expect(context.s3Client.send).to.have.been.called;
161-
});
162-
163-
it('should handle cache warming partial success', async () => {
164-
const result = await prepareTrafficAnalysisRequest(
165-
auditUrl,
166-
context,
167-
site,
168-
'weekly',
169-
);
170-
171-
expect(result).to.have.property('auditResult');
172-
// Verify cache warming checked S3 for existing cache files
173-
expect(context.s3Client.send).to.have.been.called;
174-
});
175-
176-
it('should warm cache with temporal parameters', async () => {
177-
const result = await prepareTrafficAnalysisRequest(
178-
auditUrl,
179-
context,
180-
site,
181-
'weekly',
182-
);
183-
184-
expect(result).to.have.property('auditResult');
185-
// Cache warming should check S3 for existing cache files
186-
expect(context.s3Client.send).to.have.been.called;
187167
});
188168
});
189169

190170
describe('sendRequestToMystique', () => {
191-
it('should send weekly message to Mystique correctly', async () => {
171+
it('should send weekly message to Mystique correctly', async function() {
172+
this.timeout(5000); // 5 second timeout for this test
173+
192174
const auditData = {
193175
id: auditId,
194176
auditResult: {
@@ -198,6 +180,7 @@ describe('Paid Traffic Analysis Handler', () => {
198180
siteId,
199181
temporalCondition: '(year=2025 AND month=1 AND week=2)',
200182
},
183+
period: 'weekly',
201184
};
202185

203186
await sendRequestToMystique(auditUrl, auditData, context, site);
@@ -223,7 +206,9 @@ describe('Paid Traffic Analysis Handler', () => {
223206
);
224207
});
225208

226-
it('should send monthly message to Mystique correctly', async () => {
209+
it('should send monthly message to Mystique correctly', async function() {
210+
this.timeout(5000); // 5 second timeout for this test
211+
227212
const auditData = {
228213
id: auditId,
229214
auditResult: {
@@ -232,6 +217,7 @@ describe('Paid Traffic Analysis Handler', () => {
232217
siteId,
233218
temporalCondition: '(year=2024 AND month=12)',
234219
},
220+
period: 'monthly',
235221
};
236222

237223
await sendRequestToMystique(auditUrl, auditData, context, site);
@@ -264,17 +250,14 @@ describe('Paid Traffic Analysis Handler', () => {
264250
context.site = site;
265251
context.finalUrl = auditUrl;
266252

267-
// Let the function call the real prepareTrafficAnalysisRequest
268253
const result = await weeklyImportDataStep(context);
269254

270-
// Verify the payload structure that will be sent to import worker
271255
expect(result).to.have.property('auditResult');
272256
expect(result).to.have.property('fullAuditRef', auditUrl);
273257
expect(result).to.have.property('type', 'traffic-analysis');
274258
expect(result).to.have.property('siteId', siteId);
275259
expect(result).to.have.property('allowOverwrite', false);
276260

277-
// Verify the auditResult has the expected temporal structure
278261
expect(result.auditResult).to.have.property('year');
279262
expect(result.auditResult).to.have.property('week');
280263
expect(result.auditResult).to.have.property('month');
@@ -301,15 +284,14 @@ describe('Paid Traffic Analysis Handler', () => {
301284
context.site = site;
302285
context.audit = mockAudit;
303286

304-
// Let the function call the real sendRequestToMystique
305287
const result = await weeklyProcessAnalysisStep(context);
306288

307289
expect(result).to.deep.equal({
308290
status: 'complete',
309291
findings: ['Traffic analysis completed and sent to Mystique'],
310292
});
311293

312-
// Verify SQS message was sent (sendRequestToMystique calls context.sqs.sendMessage)
294+
// Verify SQS message was sent
313295
expect(mockSqs.sendMessage).to.have.been.called;
314296
});
315297
});
@@ -328,6 +310,7 @@ describe('Paid Traffic Analysis Handler', () => {
328310
siteId,
329311
temporalCondition: '(year=2025 AND month=1)',
330312
},
313+
period: 'weekly',
331314
};
332315

333316
await expect(

0 commit comments

Comments
 (0)