Last active
June 9, 2016 05:18
-
-
Save tizzo/1511b846d9121cf3fae27789b89210a9 to your computer and use it in GitHub Desktop.
Probo CM Build Size Reporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var through2 = require('through2'); | |
var split2 = require('split2'); | |
/** | |
* The builds and organizations and project. | |
*/ | |
var buildData = { | |
totals: { | |
size: 0, | |
builds: 0, | |
}, | |
owner: {}, | |
project: {}, | |
}; | |
process.stdin | |
.pipe(split2(JSON.parse)) | |
.pipe(through2.obj(function(build, enc, cb) { | |
if (!build || !build.project || !build.project.name || !build.project.owner) { | |
console.error(`${id} had bad data!`); | |
return cb(); | |
} | |
var owner = `${build.project.provider.slug}:${build.project.owner}`; | |
buildData.owner[owner] = buildData.owner[owner] || { | |
projects: {}, | |
size: 0, | |
}; | |
buildData | |
buildData.owner[owner].id = build.project.organizationId; | |
buildData.owner[owner].projects[build.project.name] = buildData.owner[owner].projects[build.project.name] || {space: 0, builds: 0}; | |
buildData.owner[owner].projects[build.project.name].builds++; | |
if (build.diskSpace && build.diskSpace.realBytes) { | |
buildData.totals.size += build.diskSpace.realBytes; | |
buildData.totals.builds++; | |
buildData.owner[owner].projects[build.project.name].space += build.diskSpace.realBytes; | |
buildData.owner[owner]['size'] += build.diskSpace.realBytes; | |
} | |
else { | |
// console.log('NO container size data', build.container); | |
} | |
// {{ project.provider.slug }} {{ project.name }} {{ container.disk.containerSize }} | |
cb(); | |
}, function() { | |
renderOrganizaztionBuildData(buildData); | |
})) | |
function formatBytes(bytes,decimals) { | |
if(bytes == 0) return '0 Byte'; | |
var k = 1000; // or 1024 for binary | |
var dm = decimals + 1 || 3; | |
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | |
var i = Math.floor(Math.log(bytes) / Math.log(k)); | |
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; | |
} | |
function renderOrganizaztionBuildData(buildData) { | |
var results = arrayify(buildData.owner).sort(sizeSort); | |
console.log(`Total builds: ${buildData.totals.builds}`); | |
console.log(`Total size: ${formatBytes(buildData.totals.size)}`); | |
console.log(`Average build size: ${formatBytes(buildData.totals.size / buildData.totals.builds)}`); | |
for (let org of results) { | |
console.log(`${org.name} ${org.id} ${formatBytes(org.size, 2)}`); | |
for (let project of arrayify(org.projects)) { | |
console.log(` ${project.name} ${formatBytes(project.space, 2)} ${project.builds} builds at average size ${formatBytes(project.space / project.builds)}`); | |
} | |
} | |
} | |
function sizeSort(a, b) { | |
if (a.size < b.size) { | |
return 1; | |
} | |
if (a.size > b.size) { | |
return -1; | |
} | |
// a must be equal to b | |
return 0; | |
} | |
function arrayify(original) { | |
var output = []; | |
for (let key in original) { | |
if (original.hasOwnProperty(key)) { | |
let item = original[key]; | |
item.name = key; | |
output.push(item); | |
} | |
} | |
return output; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "usage", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"split2": "^2.1.0", | |
"through2": "^2.0.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment