1package transmissionrpc
2
3import (
4	"fmt"
5
6	"github.com/hekmon/cunits"
7)
8
9/*
10	Session Statistics
11	https://github.com/transmission/transmission/blob/2.9x/extras/rpc-spec.txt#L546
12*/
13
14// SessionStats returns all (current/cumulative) statistics.
15// https://github.com/transmission/transmission/blob/2.9x/extras/rpc-spec.txt#L548
16func (c *Client) SessionStats() (stats *SessionStats, err error) {
17	if err = c.rpcCall("session-stats", nil, &stats); err != nil {
18		err = fmt.Errorf("'session-stats' rpc method failed: %v", err)
19	}
20	return
21}
22
23// SessionStats represents all (current/cumulative) statistics.
24// https://github.com/transmission/transmission/blob/2.9x/extras/rpc-spec.txt#L554
25type SessionStats struct {
26	ActiveTorrentCount int64            `json:"activeTorrentCount"`
27	CumulativeStats    *CumulativeStats `json:"cumulative-stats"`
28	CurrentStats       *CurrentStats    `json:"current-stats"`
29	DownloadSpeed      int64            `json:"downloadSpeed"`
30	PausedTorrentCount int64            `json:"pausedTorrentCount"`
31	TorrentCount       int64            `json:"torrentCount"`
32	UploadSpeed        int64            `json:"uploadSpeed"`
33}
34
35// CumulativeStats is subset of SessionStats.
36// https://github.com/transmission/transmission/blob/2.9x/extras/rpc-spec.txt#L562
37type CumulativeStats struct {
38	DownloadedBytes int64 `json:"downloadedBytes"`
39	FilesAdded      int64 `json:"filesAdded"`
40	SecondsActive   int64 `json:"secondsActive"`
41	SessionCount    int64 `json:"sessionCount"`
42	UploadedBytes   int64 `json:"uploadedBytes"`
43}
44
45// GetDownloaded returns cumulative stats downloaded size in a handy format
46func (cs *CumulativeStats) GetDownloaded() (downloaded cunits.Bits) {
47	return cunits.ImportInByte(float64(cs.DownloadedBytes))
48}
49
50// GetUploaded returns cumulative stats uploaded size in a handy format
51func (cs *CumulativeStats) GetUploaded() (uploaded cunits.Bits) {
52	return cunits.ImportInByte(float64(cs.UploadedBytes))
53}
54
55// CurrentStats is subset of SessionStats.
56// https://github.com/transmission/transmission/blob/2.9x/extras/rpc-spec.txt#L570
57type CurrentStats struct {
58	DownloadedBytes int64 `json:"downloadedBytes"`
59	FilesAdded      int64 `json:"filesAdded"`
60	SecondsActive   int64 `json:"secondsActive"`
61	SessionCount    int64 `json:"sessionCount"`
62	UploadedBytes   int64 `json:"uploadedBytes"`
63}
64
65// GetDownloaded returns current stats downloaded size in a handy format
66func (cs *CurrentStats) GetDownloaded() (downloaded cunits.Bits) {
67	return cunits.ImportInByte(float64(cs.DownloadedBytes))
68}
69
70// GetUploaded returns current stats uploaded size in a handy format
71func (cs *CurrentStats) GetUploaded() (uploaded cunits.Bits) {
72	return cunits.ImportInByte(float64(cs.UploadedBytes))
73}
74