mirror of
https://codeberg.org/hyperreal/go-transmission-stats
synced 2024-11-25 12:13:42 +01:00
New table format for cgi-bin
This commit is contained in:
parent
309fa34b07
commit
ceef6b208d
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
@ -10,7 +11,6 @@ import (
|
|||||||
|
|
||||||
"github.com/hekmon/cunits/v2"
|
"github.com/hekmon/cunits/v2"
|
||||||
"github.com/hyperreal64/transmissionrpc"
|
"github.com/hyperreal64/transmissionrpc"
|
||||||
"github.com/olekukonko/tablewriter"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func byteCountIEC(b int64) string {
|
func byteCountIEC(b int64) string {
|
||||||
@ -60,9 +60,9 @@ func convertTime(seconds int64) string {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type trackerStats struct {
|
type SessionStat struct {
|
||||||
LeecherCount int64 `json:"leecherCount"`
|
Label string
|
||||||
SeederCount int64 `json:"seederCount"`
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
type TorrentInfo struct {
|
type TorrentInfo struct {
|
||||||
@ -73,17 +73,15 @@ type TorrentInfo struct {
|
|||||||
Seeders int64
|
Seeders int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTable(data [][]string) {
|
type TorrentStatsPageData struct {
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
Date string
|
||||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
SessionStats []SessionStat
|
||||||
table.AppendBulk(data)
|
CurrentStats []SessionStat
|
||||||
table.Render()
|
CumulativeStats []SessionStat
|
||||||
|
TorrentInfo []TorrentInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("20 text/gemini\r")
|
|
||||||
fmt.Println("```")
|
|
||||||
|
|
||||||
transmissionbt, err := transmissionrpc.New("127.0.0.1", "", "", nil)
|
transmissionbt, err := transmissionrpc.New("127.0.0.1", "", "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
@ -94,55 +92,37 @@ func main() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionStats := [][]string{
|
sessionStats := []SessionStat{
|
||||||
{"Active torrent count", fmt.Sprintf("%d", stats.ActiveTorrentCount)},
|
{Label: "Active torrent count", Value: fmt.Sprintf("%d", stats.ActiveTorrentCount)},
|
||||||
{"Download speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
|
{Label: "Download speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
|
||||||
{"Upload speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
|
{Label: "Upload speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
|
||||||
{"Paused torrent count", fmt.Sprintf("%d", stats.PausedTorrentCount)},
|
{Label: "Paused torrent count", Value: fmt.Sprintf("%d", stats.PausedTorrentCount)},
|
||||||
{"Torrent count", fmt.Sprintf("%d", stats.TorrentCount)},
|
{Label: "Torrent count", Value: fmt.Sprintf("%d", stats.TorrentCount)},
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("SESSION STATS")
|
currentStats := []SessionStat{
|
||||||
renderTable(sessionStats)
|
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.UploadedBytes))},
|
||||||
fmt.Println()
|
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.DownloadedBytes))},
|
||||||
|
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CurrentStats.FilesAdded)},
|
||||||
currentStats := [][]string{
|
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CurrentStats.SessionCount)},
|
||||||
{"Uploaded bytes", byteCountIEC(stats.CurrentStats.UploadedBytes)},
|
{Label: "Time active", Value: convertTime(stats.CurrentStats.SecondsActive)},
|
||||||
{"Downloaded bytes", byteCountIEC(stats.CurrentStats.DownloadedBytes)},
|
|
||||||
{"Files added", fmt.Sprintf("%d", stats.CurrentStats.FilesAdded)},
|
|
||||||
{"Session count", fmt.Sprintf("%d", stats.CurrentStats.SessionCount)},
|
|
||||||
{"Time active", convertTime(stats.CurrentStats.SecondsActive)},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("CURRENT STATS")
|
cumulativeStats := []SessionStat{
|
||||||
renderTable(currentStats)
|
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.UploadedBytes))},
|
||||||
fmt.Println()
|
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.DownloadedBytes))},
|
||||||
|
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CumulativeStats.FilesAdded)},
|
||||||
cumulativeStats := [][]string{
|
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CumulativeStats.SessionCount)},
|
||||||
{"Uploaded bytes", byteCountIEC(stats.CumulativeStats.UploadedBytes)},
|
{Label: "Time active", Value: convertTime(stats.CumulativeStats.SecondsActive)},
|
||||||
{"Downloaded bytes", byteCountIEC(stats.CumulativeStats.DownloadedBytes)},
|
|
||||||
{"Files added", fmt.Sprintf("%d", stats.CumulativeStats.FilesAdded)},
|
|
||||||
{"Session count", fmt.Sprintf("%d", stats.CumulativeStats.SessionCount)},
|
|
||||||
{"Time active", convertTime(stats.CumulativeStats.SecondsActive)},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("CUMULATIVE STATS")
|
|
||||||
renderTable(cumulativeStats)
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
var torrentInfo = []TorrentInfo{}
|
var torrentInfo = []TorrentInfo{}
|
||||||
var (
|
var (
|
||||||
leecherCount int64
|
leecherCount int64
|
||||||
seederCount int64
|
seederCount int64
|
||||||
)
|
)
|
||||||
|
|
||||||
fmt.Println("TORRENT STATS")
|
torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{"name", "activityDate", "totalSize", "trackerStats"}, nil)
|
||||||
torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{
|
|
||||||
"activityDate",
|
|
||||||
"name",
|
|
||||||
"totalSize",
|
|
||||||
"trackerStats",
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
} else {
|
} else {
|
||||||
@ -165,14 +145,26 @@ func main() {
|
|||||||
return torrentInfo[i].Name < torrentInfo[j].Name
|
return torrentInfo[i].Name < torrentInfo[j].Name
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, torrent := range torrentInfo {
|
data := TorrentStatsPageData{
|
||||||
torrentStats := [][]string{
|
Date: time.Now().Format(time.UnixDate),
|
||||||
{"Name", torrent.Name},
|
SessionStats: sessionStats,
|
||||||
{"Activity Date", torrent.ActivityDate.String()},
|
CurrentStats: currentStats,
|
||||||
{"Total Size", torrent.TotalSize.GiBString()},
|
CumulativeStats: cumulativeStats,
|
||||||
{"Leechers", fmt.Sprintf("%d", torrent.Leechers)},
|
TorrentInfo: torrentInfo,
|
||||||
{"Seeders", fmt.Sprintf("%d", torrent.Seeders)},
|
}
|
||||||
}
|
|
||||||
renderTable(torrentStats)
|
htmlTemplate, err := template.ParseFiles("template.html")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
htmlFile, err := os.Create(os.Getenv("HTML_FILE"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
defer htmlFile.Close()
|
||||||
|
|
||||||
|
if err = htmlTemplate.Execute(htmlFile, data); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
104
main.go
104
main.go
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
@ -11,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hekmon/cunits/v2"
|
"github.com/hekmon/cunits/v2"
|
||||||
"github.com/hyperreal64/transmissionrpc"
|
"github.com/hyperreal64/transmissionrpc"
|
||||||
|
"github.com/olekukonko/tablewriter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func byteCountIEC(b int64) string {
|
func byteCountIEC(b int64) string {
|
||||||
@ -60,9 +60,9 @@ func convertTime(seconds int64) string {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionStat struct {
|
type trackerStats struct {
|
||||||
Label string
|
LeecherCount int64 `json:"leecherCount"`
|
||||||
Value string
|
SeederCount int64 `json:"seederCount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TorrentInfo struct {
|
type TorrentInfo struct {
|
||||||
@ -73,16 +73,18 @@ type TorrentInfo struct {
|
|||||||
Seeders int64
|
Seeders int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type TorrentStatsPageData struct {
|
func renderTable(data [][]string) {
|
||||||
Date string
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
SessionStats []SessionStat
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||||
CurrentStats []SessionStat
|
table.AppendBulk(data)
|
||||||
CumulativeStats []SessionStat
|
table.Render()
|
||||||
TorrentInfo []TorrentInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
transmissionbt, err := transmissionrpc.New("127.0.0.1", "", "", nil)
|
fmt.Println("20 text/gemini\r")
|
||||||
|
fmt.Println("```")
|
||||||
|
|
||||||
|
transmissionbt, err := transmissionrpc.New("10.0.0.244", "", "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
@ -92,37 +94,55 @@ func main() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionStats := []SessionStat{
|
sessionStats := [][]string{
|
||||||
{Label: "Active torrent count", Value: fmt.Sprintf("%d", stats.ActiveTorrentCount)},
|
{"Active torrent count", fmt.Sprintf("%d", stats.ActiveTorrentCount)},
|
||||||
{Label: "Download speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
|
{"Download speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
|
||||||
{Label: "Upload speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
|
{"Upload speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
|
||||||
{Label: "Paused torrent count", Value: fmt.Sprintf("%d", stats.PausedTorrentCount)},
|
{"Paused torrent count", fmt.Sprintf("%d", stats.PausedTorrentCount)},
|
||||||
{Label: "Torrent count", Value: fmt.Sprintf("%d", stats.TorrentCount)},
|
{"Torrent count", fmt.Sprintf("%d", stats.TorrentCount)},
|
||||||
}
|
}
|
||||||
|
|
||||||
currentStats := []SessionStat{
|
fmt.Println("SESSION STATS")
|
||||||
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.UploadedBytes))},
|
renderTable(sessionStats)
|
||||||
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.DownloadedBytes))},
|
fmt.Println()
|
||||||
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CurrentStats.FilesAdded)},
|
|
||||||
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CurrentStats.SessionCount)},
|
currentStats := [][]string{
|
||||||
{Label: "Time active", Value: convertTime(stats.CurrentStats.SecondsActive)},
|
{"Uploaded bytes", byteCountIEC(stats.CurrentStats.UploadedBytes)},
|
||||||
|
{"Downloaded bytes", byteCountIEC(stats.CurrentStats.DownloadedBytes)},
|
||||||
|
{"Files added", fmt.Sprintf("%d", stats.CurrentStats.FilesAdded)},
|
||||||
|
{"Session count", fmt.Sprintf("%d", stats.CurrentStats.SessionCount)},
|
||||||
|
{"Time active", convertTime(stats.CurrentStats.SecondsActive)},
|
||||||
}
|
}
|
||||||
|
|
||||||
cumulativeStats := []SessionStat{
|
fmt.Println("CURRENT STATS")
|
||||||
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.UploadedBytes))},
|
renderTable(currentStats)
|
||||||
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.DownloadedBytes))},
|
fmt.Println()
|
||||||
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CumulativeStats.FilesAdded)},
|
|
||||||
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CumulativeStats.SessionCount)},
|
cumulativeStats := [][]string{
|
||||||
{Label: "Time active", Value: convertTime(stats.CumulativeStats.SecondsActive)},
|
{"Uploaded bytes", byteCountIEC(stats.CumulativeStats.UploadedBytes)},
|
||||||
|
{"Downloaded bytes", byteCountIEC(stats.CumulativeStats.DownloadedBytes)},
|
||||||
|
{"Files added", fmt.Sprintf("%d", stats.CumulativeStats.FilesAdded)},
|
||||||
|
{"Session count", fmt.Sprintf("%d", stats.CumulativeStats.SessionCount)},
|
||||||
|
{"Time active", convertTime(stats.CumulativeStats.SecondsActive)},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("CUMULATIVE STATS")
|
||||||
|
renderTable(cumulativeStats)
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
var torrentInfo = []TorrentInfo{}
|
var torrentInfo = []TorrentInfo{}
|
||||||
var (
|
var (
|
||||||
leecherCount int64
|
leecherCount int64
|
||||||
seederCount int64
|
seederCount int64
|
||||||
)
|
)
|
||||||
|
|
||||||
torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{"name", "activityDate", "totalSize", "trackerStats"}, nil)
|
fmt.Println("TORRENT STATS")
|
||||||
|
torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{
|
||||||
|
"activityDate",
|
||||||
|
"name",
|
||||||
|
"totalSize",
|
||||||
|
"trackerStats",
|
||||||
|
}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
} else {
|
} else {
|
||||||
@ -145,26 +165,12 @@ func main() {
|
|||||||
return torrentInfo[i].Name < torrentInfo[j].Name
|
return torrentInfo[i].Name < torrentInfo[j].Name
|
||||||
})
|
})
|
||||||
|
|
||||||
data := TorrentStatsPageData{
|
torrentStats := [][]string{
|
||||||
Date: time.Now().Format(time.UnixDate),
|
{"Name", "Activity Date", "Total Size", "Leechers", "Seeders"},
|
||||||
SessionStats: sessionStats,
|
|
||||||
CurrentStats: currentStats,
|
|
||||||
CumulativeStats: cumulativeStats,
|
|
||||||
TorrentInfo: torrentInfo,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlTemplate, err := template.ParseFiles("template.html")
|
for _, torrent := range torrentInfo {
|
||||||
if err != nil {
|
torrentStats = append(torrentStats, []string{torrent.Name, torrent.ActivityDate.String(), torrent.TotalSize.GiBString(), fmt.Sprintf("%d", torrent.Leechers), fmt.Sprintf("%d", torrent.Seeders)})
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
htmlFile, err := os.Create(os.Getenv("HTML_FILE"))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
defer htmlFile.Close()
|
|
||||||
|
|
||||||
if err = htmlTemplate.Execute(htmlFile, data); err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
}
|
||||||
|
renderTable(torrentStats)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user