Skip to content

Commit

Permalink
Refactor the JSON output. (#567)
Browse files Browse the repository at this point in the history
The structure of the Lighthouse JSON output is different and better.

See #567 for details
  • Loading branch information
michaelgerakis authored and paulirish committed Aug 9, 2016
1 parent 0269917 commit 703ded6
Show file tree
Hide file tree
Showing 9 changed files with 1,000 additions and 1,136 deletions.
5 changes: 4 additions & 1 deletion lighthouse-cli/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function createOutput(results, outputMode) {

// JSON report.
if (outputMode === 'json') {
return JSON.stringify(results.aggregations, null, 2);
return JSON.stringify({audits: results.audits, aggregations: results.aggregations}, null, 2);
}

// Pretty printed.
Expand All @@ -99,6 +99,9 @@ function createOutput(results, outputMode) {
}

item.subItems.forEach(subitem => {
// Get audit object from inside of results.audits under name subitem.
// Coming soon events are not located inside of results.audits.
subitem = results.audits[subitem] || subitem;
let lineItem = ` -- ${subitem.description}: ${subitem.score}`;
if (subitem.displayValue) {
lineItem += ` (${subitem.displayValue})`;
Expand Down
1,024 changes: 478 additions & 546 deletions lighthouse-cli/test/fixtures/sample.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lighthouse-core/aggregator/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Aggregate {
return;
}

subItems.push(filteredAndRemappedResults[e]);
subItems.push(filteredAndRemappedResults[e].name);

// Only add to the score if this aggregation contributes to the
// overall score.
Expand Down Expand Up @@ -199,13 +199,13 @@ class Aggregate {
* @param {!Array<!AuditResult>} results
* @return {!AggregationResult}
*/
static aggregate(aggregation, results) {
static aggregate(aggregation, auditResults) {
return {
name: aggregation.name,
description: aggregation.description,
scored: aggregation.scored,
categorizable: aggregation.categorizable,
score: Aggregate.compare(results, aggregation.items, aggregation.scored)
score: Aggregate.compare(auditResults, aggregation.items, aggregation.scored)
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/closure/typedefs/Aggregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ AggregationResultItem.prototype.name;
/** @type {string} */
AggregationResultItem.prototype.description;

/** @type {!Array<!AuditResult>} */
/** @type {!Array<!string>} */
AggregationResultItem.prototype.subItems;

/**
Expand Down
39 changes: 23 additions & 16 deletions lighthouse-core/report/report-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,31 @@ class ReportGenerator {
const inline = (options && options.inline) || false;

// Ensure the formatter for each extendedInfo is registered.
Object.keys(results.audits).forEach(audit => {
// Use value rather than key for audit.
audit = results.audits[audit];

if (!audit.extendedInfo) {
return;
}
if (!audit.extendedInfo.formatter) {
// HTML formatter not provided for this subItem
return;
}
const formatter = Formatter.getByName(audit.extendedInfo.formatter);
const helpers = formatter.getHelpers();
if (helpers) {
Handlebars.registerHelper(helpers);
}

Handlebars.registerPartial(audit.name, formatter.getFormatter('html'));
});

results.aggregations.forEach(aggregation => {
aggregation.score.forEach(score => {
score.subItems.forEach(subItem => {
if (!subItem.extendedInfo) {
return;
}
if (!subItem.extendedInfo.formatter) {
// HTML formatter not provided for this subItem
return;
}
const formatter = Formatter.getByName(subItem.extendedInfo.formatter);
const helpers = formatter.getHelpers();
if (helpers) {
Handlebars.registerHelper(helpers);
}

Handlebars.registerPartial(subItem.name, formatter.getFormatter('html'));
});
// Map subItem strings to auditResults from results.audits.
// Coming soon events are not in auditResults, but rather still in subItems.
score.subItems = score.subItems.map(subItem => results.audits[subItem] || subItem);
});
});

Expand Down
14 changes: 12 additions & 2 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,20 @@ class Runner {
// Only run aggregations if needed.
if (config.aggregations) {
run = run
.then(results => Aggregator.aggregate(config.aggregations, results))
.then(aggregations => {
.then(auditResults => Promise.all([
auditResults,
Aggregator.aggregate(config.aggregations, auditResults)
]))
.then(results => {
const audits = results[0];
const aggregations = results[1];
const formattedAudits = audits.reduce((formatted, audit) => {
formatted[audit.name] = audit;
return formatted;
}, {});
return {
url: opts.url,
audits: formattedAudits,
aggregations
};
});
Expand Down
32 changes: 8 additions & 24 deletions lighthouse-core/test/aggregator/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,10 @@ describe('Aggregate', () => {
overall: 0.375,
name: undefined,
description: undefined,
subItems: [{
name: 'test',
rawValue: false,
score: false,
displayValue: ''
},
{
name: 'alternate-test',
rawValue: 50,
score: 50,
displayValue: '50'
}]
subItems: [
'test',
'alternate-test'
]
});
});

Expand Down Expand Up @@ -297,18 +289,10 @@ describe('Aggregate', () => {
overall: 0,
name: undefined,
description: undefined,
subItems: [{
name: 'test',
rawValue: false,
score: false,
displayValue: ''
},
{
name: 'alternate-test',
rawValue: 50,
score: 50,
displayValue: '50'
}]
subItems: [
'test',
'alternate-test'
]
});
});

Expand Down
1,012 changes: 469 additions & 543 deletions lighthouse-core/test/results/sample.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lighthouse-core/test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ describe('Runner', () => {

return Runner.run(fakeDriver, {url, config, flags}).then(results => {
assert.equal(results.url, url);
assert.equal(results.audits['is-on-https'].name, 'is-on-https');
assert.equal(results.aggregations[0].score[0].overall, 1);
assert.equal(results.aggregations[0].score[0].subItems[0], 'is-on-https');
});
});
});

0 comments on commit 703ded6

Please sign in to comment.