(function(){let e=e=>e.toString().padStart(2,`0`),t=t=>{let n=new Date(t);return`${e(n.getHours())}:${e(n.getMinutes())}:${e(n.getSeconds())}`};var n=class{rawBuffer=[];rawHead=0;compressedBuffer=[];compressedHead=0;compressionQueue=[];constructor(e){this.config=e}addDataPoint(e){this.rawBuffer.push(e);let t=Date.now()-this.config.rawDataMinutes*60*1e3;for(;this.rawHead<this.rawBuffer.length&&this.rawBuffer[this.rawHead]?.timestamp<=t;)this.rawHead++;this.rawHead>512&&this.rawHead>this.rawBuffer.length/2&&(this.rawBuffer=this.rawBuffer.slice(this.rawHead),this.rawHead=0),this.compressionQueue.push(e),this.compressionQueue.length>=this.config.compressionRatio&&this.compressData();let n=Date.now()-this.config.compressedDataMinutes*60*1e3;for(;this.compressedHead<this.compressedBuffer.length&&this.compressedBuffer[this.compressedHead]?.timestamp<=n;)this.compressedHead++;this.compressedHead>256&&this.compressedHead>this.compressedBuffer.length/2&&(this.compressedBuffer=this.compressedBuffer.slice(this.compressedHead),this.compressedHead=0)}compressData(){if(this.compressionQueue.length===0)return;let e=this.compressionQueue.reduce((e,t)=>e+t.up,0),t=this.compressionQueue.reduce((e,t)=>e+t.down,0),n=this.compressionQueue.reduce((e,t)=>e+t.timestamp,0)/this.compressionQueue.length,r={up:e/this.compressionQueue.length,down:t/this.compressionQueue.length,timestamp:n,samples:this.compressionQueue.length};this.compressedBuffer.push(r),this.compressionQueue=[]}getDataForTimeRange(e){let n=Date.now()-e*60*1e3,r=this.rawHead;for(;r<this.rawBuffer.length&&this.rawBuffer[r]?.timestamp<=n;)r++;let i=this.rawBuffer.slice(r);if(e<=this.config.rawDataMinutes)return i;let a=Date.now()-this.config.rawDataMinutes*60*1e3,o=this.compressedHead;for(;o<this.compressedBuffer.length&&this.compressedBuffer[o]?.timestamp<=n;)o++;return[...this.compressedBuffer.slice(o).filter(e=>e.timestamp<=a).map(e=>({up:e.up,down:e.down,timestamp:e.timestamp,name:t(e.timestamp)})),...i].sort((e,t)=>e.timestamp-t.timestamp)}getStats(){return{rawBufferSize:this.rawBuffer.length-this.rawHead,compressedBufferSize:this.compressedBuffer.length-this.compressedHead,compressionQueueSize:this.compressionQueue.length,totalMemoryPoints:this.rawBuffer.length-this.rawHead+(this.compressedBuffer.length-this.compressedHead)}}clear(){this.rawBuffer=[],this.rawHead=0,this.compressedBuffer=[],this.compressedHead=0,this.compressionQueue=[]}};let r={rawDataMinutes:10,compressedDataMinutes:60,compressionRatio:5,snapshotIntervalMs:250,defaultRangeMinutes:10},i=self,a={...r},o=new n(a),s=a.defaultRangeMinutes,c=null,l,u=e=>{let t={type:`snapshot`,dataPoints:o.getDataForTimeRange(s),availableDataPoints:o.getDataForTimeRange(a.compressedDataMinutes),samplerStats:o.getStats(),rangeMinutes:s,lastTimestamp:l,reason:e};i.postMessage(t)},d=e=>{c===null&&(c=i.setTimeout(()=>{c=null,u(e)},a.snapshotIntervalMs))};i.onmessage=e=>{let r=e.data;switch(r.type){case`init`:a={...r.config},o=new n(a),s=r.config.defaultRangeMinutes,u(`init`);break;case`append`:{let e=r.payload.timestamp??Date.now(),n={up:r.payload.up||0,down:r.payload.down||0,timestamp:e,name:t(e)};l=e,o.addDataPoint(n),d(`append-throttle`);break}case`clear`:o.clear(),l=void 0,u(`clear`);break;case`setRange`:s!==r.minutes&&(s=r.minutes,u(`range-change`));break;case`requestSnapshot`:u(`request`);break;default:break}}})();