-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsites.js
executable file
·481 lines (394 loc) · 13 KB
/
sites.js
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright 2023 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 {
createResponse,
created,
badRequest,
noContent,
notFound,
ok,
} from '@adobe/spacecat-shared-http-utils';
import {
hasText,
isBoolean,
isObject,
getStoredMetrics, getRUMDomainKey, isValidUUID, deepEqual,
} from '@adobe/spacecat-shared-utils';
import { Site as SiteModel } from '@adobe/spacecat-shared-data-access';
import RUMAPIClient from '@adobe/spacecat-shared-rum-api-client';
import { SiteDto } from '../dto/site.js';
import { AuditDto } from '../dto/audit.js';
import { validateRepoUrl } from '../utils/validations.js';
import { KeyEventDto } from '../dto/key-event.js';
import { wwwUrlResolver } from '../support/utils.js';
/**
* Sites controller. Provides methods to create, read, update and delete sites.
* @param {DataAccess} dataAccess - Data access.
* @returns {object} Sites controller.
* @constructor
*/
const AHREFS = 'ahrefs';
const ORGANIC_TRAFFIC = 'organic-traffic';
const MONTH_DAYS = 30;
const TOTAL_METRICS = 'totalMetrics';
function SitesController(dataAccess, log, env) {
if (!isObject(dataAccess)) {
throw new Error('Data access required');
}
const { Audit, KeyEvent, Site } = dataAccess;
/**
* Creates a site. The site ID is generated automatically.
* @param {object} context - Context of the request.
* @return {Promise<Response>} Site response.
*/
const createSite = async (context) => {
const site = await Site.create({
organizationId: env.DEFAULT_ORGANIZATION_ID,
...context.data,
});
return createResponse(SiteDto.toJSON(site), 201);
};
/**
* Gets all sites.
* @returns {Promise<Response>} Array of sites response.
*/
const getAll = async () => {
const sites = (await Site.all()).map((site) => SiteDto.toJSON(site));
return ok(sites);
};
/**
* Gets all sites by delivery type.
* @param {object} context - Context of the request.
* @returns {Promise<Response>} Array of sites response.
*/
const getAllByDeliveryType = async (context) => {
const deliveryType = context.params?.deliveryType;
if (!hasText(deliveryType)) {
return badRequest('Delivery type required');
}
const sites = (await Site.allByDeliveryType(deliveryType))
.map((site) => SiteDto.toJSON(site));
return ok(sites);
};
/**
* Gets all sites with their latest audit. Sites without a latest audit will be included
* in the result, but will have an empty audits array. The sites are sorted by their latest
* audit scores in ascending order by default. The sortAuditsAscending parameter can be used
* to change the sort order. If a site has no latest audit, it will be sorted at the end of
* the list.
* @param {object} context - Context of the request.
* @return {Promise<Response>} Array of sites response.
*/
const getAllWithLatestAudit = async (context) => {
const auditType = context.params?.auditType;
const ascending = context.params?.ascending;
if (!hasText(auditType)) {
return badRequest('Audit type required');
}
const order = ascending === 'true' ? 'asc' : 'desc';
const sites = await Site.allWithLatestAudit(auditType, order);
const result = await Promise.all(sites
.map(async (site) => {
const audit = await site.getLatestAuditByAuditType(auditType);
return SiteDto.toJSON(site, audit);
}));
return ok(result);
};
/**
* Gets all sites as an XLS file.
* @returns {Promise<Response>} XLS file.
*/
const getAllAsXLS = async () => {
const sites = await Site.all();
return ok(SiteDto.toXLS(sites));
};
/**
* Gets all sites as a CSV file.
* @returns {Promise<Response>} CSV file.
*/
const getAllAsCSV = async () => {
const sites = await Site.all();
return ok(SiteDto.toCSV(sites));
};
const getAuditForSite = async (context) => {
const siteId = context.params?.siteId;
const auditType = context.params?.auditType;
const auditedAt = context.params?.auditedAt;
if (!isValidUUID(siteId)) {
return badRequest('Site ID required');
}
if (!hasText(auditType)) {
return badRequest('Audit type required');
}
if (!hasText(auditedAt)) {
return badRequest('Audited at required');
}
const audit = await Audit.findBySiteIdAndAuditTypeAndAuditedAt(siteId, auditType, auditedAt);
if (!audit) {
return notFound('Audit not found');
}
return ok(AuditDto.toJSON(audit));
};
/**
* Gets a site by ID.
* @param {object} context - Context of the request.
* @returns {Promise<object>} Site.
* @throws {Error} If site ID is not provided.
*/
const getByID = async (context) => {
const siteId = context.params?.siteId;
if (!isValidUUID(siteId)) {
return badRequest('Site ID required');
}
const site = await Site.findById(siteId);
if (!site) {
return notFound('Site not found');
}
return ok(SiteDto.toJSON(site));
};
/**
* Gets a site by base URL. The base URL is base64 encoded. This is to allow
* for URLs with special characters to be used as path parameters.
* @param {object} context - Context of the request.
* @returns {Promise<object>} Site.
* @throws {Error} If base URL is not provided.
*/
const getByBaseURL = async (context) => {
const encodedBaseURL = context.params?.baseURL;
if (!hasText(encodedBaseURL)) {
return badRequest('Base URL required');
}
const decodedBaseURL = Buffer.from(encodedBaseURL, 'base64').toString('utf-8').trim();
const site = await Site.findByBaseURL(decodedBaseURL);
if (!site) {
return notFound('Site not found');
}
return ok(SiteDto.toJSON(site));
};
/**
* Removes a site.
* @param {object} context - Context of the request.
* @return {Promise<Response>} Delete response.
*/
const removeSite = async (context) => {
const siteId = context.params?.siteId;
if (!isValidUUID(siteId)) {
return badRequest('Site ID required');
}
const site = await Site.findById(siteId);
if (!site) {
return notFound('Site not found');
}
await site.remove();
return noContent();
};
/**
* Updates a site
* @param {object} context - Context of the request.
* @return {Promise<Response>} Site response.
*/
const updateSite = async (context) => {
const siteId = context.params?.siteId;
if (!isValidUUID(siteId)) {
return badRequest('Site ID required');
}
const site = await Site.findById(siteId);
if (!site) {
return notFound('Site not found');
}
const requestBody = context.data;
if (!isObject(requestBody)) {
return badRequest('Request body required');
}
let updates = false;
if (isBoolean(requestBody.isLive) && requestBody.isLive !== site.getIsLive()) {
site.toggleLive();
updates = true;
}
if (hasText(requestBody.organizationId)
&& requestBody.organizationId !== site.getOrganizationId()) {
site.setOrganizationId(requestBody.organizationId);
updates = true;
}
if (requestBody.gitHubURL !== site.getGitHubURL() && validateRepoUrl(requestBody.gitHubURL)) {
site.setGitHubURL(requestBody.gitHubURL);
updates = true;
}
if (requestBody.deliveryType !== site.getDeliveryType()
&& Object.values(SiteModel.DELIVERY_TYPES).includes(requestBody.deliveryType)) {
site.setDeliveryType(requestBody.deliveryType);
updates = true;
}
if (isObject(requestBody.config)) {
site.setConfig(requestBody.config);
updates = true;
}
if (isObject(requestBody.hlxConfig) && !deepEqual(requestBody.hlxConfig, site.getHlxConfig())) {
site.setHlxConfig(requestBody.hlxConfig);
updates = true;
}
if (updates) {
const updatedSite = await site.save();
return ok(SiteDto.toJSON(updatedSite));
}
return badRequest('No updates provided');
};
/**
* Creates a key event. The key event ID is generated automatically.
* @param {object} context - Context of the request.
* @return {Promise<Response>} Key event response.
*/
const createKeyEvent = async (context) => {
const { siteId } = context.params;
const { name, type, time } = context.data;
const keyEvent = await KeyEvent.create({
siteId,
name,
type,
time,
});
return created(KeyEventDto.toJSON(keyEvent));
};
/**
* Gets key events for a site
* @param {object} context - Context of the request.
* @returns {Promise<[object]>} Key events.
* @throws {Error} If site ID is not provided.
*/
const getKeyEventsBySiteID = async (context) => {
const siteId = context.params?.siteId;
if (!isValidUUID(siteId)) {
return badRequest('Site ID required');
}
const site = await Site.findById(siteId);
if (!site) {
return notFound('Site not found');
}
const keyEvents = await site.getKeyEvents();
return ok(keyEvents.map((keyEvent) => KeyEventDto.toJSON(keyEvent)));
};
/**
* Removes a key event.
* @param {object} context - Context of the request.
* @return {Promise<Response>} Delete response.
*/
const removeKeyEvent = async (context) => {
const { keyEventId } = context.params;
if (!hasText(keyEventId)) {
return badRequest('Key Event ID required');
}
const keyEvent = await KeyEvent.findById(keyEventId);
if (!keyEvent) {
return notFound('Key Event not found');
}
await keyEvent.remove();
return noContent();
};
const getSiteMetricsBySource = async (context) => {
const siteId = context.params?.siteId;
const metric = context.params?.metric;
const source = context.params?.source;
if (!isValidUUID(siteId)) {
return badRequest('Site ID required');
}
if (!hasText(metric)) {
return badRequest('metric required');
}
if (!hasText(source)) {
return badRequest('source required');
}
const site = await Site.findById(siteId);
if (!site) {
return notFound('Site not found');
}
const metrics = await getStoredMetrics({ siteId, metric, source }, context);
return ok(metrics);
};
const getLatestSiteMetrics = async (context) => {
const siteId = context.params?.siteId;
if (!isValidUUID(siteId)) {
return badRequest('Site ID required');
}
const site = await Site.findById(siteId);
if (!site) {
return notFound('Site not found');
}
const rumAPIClient = RUMAPIClient.createFrom(context);
const domain = wwwUrlResolver(site);
try {
const domainKey = await getRUMDomainKey(site.getBaseURL(), context);
const current = await rumAPIClient.query(TOTAL_METRICS, {
domain,
domainkey: domainKey,
interval: MONTH_DAYS,
});
const total = await rumAPIClient.query(TOTAL_METRICS, {
domain,
domainkey: domainKey,
interval: 2 * MONTH_DAYS,
});
const organicTraffic = await getStoredMetrics(
{ siteId, metric: ORGANIC_TRAFFIC, source: AHREFS },
context,
);
const previousPageViews = total.totalPageViews - current.totalPageViews;
const previousCTR = (total.totalClicks - current.totalClicks) / previousPageViews;
const pageViewsChange = ((current.totalPageViews - previousPageViews)
/ previousPageViews) * 100;
const ctrChange = ((current.totalCTR - previousCTR) / previousCTR) * 100;
let cpc = 0;
if (organicTraffic.length > 0) {
const metric = organicTraffic[organicTraffic.length - 1];
cpc = metric.cost / metric.value;
}
const projectedTrafficValue = pageViewsChange * cpc;
log.info(`Got RUM metrics for site ${siteId} current: ${current.length}`);
return ok({
pageViewsChange,
ctrChange,
projectedTrafficValue,
});
} catch (error) {
if (error.message?.includes('Error retrieving the domain key')) {
log.info(`No RUM key configured for site ${siteId}: ${error.message}`);
} else {
log.error(`Error getting RUM metrics for site ${siteId}: ${error.message}`);
}
}
return ok({
pageViewsChange: 0,
ctrChange: 0,
projectedTrafficValue: 0,
});
};
return {
createSite,
getAll,
getAllAsXLS,
getAllAsCSV,
getAllWithLatestAudit,
getAuditForSite,
getByBaseURL,
getAllByDeliveryType,
getByID,
removeSite,
updateSite,
// key events
createKeyEvent,
getKeyEventsBySiteID,
removeKeyEvent,
// site metrics
getSiteMetricsBySource,
getLatestSiteMetrics,
};
}
export default SitesController;