mirror of
https://github.com/pyMC-dev/pyMC_Repeater.git
synced 2026-06-11 08:44:46 +02:00
1 line
5.2 KiB
JavaScript
1 line
5.2 KiB
JavaScript
import{U as e,V as t,o as n}from"./runtime-core.esm-bundler-CINEgm0a.js";import{t as r,v as i}from"./api-C4t_bxtk.js";import{t as a}from"./packets-B-5gWBx4.js";import{t as o}from"./system-OIM0xrD-.js";var s={0:`Unknown`,1:`Chat Node`,2:`Repeater`,3:`Room Server`,4:`Hybrid Node`},c=i(`neighbors`,()=>{let t=e({}),i=e(!1),a=e(null),o=e(48),c=n(()=>Object.values(t.value).flat()),l=n(()=>c.value.length);function u(e=10*6e4){return a.value===null?!0:Date.now()-a.value>e}async function d(e=o.value){i.value=!0,o.value=e;let n=Object.entries(s),c=await Promise.allSettled(n.map(async([t,n])=>{try{let i=[],a=0,o=0;for(;o<200;){let t=await r.get(`/adverts_by_contact_type?contact_type=${encodeURIComponent(n)}&hours=${e}&limit=500&offset=${a}`),s=t.success&&Array.isArray(t.data)?t.data:[];if(s.length===0||(i.push(...s),s.length<500))break;a+=500,o+=1}return{typeKey:t,adverts:i}}catch{return{typeKey:t,adverts:[]}}})),l={};for(let e of c)e.status===`fulfilled`&&e.value.adverts.length>0&&(l[e.value.typeKey]=e.value.adverts);t.value=l,a.value=Date.now(),i.value=!1}function f(){t.value={},i.value=!1,a.value=null,o.value=48}return{advertsByType:t,isLoading:i,lastFetched:a,currentHours:o,allAdverts:c,totalCount:l,isStale:u,fetchAll:d,reset:f}}),l={stats:3e4,packetStats:6e4,noiseFloor:15e3,recentPackets:3e4,sparklines:3e5,advertTier:6e4,neighbors:10*6e4,radioConfig:2**53-1},u=i(`dataService`,()=>{let n=o(),i=a(),s=c(),u=e({currentTier:`unknown`,advertsAllowed:0,advertsDropped:0,activePenalties:0}),d=e(!1),f=e(null),p=t({stats:`pending`,packetStats:`pending`,noiseFloor:`pending`,recentPackets:`pending`,sparklines:`pending`,advertTier:`pending`,neighbors:`pending`,radioConfig:`pending`}),m=new Map,h=new Map,g=[],_=!1,v=null;async function y(e,t=2){for(let n=0;n<t;n++)try{return await e()}catch(e){if(n===t-1)throw e;await new Promise(e=>setTimeout(e,500*2**n))}throw Error(`unreachable`)}async function b(){try{let e=(await r.get(`/advert_rate_limit_stats`))?.data;u.value={currentTier:typeof e?.adaptive?.current_tier==`string`?e.adaptive.current_tier:`unknown`,advertsAllowed:e?.stats?.adverts_allowed||0,advertsDropped:e?.stats?.adverts_dropped||0,activePenalties:Object.keys(e?.active_penalties||{}).length},m.set(`advertTier`,Date.now())}catch{}}async function x(e){if(e===`neighbors`){if(!s.isStale())return}else{let t=m.get(e);if(t!==void 0&&Date.now()-t<l[e])return}let t=h.get(e);if(t)return t;let r;switch(e){case`stats`:r=n.fetchStats().then(()=>{m.set(`stats`,Date.now()),m.set(`radioConfig`,Date.now())});break;case`radioConfig`:r=x(`stats`).then(()=>{m.set(`radioConfig`,Date.now())});break;case`packetStats`:r=i.fetchPacketStats({hours:24}).then(()=>{m.set(`packetStats`,Date.now())});break;case`noiseFloor`:r=i.fetchNoiseFloorHistory({hours:1,limit:500}).then(()=>{m.set(`noiseFloor`,Date.now())});break;case`recentPackets`:r=i.fetchRecentPackets({limit:100}).then(()=>{m.set(`recentPackets`,Date.now())});break;case`sparklines`:r=i.initializeSparklineHistory().then(()=>{m.set(`sparklines`,Date.now())});break;case`advertTier`:r=b();break;case`neighbors`:r=s.fetchAll(s.currentHours).then(()=>{});break}return h.set(e,r),r.finally(()=>h.delete(e)),r}async function S(e,t){p[e]=`loading`;try{await t(),p[e]=`done`}catch{p[e]=`error`}}async function C(){if(!_){_=!0,d.value=!0,p.stats=`loading`,f.value=`requesting`;try{await y(()=>n.fetchStats({onFirstByte:()=>{f.value=`reading`}})),m.set(`stats`,Date.now()),m.set(`radioConfig`,Date.now()),p.stats=`done`}catch{p.stats=`error`,console.error(`[DataService] Failed to fetch stats after retries`)}finally{f.value=null}await Promise.allSettled([S(`packetStats`,()=>i.fetchPacketStats({hours:24}).then(()=>{m.set(`packetStats`,Date.now())})),S(`noiseFloor`,()=>i.fetchNoiseFloorHistory({hours:1,limit:500}).then(()=>{m.set(`noiseFloor`,Date.now())})),S(`recentPackets`,()=>i.fetchRecentPackets({limit:100}).then(()=>{m.set(`recentPackets`,Date.now())}))]),await Promise.allSettled([S(`sparklines`,()=>i.initializeSparklineHistory().then(()=>{m.set(`sparklines`,Date.now())})),S(`advertTier`,()=>b()),S(`neighbors`,()=>s.fetchAll(s.currentHours).then(()=>{}))]),d.value=!1,w()}}function w(){k(),g.push(window.setInterval(()=>void x(`advertTier`),3e4)),g.push(window.setInterval(()=>void x(`packetStats`),6e4)),g.push(window.setInterval(()=>void x(`noiseFloor`),15e3)),g.push(window.setInterval(()=>void x(`sparklines`),3e5)),g.push(window.setInterval(()=>{let e=n.lastUpdated?.getTime()??0;Date.now()-e>25e3&&x(`stats`)},3e4))}function T(){v=Math.floor(Date.now()/1e3)}async function E(e){try{let t=await r.get(`/filtered_packets`,{start_timestamp:e,limit:1e3});t.success&&t.data&&(i.mergeRecentPackets(t.data),m.set(`recentPackets`,Date.now()))}catch{}}async function D(){await new Promise(e=>setTimeout(e,3e3));let e=Math.floor(Date.now()/1e3)-600,t=v===null?e:Math.max(v,e);v=null,await Promise.allSettled([x(`stats`),x(`packetStats`),E(t)])}function O(e){m.delete(e)}function k(){for(let e of g)clearInterval(e);g=[]}function A(){k(),_=!1,m.clear(),h.clear(),d.value=!1,Object.keys(p).forEach(e=>{p[e]=`pending`}),u.value={currentTier:`unknown`,advertsAllowed:0,advertsDropped:0,activePenalties:0}}return{advertTier:u,isBootstrapping:d,statsSubStatus:f,loadProgress:p,bootstrap:C,ensure:x,invalidate:O,noteDisconnect:T,onReconnect:D,stopPolling:k,reset:A}});export{s as n,c as r,u as t}; |