New table format for cgi-bin

This commit is contained in:
Jeffrey Serio 2023-05-17 18:19:34 -05:00
parent 309fa34b07
commit ceef6b208d
3 changed files with 105 additions and 107 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"html/template"
"log"
"os"
"sort"
@ -10,7 +11,6 @@ import (
"github.com/hekmon/cunits/v2"
"github.com/hyperreal64/transmissionrpc"
"github.com/olekukonko/tablewriter"
)
func byteCountIEC(b int64) string {
@ -60,9 +60,9 @@ func convertTime(seconds int64) string {
}
type trackerStats struct {
LeecherCount int64 `json:"leecherCount"`
SeederCount int64 `json:"seederCount"`
type SessionStat struct {
Label string
Value string
}
type TorrentInfo struct {
@ -73,17 +73,15 @@ type TorrentInfo struct {
Seeders int64
}
func renderTable(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(data)
table.Render()
type TorrentStatsPageData struct {
Date string
SessionStats []SessionStat
CurrentStats []SessionStat
CumulativeStats []SessionStat
TorrentInfo []TorrentInfo
}
func main() {
fmt.Println("20 text/gemini\r")
fmt.Println("```")
transmissionbt, err := transmissionrpc.New("127.0.0.1", "", "", nil)
if err != nil {
log.Fatalln(err)
@ -94,55 +92,37 @@ func main() {
log.Fatalln(err)
}
sessionStats := [][]string{
{"Active torrent count", fmt.Sprintf("%d", stats.ActiveTorrentCount)},
{"Download speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
{"Upload speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
{"Paused torrent count", fmt.Sprintf("%d", stats.PausedTorrentCount)},
{"Torrent count", fmt.Sprintf("%d", stats.TorrentCount)},
sessionStats := []SessionStat{
{Label: "Active torrent count", Value: fmt.Sprintf("%d", stats.ActiveTorrentCount)},
{Label: "Download speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
{Label: "Upload speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
{Label: "Paused torrent count", Value: fmt.Sprintf("%d", stats.PausedTorrentCount)},
{Label: "Torrent count", Value: fmt.Sprintf("%d", stats.TorrentCount)},
}
fmt.Println("SESSION STATS")
renderTable(sessionStats)
fmt.Println()
currentStats := [][]string{
{"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)},
currentStats := []SessionStat{
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.UploadedBytes))},
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.DownloadedBytes))},
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CurrentStats.FilesAdded)},
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CurrentStats.SessionCount)},
{Label: "Time active", Value: convertTime(stats.CurrentStats.SecondsActive)},
}
fmt.Println("CURRENT STATS")
renderTable(currentStats)
fmt.Println()
cumulativeStats := [][]string{
{"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)},
cumulativeStats := []SessionStat{
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.UploadedBytes))},
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.DownloadedBytes))},
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CumulativeStats.FilesAdded)},
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CumulativeStats.SessionCount)},
{Label: "Time active", Value: convertTime(stats.CumulativeStats.SecondsActive)},
}
fmt.Println("CUMULATIVE STATS")
renderTable(cumulativeStats)
fmt.Println()
var torrentInfo = []TorrentInfo{}
var (
leecherCount int64
seederCount int64
)
fmt.Println("TORRENT STATS")
torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{
"activityDate",
"name",
"totalSize",
"trackerStats",
}, nil)
torrents, err := transmissionbt.TorrentGet(context.TODO(), []string{"name", "activityDate", "totalSize", "trackerStats"}, nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
@ -165,14 +145,26 @@ func main() {
return torrentInfo[i].Name < torrentInfo[j].Name
})
for _, torrent := range torrentInfo {
torrentStats := [][]string{
{"Name", torrent.Name},
{"Activity Date", torrent.ActivityDate.String()},
{"Total Size", torrent.TotalSize.GiBString()},
{"Leechers", fmt.Sprintf("%d", torrent.Leechers)},
{"Seeders", fmt.Sprintf("%d", torrent.Seeders)},
data := TorrentStatsPageData{
Date: time.Now().Format(time.UnixDate),
SessionStats: sessionStats,
CurrentStats: currentStats,
CumulativeStats: cumulativeStats,
TorrentInfo: torrentInfo,
}
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
View File

@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"html/template"
"log"
"os"
"sort"
@ -11,6 +10,7 @@ import (
"github.com/hekmon/cunits/v2"
"github.com/hyperreal64/transmissionrpc"
"github.com/olekukonko/tablewriter"
)
func byteCountIEC(b int64) string {
@ -60,9 +60,9 @@ func convertTime(seconds int64) string {
}
type SessionStat struct {
Label string
Value string
type trackerStats struct {
LeecherCount int64 `json:"leecherCount"`
SeederCount int64 `json:"seederCount"`
}
type TorrentInfo struct {
@ -73,16 +73,18 @@ type TorrentInfo struct {
Seeders int64
}
type TorrentStatsPageData struct {
Date string
SessionStats []SessionStat
CurrentStats []SessionStat
CumulativeStats []SessionStat
TorrentInfo []TorrentInfo
func renderTable(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(data)
table.Render()
}
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 {
log.Fatalln(err)
}
@ -92,37 +94,55 @@ func main() {
log.Fatalln(err)
}
sessionStats := []SessionStat{
{Label: "Active torrent count", Value: fmt.Sprintf("%d", stats.ActiveTorrentCount)},
{Label: "Download speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
{Label: "Upload speed", Value: fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
{Label: "Paused torrent count", Value: fmt.Sprintf("%d", stats.PausedTorrentCount)},
{Label: "Torrent count", Value: fmt.Sprintf("%d", stats.TorrentCount)},
sessionStats := [][]string{
{"Active torrent count", fmt.Sprintf("%d", stats.ActiveTorrentCount)},
{"Download speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.DownloadSpeed))},
{"Upload speed", fmt.Sprintf("%s/sec", byteCountIEC(stats.UploadSpeed))},
{"Paused torrent count", fmt.Sprintf("%d", stats.PausedTorrentCount)},
{"Torrent count", fmt.Sprintf("%d", stats.TorrentCount)},
}
currentStats := []SessionStat{
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.UploadedBytes))},
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CurrentStats.DownloadedBytes))},
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CurrentStats.FilesAdded)},
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CurrentStats.SessionCount)},
{Label: "Time active", Value: convertTime(stats.CurrentStats.SecondsActive)},
fmt.Println("SESSION STATS")
renderTable(sessionStats)
fmt.Println()
currentStats := [][]string{
{"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{
{Label: "Uploaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.UploadedBytes))},
{Label: "Downloaded bytes", Value: fmt.Sprintf("%s", byteCountIEC(stats.CumulativeStats.DownloadedBytes))},
{Label: "Files added", Value: fmt.Sprintf("%d", stats.CumulativeStats.FilesAdded)},
{Label: "Session count", Value: fmt.Sprintf("%d", stats.CumulativeStats.SessionCount)},
{Label: "Time active", Value: convertTime(stats.CumulativeStats.SecondsActive)},
fmt.Println("CURRENT STATS")
renderTable(currentStats)
fmt.Println()
cumulativeStats := [][]string{
{"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 (
leecherCount 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 {
fmt.Fprintln(os.Stderr, err)
} else {
@ -145,26 +165,12 @@ func main() {
return torrentInfo[i].Name < torrentInfo[j].Name
})
data := TorrentStatsPageData{
Date: time.Now().Format(time.UnixDate),
SessionStats: sessionStats,
CurrentStats: currentStats,
CumulativeStats: cumulativeStats,
TorrentInfo: torrentInfo,
torrentStats := [][]string{
{"Name", "Activity Date", "Total Size", "Leechers", "Seeders"},
}
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)
for _, torrent := range torrentInfo {
torrentStats = append(torrentStats, []string{torrent.Name, torrent.ActivityDate.String(), torrent.TotalSize.GiBString(), fmt.Sprintf("%d", torrent.Leechers), fmt.Sprintf("%d", torrent.Seeders)})
}
renderTable(torrentStats)
}