From 325666a51f5da77fa9fa34b9290577b8986ab4d5 Mon Sep 17 00:00:00 2001 From: Leandro Almeida Date: Thu, 30 Oct 2014 13:44:29 -0300 Subject: [PATCH] log changes --- README.md | 21 ++++++++++++++---- config.example.json | 10 +++++---- lib/node-social-counter.js | 45 ++++++++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index ca4752f..437f106 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/config.example.json b/config.example.json index 35e1ca7..e867353 100644 --- a/config.example.json +++ b/config.example.json @@ -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 } } diff --git a/lib/node-social-counter.js b/lib/node-social-counter.js index 43a067f..8cc1e01 100644 --- a/lib/node-social-counter.js +++ b/lib/node-social-counter.js @@ -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'); @@ -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'); - } + } } }; @@ -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'); @@ -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 @@ -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 } @@ -251,7 +246,6 @@ if (config.secret) { data.secret = config.secret; } - log(data.json); //querystr = querystring.stringify(data); return data; } else { @@ -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; @@ -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'); } + + }; /** @@ -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; } @@ -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);