-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
563 lines (487 loc) · 18.8 KB
/
index.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Load environment variables
const dotenv = require('dotenv');
dotenv.config();
// dependencies
const lighthouse = require('lighthouse');
const chrome_launcher = require('chrome-launcher');
const db = require('./database');
const fs = require('fs');
const path = require('path');
const neat_csv = require('neat-csv');
// Is this a recurring report or no?
let should_repeat = false;
// For how long should this URL automatically be reported on?
let auto_report_lifetime = 90; // Days
// How frequently should this report be rerun
let auto_report_interval = 30; // Days between reports
let jobId = '';
// Get the JOB ID provided by Jarvis (if applicable)
for (const arg of process.argv) {
if (arg.startsWith('job-id')) {
jobId = arg.split('=')[1];
}
}
// Validate arguments
if (isNaN(auto_report_interval) || isNaN(auto_report_lifetime) ||
auto_report_interval < 1 || auto_report_lifetime < 1) {
console.log('$$$Sorry, please check your input.');
process.exit(1);
}
// Lighthouse options
const options = {
chromeFlags: ['--headless', '--disable-dev-shm-usage', '--no-sandbox']
};
// A config, don't know what it does
const config = {
extends: 'lighthouse:default',
settings: {
throttling: {
cpuSlowdownMultiplier: 2
}
}
};
// Perform the audit (returns the final report, if successful)
function performAudit (url, opts, config = null) {
return chrome_launcher.launch({ chromeFlags: opts.chromeFlags }).then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => {
return chrome.kill().then(() => results.lhr).catch(err => console.error(err));
}).catch(up => {
console.log('Killing Chrome to prevent hanging.');
chrome.kill(); // <-- Kill chrome anyway
throw up; // <- ha ha
});
}).catch(downTheGauntlet => {
throw downTheGauntlet; // <-- CHALLENGE ACCEPTED
});
}
// Take a list of urls and templates and do the whole reporting thing
// Generate report, then parse and store in the database
async function doReporting (urls_and_templates, budgets) {
// Loop through all of the urls and templates
for (let i = 0; i < urls_and_templates.length; i++) {
// Get the URL and Template
let url;
let template;
for (const prop in urls_and_templates[i]) {
if (prop.toLowerCase().includes('url')) {
url = urls_and_templates[i][prop];
} else if (prop.toLowerCase().includes('template')) {
template = urls_and_templates[i][prop];
}
}
// Logging
console.log(urls_and_templates[i]);
if (budgets) {
options.budgets = budgets;
}
// Perform the audit (catch error if needed)
try {
const report = await performAudit(url, options, config);
// Check for errors and proceed if all is well
if (report['runtimeError'] != null) {
console.error(report['runtimeError']['message']);
}else{
// Generate insert the report into the database tables
await parseReportAndStore(url, template, report);
}
} catch (e) {
console.error(e);
}
}
}
// This function parses the report and stores in the correct tables
async function parseReportAndStore (url, template, report) {
// Get the values as needed
const fetch_time = report['fetchTime'];
let page_size = report['audits']['total-byte-weight']['numericValue'];
const first_contentful_paint = report['audits']['first-contentful-paint']['numericValue'];
const max_potential_fid = report['audits']['max-potential-fid']['numericValue'];
const time_to_interactive = report['audits']['interactive']['numericValue'];
const first_meaningful_paint = report['audits']['first-meaningful-paint']['numericValue'];
const first_cpu_idle = report['audits']['first-cpu-idle']['numericValue'];
const largest_contentful_paint = report['audits']['largest-contentful-paint']['numericValue'];
const cumulative_layout_shift = report['audits']['cumulative-layout-shift']['numericValue'];
const total_blocking_time = report['audits']['total-blocking-time']['numericValue'];
const speed_index = report['audits']['speed-index']['numericValue'];
// These are lists and will have to be iterated
const network_resources = report['audits']['network-requests']['details']['items'];
const savings_opportunities = [];
// Loop through the audits to find savings opportunities
for (const audit_name in report['audits']) {
if (!report['audits'].hasOwnProperty(audit_name)) {
continue; // <-- Sanity check
}
const audit = report['audits'][audit_name];
if (audit.hasOwnProperty('details') && audit['details'] != null) {
if (audit['details']['type'] == 'opportunity') {
savings_opportunities.push({
audit_text: audit['title'],
estimated_savings: audit['details']['overallSavingsMs']
});
}
}
}
// Locate all diagnostics
const diagnostics = [];
let current_list_of_items = [];
// These are the diagnostics we care about
// mainthread-work-breakdown
// bootup-time
// font-display
// third-party-summary
// dom-size
// Main thread work breakdown
if (report['audits']['mainthread-work-breakdown']['score'] != 1 &&
report['audits']['mainthread-work-breakdown']['score'] != undefined) {
report['audits']['mainthread-work-breakdown']['details']['items'].forEach(item => {
current_list_of_items.push({
label: item['groupLabel'],
value: item['duration']
});
});
}
diagnostics.push({
diagnostic_id: 'mainthread-work-breakdown',
items: current_list_of_items,
});
current_list_of_items = [];
// bootup-time
if (report['audits']['bootup-time']['score'] != 1 &&
report['audits']['bootup-time']['score'] != undefined) {
report['audits']['bootup-time']['details']['items'].forEach(item => {
current_list_of_items.push({
label: item['url'],
value: item['total']
});
});
}
diagnostics.push({
diagnostic_id: 'bootup-time',
items: current_list_of_items,
});
current_list_of_items = [];
// font-display
if (report['audits']['font-display']['score'] != 1 &&
report['audits']['font-display']['score'] != undefined) {
report['audits']['font-display']['details']['items'].forEach(item => {
current_list_of_items.push({
label: item['url'],
value: item['wastedMs']
});
});
}
diagnostics.push({
diagnostic_id: 'font-display',
items: current_list_of_items,
});
current_list_of_items = [];
// third-party-summary
if (report['audits']['third-party-summary']['score'] != 1 &&
report['audits']['third-party-summary']['score'] != undefined) {
report['audits']['third-party-summary']['details']['items'].forEach(item => {
current_list_of_items.push({
label: item['entity']['text'],
value: item['blockingTime']
});
});
}
diagnostics.push({
diagnostic_id: 'third-party-summary',
items: current_list_of_items,
});
current_list_of_items = [];
// dom-size
if (report['audits']['dom-size']['score'] != 1 &&
report['audits']['dom-size']['score'] != undefined) {
report['audits']['dom-size']['details']['items'].forEach(item => {
current_list_of_items.push({
label: item['statistic'],
value: item['value']
});
});
}
diagnostics.push({
diagnostic_id: 'dom-size',
items: current_list_of_items,
});
// V6.0 Grab the budget metrics
const performance_budget = [];
const timing_budget = [];
if (report['audits']['performance-budget'] && report['audits']['performance-budget']['details']) {
for (const item of report['audits']['performance-budget']['details']['items']) {
const item_label = item['label'];
const item_request_count = item['requestCount'] || 0;
const item_transfer_size = item['transferSize'] || 0;
let item_count_over_budget = 0;
if (item['countOverBudget'] != null) {
item_count_over_budget = item['countOverBudget'].replace(/\D/g, '')
}
const item_size_over_budget = item['sizeOverBudget'] || 0;
performance_budget.push({
item_label,
item_request_count,
item_transfer_size,
item_count_over_budget,
item_size_over_budget
});
}
}
if (report['audits']['timing-budget'] && report['audits']['timing-budget']['details']) {
for (const item of report['audits']['timing-budget']['details']['items']) {
const item_label = item['label'];
const item_measurement = item['measurement'] || 0;
const item_over_budget = item['overBudget'] || 0;
timing_budget.push({
item_label,
item_measurement,
item_over_budget,
});
}
}
// Perform some conversions
page_size = page_size / 1024; // <-- Convert KB to MB
// Prepare the queries
const raw_reports_query_text = `INSERT INTO raw_reports (
url,
template,
fetch_time,
report,
job_id
)
VALUES (
$1, $2, $3, $4, $5
)`;
const gds_audit_query_text = `INSERT INTO gds_audits (
url,
template,
fetch_time,
page_size,
first_contentful_paint,
max_potential_fid,
time_to_interactive,
first_meaningful_paint,
first_cpu_idle,
largest_contentful_paint,
cumulative_layout_shift,
total_blocking_time,
speed_index,
job_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)`;
const resource_chart_query_text = `INSERT INTO resource_chart (
audit_url,
template,
fetch_time,
resource_url,
resource_type,
start_time,
end_time,
job_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)`;
const savings_opportunities_query_text = `INSERT INTO savings_opportunities(
audit_url,
template,
fetch_time,
audit_text,
estimated_savings,
job_id
)
VALUES (
$1, $2, $3, $4, $5, $6
)`;
const diagnostics_query_text = `INSERT INTO diagnostics(
audit_url,
template,
fetch_time,
diagnostic_id,
item_label,
item_value,
job_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7
)`;
// Prepare the params for the queries
let raw_reports_query_params = [
url,
template,
fetch_time,
report,
jobId
];
let gds_audit_query_params = [
url,
template,
fetch_time,
page_size,
first_contentful_paint,
max_potential_fid,
time_to_interactive,
first_meaningful_paint,
first_cpu_idle,
largest_contentful_paint,
cumulative_layout_shift,
total_blocking_time,
speed_index,
jobId
];
// Store async query promises here
const queryPromises = [];
// Execute the queries
await db.query(raw_reports_query_text, raw_reports_query_params);
await db.query(gds_audit_query_text, gds_audit_query_params);
// Insert all resources from the resource table into the resource chart table
for (let i = 0; i < network_resources.length; i++) {
const resource = network_resources[i];
// Filter undefined resource types
let resource_type = resource['resourceType'];
if (resource_type == null) {
resource_type = 'Other';
}
const resource_chart_query_params = [
url,
template,
fetch_time,
resource['url'],
resource_type,
resource['startTime'],
resource['endTime'],
jobId
];
let qPromise = db.query(resource_chart_query_text, resource_chart_query_params);
queryPromises.push(qPromise)
}
// Insert each savings opportunity into the correct table
for (let i = 0; i < savings_opportunities.length; i++) {
const opportunity = savings_opportunities[i];
const savings_opportunities_query_params = [
url,
template,
fetch_time,
opportunity['audit_text'],
opportunity['estimated_savings'],
jobId
];
let qPromise = db.query(savings_opportunities_query_text, savings_opportunities_query_params);
queryPromises.push(qPromise)
}
// Insert each diagnostic audit into the correct table
for (let i = 0; i < diagnostics.length; i++) {
const diag = diagnostics[i];
for (let j = 0; j < diag['items'].length; j++) {
const item = diag['items'][j];
const diagnostics_query_params = [
url,
template,
fetch_time,
diag['diagnostic_id'],
item['label'],
item['value'],
jobId
];
let qPromise = db.query(diagnostics_query_text, diagnostics_query_params);
queryPromises.push(qPromise)
}
}
// Disconnect when all promises have finished
await Promise.all(queryPromises);
console.log('Promises have returned');
}
// Process a file
async function processFile (file_path, budgets) {
try {
// Read the file
const file = fs.readFileSync(file_path);
const csv_data = await neat_csv(file);
let hasUrl = false;
let hasTemplate = false;
for (const prop in csv_data[0]) {
if (prop.toLowerCase().includes('url')) {
hasUrl = true;
} else if (prop.toLowerCase().includes('template')) {
hasTemplate = true;
}
}
// Validate that input CSV has URL and Template columns
if (!hasUrl || !hasTemplate) {
console.log('$$$Sorry, please make sure your CSV contains two columns labeled \'URL\' and \'Template\'.');
db.disconnect();
process.exit(-1);
}else{
console.log('All good!');
}
// Do reporting on the file
await doReporting(csv_data, budgets);
// Recurring reports should be saved in the DB
if (should_repeat) {
for (let i = 0; i < csv_data.length; i++) {
const record = csv_data[i];
const url = record['URL'];
const template = record['Template'];
await db.query(`DELETE FROM urls WHERE url = $1`, [url]);
await db.query(`INSERT INTO urls(url, template, interval, lifetime) VALUES($1, $2, $3, $4)`, [url, template, auto_report_interval, auto_report_lifetime]);
}
}
// All done!
console.log('Finished reporting!');
db.disconnect();
}catch (err) {
console.log('$$$Something went wrong trying to read that file.');
console.error(err);
}
}
async function doAutomaticReporting () {
console.log('No file provided, doing automatic reporting...');
// Read all URLs that need updating from the database
// If the latest date is longer ago than the interval in days, we need to update
const db_rows_that_need_updating = await db.query(`SELECT * FROM urls WHERE latest_date < now() - (interval::varchar(255) || 'days')::interval`);
const urls_that_need_updating = [];
db_rows_that_need_updating['rows'].forEach(async row => {
urls_that_need_updating.push({
URL: row['url'],
Template: row['template'],
});
// Update the latest date for this report
await db.query(`UPDATE urls SET latest_date = CURRENT_DATE WHERE id = $1`, [ row['id'] ]);
});
await doReporting(urls_that_need_updating);
// Now delete all the URLs that need deleting
console.log('Cleaning up old URLs from the DB...');
await db.query(`DELETE FROM urls WHERE start_date < now() - (lifetime::varchar(255) || 'days')::interval`);
console.log('Done automatically reporting!');
db.disconnect();
}
// Let's get started
// Connect to the database
db.connect(() => {
// Check for file input
const input_files = fs.readdirSync(path.join(__dirname, 'input'));
// Check for budget file
let budget_file;
if (fs.existsSync(path.join(__dirname, 'input', 'budget.json'))) {
budget_file = JSON.parse(fs.readFileSync(path.join(__dirname, 'input', 'budget.json')));
}
if (input_files.length > 0) {
for (const file of input_files) {
if (file.endsWith('.csv')) {
console.log('We got a file! Process it...');
processFile(path.join(__dirname, 'input', file), budget_file);
break;
}
}
}else{
doAutomaticReporting();
}
// If there is, this is an initial report
// If there is NOT, this is an automatic report
// Get the correct list of URLs
// Run the reports
// If this is an AUTOMATIC run, we are done
// Otherwise, save the list of URLs in the database (if not exists)
});