Skip to content

Commit 2d3e534

Browse files
committed
Upgraded to Lighthouse v6 and added new metrics
1 parent c6f8981 commit 2d3e534

File tree

5 files changed

+199
-260
lines changed

5 files changed

+199
-260
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.env
33
.DS_Store
4+
input

database.js

+11-87
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,23 @@
11
// dependencies
2-
const pg = require('pg');
2+
const { Client } = require('pg');
33

4-
function connect (callback) {
5-
client = new pg.Client({
4+
let client;
5+
6+
async function connect (callback) {
7+
client = new Client({
68
user: process.env.DB_USER,
79
host: process.env.DB_HOST,
810
database: process.env.DB_NAME,
911
password: process.env.DB_PASS
1012
});
1113

1214
// Connect to the database and handle errors
13-
client.connect((err) => {
14-
if (err) {
15-
console.error(err);
16-
}else{
17-
console.log('Connected to the database!');
18-
19-
// Make sure the database tables exist
20-
client.query(`CREATE TABLE IF NOT EXISTS
21-
raw_reports(
22-
id SERIAL PRIMARY KEY,
23-
url VARCHAR (2048) NOT NULL,
24-
template VARCHAR (255),
25-
fetch_time timestamp,
26-
report JSON NOT NULL
27-
)
28-
`);
29-
30-
client.query(`CREATE TABLE IF NOT EXISTS
31-
urls(
32-
id SERIAL PRIMARY KEY,
33-
url VARCHAR (2048) NOT NULL,
34-
template VARCHAR (255),
35-
start_date timestamp DEFAULT CURRENT_DATE,
36-
latest_date timestamp DEFAULT CURRENT_DATE,
37-
interval DECIMAL,
38-
lifetime DECIMAL
39-
)
40-
`);
41-
42-
client.query(`CREATE TABLE IF NOT EXISTS
43-
gds_audits(
44-
id SERIAL PRIMARY KEY,
45-
url VARCHAR (2048) NOT NULL,
46-
template VARCHAR(2048),
47-
fetch_time timestamp,
48-
page_size decimal,
49-
first_contentful_paint decimal,
50-
max_potential_fid decimal,
51-
time_to_interactive decimal,
52-
first_meaningful_paint decimal,
53-
first_cpu_idle decimal
54-
)
55-
`);
56-
57-
client.query(`CREATE TABLE IF NOT EXISTS
58-
resource_chart(
59-
id SERIAL PRIMARY KEY,
60-
audit_url VARCHAR(2048),
61-
template VARCHAR(2048),
62-
fetch_time TIMESTAMP,
63-
resource_url VARCHAR(2048),
64-
resource_type VARCHAR(2048),
65-
start_time decimal,
66-
end_time decimal
67-
)
68-
`);
69-
70-
client.query(`CREATE TABLE IF NOT EXISTS
71-
savings_opportunities(
72-
id SERIAL PRIMARY KEY,
73-
audit_url VARCHAR(2048),
74-
template VARCHAR(2048),
75-
fetch_time TIMESTAMP,
76-
audit_text VARCHAR(2048),
77-
estimated_savings decimal
78-
)
79-
`);
80-
81-
client.query(`CREATE TABLE IF NOT EXISTS
82-
diagnostics(
83-
id SERIAL PRIMARY KEY,
84-
audit_url VARCHAR(2048),
85-
template VARCHAR(2048),
86-
fetch_time TIMESTAMP,
87-
diagnostic_id VARCHAR(2048),
88-
item_label VARCHAR(2048),
89-
item_value DECIMAL
90-
)
91-
`);
92-
93-
// Call the callback
94-
callback();
95-
}
96-
});
15+
try {
16+
await client.connect();
17+
callback();
18+
} catch (error) {
19+
console.error(error);
20+
}
9721
}
9822

9923
// Disconnect from the database

index.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
// Load environment variables
2+
const dotenv = require('dotenv');
3+
dotenv.config();
4+
15
// dependencies
26
const lighthouse = require('lighthouse');
37
const chrome_launcher = require('chrome-launcher');
48
const db = require('./database');
59
const fs = require('fs');
610
const path = require('path');
711
const neat_csv = require('neat-csv');
8-
const dotenv = require('dotenv');
9-
10-
// Load environment variables
11-
dotenv.config();
1212

1313
// Is this a recurring report or no?
1414
let should_repeat = false;
@@ -41,7 +41,7 @@ if (should_repeat) {
4141
if (isNaN(auto_report_interval) || isNaN(auto_report_lifetime) ||
4242
auto_report_interval < 1 || auto_report_lifetime < 1) {
4343
console.log('$$$Sorry, please check your input.');
44-
return;
44+
process.exit(1);
4545
}
4646

4747
if (should_repeat) {
@@ -116,6 +116,10 @@ async function parseReportAndStore (url, template, report) {
116116
const time_to_interactive = report['audits']['interactive']['numericValue'];
117117
const first_meaningful_paint = report['audits']['first-meaningful-paint']['numericValue'];
118118
const first_cpu_idle = report['audits']['first-cpu-idle']['numericValue'];
119+
const largest_contentful_paint = report['audits']['largest-contentful-paint']['numericValue'];
120+
const cumulative_layout_shift = report['audits']['cumulative-layout-shift']['numericValue'];
121+
const total_blocking_time = report['audits']['total-blocking-time']['numericValue'];
122+
const speed_index = report['audits']['speed-index']['numericValue'];
119123

120124
// These are lists and will have to be iterated
121125
const network_resources = report['audits']['network-requests']['details']['items'];
@@ -252,10 +256,14 @@ async function parseReportAndStore (url, template, report) {
252256
max_potential_fid,
253257
time_to_interactive,
254258
first_meaningful_paint,
255-
first_cpu_idle
259+
first_cpu_idle,
260+
largest_contentful_paint,
261+
cumulative_layout_shift,
262+
total_blocking_time,
263+
speed_index
256264
)
257265
VALUES (
258-
$1, $2, $3, $4, $5, $6, $7, $8, $9
266+
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
259267
)`;
260268

261269
const resource_chart_query_text = `INSERT INTO resource_chart (
@@ -311,7 +319,11 @@ async function parseReportAndStore (url, template, report) {
311319
max_potential_fid,
312320
time_to_interactive,
313321
first_meaningful_paint,
314-
first_cpu_idle
322+
first_cpu_idle,
323+
largest_contentful_paint,
324+
cumulative_layout_shift,
325+
total_blocking_time,
326+
speed_index
315327
];
316328

317329
// Execute the queries
@@ -444,7 +456,6 @@ async function doAutomaticReporting () {
444456
console.log('Done automatically reporting!');
445457

446458
db.disconnect();
447-
return;
448459
}
449460

450461
// Let's get started

0 commit comments

Comments
 (0)