Refactor memory handling in Mihomo to use KiB

- Updated memory variable names and comments to reflect usage in KiB instead of KB for clarity.
- Introduced a function to convert memory values from bytes to KiB, ensuring consistent data representation across components.
- Adjusted chart data to display memory in KiB, enhancing accuracy in visualizations.
This commit is contained in:
Denozordec
2026-03-31 13:26:20 +07:00
parent c3a0a771a3
commit 8da50bd254
2 changed files with 17 additions and 10 deletions
@@ -83,7 +83,7 @@
type: 'line',
data: {
labels: histMem.map((_, i) => String(i)),
datasets: [{ label: 'KB', data: [...histMem], borderColor: '#a78bfa', tension: 0.2, fill: true }]
datasets: [{ label: 'KiB', data: [...histMem], borderColor: '#a78bfa', tension: 0.2, fill: true }]
},
options: lineOpts()
});
@@ -36,7 +36,8 @@
let downBps = $state(0);
let totalUp = $state(0);
let totalDown = $state(0);
let memKB = $state(0);
/** Память в KiB (кибибайты): из WS Mihomo приходит в байтах — нормализуем в одном месте. */
let memKiB = $state(0);
let connTotal = $state(0);
let tcpN = $state(0);
let udpN = $state(0);
@@ -48,6 +49,12 @@
let topList = $state<{ name: string; n: number }[]>([]);
const MAX_POINTS = 72;
/** Mihomo WS: `inuse` (/memory) и `memory` (/connections) — в байтах → KiB для карточки и графика. */
function mihomoWsMemoryToKiB(bytes: number): number {
if (!Number.isFinite(bytes) || bytes <= 0) return 0;
return bytes / 1024;
}
/** Mihomo hub/route/proxies.go: без query `url` URLTest часто даёт delay=0 → HTTP 503. */
const MIHOMO_DELAY_DEFAULT_URL = 'https://www.gstatic.com/generate_204';
@@ -203,8 +210,8 @@
if (line) {
try {
const o = JSON.parse(line) as { inuse?: number };
memKB = Number(o.inuse) || 0;
histMem = [...histMem, memKB];
memKiB = mihomoWsMemoryToKiB(Number(o.inuse) || 0);
histMem = [...histMem, memKiB];
while (histMem.length > MAX_POINTS) histMem.shift();
seen++;
} catch {
@@ -343,8 +350,8 @@
ws.onmessage = (ev) => {
try {
const o = JSON.parse(String(ev.data)) as { inuse?: number };
memKB = Number(o.inuse) || 0;
histMem = [...histMem, memKB];
memKiB = mihomoWsMemoryToKiB(Number(o.inuse) || 0);
histMem = [...histMem, memKiB];
while (histMem.length > MAX_POINTS) histMem.shift();
} catch {
/* ignore */
@@ -397,8 +404,8 @@
totalDown = j.downloadTotal;
}
if (typeof j.memory === 'number') {
memKB = j.memory;
histMem = [...histMem, memKB];
memKiB = mihomoWsMemoryToKiB(j.memory);
histMem = [...histMem, memKiB];
while (histMem.length > MAX_POINTS) histMem.shift();
}
let t = 0,
@@ -486,7 +493,7 @@
totalDown = 0;
upBps = 0;
downBps = 0;
memKB = 0;
memKiB = 0;
histUp = [];
histDown = [];
histMem = [];
@@ -676,7 +683,7 @@
<Card.Description>Память</Card.Description>
</Card.Header>
<Card.Content class="text-lg font-semibold">
{memKB > 0 ? `${(memKB / 1024).toFixed(1)} MiB` : '—'}
{memKiB > 0 ? `${(memKiB / 1024).toFixed(1)} MiB` : '—'}
</Card.Content>
</Card.Root>
</div>