-
Notifications
You must be signed in to change notification settings - Fork 20
/
computeStats.js
65 lines (52 loc) · 1.92 KB
/
computeStats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var inputFileName = process.argv[2] || './data/dependenciesGraph.out.graph';
var count = process.argv[3] || 100;
var centrality = require('ngraph.centrality');
var fromjson = require('ngraph.fromjson');
var pagerank = require('ngraph.pagerank');
var hitsrank = require('ngraph.hits');
var fs = require('fs');
console.log('* Date: ' + (new Date().toUTCString()) );
console.log('* Input file: `' + inputFileName + '`');
console.log('');
console.log('# Top ' + count + ' most dependent upon packages');
var graph = fromjson(fs.readFileSync(inputFileName, 'utf8'));
printTop(centrality.degree(graph, 'in'), count);
console.log('# Top ' + count + ' packages with most dependencies');
printTop(centrality.degree(graph, 'out'), count);
console.log('# Top ' + count + ' packages with highest Pagerank');
printTop(pagerank(graph, 0.85, 1e-10), count);
console.log('# Top ' + count + ' packages with highest authority in HITS rank');
printTop(hitsrank(graph), count, hitsSort, hitsPrint);
console.log('');
console.log('Data generated by https://github.com/anvaka/npmrank');
function printTop(stats, count, sort, print) {
sort = sort || byValue;
print = print || toLink;
var allKeys = Object.keys(stats);
var idx = 0;
var packages = allKeys.sort(sortProxy)
.slice(0, Math.min(count, allKeys.length))
.map(mapProxy)
.join('\n');
console.log('');
console.log(packages);
console.log('');
function sortProxy(x, y) {
return sort(stats, x, y);
}
function mapProxy(x) {
return print(stats, x, idx++);
}
function byValue(stats, x, y) {
return stats[y] - stats[x];
}
function toLink(stats, x, idx) {
return idx + '. [' + x + '](https://www.npmjs.org/package/' + x + ') - ' + stats[x];
}
}
function hitsSort(stats, x, y) {
return stats[y].authority - stats[x].authority;
}
function hitsPrint(stats, x, idx) {
return idx + '. [' + x + '](https://www.npmjs.org/package/' + x + ') - ' + stats[x].authority;
}