Skip to content

Commit

Permalink
log changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandro Almeida authored and Leandro Almeida committed Oct 30, 2014
1 parent 0090ff9 commit 325666a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ The "node_modules" directory is created with the following modules in it:
- xml2js

3) create a configuration json file (see config.example.json) with the following data:

{
"port": "9187",
"host": "127.0.0.1",
"interval": 5,
"recalc_after": 20,
"logging": true,
"endpoint": "http://www.example.com/nsc/save_counts"
"interval": 5, // cada cuanto flushea
"recalc_after": 20, // cada cuanto vuelve a consultar a las APIS de las redes
"logLevel": ["INFO", "ERROR", "DEBUG"],
"endpoint": "http://local.stable.tn.com.ar/nsc/save_counts", // a donde hace el POST (flush)
"flushtimeout": 10, // timeout del POST (flush)
"safe_hosts": ["tn.com.ar", "prepro.tn.com.ar", "local.next.tn.com.ar", "local.stable.tn.com.ar", "pre.tn.com.ar"],
"facebook": { // Facebook settings
"fb_timeout": 5,
"fb_hits_required": 3 // hits required to call facebook api
},
"twitter": { // Twitter settings
"tw_timeout": 5,
},
"google": { // Google settings
"g_timeout": 5,
}
}

Host & port: the ip address or domain for the host the server is installed in.
Expand Down
10 changes: 6 additions & 4 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
"interval": 5,
"recalc_after": 20,
"logLevel": ["INFO", "ERROR", "DEBUG"],
"syslog": true,
"endpoint": "http://local.stable.tn.com.ar/nsc/save_counts",
"flushtimeout": 10,
"fb_timeout": 5,
"tw_timeout": 5,
"g_timeout": 5,
"safe_hosts": ["tn.com.ar", "prepro.tn.com.ar", "local.next.tn.com.ar", "local.stable.tn.com.ar", "pre.tn.com.ar"],
"facebook": {
"fb_timeout": 5,
"fb_hits_required": 3
},
"twitter": {
"tw_timeout": 5
},
"google": {
"g_timeout": 5
}
}
45 changes: 28 additions & 17 deletions lib/node-social-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
{
uri:'http://graph.facebook.com/?id='+target_url,
//uri:'http://api.facebook.com/method/fql.query?query=select%20total_count,%20share_count,%20like_count,%20comment_count%20from%20link_stat%20where%20url%20="'+target_url+'"',
timeout: config.fb_timeout*1000,
timeout: config.facebook.fb_timeout*1000,
}, function(err,res,body){
if (err) { // request error handling
log('key: ' + key + ' - FACEBOOK_REQUEST_ERROR - '+ err.code, 'ERROR');
Expand All @@ -92,7 +92,7 @@
requestsInProgress[key].hits++;
if (requestsInProgress[key].hits < config.facebook.hits_required && requestsInProgress[key].hits % 10 == 0){
log('key ' + key + ' hits = ' + requestsInProgress[key].hits, 'DEBUG');
}
}
}

};
Expand All @@ -108,7 +108,7 @@
var req = request.get(
{
uri:'http://cdn.api.twitter.com/1/urls/count.json?url='+target_url,
timeout: config.tw_timeout*1000,
timeout: config.twitter.tw_timeout*1000,
}, function(err,res,body){
if (err) { // request error handling
log('key: ' + key + ' - TWITTER_REQUEST_ERROR - '+ err.code, 'ERROR');
Expand All @@ -135,7 +135,7 @@
request.post({
uri:"https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ",
headers:{'content-type': 'application/json'},
timeout: config.g_timeout*1000,
timeout: config.google.g_timeout*1000,
body:'[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'+target_url+'","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]'
}, function(err,res,body){
if (err) { // request error handling
Expand Down Expand Up @@ -203,35 +203,30 @@
if (requestsInProgress.hasOwnProperty(key)) {

if(typeof requestsInProgress[key].facebook === 'number'){
//log('FB ETA: ' + (nowTimestamp - requestsInProgress[key].facebook), 'DEBUG');
expired = nowTimestamp - requestsInProgress[key].facebook > config.recalc_after;
if(expired){
//log(key+': expired facebook ... may recalculate', 'DEBUG');
requestsInProgress[key].hits = 0;
delete requestsInProgress[key].hits;
delete requestsInProgress[key].facebook; // remove from queue
}
}

if(typeof requestsInProgress[key].twitter === 'number'){
//log('TW ETA: ' + (nowTimestamp - requestsInProgress[key].twitter), 'DEBUG');
expired = nowTimestamp - requestsInProgress[key].twitter > config.recalc_after;
if(expired){
//log(key+': expired twitter ... may recalculate', 'DEBUG');
delete requestsInProgress[key].twitter; // remove from queue
}
}
if(typeof requestsInProgress[key].google === 'number'){
//log('GO ETA: ' + (nowTimestamp - requestsInProgress[key].google), 'DEBUG');
expired = nowTimestamp - requestsInProgress[key].google > config.recalc_after;
if(expired){
//log(key+': expired google ... may recalculate', 'DEBUG');
delete requestsInProgress[key].google; // remove from queue
}
}

if(typeof requestsInProgress[key].facebook === 'undefined' &&
typeof requestsInProgress[key].twitter === 'undefined' &&
typeof requestsInProgress[key].google === 'undefined' ){
typeof requestsInProgress[key].google === 'undefined' &&
typeof requestsInProgress[key].hits === 'undefined' ){
delete requestsInProgress[key]; // remove from queue only when each count is removed
}

Expand All @@ -251,7 +246,6 @@
if (config.secret) {
data.secret = config.secret;
}
log(data.json);
//querystr = querystring.stringify(data);
return data;
} else {
Expand All @@ -260,6 +254,11 @@
};

flush = function() {

log("Request Queue size: " + ObjectLength(requestQueue), 'DEBUG');
log("Request in Progress size: " + ObjectLength(requestQueue), 'DEBUG');


var data;
if (!(config.endpoint)) {
return null;
Expand All @@ -275,14 +274,16 @@
if (err) {
log('FLUSH_ERROR - '+ err.code, 'ERROR');
} else {
console.log('--- flushed ---');
log('FLUSHED - ' + JSON.stringify(data) , 'DEBUG');
}
}
).setMaxListeners(0);

} else {
return console.info('--- flushed ---');
return log('FLUSHED - no data', 'INFO');
}


};

/**
Expand Down Expand Up @@ -315,11 +316,11 @@
console.log(logString);

// to syslog:
if(config.syslog){
/*if(config.syslog){
Syslog.init("NSC server", Syslog.LOG_PID | Syslog.LOG_ODELAY, Syslog.LOG_LOCAL0);
Syslog.log(Syslog.LOG_INFO, logString);
Syslog.close();
}
}*/
return message;

}
Expand All @@ -335,6 +336,16 @@
return false;
};

ObjectLength = function(object) {
var length = 0;
for( var key in object ) {
if( object.hasOwnProperty(key) ) {
++length;
}
}
return length;
};

server = http.createServer(function(req, res) {
var params;
params = url.parse(req.url, true);
Expand Down

0 comments on commit 325666a

Please sign in to comment.