From 217999724f2a87df756d04f57bcc11fff0e5cbee Mon Sep 17 00:00:00 2001 From: mrFq1 <1xxbx0il0@mozmail.com> Date: Fri, 8 Sep 2023 22:27:30 +0800 Subject: [PATCH] feat: connections speeds --- .../Models/DBConnectionSnapShot.swift | 39 +++++++++++++++++++ .../Connections/ConnectionsTableView.swift | 31 ++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/Sources/ClashX Dashboard/Models/DBConnectionSnapShot.swift b/Sources/ClashX Dashboard/Models/DBConnectionSnapShot.swift index 92ab954..78a84eb 100644 --- a/Sources/ClashX Dashboard/Models/DBConnectionSnapShot.swift +++ b/Sources/ClashX Dashboard/Models/DBConnectionSnapShot.swift @@ -66,6 +66,11 @@ class DBConnectionObject: NSObject { @objc let destinationIP: String? @objc let type: String + @objc var downloadSpeed: Int64 + @objc var uploadSpeed: Int64 + var downloadSpeedString: String + var uploadSpeedString: String + func isContentEqual(to source: DBConnectionObject) -> Bool { download == source.download && @@ -99,6 +104,40 @@ class DBConnectionObject: NSObject { metadata.host].first(where: { $0 != "" }) type = "\(metadata.type)(\(metadata.network))" + + downloadSpeed = 0 + uploadSpeed = 0 + downloadSpeedString = "" + uploadSpeedString = "" } + + func updateSpeeds(_ old: (download: Int64, upload: Int64)?) { + guard let old = old else { + downloadSpeed = 0 + uploadSpeed = 0 + downloadSpeedString = "" + uploadSpeedString = "" + return + } + + let byteCountFormatter = ByteCountFormatter() + + downloadSpeed = download - old.download + uploadSpeed = upload - old.upload + + if downloadSpeed >= 0 { + downloadSpeedString = byteCountFormatter.string(fromByteCount: downloadSpeed) + "/s" + } else { + downloadSpeed = 0 + downloadSpeedString = "" + } + + if uploadSpeed >= 0 { + uploadSpeedString = byteCountFormatter.string(fromByteCount: uploadSpeed) + "/s" + } else { + uploadSpeed = 0 + uploadSpeedString = "" + } + } } diff --git a/Sources/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsTableView.swift b/Sources/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsTableView.swift index dc44563..32bc246 100644 --- a/Sources/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsTableView.swift +++ b/Sources/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsTableView.swift @@ -12,6 +12,8 @@ struct ConnectionsTableView: NSViewRepresentable { case host = "Host" case sniffHost = "Sniff Host" case process = "Process" + case dlSpeed = "DL Speed" + case ulSpeed = "UL Speed" case dl = "DL" case ul = "UL" case chain = "Chain" @@ -80,6 +82,10 @@ struct ConnectionsTableView: NSViewRepresentable { sort = .init(keyPath: \DBConnectionObject.sniffHost, ascending: true) case .process: sort = .init(keyPath: \DBConnectionObject.process, ascending: true) + case .dlSpeed: + sort = .init(keyPath: \DBConnectionObject.downloadSpeed, ascending: true) + case .ulSpeed: + sort = .init(keyPath: \DBConnectionObject.uploadSpeed, ascending: true) case .dl: sort = .init(keyPath: \DBConnectionObject.download, ascending: true) case .ul: @@ -96,8 +102,6 @@ struct ConnectionsTableView: NSViewRepresentable { sort = .init(keyPath: \DBConnectionObject.destinationIP, ascending: true) case .type: sort = .init(keyPath: \DBConnectionObject.type, ascending: true) - default: - sort = nil } tableColumn.sortDescriptorPrototype = sort @@ -140,6 +144,9 @@ struct ConnectionsTableView: NSViewRepresentable { var conns = data.map(DBConnectionObject.init) + let connHistorys = context.coordinator.connHistorys + conns.forEach { + $0.updateSpeeds(connHistorys[$0.id]) } conns = updateSorts(conns, tableView: tableView) @@ -184,6 +191,7 @@ struct ConnectionsTableView: NSViewRepresentable { var parent: ConnectionsTableView var conns = [DBConnectionObject]() + var connHistorys = [String: (download: Int64, upload: Int64)]() init(parent: ConnectionsTableView) { self.parent = parent @@ -193,6 +201,19 @@ struct ConnectionsTableView: NSViewRepresentable { let changes = conns.difference(from: self.conns) { $0.id == $1.id } + + for change in changes { + switch change { + case .remove(_, let conn, _): + connHistorys[conn.id] = nil + default: + break + } + } + conns.forEach { + connHistorys[$0.id] = ($0.download, $0.upload) + } + guard let partialChanges = self.conns.applying(changes) else { return } @@ -232,6 +253,12 @@ struct ConnectionsTableView: NSViewRepresentable { return conn.sniffHost case .process: return conn.process + case .dlSpeed: + return conn.downloadSpeedString +// return conn.downloadSpeed + case .ulSpeed: + return conn.uploadSpeedString +// return conn.uploadSpeed case .dl: return conn.downloadString case .ul: