Skip to content

Commit

Permalink
Merge pull request #249 from wafers/#247
Browse files Browse the repository at this point in the history
Add timestamp to logs, use searchTally and detailSearch logging. Resolves #247.
  • Loading branch information
WesTyler committed Mar 6, 2016
2 parents 6e7ad7c + c168a38 commit 50e9da8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 38 deletions.
75 changes: 61 additions & 14 deletions server/helpers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
// Imports
var fs = require('fs');
var Registry = require('npm-registry');
var downloadCount = require('npm-download-counts');
var moment = require('moment');
var _ = require('underscore');
var request = require('request');
var cheerio = require('cheerio');
var npm = new Registry({
registry: "http://skimdb.npmjs.com/registry/",
retries: 3
});
var db = require(__dirname + '/dbParsing.js');

var fs = require('fs'),
Registry = require('npm-registry'),
downloadCount = require('npm-download-counts'),
moment = require('moment'),
_ = require('underscore'),
request = require('request'),
cheerio = require('cheerio'),
npm = new Registry({
registry: "http://skimdb.npmjs.com/registry/",
retries: 3
}),
db = require(__dirname + '/dbParsing.js'),
MongoClient = require('mongodb').MongoClient,
mongoUrl = process.env.MONGOLAB_URI ? process.env.MONGOLAB_URI : require('./config').mongo.url,
mongoCollections = {
searches : true,
details : true,
topModules : true,
searchTally: true
};

///////////////// HELPER FUNCTIONS /////////////////
// Returns an array of all the dependents
Expand Down Expand Up @@ -375,4 +382,44 @@ var relatedKeywordSearch = module.exports.relatedKeywordSearch = function(keywor
}

// TOP MODULES GET ENDPOINT helper-----------------------------------------------------------------
var getTopModules = module.exports.getTopModules = db.fetchTopModuleData;
var getTopModules = module.exports.getTopModules = db.fetchTopModuleData;

// MONGO LOGGING helper
var mongoLogger = module.exports.mongoLogger = function(collection, moduleName) {
return MongoClient.connect(mongoUrl, function(err, db) {
if(err) console.log('MONGO ERR', err);

var collectionType = db.collection(collection);
switch (collection) {
case 'searches':
var logObject = {
search : moduleName,
timestamp: new Date()
};

collectionType.insert(logObject);
db.close();
break;

case 'searchTally':
collectionType.find().toArray(function(err, results) {
results[0][moduleName] = results[0][moduleName] + 1 || 1;

collectionType.update({}, results[0]);

db.close();
});
break;

case 'details':
var logObject = {
search : moduleName,
timestamp: new Date()
};

collectionType.insert(logObject);
db.close();
break;
}
});
}
30 changes: 6 additions & 24 deletions server/requestHandlers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
// Add request handlers to routes in here
var helpers = require('./helpers.js');
var cache = require('./cache.js');
var MongoClient = require('mongodb').MongoClient;

var mongoUrl = process.env.MONGOLAB_URI;
var mongoCollections = {
searches: true,
details: true,
topModules: true
};
var helpers = require('./helpers.js'),
cache = require('./cache.js');

// Old npm search crawling + parsing for search results
var npmSearch = module.exports.npmSearch = function(req, res) {
Expand All @@ -29,20 +21,8 @@ var search = module.exports.search = function(req, res) {
key = "SEARCH_"+moduleName

// Log search to Mongo
MongoClient.connect(mongoUrl, function(err, db) {
if(err) console.log('MONGO ERR', err);

var searches = db.collection('searches');
var logObject = {
search : key,
timestamp: new Date()
};

searches.insert(logObject, function(err, result) {
if(err) console.log('MONGO ERR', err);
db.close();
});
});
helpers.mongoLogger('searches', moduleName);
helpers.mongoLogger('searchTally', moduleName);

console.log("Memcached key is : " + key)
cache.get(key, function(err, value) {
Expand Down Expand Up @@ -103,11 +83,13 @@ var detailedSearch = module.exports.detailedSearch = function(req, res) {
console.log('ERROR IN detailedSearch FETCHING') /*console.log(err)*/
} else {
cache.set("DETAILEDSEARCH_" + moduleName, results, function() {
helpers.mongoLogger('details', moduleName);
res.json(results);
})
}
})
} else {
helpers.mongoLogger('details', moduleName);
res.json(value)
}
})
Expand Down

0 comments on commit 50e9da8

Please sign in to comment.