Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions server/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { log, getPRNumber } = require('./utils');
const db = require('./db');
const gh = require('./github');
const printDeltaTable = require('./delta-table');
const { sumSizesOf, ZERO_SIZE } = require('./delta');

const REPO = 'Automattic/wp-calypso';
const WATERMARK = 'c52822';
Expand Down Expand Up @@ -35,6 +36,49 @@ function groupByArea(deltas) {
});
}

function totalDeltasForArea(areaDelta, delta) {
if (!areaDelta) {
return {...ZERO_SIZE};
}

// Produce an array of arrays:
// [ [ chunks in use in first commit ] , [ chunks in use in second commit ] ]
// The items will be unique inside each array.
const chunksInUse = ['firstChunks', 'secondChunks']
.map(chunkType => areaDelta.reduce(
(acc, group) => {
for (const chunk of group[chunkType]) {
acc.add(chunk);
}
return acc;
},
new Set()
))
.map(set => [...set]);

// Produce an array of size objects, representing the sum of all the chunks for each commit:
// [ { stat_size: 0, parsed_size: 0, gzip_size: 0 }, { stat_size: 0, parsed_size: 0, gzip_size: 0 } ]
// The first object is for the first commit, and the second object for the second commit.
const chunkSizes = ['firstSizes', 'secondSizes']
.map((property, index) => chunksInUse[index].reduce(
(acc, chunkName) => {
const chunk = delta.chunks.find(chunk => chunk.name === chunkName) || {};
acc = sumSizesOf(acc, chunk[property]);
return acc;
},
{...ZERO_SIZE}
));

// Produce a single object with the delta between first and second commit:
// { stat_size: 0, parsed_size: 0, gzip_size: 0 }
let deltaSizes = {};
for (const sizeType in chunkSizes[0]) {
deltaSizes[sizeType] = chunkSizes[1][sizeType] - chunkSizes[0][sizeType];
}

return deltaSizes;
}

const AREAS = [
{
id: 'runtime',
Expand Down Expand Up @@ -90,8 +134,11 @@ function watermarkString(watermark) {
return `icfy-watermark: ${watermark}`;
}

async function statsMessage(push) {
const delta = await db.getPushDelta(push.ancestor, push.sha, { extractManifestGroup: true });
async function getDelta(push) {
return await db.getPushDelta(push.ancestor, push.sha, { extractManifestGroup: true });
}

function statsMessage(delta) {
const byArea = groupByArea(delta.groups);

const message = [];
Expand All @@ -112,7 +159,7 @@ async function statsMessage(push) {
continue;
}

const bytesDelta = _.reduce(areaDelta, (sum, delta) => sum + delta.deltaSizes.gzip_size, 0);
const bytesDelta = totalDeltasForArea(areaDelta, delta).gzip_size || 0;
const changedBytes = Math.abs(bytesDelta);
const suffix = bytesDelta < 0 ? 'removed 📉' : 'added 📈';

Expand Down Expand Up @@ -187,7 +234,7 @@ module.exports = async function commentOnGithub(sha) {

const [firstComment, ...otherComments] = await getOurPRCommentIDs(REPO, prNumber);

const message = await statsMessage(push);
const message = statsMessage(await getDelta(push));

if (!firstComment) {
log('Posting first comment on PR', prNumber);
Expand Down
11 changes: 11 additions & 0 deletions server/delta.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require('lodash');

const sizes = ['stat_size', 'parsed_size', 'gzip_size'];
const ZERO_SIZE = sizes.reduce((acc, size) => ({...acc, [size]: 0}), {});

function sizesOf(stat) {
return stat ? _.pick(stat, sizes) : null;
Expand Down Expand Up @@ -225,6 +226,8 @@ function deltaFromStatsAndGroups(firstStats, firstGroups, secondStats, secondGro
const secondSizes = sizesOfGroup(secondGroup, secondStats);
const deltaSizes = deltaSizesOf(firstSizes, secondSizes);
const deltaPercents = deltaPercentsOf(firstSizes, deltaSizes);
const firstChunks = (firstGroup || {}).chunks || [];
const secondChunks = (secondGroup || {}).chunks || [];

if (isDeltaEligible(deltaSizes)) {
deltas.push({
Expand All @@ -233,6 +236,8 @@ function deltaFromStatsAndGroups(firstStats, firstGroups, secondStats, secondGro
secondSizes,
deltaSizes,
deltaPercents,
firstChunks,
secondChunks,
});
}
}
Expand All @@ -243,12 +248,16 @@ function deltaFromStatsAndGroups(firstStats, firstGroups, secondStats, secondGro
const firstSizes = null;
const secondSizes = sizesOfGroup(secondGroup, secondStats);
const deltaSizes = deltaSizesOf(firstSizes, secondSizes);
const firstChunks = [];
const secondChunks = secondGroup.chunks || [];

deltas.push({
name,
firstSizes,
secondSizes,
deltaSizes,
firstChunks,
secondChunks,
});
}
}
Expand All @@ -258,3 +267,5 @@ function deltaFromStatsAndGroups(firstStats, firstGroups, secondStats, secondGro

exports.deltaFromStats = deltaFromStats;
exports.deltaFromStatsAndGroups = deltaFromStatsAndGroups;
exports.sumSizesOf = sumSizesOf;
exports.ZERO_SIZE = ZERO_SIZE;