-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroundup.js
More file actions
220 lines (171 loc) · 5.85 KB
/
roundup.js
File metadata and controls
220 lines (171 loc) · 5.85 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
db = db.getSisterDB( "mongousage" )
// --- utils ----
function twoLetter(z){
x = '' + z;
if ( x.length < 2 )
return '0' + x;
return x;
}
function getWeek(x){
return x.week.year + '-' + twoLetter( x.week.month ) + '-' + twoLetter( x.week.day );
}
function getMonth(x){
return x.date.year + '-' + twoLetter( x.date.month );
}
function firstPiece(x, bulkOthers){
if ( ! x )
return "";
var idx = x.indexOf( "/" );
if ( idx == 0 ){
x = x.substring(1);
idx = x.indexOf( "/" );;
}
if ( idx < 0 ){
if (bulkOthers) {
return "unknown";
}
else {
return x;
}
}
return x.substring( 0 , idx );
}
function parseVersion( file ){
if ( ! file )
return null;
if ( typeof ( myre ) == "undefined" ){
myre = new RegExp( /(\d+\.\d+\.\d+)(.*)/ )
}
x = myre.exec( file )
if ( ! x )
return null;
var v = x[1];
if ( x[2].indexOf( "-rc" ) == 0 ){
v += x[2].substring( 0 , x[2].indexOf( "." ) );
}
return v;
}
assert.eq( "foo" , firstPiece( "/foo/asd" ) )
assert.eq( "foo" , firstPiece( "foo/asd" ) )
assert.eq( "foo" , firstPiece( "/foo/" ) )
assert.eq( "05" , twoLetter( "5" ) , "test1a" )
assert.eq( "05" , twoLetter( "05" ) , "tes1b")
assert.eq( "1.4.4" , parseVersion( "/win32/mongodb-win32-i386-1.4.4.zip" ) )
assert.eq( "1.4.4-rc2" , parseVersion( "/win32/mongodb-win32-i386-1.4.4-rc2.zip" ) )
db.system.js.save( { _id : "twoLetter" , value : twoLetter } );
db.system.js.save( { _id : "getWeek" , value : getWeek } );
db.system.js.save( { _id : "getMonth" , value : getMonth } );
db.system.js.save( { _id : "firstPiece" , value : firstPiece } );
db.system.js.save( { _id : "parseVersion" , value : parseVersion } );
db.downloads.ensureIndex( { day : 1 } )
// map/reduce helpers
simpleSum = function(k,values){
return Array.sum( values );
}
assert.eq( 5 , simpleSum( "x" , [ 1 , 4 ] ) )
function downloadSummary(){
// --- build ip tables ----
m = function(){
emit( { t : getMonth(this) , ip : this.ip } , 1 );
}
res = db.downloads.mapReduce( m , simpleSum , { out : "gen.monthly.ip" } )
m = function(){
emit( { t : getWeek(this) , ip : this.ip } , 1 );
}
res = db.downloads.mapReduce( m , simpleSum , { out : "gen.weekly.ip" } )
// ---- do rollups for downloads
rollup = function(key,values){
var res = { total : 0 , unique : 0 };
for ( var i=0; i<values.length; i++ ){
res.total += values[i].total;
res.unique += values[i].unique;
}
return res;
}
m = function(){
emit( this._id.t , { total : this.value , unique : 1 } )
}
res = db.gen.monthly.ip.mapReduce( m , rollup , { out : "gen.monthly" } )
res.find().sort( { _id : -1 } ).forEach( printjsononeline )
res = db.gen.weekly.ip.mapReduce( m , rollup , { out : "gen.weekly" } )
res.find().sort( { _id : -1 } ).forEach( printjsononeline )
}
// top domains
function doDomains( numDays ){
var since = new Date( new Date().getTime() - ( 86400 * 1000 * numDays ) )
var q =
{
reverseDomain : { $exists : true } ,
day : { $gt : since }
}
print( "top domains since: " + since + " \t" + db.downloads.find( q ).count() + " / " + db.downloads.find( { day : { $gt : since } } ).count() )
var coll = "gen.domains.day" + numDays;
db.downloads.mapReduce( function(){ emit( this.reverseDomain , 1 ); } ,
simpleSum ,
{ out : coll , query : q } );
db[coll].ensureIndex( { value : 1 } )
}
// top files
function topFiles( numDays ){
var since = new Date( new Date().getTime() - ( 86400 * 1000 * numDays ) )
print( "topFiles since: " + since )
var q = { day : { $gt : since } }
var coll = "gen.files.day" + numDays;
db.downloads.mapReduce( function(){ emit( this["uri-stem"] , 1 ); } ,
simpleSum ,
{ out : coll , query : q } );
db[coll].ensureIndex( { value : 1 } )
}
function topPieces( numDays ){
var since = new Date( new Date().getTime() - ( 86400 * 1000 * numDays ) )
print( "topPieces since: " + since )
var q = { day : { $gt : since } }
var coll = "gen.firstPiece.day" + numDays;
db.downloads.mapReduce( function(){ emit( firstPiece( this["uri-stem"] ) , 1 ); } ,
simpleSum ,
{ out : coll , query : q } );
db[coll].ensureIndex( { value : 1 } )
}
function doGroups( numDays ){
doDomains( numDays )
topFiles( numDays )
topPieces( numDays );
}
function doVersions(){
res = db.downloads.mapReduce(
function(){
v = parseVersion( this["uri-stem"] );
if ( v )
emit( v , { count : 1 , minDate : this.day } );
} ,
function( k , vs ) {
var out = { count : 0 , minDate : vs[0].minDate }
for ( var i=0; i<vs.length; i++ ) {
out.count += vs[i].count;
if ( vs[i].minDate < out.minDate )
out.minDate = vs[i].minDate;
}
return out;
} , { out : "gen.versions" } );
res.find().sort( { _id : -1 } ).forEach( printjson );
}
function osSummary(){
m = function(){
emit( { t : getMonth(this) , os : firstPiece(this["uri-stem"], true) } , 2 );
}
res = db.downloads.mapReduce( m , simpleSum , { out : "gen.monthly.os" } )
}
function versionDist(){
m = function(){
emit( { t : getMonth(this) , version : parseVersion(this["uri-stem"]) } , 2 );
}
res = db.downloads.mapReduce( m , simpleSum , { out : "gen.monthly.version" } )
}
//downloadSummary();
//doGroups( 7 )
//doGroups( 15 )
//doGroups( 30 )
//
//doVersions()
osSummary()
versionDist()