From a1876e03e0af0f6e3a0b9aadaa6412b801005f40 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 8 Nov 2016 22:47:41 +0100 Subject: changes: fixes and improvements --- modules/changes/changes.js | 52 +++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'modules/changes/changes.js') diff --git a/modules/changes/changes.js b/modules/changes/changes.js index 67e4e43..4e9d87e 100644 --- a/modules/changes/changes.js +++ b/modules/changes/changes.js @@ -169,11 +169,16 @@ function refreshURLs(cb){ function collectLastHashes(){ var lasthashes={}; - for(url in urls){ + var idx; + for(var url in urls){ if(urls[url].timeline.length==0){ lasthashes[url]=null; } else { - lasthashes[url]=urls[url].timeline[urls[url].timeline.length-1][1]; + idx=urls[url].timeline.length-1; + lasthashes[url]={ + hash: urls[url].timeline[idx][1], + date: urls[url].timeline[idx][0] + }; } } return lasthashes; @@ -181,7 +186,7 @@ function collectLastHashes(){ function sendMail(tomail,body,cb){ var proc=child_process.spawn("sendmail",[tomail],{stdio:["pipe",process.stdout,process.stderr]}); - proc.stdin.error(function(e){ + proc.stdin.on("error",function(e){ console.log(e); cb(e); }); @@ -195,14 +200,13 @@ function constructMailBody(tomail,diffs){ var body="To: "+tomail+"\n"+ "From: changes \n"+ "Subject: changes notification mail\n\n"+ - "The changes module recorded "+ - "some changes in watched webpages.\n"+ + "The changes module recorded some changes in watched webpages.\n"+ "The URLs and their old and new content hashes are shown below.\n\n"; var i; for(i=0;i"+diffs[i].url+"\n"+ - " was: "+diffs[i].oldhash+"\n"+ - " now: "+diffs[i].newhash+"\n"; + body+="- "+diffs[i].url+"\n"+ + " was: "+diffs[i].oldhash+" ("+diffs[i].olddate+")\n"+ + " now: "+diffs[i].newhash+" ("+diffs[i].newdate+")\n"; } body+="\n\n"+ "- webserver\n"; @@ -213,23 +217,28 @@ function performNotify(settings,cb){ var oldhashes=collectLastHashes(); refreshURLs(function(){ var newhashes=collectLastHashes(); - var diffs=[]; // [{url, oldhash, newhash}] - for(url in newhashes){ - if(newhashes[url]==oldhashes[url])continue; + var diffs=[]; // [{url, oldhash, olddate, newhash, newdate}] + for(var url in newhashes){ + if(newhashes[url].hash==oldhashes[url].hash)continue; diffs.push({ url: url, - oldhash: oldhashes[url], - newhash: newhashes[url] + oldhash: oldhashes[url].hash, + olddate: oldhashes[url].date, + newhash: newhashes[url].hash, + newdate: newhashes[url].date }); } - sendMail(settings.to,constructMailBody(settings.to,diffs),cb); + if(diffs.length!=0){ + sendMail(settings.to,constructMailBody(settings.to,diffs),cb); + } }); } function performCleanup(){ - //keep only the last hash and the last different hash intact - //(if there is no different hash, delete everything but the last) - for(url in urls){ + for(var url in urls){ + /* + //keep only the last hash and the last different hash intact + //(if there is no different hash, delete everything but the last) if(urls[url].timeline.length<=2)continue; var i,lasthash,prevhash=null,prevhashidx=-1,tllen; tllen=urls[url].timeline.length; @@ -247,6 +256,15 @@ function performCleanup(){ urls[url].timeline.splice(prevhashidx+1,tllen-prevhashidx-2); urls[url].timeline.splice(0,prevhashidx); } + */ + + //keep only the last record of a specific hash intact + var i; + for(i=urls[url].timeline.length-2;i>=0;i--){ + if(urls[url].timeline[i][1]==urls[url].timeline[i+1][1]){ + urls[url].timeline.splice(i,1); + } + } } } -- cgit v1.2.3-54-g00ecf