You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// a time reference is needed to keep track of batches.varintBatchStartTime=Date.now();// an array to hold the batchesvararrBatch=[];//a function to be called on eventsvarfnOnEvent=function(objEvent){varintNow=Date.now();//always add the event to the batcharrBatch.push(objEvent);if(intBatchStartTime+1000<intNow){//time to process call the function that processes//note that if the array isnt copied it will have a race conditionfnProcessBatch(arrBatch);//reset the batcharrBatch=[];intBatchStartTime=intNow;}}
Tail a log file with node-tail
//watch a filetail=newTail("fileToTail");//fire every time a new line comes intail.on("line",function(strLine){//call your defined function for the event//note: in most cases you'll need to parse the stringfnOnEvent(strLine);});
Records per second
//use set interval for timing, this var will be a reference to itvarobjThrottle={};//assuming arrRecords is a collection of recordsobjThrottle=setInterval(function(){//assuming fnSendRecord is the function to run for each recordfnSendRecord(arrRecords[intIndex]);if(intIndex===intRecords-1){clearInterval(objThrottle);}else{intIndex++;}},intDelay);
Set a Time To Live
//assume objMsg is already defined//in this scenario a severity property is already present as a numberif(typeofobjMsg.severity==='number'){//need a timestamp as a point of referenceobjMsg.timestamp=Date.now();//ttl is set in millisecondsobjMsg.ttl=objMsg.severity*60000;}
Timing Out
//run inside a function that scans all persistent objects//get the current timestampvarintNow=Date.now();// arrObjects as a collection of objectsfor(vari=0;i<arrobjects.length;i++){if(arrObjects[i].timestamp+arrObjects[i].ttl<intNow){//this is past the timeout threshold//call whatever function is defined to remove the timed out objectfnRemove(arrObjects[i]);}}
Time in State
//run inside a function that scans all persistent objects//get the current timestampvarintNow=Date.now();// arrObjects as a collection of objectsfor(vari=0;i<arrobjects.length;i++){if(arrObjects[i].timestamp+arrObjects[i].ttl<intNow){//this is past the timeout threshold//call whatever function is defined to remove the timed out objectif(arrObjects[i].severity>0){//reduce the severity and recalc the ttlarrObjects[i].severity--;arrObjects[i].ttl=objMsg.severity*60000;}else{fnRemove(arrObjects[i]);}}}