-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
353 lines (307 loc) · 11.4 KB
/
Copy pathhandler.js
File metadata and controls
353 lines (307 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright 2025 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 fs from 'fs';
import path from 'path';
import { isNonEmptyArray } from '@adobe/spacecat-shared-utils';
import { ok } from '@adobe/spacecat-shared-http-utils';
import { say } from '../../utils/slack-utils.js';
const TASK_TYPE = 'cwv-demo-suggestions-processor';
const LCP = 'lcp';
const CLS = 'cls';
const INP = 'inp';
const DEMO = 'demo';
const MAX_CWV_DEMO_SUGGESTIONS = 2;
/**
* Maps metric types to their corresponding markdown files
*/
const CWV_GENERIC_SUGGESTIONS = {
lcp: ['lcp1.md', 'lcp2.md', 'lcp3.md'],
cls: ['cls1.md', 'cls2.md'],
inp: ['inp1.md'],
};
/**
* Loads content from a markdown file following the spacecat-api-service pattern
* @param {string} fileName - The name of the file to load
* @param {object} logger - The logger object
* @param {object} env - The environment object
* @param {object} slackContext - The Slack context object
* @returns {string} The file content
* @throws {Error} If the file cannot be read
*/
const loadSuggestionContent = async (fileName, logger, env, slackContext) => {
try {
const filePath = path.resolve(process.cwd(), 'static', fileName);
const data = fs.readFileSync(filePath, 'utf-8');
return data;
} catch (error) {
logger.error(`Failed to load suggestion content from "${fileName}": ${error.message}`);
await say(env, logger, slackContext, `❌ Failed to load suggestion content from "${fileName}": ${error.message}`);
throw new Error(`Failed to load suggestion content from "${fileName}": ${error.message}`);
}
};
/**
* Gets a random suggestion for the given issue type using the spacecat-api-service pattern
* @param {string} issueType - The type of issue (lcp, cls, inp)
* @param {object} logger - The logger object for error logging
* @param {object} env - The environment object
* @param {object} slackContext - The Slack context object
* @returns {Promise<string|null>} A random suggestion or null if none available
*/
async function getRandomSuggestion(issueType, logger, env, slackContext) {
const files = CWV_GENERIC_SUGGESTIONS[issueType];
/* c8 ignore start */
// Defensive check: CWV_GENERIC_SUGGESTIONS is a hardcoded map with valid arrays.
// This path only occurs if the map is misconfigured, which doesn't happen in production.
if (!isNonEmptyArray(files)) {
await say(env, logger, slackContext, `No files found for issue type: ${issueType} and files: ${files}`);
return null;
}
/* c8 ignore stop */
const randomIndex = Math.floor(Math.random() * files.length);
const fileName = files[randomIndex];
try {
const content = await loadSuggestionContent(fileName, logger, env, slackContext);
return content;
} catch (error) {
logger.error(`Failed to get random suggestion for ${issueType}: ${error.message}`);
await say(env, logger, slackContext, `❌ Failed to get random suggestion for ${issueType}: ${error.message}`);
return null;
}
}
/**
* CWV thresholds for determining if metrics have issues
*/
const CWV_THRESHOLDS = {
lcp: 2500, // 2.5 seconds
cls: 0.1, // 0.1
inp: 200, // 200 milliseconds
};
/**
* Gets metric issues based on CWV thresholds
* @param {object} metrics - The metrics object
* @returns {Array} Array of issue types
*/
function getMetricIssues(metrics) {
const issues = [];
if (metrics?.lcp > CWV_THRESHOLDS[LCP]) {
issues.push(LCP);
}
if (metrics?.cls > CWV_THRESHOLDS[CLS]) {
issues.push(CLS);
}
if (metrics?.inp > CWV_THRESHOLDS[INP]) {
issues.push(INP);
}
return issues;
}
/**
* Checks if a suggestion has existing issues
* @param {object} suggestion - The suggestion object
* @returns {boolean} True if suggestion has existing issues
*/
function hasExistingIssues(suggestion) {
const data = suggestion.getData();
return (data.issues && Array.isArray(data.issues) && data.issues.length > 0)
|| data.genericSuggestions === true;
}
/**
* Updates a suggestion with generic CWV issues (as per requirements)
* @param {object} suggestion - The suggestion object
* @param {Array} metricIssues - Array of metric issue types
* @param {object} logger - The logger object
* @param {object} env - The environment object
* @param {object} slackContext - The Slack context object
* @returns {number} Number of issues successfully added
*/
async function updateSuggestionWithGenericIssues(
suggestion,
metricIssues,
logger,
env,
slackContext,
) {
let issuesAdded = 0;
try {
const data = suggestion.getData();
if (!data.issues) {
data.issues = [];
}
// Process all issue types in parallel to avoid await in loop
const suggestionPromises = metricIssues.map(async (issueType) => {
const randomSuggestion = await getRandomSuggestion(issueType, logger, env, slackContext);
return { issueType, randomSuggestion };
});
const suggestions = await Promise.all(suggestionPromises);
for (const { issueType, randomSuggestion } of suggestions) {
if (randomSuggestion) {
const genericIssue = {
type: issueType,
value: randomSuggestion,
};
data.issues.push(genericIssue);
data.genericSuggestions = true;
issuesAdded += 1;
}
}
suggestion.setData(data);
suggestion.setUpdatedBy('system');
await suggestion.save();
} catch (error) {
logger.error(`Error updating suggestion ${suggestion.getId()} with generic issues:`, error);
await say(env, logger, slackContext, `❌ Error updating suggestion ${suggestion.getId()}: ${error.message}`);
}
return issuesAdded;
}
/**
* Processes a single opportunity according to exact requirements
* @param {object} opportunity - The opportunity object
* @param {object} logger - The logger object
* @param {object} env - The environment object
* @param {object} slackContext - The Slack context object
* @returns {number} Number of suggestions updated
*/
async function processCWVOpportunity(opportunity, logger, env, slackContext) {
try {
const allSuggestions = await opportunity.getSuggestions();
// Filter to only process suggestions with "new" status
const suggestions = allSuggestions.filter((suggestion) => suggestion.getStatus() === 'NEW');
// Check if any suggestion has existing issues
const hasSuggestionsWithIssues = suggestions.some(hasExistingIssues);
if (hasSuggestionsWithIssues) {
await say(env, logger, slackContext, `ℹ️ Opportunity ${opportunity.getId()} already has suggestions, skipping generic suggestions`);
return 0;
}
await say(env, logger, slackContext, `✅ Opportunity ${opportunity.getId()} has no existing suggestions, adding generic suggestions`);
// Sort suggestions by pageviews (descending)
const sortedSuggestions = suggestions
.filter((suggestion) => {
const data = suggestion.getData();
return data?.pageviews > 0;
})
.sort((a, b) => b.getData().pageviews - a.getData().pageviews);
// Find first 2 suggestions with LCP/CLS/INP issues
const suggestionsToUpdate = [];
const sayPromises = [];
for (const suggestion of sortedSuggestions) {
if (suggestionsToUpdate.length >= MAX_CWV_DEMO_SUGGESTIONS) break;
const data = suggestion.getData();
const metrics = data.metrics || [];
// Check if suggestion has any LCP/CLS/INP issues
let hasCWVIssues = false;
let metricIssues = [];
for (const metric of metrics) {
const issues = getMetricIssues(metric);
if (issues.length > 0) {
hasCWVIssues = true;
metricIssues = issues;
break; // Take first set of issues found
}
}
if (hasCWVIssues) {
suggestionsToUpdate.push({ suggestion, metricIssues });
}
}
await Promise.all(sayPromises);
if (suggestionsToUpdate.length === 0) {
return 0;
}
// Add generic suggestions to selected suggestions
const updatePromises = suggestionsToUpdate.map(async ({ suggestion, metricIssues }) => {
const issuesAdded = await updateSuggestionWithGenericIssues(
suggestion,
metricIssues,
logger,
env,
slackContext,
);
return { suggestion, issuesAdded };
});
const results = await Promise.all(updatePromises);
const totalIssuesAdded = results.reduce((sum, { issuesAdded }) => sum + issuesAdded, 0);
// Log information about generic suggestions added
if (totalIssuesAdded > 0) {
await say(env, logger, slackContext, `🎯 Added ${totalIssuesAdded} generic CWV suggestions for opportunity ${opportunity.getId()}`);
} else {
await say(env, logger, slackContext, `❌ No generic CWV suggestions added for opportunity ${opportunity.getId()}`);
}
return suggestionsToUpdate.length;
} catch (error) {
logger.error(`Error processing opportunity ${opportunity.getId()}:`, error);
await say(env, logger, slackContext, `❌ Error processing opportunity ${opportunity.getId()}: ${error.message}`);
return 0;
}
}
/**
* Runs the CWV demo suggestions processor
* @param {object} message - The message object
* @param {object} context - The context object
*/
export async function runCwvDemoSuggestionsProcessor(message, context) {
const { log, env, dataAccess } = context;
const { Site } = dataAccess;
const {
siteId, organizationId, taskContext,
} = message;
const { profile, slackContext } = taskContext || {};
log.info('Processing CWV demo suggestions for site:', {
taskType: TASK_TYPE,
siteId,
organizationId,
profile,
});
try {
if (!profile || profile !== DEMO) {
return ok({
message: 'CWV processing skipped - not a demo profile',
reason: 'non-demo-profile',
profile,
suggestionsAdded: 0,
});
}
const site = await Site.findById(siteId);
if (!site) {
log.error(`Site not found for siteId: ${siteId}`);
return ok({
message: 'Site not found',
suggestionsAdded: 0,
});
}
const opportunities = await site.getOpportunities();
const cwvOpportunities = opportunities.filter((opp) => opp.getType() === 'cwv');
if (cwvOpportunities.length === 0) {
await say(env, log, slackContext, 'No CWV opportunities found for site, skipping generic suggestions');
return ok({
message: 'No CWV opportunities found',
suggestionsAdded: 0,
});
}
const suggestionsUpdated = await processCWVOpportunity(
cwvOpportunities[0],
log,
env,
slackContext,
);
return ok({
message: 'CWV demo suggestions processor completed',
opportunitiesProcessed: 1,
suggestionsAdded: suggestionsUpdated,
});
} catch (error) {
log.error('Error in CWV demo suggestions processor:', error);
return ok({
message: 'CWV demo suggestions processor completed with errors',
error: error.message,
suggestionsAdded: 0,
});
}
}
export default runCwvDemoSuggestionsProcessor;