Skip to content

Commit 703ded6

Browse files
michaelgerakispaulirish
authored andcommitted
Refactor the JSON output. (#567)
The structure of the Lighthouse JSON output is different and better. See #567 for details
1 parent 0269917 commit 703ded6

File tree

9 files changed

+1000
-1136
lines changed

9 files changed

+1000
-1136
lines changed

lighthouse-cli/printer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function createOutput(results, outputMode) {
8181

8282
// JSON report.
8383
if (outputMode === 'json') {
84-
return JSON.stringify(results.aggregations, null, 2);
84+
return JSON.stringify({audits: results.audits, aggregations: results.aggregations}, null, 2);
8585
}
8686

8787
// Pretty printed.
@@ -99,6 +99,9 @@ function createOutput(results, outputMode) {
9999
}
100100

101101
item.subItems.forEach(subitem => {
102+
// Get audit object from inside of results.audits under name subitem.
103+
// Coming soon events are not located inside of results.audits.
104+
subitem = results.audits[subitem] || subitem;
102105
let lineItem = ` -- ${subitem.description}: ${subitem.score}`;
103106
if (subitem.displayValue) {
104107
lineItem += ` (${subitem.displayValue})`;

lighthouse-cli/test/fixtures/sample.json

Lines changed: 478 additions & 546 deletions
Large diffs are not rendered by default.

lighthouse-core/aggregator/aggregate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Aggregate {
171171
return;
172172
}
173173

174-
subItems.push(filteredAndRemappedResults[e]);
174+
subItems.push(filteredAndRemappedResults[e].name);
175175

176176
// Only add to the score if this aggregation contributes to the
177177
// overall score.
@@ -199,13 +199,13 @@ class Aggregate {
199199
* @param {!Array<!AuditResult>} results
200200
* @return {!AggregationResult}
201201
*/
202-
static aggregate(aggregation, results) {
202+
static aggregate(aggregation, auditResults) {
203203
return {
204204
name: aggregation.name,
205205
description: aggregation.description,
206206
scored: aggregation.scored,
207207
categorizable: aggregation.categorizable,
208-
score: Aggregate.compare(results, aggregation.items, aggregation.scored)
208+
score: Aggregate.compare(auditResults, aggregation.items, aggregation.scored)
209209
};
210210
}
211211
}

lighthouse-core/closure/typedefs/Aggregation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ AggregationResultItem.prototype.name;
9797
/** @type {string} */
9898
AggregationResultItem.prototype.description;
9999

100-
/** @type {!Array<!AuditResult>} */
100+
/** @type {!Array<!string>} */
101101
AggregationResultItem.prototype.subItems;
102102

103103
/**

lighthouse-core/report/report-generator.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,31 @@ class ReportGenerator {
171171
const inline = (options && options.inline) || false;
172172

173173
// Ensure the formatter for each extendedInfo is registered.
174+
Object.keys(results.audits).forEach(audit => {
175+
// Use value rather than key for audit.
176+
audit = results.audits[audit];
177+
178+
if (!audit.extendedInfo) {
179+
return;
180+
}
181+
if (!audit.extendedInfo.formatter) {
182+
// HTML formatter not provided for this subItem
183+
return;
184+
}
185+
const formatter = Formatter.getByName(audit.extendedInfo.formatter);
186+
const helpers = formatter.getHelpers();
187+
if (helpers) {
188+
Handlebars.registerHelper(helpers);
189+
}
190+
191+
Handlebars.registerPartial(audit.name, formatter.getFormatter('html'));
192+
});
193+
174194
results.aggregations.forEach(aggregation => {
175195
aggregation.score.forEach(score => {
176-
score.subItems.forEach(subItem => {
177-
if (!subItem.extendedInfo) {
178-
return;
179-
}
180-
if (!subItem.extendedInfo.formatter) {
181-
// HTML formatter not provided for this subItem
182-
return;
183-
}
184-
const formatter = Formatter.getByName(subItem.extendedInfo.formatter);
185-
const helpers = formatter.getHelpers();
186-
if (helpers) {
187-
Handlebars.registerHelper(helpers);
188-
}
189-
190-
Handlebars.registerPartial(subItem.name, formatter.getFormatter('html'));
191-
});
196+
// Map subItem strings to auditResults from results.audits.
197+
// Coming soon events are not in auditResults, but rather still in subItems.
198+
score.subItems = score.subItems.map(subItem => results.audits[subItem] || subItem);
192199
});
193200
});
194201

lighthouse-core/runner.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,20 @@ class Runner {
8383
// Only run aggregations if needed.
8484
if (config.aggregations) {
8585
run = run
86-
.then(results => Aggregator.aggregate(config.aggregations, results))
87-
.then(aggregations => {
86+
.then(auditResults => Promise.all([
87+
auditResults,
88+
Aggregator.aggregate(config.aggregations, auditResults)
89+
]))
90+
.then(results => {
91+
const audits = results[0];
92+
const aggregations = results[1];
93+
const formattedAudits = audits.reduce((formatted, audit) => {
94+
formatted[audit.name] = audit;
95+
return formatted;
96+
}, {});
8897
return {
8998
url: opts.url,
99+
audits: formattedAudits,
90100
aggregations
91101
};
92102
});

lighthouse-core/test/aggregator/aggregate.js

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,10 @@ describe('Aggregate', () => {
251251
overall: 0.375,
252252
name: undefined,
253253
description: undefined,
254-
subItems: [{
255-
name: 'test',
256-
rawValue: false,
257-
score: false,
258-
displayValue: ''
259-
},
260-
{
261-
name: 'alternate-test',
262-
rawValue: 50,
263-
score: 50,
264-
displayValue: '50'
265-
}]
254+
subItems: [
255+
'test',
256+
'alternate-test'
257+
]
266258
});
267259
});
268260

@@ -297,18 +289,10 @@ describe('Aggregate', () => {
297289
overall: 0,
298290
name: undefined,
299291
description: undefined,
300-
subItems: [{
301-
name: 'test',
302-
rawValue: false,
303-
score: false,
304-
displayValue: ''
305-
},
306-
{
307-
name: 'alternate-test',
308-
rawValue: 50,
309-
score: 50,
310-
displayValue: '50'
311-
}]
292+
subItems: [
293+
'test',
294+
'alternate-test'
295+
]
312296
});
313297
});
314298

lighthouse-core/test/results/sample.json

Lines changed: 469 additions & 543 deletions
Large diffs are not rendered by default.

lighthouse-core/test/runner.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ describe('Runner', () => {
220220

221221
return Runner.run(fakeDriver, {url, config, flags}).then(results => {
222222
assert.equal(results.url, url);
223+
assert.equal(results.audits['is-on-https'].name, 'is-on-https');
223224
assert.equal(results.aggregations[0].score[0].overall, 1);
225+
assert.equal(results.aggregations[0].score[0].subItems[0], 'is-on-https');
224226
});
225227
});
226228
});

0 commit comments

Comments
 (0)