2021-11-16 12:13:58 +00:00
|
|
|
package torrent
|
2020-05-02 12:06:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-11-16 12:13:58 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
2020-05-02 12:06:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrTorrentNotFound = errors.New("torrent not found")
|
|
|
|
|
2020-05-08 14:37:43 +00:00
|
|
|
type PieceStatus string
|
2020-05-02 12:06:18 +00:00
|
|
|
|
|
|
|
const (
|
2020-05-08 14:37:43 +00:00
|
|
|
Checking PieceStatus = "H"
|
|
|
|
Partial PieceStatus = "P"
|
|
|
|
Complete PieceStatus = "C"
|
|
|
|
Waiting PieceStatus = "W"
|
|
|
|
Error PieceStatus = "?"
|
2020-05-02 12:06:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PieceChunk struct {
|
2020-05-18 17:42:23 +00:00
|
|
|
Status PieceStatus `json:"status"`
|
|
|
|
NumPieces int `json:"numPieces"`
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TorrentStats struct {
|
2020-05-18 17:42:23 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
DownloadedBytes int64 `json:"downloadedBytes"`
|
|
|
|
UploadedBytes int64 `json:"uploadedBytes"`
|
2020-05-21 16:44:57 +00:00
|
|
|
Peers int `json:"peers"`
|
|
|
|
Seeders int `json:"seeders"`
|
2020-05-18 17:42:23 +00:00
|
|
|
TimePassed float64 `json:"timePassed"`
|
|
|
|
PieceChunks []*PieceChunk `json:"pieceChunks"`
|
|
|
|
TotalPieces int `json:"totalPieces"`
|
2020-06-15 09:14:28 +00:00
|
|
|
PieceSize int64 `json:"pieceSize"`
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
type byName []*TorrentStats
|
|
|
|
|
|
|
|
func (a byName) Len() int { return len(a) }
|
|
|
|
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a byName) Less(i, j int) bool { return a[i].Name < a[j].Name }
|
|
|
|
|
2020-05-02 12:06:18 +00:00
|
|
|
type GlobalTorrentStats struct {
|
2020-05-18 17:42:23 +00:00
|
|
|
DownloadedBytes int64 `json:"downloadedBytes"`
|
|
|
|
UploadedBytes int64 `json:"uploadedBytes"`
|
|
|
|
TimePassed float64 `json:"timePassed"`
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
type RouteStats struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
TorrentStats []*TorrentStats `json:"torrentStats"`
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:09:05 +00:00
|
|
|
type ByName []*RouteStats
|
|
|
|
|
|
|
|
func (a ByName) Len() int { return len(a) }
|
|
|
|
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
type stat struct {
|
2020-05-02 12:06:18 +00:00
|
|
|
totalDownloadBytes int64
|
|
|
|
downloadBytes int64
|
|
|
|
totalUploadBytes int64
|
|
|
|
uploadBytes int64
|
2020-05-21 16:44:57 +00:00
|
|
|
peers int
|
|
|
|
seeders int
|
2020-05-02 12:06:18 +00:00
|
|
|
time time.Time
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
type Stats struct {
|
|
|
|
mut sync.Mutex
|
2020-05-18 17:42:23 +00:00
|
|
|
torrents map[string]*torrent.Torrent
|
2021-11-16 12:13:58 +00:00
|
|
|
torrentsByRoute map[string]map[string]*torrent.Torrent
|
|
|
|
previousStats map[string]*stat
|
2020-05-02 12:06:18 +00:00
|
|
|
|
|
|
|
gTime time.Time
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
func NewStats() *Stats {
|
|
|
|
return &Stats{
|
2020-05-18 17:42:23 +00:00
|
|
|
gTime: time.Now(),
|
|
|
|
torrents: make(map[string]*torrent.Torrent),
|
2021-11-16 12:13:58 +00:00
|
|
|
torrentsByRoute: make(map[string]map[string]*torrent.Torrent),
|
|
|
|
previousStats: make(map[string]*stat),
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 10:07:54 +00:00
|
|
|
func (s *Stats) AddRoute(route string) {
|
|
|
|
_, ok := s.torrentsByRoute[route]
|
|
|
|
if !ok {
|
|
|
|
s.torrentsByRoute[route] = make(map[string]*torrent.Torrent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
func (s *Stats) Add(route string, t *torrent.Torrent) {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
|
|
|
h := t.InfoHash().String()
|
|
|
|
|
|
|
|
s.torrents[h] = t
|
|
|
|
s.previousStats[h] = &stat{}
|
2020-05-18 17:42:23 +00:00
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
_, ok := s.torrentsByRoute[route]
|
|
|
|
if !ok {
|
|
|
|
s.torrentsByRoute[route] = make(map[string]*torrent.Torrent)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.torrentsByRoute[route][h] = t
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
func (s *Stats) Del(route, hash string) {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
delete(s.torrents, hash)
|
|
|
|
delete(s.previousStats, hash)
|
|
|
|
ts, ok := s.torrentsByRoute[route]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(ts, hash)
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
func (s *Stats) Stats(hash string) (*TorrentStats, error) {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2020-05-02 12:06:18 +00:00
|
|
|
t, ok := s.torrents[hash]
|
|
|
|
if !(ok) {
|
|
|
|
return nil, ErrTorrentNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
return s.stats(now, t, true), nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
func (s *Stats) RoutesStats() []*RouteStats {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
var out []*RouteStats
|
|
|
|
for r, tl := range s.torrentsByRoute {
|
|
|
|
var tStats []*TorrentStats
|
|
|
|
for _, t := range tl {
|
|
|
|
ts := s.stats(now, t, true)
|
|
|
|
tStats = append(tStats, ts)
|
|
|
|
}
|
2021-11-16 12:13:58 +00:00
|
|
|
|
|
|
|
sort.Sort(byName(tStats))
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
rs := &RouteStats{
|
|
|
|
Name: r,
|
|
|
|
TorrentStats: tStats,
|
|
|
|
}
|
|
|
|
out = append(out, rs)
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
return out
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
func (s *Stats) GlobalStats() *GlobalTorrentStats {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2020-05-02 12:06:18 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
var totalDownload int64
|
|
|
|
var totalUpload int64
|
|
|
|
for _, torrent := range s.torrents {
|
|
|
|
tStats := s.stats(now, torrent, false)
|
|
|
|
totalDownload += tStats.DownloadedBytes
|
|
|
|
totalUpload += tStats.UploadedBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
timePassed := now.Sub(s.gTime)
|
|
|
|
s.gTime = now
|
|
|
|
|
|
|
|
return &GlobalTorrentStats{
|
|
|
|
DownloadedBytes: totalDownload,
|
|
|
|
UploadedBytes: totalUpload,
|
2020-05-08 14:37:43 +00:00
|
|
|
TimePassed: timePassed.Seconds(),
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
func (s *Stats) stats(now time.Time, t *torrent.Torrent, chunks bool) *TorrentStats {
|
2020-05-02 12:06:18 +00:00
|
|
|
ts := &TorrentStats{}
|
2021-11-16 12:13:58 +00:00
|
|
|
prev, ok := s.previousStats[t.InfoHash().String()]
|
|
|
|
if !ok {
|
|
|
|
return &TorrentStats{}
|
|
|
|
}
|
2020-05-02 12:06:18 +00:00
|
|
|
if s.returnPreviousMeasurements(now) {
|
|
|
|
ts.DownloadedBytes = prev.downloadBytes
|
|
|
|
ts.UploadedBytes = prev.uploadBytes
|
|
|
|
} else {
|
|
|
|
st := t.Stats()
|
|
|
|
rd := st.BytesReadData.Int64()
|
|
|
|
wd := st.BytesWrittenData.Int64()
|
2021-11-16 12:13:58 +00:00
|
|
|
ist := &stat{
|
2020-05-02 12:06:18 +00:00
|
|
|
downloadBytes: rd - prev.totalDownloadBytes,
|
|
|
|
uploadBytes: wd - prev.totalUploadBytes,
|
|
|
|
totalDownloadBytes: rd,
|
|
|
|
totalUploadBytes: wd,
|
|
|
|
time: now,
|
2020-05-21 16:44:57 +00:00
|
|
|
peers: st.TotalPeers,
|
|
|
|
seeders: st.ConnectedSeeders,
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts.DownloadedBytes = ist.downloadBytes
|
|
|
|
ts.UploadedBytes = ist.uploadBytes
|
2020-05-21 16:44:57 +00:00
|
|
|
ts.Peers = ist.peers
|
|
|
|
ts.Seeders = ist.seeders
|
2020-05-02 12:06:18 +00:00
|
|
|
|
|
|
|
s.previousStats[t.InfoHash().String()] = ist
|
|
|
|
}
|
|
|
|
|
2020-05-08 14:37:43 +00:00
|
|
|
ts.TimePassed = now.Sub(prev.time).Seconds()
|
2020-05-18 17:42:23 +00:00
|
|
|
var totalPieces int
|
2020-05-02 12:06:18 +00:00
|
|
|
if chunks {
|
|
|
|
var pch []*PieceChunk
|
|
|
|
for _, psr := range t.PieceStateRuns() {
|
|
|
|
var s PieceStatus
|
|
|
|
switch {
|
|
|
|
case psr.Checking:
|
|
|
|
s = Checking
|
|
|
|
case psr.Partial:
|
|
|
|
s = Partial
|
|
|
|
case psr.Complete:
|
|
|
|
s = Complete
|
|
|
|
case !psr.Ok:
|
|
|
|
s = Error
|
|
|
|
default:
|
|
|
|
s = Waiting
|
|
|
|
}
|
|
|
|
|
|
|
|
pch = append(pch, &PieceChunk{
|
|
|
|
Status: s,
|
|
|
|
NumPieces: psr.Length,
|
|
|
|
})
|
2020-05-18 17:42:23 +00:00
|
|
|
totalPieces += psr.Length
|
2020-05-02 12:06:18 +00:00
|
|
|
}
|
|
|
|
ts.PieceChunks = pch
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
ts.Hash = t.InfoHash().String()
|
|
|
|
ts.Name = t.Name()
|
|
|
|
ts.TotalPieces = totalPieces
|
2020-06-09 10:06:12 +00:00
|
|
|
|
|
|
|
if t.Info() != nil {
|
|
|
|
ts.PieceSize = t.Info().PieceLength
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:18 +00:00
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
const gap time.Duration = 2 * time.Second
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
func (s *Stats) returnPreviousMeasurements(now time.Time) bool {
|
2020-05-02 12:06:18 +00:00
|
|
|
return now.Sub(s.gTime) < gap
|
|
|
|
}
|