feat: connections speeds

This commit is contained in:
mrFq1
2023-09-08 22:27:30 +08:00
parent a25ca56d1c
commit 217999724f
2 changed files with 68 additions and 2 deletions

View File

@@ -66,6 +66,11 @@ class DBConnectionObject: NSObject {
@objc let destinationIP: String? @objc let destinationIP: String?
@objc let type: 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 { func isContentEqual(to source: DBConnectionObject) -> Bool {
download == source.download && download == source.download &&
@@ -99,6 +104,40 @@ class DBConnectionObject: NSObject {
metadata.host].first(where: { $0 != "" }) metadata.host].first(where: { $0 != "" })
type = "\(metadata.type)(\(metadata.network))" 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 = ""
}
}
} }

View File

@@ -12,6 +12,8 @@ struct ConnectionsTableView<Item: Hashable>: NSViewRepresentable {
case host = "Host" case host = "Host"
case sniffHost = "Sniff Host" case sniffHost = "Sniff Host"
case process = "Process" case process = "Process"
case dlSpeed = "DL Speed"
case ulSpeed = "UL Speed"
case dl = "DL" case dl = "DL"
case ul = "UL" case ul = "UL"
case chain = "Chain" case chain = "Chain"
@@ -80,6 +82,10 @@ struct ConnectionsTableView<Item: Hashable>: NSViewRepresentable {
sort = .init(keyPath: \DBConnectionObject.sniffHost, ascending: true) sort = .init(keyPath: \DBConnectionObject.sniffHost, ascending: true)
case .process: case .process:
sort = .init(keyPath: \DBConnectionObject.process, ascending: true) 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: case .dl:
sort = .init(keyPath: \DBConnectionObject.download, ascending: true) sort = .init(keyPath: \DBConnectionObject.download, ascending: true)
case .ul: case .ul:
@@ -96,8 +102,6 @@ struct ConnectionsTableView<Item: Hashable>: NSViewRepresentable {
sort = .init(keyPath: \DBConnectionObject.destinationIP, ascending: true) sort = .init(keyPath: \DBConnectionObject.destinationIP, ascending: true)
case .type: case .type:
sort = .init(keyPath: \DBConnectionObject.type, ascending: true) sort = .init(keyPath: \DBConnectionObject.type, ascending: true)
default:
sort = nil
} }
tableColumn.sortDescriptorPrototype = sort tableColumn.sortDescriptorPrototype = sort
@@ -140,6 +144,9 @@ struct ConnectionsTableView<Item: Hashable>: NSViewRepresentable {
var conns = data.map(DBConnectionObject.init) var conns = data.map(DBConnectionObject.init)
let connHistorys = context.coordinator.connHistorys
conns.forEach {
$0.updateSpeeds(connHistorys[$0.id])
} }
conns = updateSorts(conns, tableView: tableView) conns = updateSorts(conns, tableView: tableView)
@@ -184,6 +191,7 @@ struct ConnectionsTableView<Item: Hashable>: NSViewRepresentable {
var parent: ConnectionsTableView var parent: ConnectionsTableView
var conns = [DBConnectionObject]() var conns = [DBConnectionObject]()
var connHistorys = [String: (download: Int64, upload: Int64)]()
init(parent: ConnectionsTableView) { init(parent: ConnectionsTableView) {
self.parent = parent self.parent = parent
@@ -193,6 +201,19 @@ struct ConnectionsTableView<Item: Hashable>: NSViewRepresentable {
let changes = conns.difference(from: self.conns) { let changes = conns.difference(from: self.conns) {
$0.id == $1.id $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 { guard let partialChanges = self.conns.applying(changes) else {
return return
} }
@@ -232,6 +253,12 @@ struct ConnectionsTableView<Item: Hashable>: NSViewRepresentable {
return conn.sniffHost return conn.sniffHost
case .process: case .process:
return conn.process return conn.process
case .dlSpeed:
return conn.downloadSpeedString
// return conn.downloadSpeed
case .ulSpeed:
return conn.uploadSpeedString
// return conn.uploadSpeed
case .dl: case .dl:
return conn.downloadString return conn.downloadString
case .ul: case .ul: