Skip to content

Commit

Permalink
feat: added total IO in the Network module when the app is started (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Sep 7, 2024
1 parent 1667879 commit 5e34255
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Kit/module/reader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ open class Reader<T: Codable>: NSObject, ReaderInternal_p {

super.init()
DB.shared.setup(T.self, "\(module.rawValue)@\(self.name)")
self.setup()

if let lastValue = DB.shared.findOne(T.self, key: "\(module.rawValue)@\(self.name)") {
self.value = lastValue
callback(lastValue)
}
self.setup()

debug("Successfully initialize reader", log: self.log)
}
Expand Down Expand Up @@ -161,6 +160,10 @@ open class Reader<T: Codable>: NSObject, ReaderInternal_p {
self.interval = Double(value)
self.repeatTask?.reset(seconds: value, restart: true)
}

public func save(_ value: T) {
DB.shared.insert(key: "\(self.module.rawValue)@\(self.name)", value: value, ts: self.history, force: true)
}
}

extension Reader: Reader_p {
Expand Down
4 changes: 2 additions & 2 deletions Kit/plugins/DB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public class DB {
}
}

public func insert(key: String, value: Codable, ts: Bool = true) {
public func insert(key: String, value: Codable, ts: Bool = true, force: Bool = false) {
self.values[key] = value
guard let blobData = try? JSONEncoder().encode(value) else { return }

if ts {
self.lldb?.insert("\(key)@\(Date().currentTimeSeconds())", value: String(decoding: blobData, as: UTF8.self))
}

if let ts = self.writeTS[key], (Date().timeIntervalSince1970-ts.timeIntervalSince1970) < 30 { return }
if !force, let ts = self.writeTS[key], (Date().timeIntervalSince1970-ts.timeIntervalSince1970) < 30 { return }

self.lldb?.insert(key, value: String(decoding: blobData, as: UTF8.self))
self.writeTS[key] = Date()
Expand Down
2 changes: 2 additions & 0 deletions Modules/Net/popup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@ internal class Popup: PopupWrapper {

@objc private func resetTotalNetworkUsage() {
NotificationCenter.default.post(name: .resetTotalNetworkUsage, object: nil, userInfo: nil)
self.totalUploadField?.stringValue = Units(bytes: 0).getReadableMemory()
self.totalDownloadField?.stringValue = Units(bytes: 0).getReadableMemory()
self.lastReset = Date()
}

Expand Down
6 changes: 6 additions & 0 deletions Modules/Net/readers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ internal class UsageReader: Reader<Network_Usage> {
self.getDetails()
}
}

if let usage = self.value {
self.usage = usage
self.usage.bandwidth = Bandwidth()
}
}

public override func terminate() {
Expand Down Expand Up @@ -422,6 +427,7 @@ internal class UsageReader: Reader<Network_Usage> {

@objc func resetTotalNetworkUsage() {
self.usage.total = Bandwidth()
self.save(self.usage)
}
}

Expand Down

0 comments on commit 5e34255

Please sign in to comment.