Datacollector break free (#1767)

This commit is contained in:
Peter Hedenskog 2017-10-29 19:57:00 +01:00 committed by GitHub
parent 27717ff413
commit ba1d40d9e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 109 additions and 38 deletions

View File

@ -6,11 +6,12 @@ const merge = require('lodash.merge'),
set = require('lodash.set');
class DataCollector {
constructor(context) {
this.resultUrls = context.resultUrls;
constructor(resultUrls) {
this.resultUrls = resultUrls;
this.urlRunPages = {};
this.urlPages = {};
this.summaryPages = {};
this.browsertimeScreenshots = false;
}
_addUrl(url) {
@ -73,6 +74,10 @@ class DataCollector {
set(this.urlPages[url], 'errors', errors);
}
useBrowsertimeScreenshots() {
this.browsertimeScreenshots = true;
}
addDataForUrl(url, typePath, data, runIndex) {
this._addUrl(url);

View File

@ -172,7 +172,8 @@ class HTMLBuilder {
runPages,
summaryPageHAR,
medianRun,
browser
browser,
hasScreenShots: dataCollector.browsertimeScreenshots
}).tap(() =>
Promise.resolve(Object.keys(runPages)).map(runIndex =>
this._renderUrlRunPage(url, runIndex, {
@ -182,7 +183,8 @@ class HTMLBuilder {
pageInfo: runPages[runIndex],
options,
runPages,
browser
browser,
hasScreenShots: dataCollector.browsertimeScreenshots
})
)
);

View File

@ -5,7 +5,7 @@ const HTMLBuilder = require('./htmlBuilder');
const get = require('lodash.get');
const set = require('lodash.set');
const reduce = require('lodash.reduce');
const DataCollector = require('../../support/dataCollector');
const DataCollector = require('./dataCollector');
// lets keep this in the HTML context, since we need things from the
// regular options object in the output
@ -20,7 +20,7 @@ module.exports = {
this.make = context.messageMaker('html').make;
this.options = merge({}, defaultConfig, options);
this.HTMLBuilder = new HTMLBuilder(context, this.options);
this.dataCollector = new DataCollector(context);
this.dataCollector = new DataCollector(context.resultUrls);
this.maxAssets = get(options, 'maxAssets', 20);
},
processMessage(message, queue) {
@ -148,6 +148,10 @@ module.exports = {
dataCollector.addSummary('detailed', data);
break;
}
case 'browsertime.screenshot': {
dataCollector.useBrowsertimeScreenshots();
break;
}
case 'sitespeedio.render': {
return this.HTMLBuilder.render(dataCollector).then(() => {
queue.postMessage(make('html.finished'));

View File

@ -93,7 +93,7 @@ block content
td #{d.webpagetest.pageSummary.data.median.firstView.requestsFull}
.one-half.column
if d.browsertime && options.browsertime.screenshot && d.browsertime.pageSummary && d.browsertime.pageSummary.screenshots
if d.browsertime && hasScreenShots && d.browsertime.pageSummary && d.browsertime.pageSummary.screenshots
- var width = options.mobile ? 150 : '100%';
a(href='data/screenshots/0.png')
img.screenshot(src='data/screenshots/0.png', width=width, alt='Screenshot of run 1')

View File

@ -98,7 +98,7 @@ block content
.one-half.column
//- No good way to detect if we have screenshots or not right now for a run
if d.browsertime && options.browsertime.screenshot
if d.browsertime && hasScreenShots
- screenshotName = 'data/screenshots/' + runIndex + '.png'
- var width = options.mobile ? 150 : '100%';
a(href=screenshotName)

View File

@ -0,0 +1,65 @@
'use strict';
const merge = require('lodash.merge'),
get = require('lodash.get'),
set = require('lodash.set');
class DataCollector {
constructor(context) {
this.resultUrls = context.resultUrls;
this.urlRunPages = {};
this.urlPages = {};
this.summaryPage = {};
}
_addUrl(url) {
if (!this.urlPages[url]) {
this.urlPages[url] = {
path: this.resultUrls.relativeSummaryPageUrl(url),
data: {}
};
this.urlRunPages[url] = [];
}
}
getSummary() {
return this.summaryPage;
}
getURLs() {
return Object.keys(this.urlPages);
}
getURLData(url) {
return this.urlPages[url];
}
addErrorForUrl(url, source, data) {
this._addUrl(url);
const errors = get(this.urlPages[url], 'errors', {});
errors[source] = data;
set(this.urlPages[url], 'errors', errors);
}
addDataForUrl(url, typePath, data, runIndex) {
this._addUrl(url);
if (runIndex !== undefined) {
let runData = this.urlRunPages[url][runIndex] || {
runIndex,
data: {}
};
set(runData.data, typePath, data);
this.urlRunPages[url][runIndex] = runData;
} else {
set(this.urlPages[url].data, typePath, data);
}
}
addSummary(data) {
merge(this.summaryPage, data);
}
}
module.exports = DataCollector;

View File

@ -6,7 +6,7 @@ const log = require('intel').getLogger('sitespeedio.plugin.slack');
const Slack = require('node-slack');
const merge = require('lodash.merge');
const set = require('lodash.set');
const DataCollector = require('../../support/dataCollector');
const DataCollector = require('./dataCollector');
const getAttachments = require('./attachements');
const getSummary = require('./summary');
@ -20,12 +20,11 @@ const defaultConfig = {
limitMetric: 'coachScore'
};
function send(options) {
function send(options, dataCollector, context) {
const slackOptions = merge({}, defaultConfig, options.slack);
const slack = new Slack(slackOptions.hookUrl);
const type = slackOptions.type;
const pageErrors = [];
const dataCollector = this.dataCollector;
let logo = 'https://www.sitespeed.io/img/slack/sitespeed-logo-slack.png';
let channel = slackOptions.channel;
@ -46,8 +45,8 @@ function send(options) {
const sum = getSummary(
dataCollector,
pageErrors,
this.resultUrls,
this.context,
context.resultUrls,
context.name,
options
);
text += sum.summaryText + '\n' + sum.errorText;
@ -56,7 +55,11 @@ function send(options) {
let attachments = [];
if (['url', 'all', 'error'].includes(type)) {
attachments = getAttachments(dataCollector, this.resultUrls, slackOptions);
attachments = getAttachments(
dataCollector,
context.resultUrls,
slackOptions
);
}
if ((type === 'error' && pageErrors.length > 0) || type !== 'error') {
@ -88,7 +91,6 @@ module.exports = {
open(context, options) {
throwIfMissing(options.slack, ['hookUrl', 'userName'], 'slack');
this.dataCollector = new DataCollector(context);
this.resultUrls = context.resultUrls;
this.context = context;
this.options = options;
},
@ -121,7 +123,7 @@ module.exports = {
case 'browsertime.summary': {
const data = {};
set(data, message.type, message.data);
dataCollector.addSummary('index', data);
dataCollector.addSummary(data);
break;
}
@ -129,7 +131,7 @@ module.exports = {
// if we have set a result base URL wait on
// the content to be uploaded
if (!this.options.resultBaseURL) {
return send(this.options);
return send(this.options, dataCollector, this.context);
}
break;
}
@ -137,7 +139,7 @@ module.exports = {
case 'gc.finished':
case 'ftp.finished':
case 's3.finished': {
return send(this.options);
return send(this.options, dataCollector, this.context);
}
}
},

View File

@ -3,8 +3,8 @@ const util = require('util');
const get = require('lodash.get');
const h = require('../../support/helpers');
module.exports = function(dataCollector, errors, resultUrls, context, options) {
const base = dataCollector.getSummary('index') || {};
module.exports = function(dataCollector, errors, resultUrls, name, options) {
const base = dataCollector.getSummary() || {};
const metrics = {
firstPaint: {
name: 'First paint',
@ -58,7 +58,7 @@ module.exports = function(dataCollector, errors, resultUrls, context, options) {
let summaryText =
`${h.plural(dataCollector.getURLs().length, 'page')} analyzed for ${h.short(
context.name,
name,
30
)} ` +
`(${h.plural(options.browsertime.iterations, 'run')}, ` +

View File

@ -1,23 +1,21 @@
'use strict';
const textBuilder = require('./textBuilder');
const DataCollector = require('../../support/dataCollector');
const merge = require('lodash.merge');
const set = require('lodash.set');
module.exports = {
open(context, options) {
this.dataCollector = new DataCollector(context);
this.metrics = {};
this.options = options;
this.context = context;
},
processMessage(message) {
const dataCollector = this.dataCollector;
if (message.type.endsWith('.summary')) {
const data = {};
set(data, message.type, message.data);
dataCollector.addSummary('text', data);
merge(this.metrics, data);
}
},
close(options) {
@ -25,6 +23,6 @@ module.exports = {
const renderer = this.options.summaryDetail
? textBuilder.renderSummary
: textBuilder.renderBriefSummary;
return renderer(this.dataCollector, this.context, options);
return renderer(this.metrics, this.context, options);
}
};

View File

@ -35,13 +35,8 @@ function getHeader(context, options) {
);
}
function getBoxes(dataCollector) {
return flatten(
chunk(
summaryBoxesSetup(dataCollector.getSummary('text')).filter(Boolean),
3
)
);
function getBoxes(metrics) {
return flatten(chunk(summaryBoxesSetup(metrics).filter(Boolean), 3));
}
// foo bar -> fb
@ -55,9 +50,9 @@ function noop(a) {
}
module.exports = {
renderSummary(dataCollector, context, options) {
renderSummary(metrics, context, options) {
let out = getHeader(context, options);
let rows = getBoxes(dataCollector).map(b => {
let rows = getBoxes(metrics).map(b => {
const marker = getMarker(b.label),
c = getColor(b.label);
// color.xterm(ansi)(label),
@ -70,14 +65,14 @@ module.exports = {
options.summary = { out: `${out}\n` + table(rows, tableOpts) };
},
renderBriefSummary(dataCollector, context, options) {
renderBriefSummary(metrics, context, options) {
let out = getHeader(context, options);
const lines = [],
scores = [];
let size = '',
reqs = '',
rum = '';
getBoxes(dataCollector).map(b => {
getBoxes(metrics).map(b => {
let c = getColor(b.label),
val = b.median,
name;