]> Sergey Matveev's repositories - btrtrc.git/commitdiff
fifos/top-seed
authorSergey Matveev <stargrave@stargrave.org>
Mon, 28 Nov 2022 15:26:30 +0000 (18:26 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 13 Jan 2023 08:32:42 +0000 (11:32 +0300)
cmd/btrtrc/USAGE
cmd/btrtrc/fifos.go
cmd/btrtrc/main.go
cmd/btrtrc/sort.go

index 40d58782a38d62031bd4ddd9c4bd5dfaa41177f8..8800d9b368061b370eff3bec68c95f90798eb057 100644 (file)
@@ -46,6 +46,7 @@ fifos subdirectory will be created with following FIFO files:
   * amount of downloaded/uploaded traffic during current session
   * remote address with port
   * client's name
+* fifos/top-seed -- list torrents sorted by total transfer data amount
 
 For each torrent, corresponding .torrent file will be created.
 Additional symbolic link with torrent's name will lead to HASH.torrent.
index cb6139c8a7bd7023be0df3189fd01e3ea7fa4bc3..c7466926bdf662a61adab449eaa55316c6071046 100644 (file)
@@ -223,6 +223,47 @@ func fifoDHTList(c *torrent.Client) {
        }
 }
 
+type topTorrent struct {
+       infoHash metainfo.Hash
+       name     string
+       tx       int64
+}
+
+func fifoTopSeed(c *torrent.Client) {
+       pth := path.Join(FIFOsDir, "top-seed")
+       recreateFIFO(pth)
+       for {
+               fd, err := os.OpenFile(pth, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
+               if err != nil {
+                       log.Println("OpenFile:", pth, err)
+                       time.Sleep(time.Second)
+                       continue
+               }
+               var ts []*topTorrent
+               for _, t := range c.Torrents() {
+                       if t.Info() == nil {
+                               continue
+                       }
+                       ts = append(ts, &topTorrent{
+                               infoHash: t.InfoHash(),
+                               name:     t.Name(),
+                               tx:       TxStats[t.InfoHash()],
+                       })
+               }
+               sort.Sort(ByTxTraffic(ts))
+               for _, t := range ts {
+                       fmt.Fprintf(fd,
+                               "%s%s%s %s%40s%s %s\n",
+                               Blue, t.infoHash.HexString(), Reset,
+                               Green, shortenName(t.name), Reset,
+                               humanize.IBytes(uint64(t.tx)),
+                       )
+               }
+               fd.Close()
+               time.Sleep(time.Second)
+       }
+}
+
 type stringAddr string
 
 func (stringAddr) Network() string   { return "" }
@@ -358,6 +399,7 @@ func fifosCleanup() {
        os.Remove(path.Join(FIFOsDir, "dht"))
        os.Remove(path.Join(FIFOsDir, "add"))
        os.Remove(path.Join(FIFOsDir, "del"))
+       os.Remove(path.Join(FIFOsDir, "top-seed"))
        os.RemoveAll(path.Join(FIFOsDir, PeersDir))
        os.RemoveAll(path.Join(FIFOsDir, FilesDir))
 }
index 880348be67ec56af8ffd1c05e187c6da9280656f..63e6c4fff24a18c9c4e0d2cba14ad5019eae61d6 100644 (file)
@@ -81,6 +81,7 @@ func main() {
        Jobs.Add(1)
        go overallStatus(client)
        go fifoList(client)
+       go fifoTopSeed(client)
        go fifoDHTList(client)
        go fifoAdd(client)
        go fifoDel(client)
index cfb6478c68eda6be163cc026075db62998cdd5af..5b1ad858ff88de7c1e6a006bf0de92281ad1a2ce 100644 (file)
@@ -20,6 +20,20 @@ func (a ByInfoHash) Less(i, j int) bool {
        return a[i].InfoHash().HexString() < a[j].InfoHash().HexString()
 }
 
+type ByTxTraffic []*topTorrent
+
+func (a ByTxTraffic) Len() int {
+       return len(a)
+}
+
+func (a ByTxTraffic) Swap(i, j int) {
+       a[i], a[j] = a[j], a[i]
+}
+
+func (a ByTxTraffic) Less(i, j int) bool {
+       return a[i].tx < a[j].tx
+}
+
 type ByPeerID []*torrent.PeerConn
 
 func (a ByPeerID) Len() int {