Skip to content

Commit 3e8de45

Browse files
authored
Merge pull request #2 from ibm-cds-labs/i1
i1
2 parents d07ab1f + c3a13da commit 3e8de45

File tree

4 files changed

+307
-30
lines changed

4 files changed

+307
-30
lines changed

index.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const path = require('path');
2424
const SimpleDataVis = require('simple-data-vis');
2525

2626
// to enable debugging, set environment variable DEBUG to nps or *
27-
const debug = require('debug')('npmwd');
27+
const debug = require('debug')('npmjs');
2828

2929
const init = require('./lib/initialize.js');
3030

@@ -60,7 +60,7 @@ const init = require('./lib/initialize.js');
6060
console.log('Service is initializing...');
6161

6262
// initialize Cloudant repository and load defaults
63-
init(appEnv, function(err, dataRepository, metaRepository, collector) {
63+
init(appEnv, function(err, dataRepository, metaRepository, collector, indexer) {
6464

6565
//
6666
// dataRepository - cloudant handle for data database
@@ -71,23 +71,46 @@ const init = require('./lib/initialize.js');
7171
process.exit(1);
7272
}
7373

74-
/* var year = 2015;
75-
//var month = null;
74+
// asynchronously build index and collect missing statistics
75+
debug('Building index ...');
76+
indexer.buildIndex(function(err) {
77+
if(err) {
78+
console.error('Index build error: ' + err);
79+
}
80+
else {
81+
debug('Index was built. Identifying gaps ...');
82+
indexer.inspectIndex(null,
83+
function(err, todolist) {
84+
if(err) {
85+
console.error('Index inspection error: ' + err);
86+
}
87+
else {
88+
debug('Collecting missing statistics ... ');
89+
collector.collect(todolist,
90+
function(err, data) {
91+
if(err) {
92+
console.error('Statistics collector error: ' + err);
93+
}
94+
else {
95+
debug('Statistics collection results: ' + JSON.stringify(data));
96+
}
97+
});
98+
}
99+
});
100+
}
101+
});
76102

77-
collector.collectYear(['ibm-graph-client', 'couchimport', 'cf-deployment-tracker-client', 'cachemachine', 'cloudant-envoy', 'nosqlimport','nosqlimport-couchdb','nosqlimport-mongodb', 'nosqlelasticsearch'],
78-
year,
79-
//month,
80-
function (err) {
81-
if(err) {
82-
console.log("Collection error: " + err);
83-
}
84-
else {
85-
console.log("Collection done");
86-
}
87103

104+
/* TODO remove
105+
// seed statistics
106+
collector.collectMonth('cloudant',
107+
2015,
108+
12,
109+
function(err) {
110+
console.log(err);
88111
});
89-
90112
*/
113+
91114
var app = express();
92115
app.use(bodyParser.urlencoded({extended: false}));
93116
// use https://www.npmjs.com/package/express-handlebars as view engine
@@ -124,7 +147,7 @@ const init = require('./lib/initialize.js');
124147
// render stats page
125148
collector.getPackages(function(err, packages) {
126149
if(err) {
127-
console.log("/stats: error retrieving package list: " + err);
150+
console.log('/stats: error retrieving package list: ' + err);
128151
res.render('stats');
129152
}
130153
res.render('stats', {packages:packages});

lib/collector.js

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function NPMJSCollector(repository) {
4242
NPMJSCollector.prototype.getPackages = function(callback) {
4343

4444
if(! this.dataRepository) {
45-
return callback("No data repository is defined.");
45+
return callback('No data repository is defined.');
4646
}
4747

4848
var options = {
@@ -85,31 +85,35 @@ NPMJSCollector.prototype.collectYear = function(packages, year, callback) {
8585
NPMJSCollector.prototype.collectMonth = function(packages, year, month, callback) {
8686

8787
if(! this.dataRepository) {
88-
return callback("No data repository is defined.");
88+
return callback('No data repository is defined.');
8989
}
9090

9191
if(! packages) {
9292
return callback(); // nothing to do
9393
}
9494

95+
// TODO
96+
// check year/month parms
97+
98+
9599
if(! Array.isArray(packages)) {
96100
packages = [packages];
97101
}
98102

99103
var dateRangeStart = new Date(year, month - 1,1).toISOString().substring(0,10);
100104
var dateRangeEnd = new Date(year, month, 0).toISOString().substring(0,10);
101105

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);
103107

104108
// submit npmjs request
105109
// 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},
107111
function(err, data) {
108112
if(err) {
109113
return callback(err);
110114
}
111115

112-
if(data.hasOwnProperty("error")) {
116+
if(data.hasOwnProperty('error')) {
113117
if(data.error === 'no stats for this package for this range (0008)') {
114118
return callback(); // nothing to do; don't return an error
115119
}
@@ -118,22 +122,22 @@ NPMJSCollector.prototype.collectMonth = function(packages, year, month, callback
118122
}
119123
}
120124

121-
debug("Err: " + JSON.stringify(err));
122-
debug("Data: " + JSON.stringify(data));
125+
debug('Err: ' + JSON.stringify(err));
126+
debug('Data: ' + JSON.stringify(data));
123127

124128
// iterate through packages and create a stats doc for each
125129
var statsDocs = [];
126130
var root = null;
127131
var total = 0;
128132
_.forEach(packages,
129133
function(packageName) {
130-
debug("Checking for download information for package " + packageName);
134+
debug('Checking for download information for package ' + packageName);
131135
root = null;
132136
if(data.hasOwnProperty(packageName)) {
133137
root = data[packageName];
134138
}
135139
else {
136-
if(data.hasOwnProperty("downloads")) {
140+
if(data.hasOwnProperty('downloads')) {
137141
root = data;
138142
}
139143
}
@@ -145,10 +149,10 @@ NPMJSCollector.prototype.collectMonth = function(packages, year, month, callback
145149
});
146150

147151
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',
152156
month: root.start.substring(0, 7),
153157
total: total,
154158
range: {
@@ -174,4 +178,39 @@ NPMJSCollector.prototype.collectMonth = function(packages, year, month, callback
174178
}.bind(this));
175179
};
176180

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+
177216
module.exports = NPMJSCollector;

0 commit comments

Comments
 (0)