Skip to content

Commit

Permalink
filter unneeded aggregations from config before extension runs (Googl…
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny authored and andrewrota committed Jan 13, 2017
1 parent 24141d6 commit b7f7d64
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 41 deletions.
69 changes: 49 additions & 20 deletions lighthouse-extension/app/src/lighthouse-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,59 @@ const STORAGE_KEY = 'lighthouse_audits';
const _flatten = arr => [].concat.apply([], arr);

/**
* @param {!Connection} connection
* @param {string} url
* @param {!Object} options Lighthouse options.
* @param {!Array<string>} requestedAuditNames Names of audits to run.
* @return {!Promise}
* Filter out any unrequested aggregations from the config. If any audits are
* no longer needed by any remaining aggregations, filter out those as well.
* @param {!Object} config Lighthouse config object.
* @param {!Object<boolean>} requestedAggregations
*/
window.runLighthouseForConnection = function(connection, url, options, requestedAuditNames) {
// Always start with a freshly parsed default config.
const runConfig = JSON.parse(JSON.stringify(defaultConfig));
function filterConfig(config, requestedAggregations) {
config.aggregations = config.aggregations.filter(aggregation => {
// First filter out single `item` aggregations, which use top level name.
if (aggregation.items.length === 1) {
return requestedAggregations[aggregation.name];
}

// Next, filter the `items` array of aggregations with multiple sub-aggregations.
aggregation.items = aggregation.items.filter(item => {
return requestedAggregations[item.name];
});

// Finally, filter out any aggregations with no sub-aggregations remaining.
return aggregation.items.length > 0;
});

// Find audits required for remaining aggregations.
const requestedItems = _flatten(config.aggregations.map(aggregation => aggregation.items));
const auditsArray = _flatten(requestedItems.map(item => Object.keys(item.audits)));
const requestedAuditNames = new Set(auditsArray);

// The config file has a list of paths of audits to run. `requestedAuditNames`
// is a list of audit names to run. Map audit paths to audit names, then
// The `audits` property in the config is a list of paths of audits to run.
// `requestedAuditNames` is a list of audit *names*. Map paths to names, then
// filter out any paths of audits with names that weren't requested.
requestedAuditNames = new Set(requestedAuditNames);
const auditPathToName = new Map(Config.requireAudits(runConfig.audits)
const auditPathToName = new Map(Config.requireAudits(config.audits)
.map((AuditClass, index) => {
const auditPath = runConfig.audits[index];
const auditPath = config.audits[index];
const auditName = AuditClass.meta.name;
return [auditPath, auditName];
}));
runConfig.audits = runConfig.audits.filter(auditPath => {
config.audits = config.audits.filter(auditPath => {
const auditName = auditPathToName.get(auditPath);
return requestedAuditNames.has(auditName);
});
}

/**
* @param {!Connection} connection
* @param {string} url
* @param {!Object} options Lighthouse options.
* @param {!Object<boolean>} requestedAggregations Names of aggregations to include.
* @return {!Promise}
*/
window.runLighthouseForConnection = function(connection, url, options, requestedAggregations) {
// Always start with a freshly parsed default config.
const runConfig = JSON.parse(JSON.stringify(defaultConfig));

filterConfig(runConfig, requestedAggregations);
const config = new Config(runConfig);

// Add url and config to fresh options object.
Expand All @@ -64,15 +93,15 @@ window.runLighthouseForConnection = function(connection, url, options, requested

/**
* @param {!Object} options Lighthouse options.
* @param {!Array<string>} requestedAuditNames Names of audits to run.
* @param {!Object<boolean>} requestedAggregations Names of aggregations to include.
* @return {!Promise}
*/
window.runLighthouseInExtension = function(options, requestedAuditNames) {
window.runLighthouseInExtension = function(options, requestedAggregations) {
// Default to 'info' logging level.
log.setLevel('info');
const connection = new ExtensionProtocol();
return connection.getCurrentTabURL()
.then(url => window.runLighthouseForConnection(connection, url, options, requestedAuditNames))
.then(url => window.runLighthouseForConnection(connection, url, options, requestedAggregations))
.then(results => {
const blobURL = window.createReportPageAsBlob(results, 'extension');
chrome.tabs.create({url: blobURL});
Expand All @@ -83,14 +112,14 @@ window.runLighthouseInExtension = function(options, requestedAuditNames) {
* @param {!RawProtocol.Port} port
* @param {string} url
* @param {!Object} options Lighthouse options.
* @param {!Array<string>} requestedAuditNames Names of audits to run.
* @param {!Object<boolean>} requestedAggregations Names of aggregations to include.
* @return {!Promise}
*/
window.runLighthouseInWorker = function(port, url, options, requestedAuditNames) {
window.runLighthouseInWorker = function(port, url, options, requestedAggregations) {
// Default to 'info' logging level.
log.setLevel('info');
const connection = new RawProtocol(port);
return window.runLighthouseForConnection(connection, url, options, requestedAuditNames);
return window.runLighthouseForConnection(connection, url, options, requestedAggregations);
};

/**
Expand Down
23 changes: 2 additions & 21 deletions lighthouse-extension/app/src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
'use strict';

const _flatten = arr => [].concat.apply([], arr);

document.addEventListener('DOMContentLoaded', _ => {
const background = chrome.extension.getBackgroundPage();
const defaultAggregations = background.getDefaultAggregations();
Expand Down Expand Up @@ -121,22 +119,6 @@ document.addEventListener('DOMContentLoaded', _ => {
return frag;
}

/**
* Returns an array of names of audits from the selected aggregation
* categories.
* @param {!Object<boolean>} selectedAggregations
* @return {!Array<string>}
*/
function getAuditsFromSelected(selectedAggregations) {
const auditLists = defaultAggregations.filter(aggregation => {
return selectedAggregations[aggregation.name];
}).map(selectedAggregation => {
return selectedAggregation.audits;
});

return _flatten(auditLists);
}

background.listenForStatus(logstatus);
background.loadSelectedAggregations().then(aggregations => {
const frag = generateOptionsList(optionsList, aggregations);
Expand All @@ -148,14 +130,13 @@ document.addEventListener('DOMContentLoaded', _ => {
feedbackEl.textContent = '';

background.loadSelectedAggregations()
.then(getAuditsFromSelected)
.then(selectedAudits => {
.then(selectedAggregations => {
return background.runLighthouseInExtension({
flags: {
disableCpuThrottling: true
},
restoreCleanState: true
}, selectedAudits);
}, selectedAggregations);
})
.catch(err => {
let message = err.message;
Expand Down

0 comments on commit b7f7d64

Please sign in to comment.