Skip to content

Commit fa9f9d2

Browse files
committed
support month correctly
1 parent 82b5121 commit fa9f9d2

File tree

2 files changed

+139
-18
lines changed

2 files changed

+139
-18
lines changed

src/paid-traffic-analysis/handler.js

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
import { getWeekInfo, getMonthInfo } from '@adobe/spacecat-shared-utils';
12+
import { getWeekInfo, getMonthInfo, getLastNumberOfWeeks } from '@adobe/spacecat-shared-utils';
1313
import { Audit } from '@adobe/spacecat-shared-data-access';
1414
import { wwwUrlResolver } from '../common/index.js';
1515
import { AuditBuilder } from '../common/audit-builder.js';
@@ -103,28 +103,93 @@ export async function sendRequestToMystique(auditUrl, auditData, context, site)
103103
log.info(`[traffic-analysis-audit] [siteId: ${siteId}] [baseUrl:${siteId}] Completed mystique evaluation step`);
104104
}
105105

106+
function getWeeksForMonth(targetMonth, targetYear) {
107+
// Get the last 6 weeks to ensure we cover the entire target month
108+
const weeks = getLastNumberOfWeeks(6);
109+
110+
// Filter weeks that belong to the target month
111+
return weeks.filter(({ week, year }) => {
112+
// Get week info to determine which months this week spans
113+
const { month: weekMonth } = getWeekInfo(week, year);
114+
// Include weeks that overlap with the target month
115+
return year === targetYear && weekMonth === targetMonth;
116+
});
117+
}
118+
106119
async function importDataStep(context, period) {
107-
const { site, finalUrl, log } = context;
120+
const {
121+
site, finalUrl, log, sqs, dataAccess,
122+
} = context;
108123
const siteId = site.getId();
109124
const allowOverwrite = false;
110125
log.info(`[traffic-analysis-import-${period}] Starting import data step for siteId: ${siteId}, url: ${finalUrl}`);
111126

112-
const analysisResult = await prepareTrafficAnalysisRequest(
113-
finalUrl,
114-
context,
115-
site,
116-
period,
117-
);
118-
119-
log.info(`[traffic-analysis-import-${period}] Prepared audit result for siteId: ${siteId}, sending to import worker with allowOverwrite: ${allowOverwrite}`);
120-
121-
return {
122-
auditResult: analysisResult.auditResult,
123-
fullAuditRef: finalUrl,
124-
type: 'traffic-analysis',
125-
siteId,
126-
allowOverwrite,
127-
};
127+
if (period === 'monthly') {
128+
const { month, year } = getMonthInfo();
129+
const { Configuration } = dataAccess;
130+
const configuration = await Configuration.findLatest();
131+
132+
// Get all weeks that overlap with this month
133+
const weeksInMonth = getWeeksForMonth(month, year);
134+
135+
log.info(`[traffic-analysis-import-monthly] Found ${weeksInMonth.length} weeks for month ${month}/${year}`);
136+
137+
// Send import requests for each week in the month
138+
for (const weekInfo of weeksInMonth) {
139+
const { temporalCondition } = getWeekInfo(weekInfo.week, weekInfo.year);
140+
141+
const message = {
142+
type: 'traffic-analysis',
143+
siteId,
144+
auditContext: {
145+
week: weekInfo.week,
146+
year: weekInfo.year,
147+
month: weekInfo.month,
148+
temporalCondition,
149+
},
150+
allowOverwrite,
151+
};
152+
153+
log.info(`[traffic-analysis-import-monthly] Sending import request for week ${weekInfo.week}/${weekInfo.year}`);
154+
// eslint-disable-next-line no-await-in-loop
155+
await sqs.sendMessage(configuration.getQueues().imports, message);
156+
}
157+
158+
// Return the last week for the main audit flow
159+
const lastWeek = weeksInMonth[weeksInMonth.length - 1];
160+
const { temporalCondition } = getWeekInfo(lastWeek.week, lastWeek.year);
161+
162+
return {
163+
auditResult: {
164+
year,
165+
month,
166+
week: lastWeek.week,
167+
siteId,
168+
temporalCondition,
169+
},
170+
fullAuditRef: finalUrl,
171+
type: 'traffic-analysis',
172+
siteId,
173+
allowOverwrite,
174+
};
175+
} else {
176+
const analysisResult = await prepareTrafficAnalysisRequest(
177+
finalUrl,
178+
context,
179+
site,
180+
period,
181+
);
182+
183+
log.info(`[traffic-analysis-import-${period}] Prepared audit result for siteId: ${siteId}, sending to import worker with allowOverwrite: ${allowOverwrite}`);
184+
185+
return {
186+
auditResult: analysisResult.auditResult,
187+
fullAuditRef: finalUrl,
188+
type: 'traffic-analysis',
189+
siteId,
190+
allowOverwrite,
191+
};
192+
}
128193
}
129194

130195
async function processAnalysisStep(context, period) {

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
prepareTrafficAnalysisRequest,
2121
sendRequestToMystique,
2222
weeklyImportDataStep,
23+
monthlyImportDataStep,
2324
weeklyProcessAnalysisStep,
2425
} from '../../../src/paid-traffic-analysis/handler.js';
2526
import { AWSAthenaClient } from '@adobe/spacecat-shared-athena-client';
@@ -318,4 +319,59 @@ describe('Paid Traffic Analysis Handler', () => {
318319
).to.be.rejectedWith('SQS Error');
319320
});
320321
});
322+
323+
describe('monthlyImportDataStep', () => {
324+
it('should send multiple weekly import messages and return correct structure', async () => {
325+
const mockConfiguration = {
326+
getQueues: sandbox.stub().returns({ imports: 'test-import-queue' }),
327+
};
328+
329+
context.dataAccess = {
330+
Configuration: { findLatest: sandbox.stub().resolves(mockConfiguration) },
331+
};
332+
context.site = site;
333+
context.finalUrl = auditUrl;
334+
335+
const result = await monthlyImportDataStep(context);
336+
337+
// Validate return structure
338+
const expectedResult = {
339+
auditResult: {
340+
year: 2024,
341+
month: 12,
342+
week: sinon.match.number,
343+
siteId,
344+
temporalCondition: sinon.match.string,
345+
},
346+
fullAuditRef: auditUrl,
347+
type: 'traffic-analysis',
348+
siteId,
349+
allowOverwrite: false,
350+
};
351+
352+
expect(result).to.deep.match(expectedResult);
353+
354+
// Validate SQS messages were sent (should be total weeks - 1)
355+
expect(mockSqs.sendMessage.callCount).to.equal(result.totalWeeks - 1); // or just check > 0 if we don't know exact count
356+
357+
// Validate message structure for each call
358+
const expectedMessage = {
359+
type: 'traffic-analysis',
360+
siteId,
361+
allowOverwrite: false,
362+
auditContext: {
363+
week: sinon.match.number,
364+
year: sinon.match.number,
365+
month: sinon.match.number,
366+
temporalCondition: sinon.match.string,
367+
},
368+
};
369+
370+
mockSqs.sendMessage.getCalls().forEach(call => {
371+
const [queueUrl, message] = call.args;
372+
expect(queueUrl).to.equal('test-import-queue');
373+
expect(message).to.deep.match(expectedMessage);
374+
});
375+
});
376+
});
321377
});

0 commit comments

Comments
 (0)