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 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 = ""
}
}
}