Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Introducing a new Stats visitor

The stats visitor simply generates an HTML report file about the source
folder, giving metrics about the number of lines of codes, number of
files, and types of files
  • Loading branch information...
commit cb86ff1dd9bf86c337ba4b1255ca3b44f3e33b86 1 parent f8d97f7
Patrick Brosset authored
View
4 .minni
@@ -9,7 +9,7 @@ packman todo list
[ ] make it easier to pass configuration to visitors, even from extra custom command line args, so that for instance it's easy to run packman twice with the conditional visitor: packman -Dbrowser=IE7 and packman -Dbrowser=FF below)
[ ] Put in place custom visitors args from the command line: something like -Dkey1=value1 -Dkey2=value2 etc...
[ ] Do the same from the config itself, dedicated place in the config to store custom args, and the custom args from the command line will come in the same object too if conceptually, the whole config object is open)
-[ ] stats visitor, runs onEnd, outputs a txt file containing stats about the packaged files, how many pakaged files, their size and gzipped), how many files are inside each package ...
[ ] split further the files in lib utils, yaml, ...) for easier testing
-[x] would be good if visitors could also have a onBeforeResolve or something, to be able to treat the packages config even before its looked at by packman
+[x] would be good if visitors could also have a onBeforeResolve or something, to be able to treat the packages config even before its looked at by packman
+[x] stats visitor, runs onEnd, outputs a txt file containing stats about the packaged files, how many pakaged files, their size and gzipped), how many files are inside each package ...
View
3  package.json
@@ -23,7 +23,8 @@
"uglify-js": "",
"coffee-script": "",
"less": "",
- "mocha": ""
+ "mocha": "",
+ "bind": "0.1.5"
},
"devDependencies": {},
"scripts": {
View
18 test/stats.yaml
@@ -0,0 +1,18 @@
+source: ../ariatemplates/src
+destination: ./stats
+packages:
+ stats.html:
+ visitors:
+ - stats
+ files:
+ includes:
+ - "**/*"
+ excludes:
+ - ".git/**/*"
+ - "node_modules/**/*"
+ - ".*"
+ - "*sublime*"
+ - "**/*.gif"
+ - "**/*.png"
+ - "**/*.jpg"
+ - "**/*.jpeg"
View
96 visitors/stats-tpl.html
@@ -0,0 +1,96 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title>Stats for (:sourceFolder:)</title>
+ <link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
+ <style type="text/css">
+ .pie {
+ height: 500px;
+ }
+ </style>
+ </head>
+ <body>
+ <div class="container-fluid">
+ <h1>Stats for (:sourceFolder:)</h1>
+ <h2>(:nbOfFiles:) files / (:nbOfTotalLines:) lines (~ (:averageLinesPerFile:) per file)</h2>
+ <div class="row-fluid">
+ <div class="pie span6" id="fileSizePie"></div>
+ <div class="pie span6" id="fileTypesPie"></div>
+ </div>
+ </div>
+ <!-- ul>(:allFileStats ~
+ <li>File [:path:] has [:nb:] lines</li>
+ :)
+ </ul -->
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+ <script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>
+ <script type="text/javascript">
+ $(function () {
+ var fileTypesPie, fileSizePie;
+ $(document).ready(function() {
+ fileTypesPie = new Highcharts.Chart({
+ chart: {
+ renderTo: 'fileTypesPie',
+ plotBackgroundColor: null,
+ plotBorderWidth: null,
+ plotShadow: false
+ },
+ title: {
+ text: 'File types'
+ },
+ tooltip: {
+ pointFormat: '<b>{point.y} *.{point.name} files ({point.percentage}%)</b>',
+ percentageDecimals: 1
+ },
+ plotOptions: {
+ pie: {
+ allowPointSelect: false,
+ cursor: 'default'
+ }
+ },
+ series: [{
+ type: 'pie',
+ name: 'File types',
+ data: [
+ (:fileTypes ~
+ ['[:type:]', [:nb:]],
+ :)
+ ]
+ }]
+ });
+
+ fileSizePie = new Highcharts.Chart({
+ chart: {
+ renderTo: 'fileSizePie',
+ plotBackgroundColor: null,
+ plotBorderWidth: null,
+ plotShadow: false
+ },
+ title: {
+ text: 'File sizes'
+ },
+ tooltip: {
+ pointFormat: '<b>{point.y} files below {point.name} lines ({point.percentage}%)</b>',
+ percentageDecimals: 1
+ },
+ plotOptions: {
+ pie: {
+ allowPointSelect: false,
+ cursor: 'default'
+ }
+ },
+ series: [{
+ type: 'pie',
+ name: 'File sizes',
+ data: [
+ (:fileSizes ~
+ ['[:max:]', [:nb:]],
+ :)
+ ]
+ }]
+ });
+ });
+ });
+ </script>
+ </body>
+</html>
View
96 visitors/stats.js
@@ -0,0 +1,96 @@
+var fs = require('fs');
+var bind = require('bind');
+
+module.exports.onFileContent = function(callback, config, fileObject) {
+ var lineNb = fileObject.content.split('\n').length;
+
+ if(!fileObject.packageFile.stats) {
+ fileObject.packageFile.stats = [];
+ }
+
+ fileObject.packageFile.stats.push({
+ path: fileObject.path,
+ nb: lineNb
+ });
+
+ fileObject.content = '';
+ callback();
+};
+
+function getFileSizes(stats) {
+ var below10 = 0;
+ var below100 = 0;
+ var below500 = 0;
+ var below1000 = 0;
+ var above = 0;
+
+ for(var i = 0; i < stats.length; i++) {
+ if(stats[i].nb < 10) {
+ below10++;
+ } else if(stats[i].nb < 100) {
+ below100++;
+ } else if(stats[i].nb < 500) {
+ below500++;
+ } else if(stats[i].nb < 1000) {
+ below1000++;
+ } else {
+ above++;
+ }
+ }
+
+ return [
+ {max: 10, nb: below10},
+ {max: 100, nb: below100},
+ {max: 500, nb: below500},
+ {max: 1000, nb: below1000},
+ {max: Infinity, nb: above},
+ ];
+};
+
+function getFileTypes(stats) {
+ var exts = {};
+
+ for(var i = 0; i < stats.length; i++) {
+ var extPos = stats[i].path.lastIndexOf('.');
+ var ext = stats[i].path.substring(extPos + 1);
+ if(!exts[ext]) {
+ exts[ext] = 0;
+ }
+ exts[ext]++;
+ }
+
+ var fileTypes = [];
+ for(var type in exts) {
+ fileTypes.push({type: type, nb: exts[type]});
+ }
+ return fileTypes;
+};
+
+module.exports.onPackageEnd = function(callback, config, packageFileObject) {
+ var now = new Date();
+ packageFileObject.path = 'Stats ' + now + '.html';
+
+ var stats = packageFileObject.stats,
+ totalLines = 0;
+
+ for(var i = 0; i < stats.length; i ++) {
+ totalLines += stats[i].nb;
+ }
+
+ stats.sort(function(a, b) {
+ return b.nb - a.nb;
+ });
+
+ bind.toFile('./visitors/stats-tpl.html', {
+ sourceFolder: config.source,
+ allFileStats: stats,
+ nbOfFiles: stats.length,
+ nbOfTotalLines: totalLines,
+ averageLinesPerFile: Math.round(totalLines / stats.length),
+ fileSizes: getFileSizes(stats),
+ fileTypes: getFileTypes(stats)
+ }, function(html) {
+ packageFileObject.content = html;
+ callback();
+ });
+};
Please sign in to comment.
Something went wrong with that request. Please try again.