Files
MikrotikManager/hooks/use-traffic-live.ts
T
Denozordec d9f623ccda
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m29s
Docker images / frontend-image (push) Successful in 2m9s
Docker images / updater-image (push) Successful in 40s
Docker images / netflow-agent-image (push) Successful in 31s
Docker images / backend-image (push) Successful in 1m58s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
fix(map): показать текущий трафик туннелей
2026-11-23 14:39:40 +07:00

128 lines
5.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client"
import { useEffect, useState } from "react"
import { resolveApiUrl, withAuthHeaders } from "@/shared/api/http-client"
export interface TrafficLiveSample {
rxMbps: number
txMbps: number
at: string
timestampKind?: "backend-receipt"
}
export function isTrafficLiveSample(value: unknown): value is TrafficLiveSample {
const sample = value as Partial<TrafficLiveSample> | null
return !!sample && typeof sample.rxMbps === "number" && Number.isFinite(sample.rxMbps) && sample.rxMbps >= 0 &&
typeof sample.txMbps === "number" && Number.isFinite(sample.txMbps) && sample.txMbps >= 0 &&
typeof sample.at === "string" && Number.isFinite(Date.parse(sample.at)) &&
(sample.timestampKind === undefined || sample.timestampKind === "backend-receipt")
}
function parseSseBlock(block: string): { event: string; data: string } {
let event = "message"
const dataLines: string[] = []
for (const line of block.split(/\r?\n/)) {
if (line.startsWith("event:")) event = line.slice(6).trim()
else if (line.startsWith("data:")) dataLines.push(line.slice(5).trim())
}
return { event, data: dataLines.join("\n") }
}
export function useTrafficLive(opts: {
enabled: boolean
backendUrl: string
serverId: string
iface: string
reloadKey?: number
}): { sample: TrafficLiveSample | null; error: string | null; receivedAt: number | null } {
const scope = JSON.stringify([opts.backendUrl, opts.serverId, opts.iface])
const key = JSON.stringify([opts.enabled, scope, opts.reloadKey ?? 0])
const [result, setResult] = useState<{ key: string; scope: string; sample: TrafficLiveSample | null; receivedAt: number | null; error: string | null } | null>(null)
useEffect(() => {
if (!opts.enabled || !opts.serverId) return
const ac = new AbortController()
let lastStreamError: string | null = null
// A new connection owns the status; the same device's previous sample remains historical.
// eslint-disable-next-line react-hooks/set-state-in-effect
setResult(previous => ({ key, scope, sample: previous?.scope === scope ? previous.sample : null,
receivedAt: previous?.scope === scope ? previous.receivedAt : null, error: "Ожидание нового live-измерения" }))
let sampleTimer: ReturnType<typeof setTimeout> | undefined
const stopTimer = () => clearTimeout(sampleTimer)
const setError = (error: string) => {
lastStreamError = error
stopTimer()
if (!ac.signal.aborted) setResult(previous => ({ key, scope, sample: previous?.scope === scope ? previous.sample : null, receivedAt: previous?.scope === scope ? previous.receivedAt : null, error }))
}
const ifaceQ =
opts.iface && opts.iface !== "__all__"
? `?iface=${encodeURIComponent(opts.iface)}`
: ""
const path = `/api/traffic/servers/${encodeURIComponent(opts.serverId)}/live${ifaceQ}`
const url = resolveApiUrl(opts.backendUrl, path)
// Backend ticks every 1.5–2s; tolerate several missed samples. Heartbeats do not reset this timer.
const awaitSample = () => {
stopTimer()
sampleTimer = setTimeout(() => setError("Нет обновлений live более 15 секунд"), 15_000)
}
awaitSample()
let buf = ""
void (async () => {
try {
const res = await fetch(url, {
headers: withAuthHeaders({ Accept: "text/event-stream" }),
signal: ac.signal,
credentials: "include",
})
if (ac.signal.aborted) return
if (!res.ok || !res.body) {
const body = await res.json().catch(() => null) as { error?: unknown } | null
setError(`${typeof body?.error === "string" ? body.error : "Live-поток недоступен"} (HTTP ${res.status})`)
return
}
const reader = res.body.getReader()
const decoder = new TextDecoder()
while (!ac.signal.aborted) {
const { done, value } = await reader.read()
if (ac.signal.aborted) return
if (done) {
setError(lastStreamError ?? "Поток live завершён")
break
}
buf += decoder.decode(value, { stream: true })
const parts = buf.split(/\r?\n\r?\n/)
buf = parts.pop() ?? ""
for (const raw of parts) {
if (!raw.trim()) continue
const ev = parseSseBlock(raw)
if (ev.event === "sample" && ev.data) {
const parsed: unknown = JSON.parse(ev.data)
if (!isTrafficLiveSample(parsed)) {
throw new Error("Некорректные данные live-потока")
}
awaitSample()
lastStreamError = null
setResult({ key, scope, sample: parsed, receivedAt: Date.now(), error: null })
} else if (ev.event === "error" && ev.data) {
const parsed = JSON.parse(ev.data) as { error?: unknown } | null
setError(typeof parsed?.error === "string" ? parsed.error : "Некорректный ответ об ошибке live-потока")
}
}
}
} catch (e) {
if (ac.signal.aborted) return
setError(e instanceof Error ? e.message : "live error")
} finally {
stopTimer()
ac.abort()
}
})()
return () => { stopTimer(); ac.abort() }
}, [opts.enabled, opts.backendUrl, opts.serverId, opts.iface, key, scope])
return opts.enabled && result?.scope === scope ? { sample: result.sample, error: result.key === key ? result.error : "Ожидание нового live-измерения", receivedAt: result.receivedAt } : { sample: null, error: null, receivedAt: null }
}