forked from medic/medic-bulk-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats
executable file
·69 lines (64 loc) · 1.4 KB
/
stats
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
66
67
68
69
#!/usr/bin/env node
var input = '',
stats;
var countParents = function(data) {
var parent = data.parent,
contact = data.contact;
if (!stats.type) {
stats.type = data.type;
}
if (!stats.name) {
stats.name = data.name;
}
if (!stats.id) {
stats.id = data._id;
}
if (!stats.rev) {
stats.rev = data._rev;
}
if (parent) {
stats.depth++;
countParents(parent);
}
if (contact) {
stats.depth++;
countParents(contact);
}
};
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
input += chunk;
}
});
process.stdin.on('end', function() {
var output = [],
data = JSON.parse(input);
if (data.rows) {
data = data.rows;
} else if (data.docs) {
data = data.docs;
} else if (data.constructor !== Array) {
data = [data];
}
data.forEach(function(row) {
if (!row || typeof row !== 'object') {
return;
}
stats = { depth: 0 };
stats.length = JSON.stringify(row).length;
countParents(row.doc || row);
output.push(stats);
});
if (output.length <= 1) {
console.log(JSON.stringify(output));
} else {
var totals = { docs: output.length, depth: 0, length: 0 };
output.forEach(function(stat) {
totals.depth += stat.depth;
totals.length += stat.length;
});
console.log(JSON.stringify(totals));
}
});