Skip to content

Commit

Permalink
Merge pull request #260 from gemini-testing/merge-stats-per-browser
Browse files Browse the repository at this point in the history
fix: merge stats per browser
  • Loading branch information
CatWithApple authored Aug 23, 2019
2 parents 5d7dcef + 0a066df commit 1af4009
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 119 deletions.
80 changes: 60 additions & 20 deletions lib/merge-reports/data-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const _ = require('lodash');
const Promise = require('bluebird');
const fs = require('fs-extra');
const {findNode, setStatusForBranch} = require('../static/modules/utils');
const {getDataFrom, getStatNameForStatus, getImagePaths} = require('./utils');
const {collectBrowsersFrom, getStatNameForStatus, getImagePaths} = require('./utils');
const {logger} = require('../server-utils');

const defaults = (data) => _.defaults(data, {suites: [], skips: [], config: {}});
Expand Down Expand Up @@ -88,7 +88,7 @@ module.exports = class DataTree {
setStatusForBranch(this._data.suites, parentSuitePath);
}

this._mergeStatistics(suite);
this._mergeSuiteStatistics(suite);
await this._moveImages(suite, {fromFields: ['result', 'retries']});
}

Expand All @@ -99,16 +99,15 @@ module.exports = class DataTree {

setStatusForBranch(this._data.suites, suitePath);

this._mergeStatistics(bro);
this._mergeBrowserStatistics(bro);
await this._moveImages(bro, {fromFields: ['result', 'retries']});
}

_moveTestResultToRetries(existentBro) {
existentBro.retries.push(existentBro.result);

this._data.retries += 1;
const statName = getStatNameForStatus(existentBro.result.status);
this._data[statName] -= 1;
this._increaseRetries(existentBro.name);
this._decreaseStats(existentBro.name, existentBro.result.status);
}

async _addTestRetries(existentBro, retries) {
Expand All @@ -122,32 +121,34 @@ module.exports = class DataTree {
retry = this._changeFieldsWithAttempt(retry, {newAttempt});

existentBro.retries.push(retry);
this._data.retries += 1;
this._increaseRetries(existentBro.name);
}

async _changeTestResult(existentBro, result, suitePath) {
await this._moveImages(result, {newAttempt: existentBro.retries.length});
existentBro.result = this._changeFieldsWithAttempt(result, {newAttempt: existentBro.retries.length});

const statName = getStatNameForStatus(existentBro.result.status);
this._data[statName] += 1;
this._increaseStats(existentBro.name, existentBro.result.status);

setStatusForBranch(this._data.suites, suitePath);
}

_mergeStatistics(node) {
const testResultStatuses = getDataFrom(node, {fieldName: 'status', fromFields: 'result'});
_mergeSuiteStatistics(suite) {
const browsers = collectBrowsersFrom(suite);

testResultStatuses.forEach((testStatus) => {
const statName = getStatNameForStatus(testStatus);
if (this._data.hasOwnProperty(statName)) {
this._data.total += 1;
this._data[statName] += 1;
}
});
for (const bro of browsers) {
this._mergeBrowserStatistics(bro);
}
}

const testRetryStatuses = getDataFrom(node, {fieldName: 'status', fromFields: 'retries'});
this._data.retries += testRetryStatuses.length;
_mergeBrowserStatistics(bro) {
if (getStatNameForStatus(_.get(bro, 'result.status'))) {
this._increaseTotal(bro.name);
this._increaseStats(bro.name, bro.result.status);
}
if (bro.retries && bro.retries.length > 0) {
this._increaseRetries(bro.name, bro.retries.length);
}
}

async _moveImages(node, {newAttempt, fromFields}) {
Expand Down Expand Up @@ -188,4 +189,43 @@ module.exports = class DataTree {
const existentNode = findNode(this._data.suites, suitePath);
return _.find(_.get(existentNode, 'browsers'), {name: browserId});
}

_changeStatsByStatName(browserName, statName, value) {
this._data[statName] += value;

// stats per browser
if (!this._data.hasOwnProperty('perBrowser')) {
this._data.perBrowser = {};
}
if (!this._data.perBrowser.hasOwnProperty(browserName)) {
this._data.perBrowser[browserName] = {
'total': 0,
'passed': 0,
'failed': 0,
'skipped': 0,
'retries': 0
};
}
this._data.perBrowser[browserName][statName] += value;
}

_increaseRetries(browserName, value = 1) {
this._changeStatsByStatName(browserName, 'retries', value);
}

_increaseTotal(browserName) {
this._changeStatsByStatName(browserName, 'total', 1);
}

_increaseStats(browserName, status) {
const statName = getStatNameForStatus(status);

this._changeStatsByStatName(browserName, statName, 1);
}

_decreaseStats(browserName, status) {
const statName = getStatNameForStatus(status);

this._changeStatsByStatName(browserName, statName, -1);
}
};
25 changes: 16 additions & 9 deletions lib/merge-reports/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
const _ = require('lodash');
const {SUCCESS, FAIL, ERROR, SKIPPED} = require('../constants/test-statuses');

const statusToStat = {
[SUCCESS]: 'passed',
[FAIL]: 'failed',
[ERROR]: 'failed',
[SKIPPED]: 'skipped'
};

function collectBrowsersFrom(node) {
const browsers = node.browsers ? node.browsers : [];
const browsersFromChildren = node.children ? _.flatten(node.children.map((child) => collectBrowsersFrom(child))) : [];

return [...browsers, ...browsersFromChildren];
}

function getDataFrom(node, {fieldName, fromFields}) {
if (!fromFields) {
return [].concat(_.get(node, fieldName, []));
Expand All @@ -23,13 +37,6 @@ function getImagePaths(node, fromFields) {
}

function getStatNameForStatus(status) {
const statusToStat = {
[SUCCESS]: 'passed',
[FAIL]: 'failed',
[ERROR]: 'failed',
[SKIPPED]: 'skipped'
};

return statusToStat[status];
}

Expand All @@ -41,7 +48,7 @@ function walk(node, cb, fn) {
}

module.exports = {
getDataFrom,
getImagePaths,
getStatNameForStatus
getStatNameForStatus,
collectBrowsersFrom
};
Loading

0 comments on commit 1af4009

Please sign in to comment.