]> Sergey Matveev's repositories - btrtrc.git/blob - cmd/btrtrc/status.go
cmd/btrtrc client
[btrtrc.git] / cmd / btrtrc / status.go
1 package main
2
3 import (
4         "log"
5         "time"
6
7         "github.com/anacrolix/torrent"
8         "github.com/dustin/go-humanize"
9 )
10
11 func overallStatus(c *torrent.Client) {
12         tick := time.Tick(time.Second)
13         var prev torrent.ConnStats
14         for {
15                 select {
16                 case <-Cancel:
17                         Jobs.Done()
18                         return
19                 case <-tick:
20                 }
21                 stats := c.ConnStats()
22                 var peers int
23                 for _, t := range c.Torrents() {
24                         if t.Info() == nil {
25                                 continue
26                         }
27                         tStats := t.Stats()
28                         cur := tStats.Copy()
29                         TorrentStatsM.Lock()
30                         prev := TorrentStats[t.InfoHash()].stats
31                         TorrentStats[t.InfoHash()] = TorrentStat{
32                                 stats:   cur,
33                                 rxSpeed: cur.BytesReadData.Int64() - prev.BytesReadData.Int64(),
34                                 txSpeed: cur.BytesWrittenData.Int64() - prev.BytesWrittenData.Int64(),
35                         }
36                         TorrentStatsM.Unlock()
37                         peers += tStats.ActivePeers
38                 }
39                 log.Printf(
40                         "%s / %s | %d | %s%d%s / %s%d%s",
41                         humanize.IBytes(uint64(stats.BytesRead.Int64())),
42                         humanize.IBytes(uint64(stats.BytesWritten.Int64())),
43                         peers,
44                         Green, (stats.BytesRead.Int64()-prev.BytesRead.Int64())/1024, Reset,
45                         Magenta, (stats.BytesWritten.Int64()-prev.BytesWritten.Int64())/1024, Reset,
46                 )
47                 prev = stats
48         }
49 }