LAN-Scanner: Sparkline breiter/schnelleres Fenster, Traffic in MB/s

TrafficSparkline-Breite 50pt→200pt, Zeitfenster 10s→30s, Poll-Intervall
3s→0,1s. Traffic-Anzeige von auto-skaliertem kbps/Mbps/Gbps auf festes
MB/s umgestellt. Alles per Nutzeranfrage iterativ angepasst, noch nicht
live gegen echten Router getestet (siehe found.md Fund 4). Manual.md +
Manual.pdf mit aktualisiert (build-manual.py). Fund 5 (App-Settings/
Preferences-Bereich) als offene Feature-Idee in found.md notiert, noch
nicht umgesetzt.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Kay
2026-09-16 22:30:22 +02:00
co-authored by Claude Sonnet 5
parent e2ffcaa250
commit 53a2931239
6 changed files with 52 additions and 30 deletions
@@ -14,8 +14,9 @@ struct InterfaceTraffic: Equatable {
/// One timestamped throughput reading, kept in a short rolling per-port history so the
/// LAN-Scanner's port headers can show a small sparkline of the last few seconds, not just the
/// current instantaneous value (Nutzerwunsch: "ein kleines Liniendiagramm der letzten 10 Sekunden
/// ... pro port"). Timestamped (not just appended in order) so trimming to "last 10 seconds" stays
/// correct even if a poll tick is ever delayed or skipped, rather than assuming a fixed cadence.
/// ... pro port", window later widened to 30s). Timestamped (not just appended in order) so
/// trimming to a fixed window stays correct even if a poll tick is ever delayed or skipped,
/// rather than assuming a fixed cadence.
struct TrafficSample: Identifiable {
let id = UUID()
let timestamp: Date
@@ -156,21 +156,14 @@ struct DevicesView: View {
}
}
/// Same unit-suffix style RouterOS itself reports live traffic in (`SSHTransport.
/// parseBitsPerSecond`'s doc comment: "50.7kbps", "34.0kbps") kept consistent instead of
/// inventing a different display format for the same underlying number.
/// Fixed MB/s (megabytes/second, not auto-scaling kbps/Mbps/Gbps) per explicit request
/// bits-per-second from RouterOS divided by 8 (bytes) then by 1,000,000 (MB).
private static func formatTraffic(_ traffic: InterfaceTraffic) -> String {
"\(formatBitsPerSecond(traffic.rxBitsPerSecond))\(formatBitsPerSecond(traffic.txBitsPerSecond))"
"\(formatMegabytesPerSecond(traffic.rxBitsPerSecond))\(formatMegabytesPerSecond(traffic.txBitsPerSecond))"
}
private static func formatBitsPerSecond(_ bitsPerSecond: Int) -> String {
let units: [(suffix: String, divisor: Double)] = [
("Gbps", 1_000_000_000), ("Mbps", 1_000_000), ("kbps", 1_000)
]
for unit in units where Double(bitsPerSecond) >= unit.divisor {
return String(format: "%.1f%@", Double(bitsPerSecond) / unit.divisor, unit.suffix)
}
return "\(bitsPerSecond)bps"
private static func formatMegabytesPerSecond(_ bitsPerSecond: Int) -> String {
String(format: "%.2fMB/s", Double(bitsPerSecond) / 8 / 1_000_000)
}
@ViewBuilder
@@ -342,9 +335,9 @@ private extension View {
}
}
/// Small sparkline next to each port header last-10-seconds rolling window from
/// `DevicesViewModel.portTrafficHistory` (Nutzerwunsch: "ein kleines Liniendiagramm der letzten
/// 10 Sekunden ... pro Port"). No axes/labels by design a trend glance, not a readable chart;
/// Small sparkline next to each port header last-30-seconds rolling window from
/// `DevicesViewModel.portTrafficHistory` (window widened from the original 10s per explicit
/// follow-up request). No axes/labels by design a trend glance, not a readable chart;
/// the exact current numbers are already shown right next to it via the / text. Stays an empty
/// fixed-size placeholder (not collapsing/disappearing) with fewer than two points, so the row
/// layout doesn't jump around during the first couple of poll ticks after opening the tab.
@@ -369,7 +362,7 @@ private struct TrafficSparkline: View {
Color.clear
}
}
.frame(width: 50, height: 16)
.frame(width: 200, height: 16)
}
}
@@ -39,10 +39,10 @@ final class DevicesViewModel: ObservableObject {
/// interface name), not by device). "unbekannt" (devices RouterOS couldn't resolve onto a
/// real port) is never polled it isn't a real interface.
@Published private(set) var portTraffic: [String: InterfaceTraffic] = [:]
/// Rolling last-10-seconds history per port, for the small sparkline next to each port
/// header (Nutzerwunsch: "ein kleines Liniendiagramm der letzten 10 Sekunden ... pro Port").
/// Trimmed by actual elapsed time each tick, not by a fixed sample count, so it stays a true
/// "last 10 seconds" window even if a poll tick is ever late.
/// Rolling last-30-seconds history per port, for the small sparkline next to each port
/// header (window widened from 10s to 30s per explicit follow-up request). Trimmed by
/// actual elapsed time each tick, not by a fixed sample count, so it stays a true
/// "last 30 seconds" window even if a poll tick is ever late.
@Published private(set) var portTrafficHistory: [String: [TrafficSample]] = [:]
private let trafficMonitor = InterfaceTrafficMonitor()
private var trafficPollingTask: Task<Void, Never>?
@@ -61,9 +61,10 @@ final class DevicesViewModel: ObservableObject {
self.networkToolsService = networkToolsService
}
/// Same 3s cadence/reasoning as `ConnectViewModel.startTrafficPolling` re-reads
/// `portGroups` every tick so a newly-appearing port (e.g. a VLAN interface added via the
/// Setup wizard while this tab is open) gets picked up without restarting the poll.
/// 0.1s cadence (faster than `ConnectViewModel.startTrafficPolling`'s 3s, per explicit
/// request this tab's sparkline needs a finer-grained trend, not just the link dot)
/// re-reads `portGroups` every tick so a newly-appearing port (e.g. a VLAN interface added
/// via the Setup wizard while this tab is open) gets picked up without restarting the poll.
func startTrafficPolling(credentials: RouterOSCredentials) {
stopTrafficPolling()
trafficPollingTask = Task {
@@ -76,13 +77,13 @@ final class DevicesViewModel: ObservableObject {
appendTrafficHistory(traffic, at: Date())
}
}
try? await Task.sleep(for: .seconds(3))
try? await Task.sleep(for: .milliseconds(100))
}
}
}
private func appendTrafficHistory(_ traffic: [String: InterfaceTraffic], at timestamp: Date) {
let cutoff = timestamp.addingTimeInterval(-10)
let cutoff = timestamp.addingTimeInterval(-30)
for (name, sample) in traffic {
var history = portTrafficHistory[name] ?? []
history.append(TrafficSample(timestamp: timestamp, totalBitsPerSecond: sample.rxBitsPerSecond + sample.txBitsPerSecond))