]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fixed lame speed calculation
authorSergey Matveev <stargrave@stargrave.org>
Mon, 28 Nov 2022 08:21:33 +0000 (11:21 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 13 Jan 2023 08:32:42 +0000 (11:32 +0300)
cmd/btrtrc/list.go
cmd/btrtrc/status.go

index dcbb8b99415610c99a85ef295a75c6872b526c4f..89d9614634532078a51a8aa314c4c7149fe4dfa7 100644 (file)
@@ -26,8 +26,14 @@ const (
        FilesDir         = "files"
 )
 
+type TorrentStat struct {
+       stats   torrent.ConnStats
+       rxSpeed int64
+       txSpeed int64
+}
+
 var (
-       TorrentStats  = map[metainfo.Hash]torrent.TorrentStats{}
+       TorrentStats  = map[metainfo.Hash]TorrentStat{}
        TorrentStatsM sync.RWMutex
 )
 
@@ -75,11 +81,9 @@ func fifoList(c *torrent.Client) {
                        TorrentStatsM.RLock()
                        prev := TorrentStats[t.InfoHash()]
                        TorrentStatsM.RUnlock()
-                       rxSpeed := stats.BytesReadData.Int64() - prev.BytesReadData.Int64()
-                       txSpeed := stats.BytesWrittenData.Int64() - prev.BytesWrittenData.Int64()
                        var eta string
-                       if done < 100 && rxSpeed > 0 {
-                               etaRaw := time.Duration((t.Length() - t.BytesCompleted()) / rxSpeed)
+                       if done < 100 && prev.rxSpeed > 0 {
+                               etaRaw := time.Duration((t.Length() - t.BytesCompleted()) / prev.rxSpeed)
                                etaRaw *= time.Second
                                eta = etaRaw.String()
                        }
@@ -90,8 +94,8 @@ func fifoList(c *torrent.Client) {
                                humanize.IBytes(uint64(t.Length())),
                                percColour, done, Reset,
                                ratio,
-                               Green, rxSpeed/1024, Reset,
-                               Magenta, txSpeed/1024, Reset,
+                               Green, prev.rxSpeed/1024, Reset,
+                               Magenta, prev.txSpeed/1024, Reset,
                                stats.TotalPeers,
                                stats.PendingPeers,
                                stats.ActivePeers,
index f040d554824b56f04e7e2fcd8f84e64106fd0cbe..3d937efabd445b8f250f09ee64d53f6c91feb71d 100644 (file)
@@ -25,8 +25,14 @@ func overallStatus(c *torrent.Client, cancel chan struct{}) {
                                continue
                        }
                        tStats := t.Stats()
+                       cur := tStats.Copy()
                        TorrentStatsM.Lock()
-                       TorrentStats[t.InfoHash()] = tStats
+                       prev := TorrentStats[t.InfoHash()].stats
+                       TorrentStats[t.InfoHash()] = TorrentStat{
+                               stats:   cur,
+                               rxSpeed: cur.BytesReadData.Int64() - prev.BytesReadData.Int64(),
+                               txSpeed: cur.BytesWrittenData.Int64() - prev.BytesWrittenData.Int64(),
+                       }
                        TorrentStatsM.Unlock()
                        peers += tStats.ActivePeers
                }