|
9 | 9 | * OF ANY KIND, either express or implied. See the License for the specific language
|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 |
| -import { getWeekInfo, getMonthInfo } from '@adobe/spacecat-shared-utils'; |
| 12 | +import { getWeekInfo, getMonthInfo, getLastNumberOfWeeks } from '@adobe/spacecat-shared-utils'; |
13 | 13 | import { Audit } from '@adobe/spacecat-shared-data-access';
|
14 | 14 | import { wwwUrlResolver } from '../common/index.js';
|
15 | 15 | import { AuditBuilder } from '../common/audit-builder.js';
|
@@ -103,28 +103,93 @@ export async function sendRequestToMystique(auditUrl, auditData, context, site)
|
103 | 103 | log.info(`[traffic-analysis-audit] [siteId: ${siteId}] [baseUrl:${siteId}] Completed mystique evaluation step`);
|
104 | 104 | }
|
105 | 105 |
|
| 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 | + |
106 | 119 | async function importDataStep(context, period) {
|
107 |
| - const { site, finalUrl, log } = context; |
| 120 | + const { |
| 121 | + site, finalUrl, log, sqs, dataAccess, |
| 122 | + } = context; |
108 | 123 | const siteId = site.getId();
|
109 | 124 | const allowOverwrite = false;
|
110 | 125 | log.info(`[traffic-analysis-import-${period}] Starting import data step for siteId: ${siteId}, url: ${finalUrl}`);
|
111 | 126 |
|
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 | + } |
128 | 193 | }
|
129 | 194 |
|
130 | 195 | async function processAnalysisStep(context, period) {
|
|
0 commit comments