@@ -42,7 +42,7 @@ function NPMJSCollector(repository) {
42
42
NPMJSCollector . prototype . getPackages = function ( callback ) {
43
43
44
44
if ( ! this . dataRepository ) {
45
- return callback ( " No data repository is defined." ) ;
45
+ return callback ( ' No data repository is defined.' ) ;
46
46
}
47
47
48
48
var options = {
@@ -85,31 +85,35 @@ NPMJSCollector.prototype.collectYear = function(packages, year, callback) {
85
85
NPMJSCollector . prototype . collectMonth = function ( packages , year , month , callback ) {
86
86
87
87
if ( ! this . dataRepository ) {
88
- return callback ( " No data repository is defined." ) ;
88
+ return callback ( ' No data repository is defined.' ) ;
89
89
}
90
90
91
91
if ( ! packages ) {
92
92
return callback ( ) ; // nothing to do
93
93
}
94
94
95
+ // TODO
96
+ // check year/month parms
97
+
98
+
95
99
if ( ! Array . isArray ( packages ) ) {
96
100
packages = [ packages ] ;
97
101
}
98
102
99
103
var dateRangeStart = new Date ( year , month - 1 , 1 ) . toISOString ( ) . substring ( 0 , 10 ) ;
100
104
var dateRangeEnd = new Date ( year , month , 0 ) . toISOString ( ) . substring ( 0 , 10 ) ;
101
105
102
- debug ( " Collecting download statistics for " + packages . toString ( ) + " for date range " + dateRangeStart + " and " + dateRangeEnd ) ;
106
+ debug ( ' Collecting download statistics for ' + packages . toString ( ) + ' for date range ' + dateRangeStart + ' and ' + dateRangeEnd ) ;
103
107
104
108
// submit npmjs request
105
109
// sample: https://api.npmjs.org/downloads/range/2016-01-01:2016-02-01/cloudant
106
- this . npmjsClient . getAll ( { dateRange : dateRangeStart + ":" + dateRangeEnd , packages : packages } ,
110
+ this . npmjsClient . getAll ( { dateRange : dateRangeStart + ':' + dateRangeEnd , packages : packages } ,
107
111
function ( err , data ) {
108
112
if ( err ) {
109
113
return callback ( err ) ;
110
114
}
111
115
112
- if ( data . hasOwnProperty ( " error" ) ) {
116
+ if ( data . hasOwnProperty ( ' error' ) ) {
113
117
if ( data . error === 'no stats for this package for this range (0008)' ) {
114
118
return callback ( ) ; // nothing to do; don't return an error
115
119
}
@@ -118,22 +122,22 @@ NPMJSCollector.prototype.collectMonth = function(packages, year, month, callback
118
122
}
119
123
}
120
124
121
- debug ( " Err: " + JSON . stringify ( err ) ) ;
122
- debug ( " Data: " + JSON . stringify ( data ) ) ;
125
+ debug ( ' Err: ' + JSON . stringify ( err ) ) ;
126
+ debug ( ' Data: ' + JSON . stringify ( data ) ) ;
123
127
124
128
// iterate through packages and create a stats doc for each
125
129
var statsDocs = [ ] ;
126
130
var root = null ;
127
131
var total = 0 ;
128
132
_ . forEach ( packages ,
129
133
function ( packageName ) {
130
- debug ( " Checking for download information for package " + packageName ) ;
134
+ debug ( ' Checking for download information for package ' + packageName ) ;
131
135
root = null ;
132
136
if ( data . hasOwnProperty ( packageName ) ) {
133
137
root = data [ packageName ] ;
134
138
}
135
139
else {
136
- if ( data . hasOwnProperty ( " downloads" ) ) {
140
+ if ( data . hasOwnProperty ( ' downloads' ) ) {
137
141
root = data ;
138
142
}
139
143
}
@@ -145,10 +149,10 @@ NPMJSCollector.prototype.collectMonth = function(packages, year, month, callback
145
149
} ) ;
146
150
147
151
statsDocs . push ( {
148
- _id : root [ " package" ] + "_" + root . start . substring ( 0 , 7 ) ,
149
- type : " stats" ,
150
- package : root [ " package" ] ,
151
- language : " javascript" ,
152
+ _id : root [ ' package' ] + '_' + root . start . substring ( 0 , 7 ) ,
153
+ type : ' stats' ,
154
+ package : root [ ' package' ] ,
155
+ language : ' javascript' ,
152
156
month : root . start . substring ( 0 , 7 ) ,
153
157
total : total ,
154
158
range : {
@@ -174,4 +178,39 @@ NPMJSCollector.prototype.collectMonth = function(packages, year, month, callback
174
178
} . bind ( this ) ) ;
175
179
} ;
176
180
181
+ /**
182
+ * Collects the statistics for todolist
183
+ * @param todolist
184
+ * @param callback invoked when processing is finished; returns (err, data)
185
+ */
186
+ NPMJSCollector . prototype . collect = function ( todolist , callback ) {
187
+ if ( ! todolist ) {
188
+ // nothing to do
189
+ return callback ( ) ;
190
+ }
191
+ // process TODO list
192
+ async . eachLimit ( Object . keys ( todolist ) ,
193
+ 1 ,
194
+ function ( year , asyncCallbackYear ) {
195
+ async . eachLimit (
196
+ Object . keys ( todolist [ year ] ) ,
197
+ 1 ,
198
+ function ( month , asyncCallbackMonth ) {
199
+ this . collectMonth ( todolist [ year ] [ month ] ,
200
+ year ,
201
+ month ,
202
+ function ( err , data ) {
203
+ debug ( data ) ;
204
+ return asyncCallbackMonth ( err ) ;
205
+ } ) ;
206
+ } . bind ( this ) ,
207
+ function ( err ) {
208
+ return asyncCallbackYear ( err ) ;
209
+ } ) ;
210
+ } . bind ( this ) ,
211
+ function ( err ) {
212
+ return callback ( err ) ;
213
+ } ) ;
214
+ } ;
215
+
177
216
module . exports = NPMJSCollector ;
0 commit comments