This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
collectionSizes.js
117 lines (102 loc) · 2.49 KB
/
collectionSizes.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* loop on all collections and output their counts and size in bytes.
*
* Prints in a tabular format.
*
* Call as:
* $ mongo <databasename> collectionSizes.js
*/
var i, size, count,
collections = db.getCollectionNames();
totalCount = 0,
totalSize = 0,
totalUnused = 0;
print(renderLine('wrap'));
print(renderLine('header', db + " collections", "Count", "Unused (Bytes)", "Size (Bytes)"));
print(renderLine('divider'));
for(i in collections) {
if (typeof(db[collections[i]].totalSize) !== "function") {
continue;
}
count = db[collections[i]].count();
size = db[collections[i]].totalSize();
unused = db[collections[i]].storageSize() - db[collections[i]].dataSize();
print(renderLine('data', collections[i], count, unused, size));
totalCount += count;
totalSize += size;
totalUnused += unused;
}
print(renderLine('divider'));
print(renderLine('data', 'Totals:', totalCount, totalUnused, totalSize));
print(renderLine('wrap'));
percent = Math.round(totalUnused * 100 / totalSize);
print(percent + '% unused space');
/**
* A helper function to Render a line of the table
*
* @param type $type data, divider or wrap
* @param one $one
* @param two $two
* @param three $three
*
* @return a string
*/
function renderLine(type, one, two, three, four) {
var line = '',
widths = [36, 14, 20, 20],
padding = ['right', 'left', 'left', 'left'];
if (type === 'divider') {
return renderLine('data', '', '', '', '').replace(/ /g, '-');
}
if (type === 'wrap') {
return renderLine('data', '', '', '', '').replace(/./g, '-');
}
if (type === 'header') {
padding = ['center', 'center', 'center', 'center'];
}
return '| ' + pad(one, widths[0], padding[0]) + ' | ' + pad(two, widths[1], padding[1]) + ' | ' + pad(three, widths[2], padding[2]) + ' | ' + pad(four, widths[3], padding[3]) + ' |';
}
/**
* Pad the input string to the specified length
*
* dir defaults to left
* chr defaults to a space
*
* @param string str
* @param int length
* @param string dir (left, right or center)
* @param string chr
* @return string
* @access public
*/
function pad(str, length, dir, chr) {
if (!dir) {
dir = "left";
}
if (!chr) {
chr = " ";
}
str = "" + str;
if (dir === "left") {
while (str.length < length) {
str = chr + str;
}
return str;
}
if (dir === "center") {
var i = 0;
while (str.length < length) {
if (i % 2) {
str = str + chr;
} else {
str = chr + str;
}
i++;
}
return str;
}
while (str.length < length) {
str = str + chr;
}
return str;
}