From 5a751cf4b16045492477804c4de1e2e60809dcd6 Mon Sep 17 00:00:00 2001 From: Tobias Lidskog Date: Mon, 9 May 2016 22:47:55 +0200 Subject: [PATCH] Structure assets data similar to domains data. --- lib/plugins/assets/aggregator.js | 9 ++++----- lib/plugins/assets/index.js | 7 +++++-- lib/plugins/html/index.js | 11 ++++++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/plugins/assets/aggregator.js b/lib/plugins/assets/aggregator.js index 057cb2e28..2973af3f1 100644 --- a/lib/plugins/assets/aggregator.js +++ b/lib/plugins/assets/aggregator.js @@ -1,9 +1,9 @@ 'use strict'; +const assets = {}; + module.exports = { - assets: {}, addToAggregate(data) { - var assets = this.assets; data.assets.forEach(function(asset) { const url = asset.url; const urlInfo = assets[url] || { @@ -14,12 +14,11 @@ module.exports = { size: asset.contentSize, requestCount: 0 }; - urlInfo.requestCount += 1; + urlInfo.requestCount++; assets[url] = urlInfo; }); }, summarize() { - const urls = Object.keys(this.assets); - return urls.map((url) => this.assets[url]); + return assets; } }; diff --git a/lib/plugins/assets/index.js b/lib/plugins/assets/index.js index c729e4b23..1cef012b2 100644 --- a/lib/plugins/assets/index.js +++ b/lib/plugins/assets/index.js @@ -2,12 +2,15 @@ let messageMaker = require('../../support/messageMaker'), path = require('path'), + isNotEmpty = require('../../support/util').isNotEmpty, aggregator = require('./aggregator'); const make = messageMaker('assets').make; module.exports = { - name() { return path.basename(__dirname); }, + name() { + return path.basename(__dirname); + }, processMessage(message, queue) { switch (message.type) { case 'pagexray.run': @@ -19,7 +22,7 @@ module.exports = { case 'summarize': { const summary = aggregator.summarize(); - if (summary.length > 0) { + if (isNotEmpty(summary)) { queue.postMessage(make('assets.aggregate', summary)); } break; diff --git a/lib/plugins/html/index.js b/lib/plugins/html/index.js index c542798d2..6c8d3c105 100644 --- a/lib/plugins/html/index.js +++ b/lib/plugins/html/index.js @@ -42,9 +42,14 @@ module.exports = { case 'assets.aggregate': { - const count = 10, - fullCount = Object.keys(message.data).length, - topAssets = message.data + const assetList = reduce(message.data, (assetList, asset) => { + assetList.push(asset); + return assetList; + }, []); + + const count = 20, + fullCount = Object.keys(assetList).length, + topAssets = assetList .sort((a, b) => b.requestCount - a.requestCount) .splice(0, count); return this.HTMLBuilder.addDataForSummaryPage('assets', {topAssets, count, fullCount});