Skip to content

Commit

Permalink
initial support for collation and archival of report data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrod Overson committed Mar 19, 2013
1 parent 2e5f43a commit 189d7e2
Show file tree
Hide file tree
Showing 72 changed files with 836 additions and 6,855 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tmp
node_module
node_modules
.idea
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module.exports = function(grunt) {
},
function(err, result, code){
console.log(result.stdout);
console.log(result.stderr);
if (err || code !== 0) {
grunt.fatal('Running plato binary failed');
}
Expand Down
29 changes: 29 additions & 0 deletions lib/models/FileHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

'use strict';

var History = require('./History');

module.exports = FileHistory;

function FileHistory(data) {
History.call(this, data);
}

FileHistory.prototype = Object.create(History.prototype);


FileHistory.prototype.addReport = function(report) {
var date = report.date || new Date().toUTCString();
this.push({
date : date,
sloc : report.complexity.aggregate.complexity.sloc.physical,
lloc : report.complexity.aggregate.complexity.sloc.logical,
functions : report.complexity.functions.length,
deliveredBugs : report.complexity.aggregate.complexity.halstead.bugs,
maintainability: report.maintainability,
lintErrors : report.jshint.messages.length,
difficulty: report.complexity.aggregate.complexity.halstead.difficulty
});
return this;
};

30 changes: 30 additions & 0 deletions lib/models/History.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

'use strict';

var _ = require('lodash');

module.exports = History;

function History(data) {
this.length = 0;
if (data && data.length) {
// using lodash to catch array-like objects
_.each(data, function(record, index){
this[index] = _.cloneDeep(record);
}.bind(this));
this.length = data.length;
}
}

History.prototype.push = function(obj) {
this[this.length] = obj;
this.length++;
};

History.prototype.toJSON = function() {
var obj = [];
_.each(this, function(val,index){
obj[index] = _.cloneDeep(val);
}.bind(this));
return obj;
};
30 changes: 30 additions & 0 deletions lib/models/OverviewHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

'use strict';

// local lib
var History = require('./History');

module.exports = OverviewHistory;

function OverviewHistory(data) {
History.call(this, data);
}

OverviewHistory.prototype = Object.create(History.prototype);

OverviewHistory.prototype.addReport = function(report) {
var date = report.date || new Date().toUTCString();
this.push({
date : date,
total : {
sloc : report.summary.total.sloc,
maintainability: report.summary.total.maintainability
},
average : {
sloc : report.summary.average.sloc,
maintainability: report.summary.average.maintainability
}
});
return this;
};

41 changes: 32 additions & 9 deletions lib/plato.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* plato
* https://github.com/joverson/plato
* https://github.com/jsoverson/plato
*
* Copyright (c) 2012 Jarrod Overson
* Licensed under the MIT license.
Expand All @@ -19,6 +19,8 @@ var _ = require('lodash');

// local lib
var util = require('./util'),
OverviewHistory = require('./models/OverviewHistory'),
FileHistory = require('./models/FileHistory'),
Logger = require('./logger'),
reporters = {
complexity : require('./reporters/complexity'),
Expand Down Expand Up @@ -96,7 +98,6 @@ exports.inspect = function(files, outputDir, options, done) {
error = true;
log.error('Error reading file : ', e.toString());
log.error(e.stack);
return;
}
});

Expand All @@ -119,12 +120,13 @@ exports.inspect = function(files, outputDir, options, done) {
} else {
fs.mkdirp(fileOutputDir,function(){
runReports(files,function(){
var reportFile = path.join(outputDir, 'report');
var reportFilePrefix = path.join(outputDir, 'report');
var overview = path.join(outputDir, 'index.html');

fs.copy(assets, path.join(outputDir, 'assets'), function(){
var overviewReport = exports.getOverviewReport(reports);
writeReport(reportFile, overviewReport);
updateHistoricalOverview(reportFilePrefix, overviewReport);
writeReport(reportFilePrefix, overviewReport);
writeOverview(overview, overviewReport, {
title : options.title,
flags : flags
Expand Down Expand Up @@ -180,19 +182,37 @@ exports.getOverviewReport = function (reports) {
};
};

function updateHistoricalOverview(outfilePrefix, overview) {
var existingData = util.readJSON(outfilePrefix + '.history.json') || {};
var history = new OverviewHistory(existingData);
history.addReport(overview);
writeReport(outfilePrefix + '.history', history.toJSON(), '__history');
}

function updateHistoricalReport(outfilePrefix, overview) {
var existingData = util.readJSON(outfilePrefix + '.history.json') || {};
var history = new FileHistory(existingData);
history.addReport(overview);
writeReport(outfilePrefix + '.history', history.toJSON(), '__history');
}



function writeFile(file, source) {
log.info('Writing file "%s".', file);
fs.writeFileSync(file, source, 'utf8');
}

function writeReport(outfile, report) {
function writeReport(outfilePrefix, report, exportName) {
var formatted = util.formatJSON(report);

writeFile(outfile + '.json', formatted);
writeFile(outfilePrefix + '.json', formatted);

var module = '__report = ' + formatted;
exportName = exportName || '__report';

writeFile(outfile + '.js', module);
var module = exportName + ' = ' + formatted;

writeFile(outfilePrefix + '.js', module);
}

function writeOverview(outfile, report, options) {
Expand All @@ -211,7 +231,10 @@ function writeFileReport(outdir, report, source) {
report : report
});
var indexPath = path.join(outdir,'index.html');
var outfilePrefix = path.join(outdir,'report');

writeFile(indexPath, parsed);
writeReport(path.join(outdir,'report'), report);
updateHistoricalReport(outfilePrefix, report, '__history');
writeReport(outfilePrefix, report);
}

22 changes: 21 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use strict';

// node api
var fs = require('fs');

// local lib
var Logger = require('./logger');

var log = new Logger(Logger.WARNING);

exports.findCommonBase = function(files) {
if (!files || files.length === 1) return '';
var first = files[0];
Expand All @@ -20,7 +28,19 @@ exports.formatJSON = function (report) {
return JSON.stringify(report, function(k,v){
if (k === 'identifiers') return ['__stripped__'];
return v;
},2);
});
};

exports.readJSON = function (file) {
var result;
log.debug('Parsing JSON from file %s', file);
try {
var src = fs.readFileSync(file);
result = JSON.parse(src);
} catch(e) {
log.warning('Could not parse JSON from file %s', file);
}
return result;
};

exports.stripComments = function (str) {
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"test": "grunt test"
},
"devDependencies": {
"grunt": "~0.4.0rc5",
"grunt-contrib-jshint": "~0.1.1rc5",
"grunt-contrib-nodeunit": "~0.1.2rc5",
"grunt-contrib-uglify": "~0.1.1rc5",
"grunt-casper": "~0.1.0"
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.3.0",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt-contrib-uglify": "~0.2.0",
"grunt-casper": "~0.1.1"
},
"keywords": [
"halstead",
Expand All @@ -48,7 +48,7 @@
"dependencies": {
"complexity-report": "~0.7.0",
"posix-getopt": "~1.0.0",
"lodash": "~1.0.0-rc.3",
"lodash": "~1.0.1",
"fs-extra": "~0.3.2",
"jshint": "~1.1.0"
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"date":"Tue, 19 Mar 2013 06:13:22 GMT","sloc":75,"lloc":54,"functions":7,"deliveredBugs":0.7123483448963878,"lintErrors":0,"difficulty":23.684523809523807},{"date":"Tue, 19 Mar 2013 06:13:34 GMT","sloc":75,"lloc":54,"functions":7,"deliveredBugs":0.7123483448963878,"lintErrors":0,"difficulty":23.684523809523807},{"date":"Tue, 19 Mar 2013 06:16:59 GMT","sloc":75,"lloc":54,"functions":7,"deliveredBugs":0.7123483448963878,"lintErrors":0,"difficulty":23.684523809523807},{"date":"Tue, 19 Mar 2013 06:17:09 GMT","sloc":75,"lloc":54,"functions":7,"deliveredBugs":0.7123483448963878,"lintErrors":0,"difficulty":23.684523809523807}]
Loading

0 comments on commit 189d7e2

Please sign in to comment.