Add traffic monitor

This commit is contained in:
Ben Adrian Sarmiento
2024-06-24 13:05:11 +02:00
parent 921908c890
commit 4049fa7d83
5 changed files with 113 additions and 14 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"os"
"runtime"
"sort"
"strings"
"github.com/debridmediamanager/zurg/internal/config"
@@ -25,7 +26,8 @@ type RootResponse struct {
Dav string `json:"dav"`
Infuse string `json:"infuse"`
Logs string `json:"logs"`
UserInfo *realdebrid.User `json:"user_info"` // Replace UserInfoType with the actual type
UserInfo *realdebrid.User `json:"user_info"`
RDTrafficUsed uint64 `json:"rd_traffic_used"`
LibrarySize int `json:"library_size"` // Number of torrents in the library
RepairQueueStr string `json:"repair_queue"` // List of torrents in the repair queue
MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB
@@ -35,7 +37,8 @@ type RootResponse struct {
PID int `json:"pid"` // Process ID
Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links
Config config.ZurgConfig `json:"config"`
OnceDoneBin []string `json:"once_done_bin"`
IDsToDelete []string `json:"ids_to_delete"`
Hosts []string `json:"hosts"`
}
func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
@@ -45,6 +48,12 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
return
}
trafficDetails, err := zr.api.GetTrafficDetails()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
@@ -65,6 +74,16 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
}
}
sortedIDs := zr.torMgr.OnceDoneBin.ToSlice()
sort.Strings(sortedIDs)
// check if real-debrid.com is in the traffic details
var rdTrafficUsed int64
rdTrafficUsed = 0
if _, ok := trafficDetails["real-debrid.com"]; ok {
rdTrafficUsed = trafficDetails["real-debrid.com"]
}
response := RootResponse{
Version: version.GetVersion(),
BuiltAt: version.GetBuiltAt(),
@@ -74,6 +93,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
Infuse: fmt.Sprintf("//%s/infuse/", req.Host),
Logs: fmt.Sprintf("//%s/logs/", req.Host),
UserInfo: userInfo,
RDTrafficUsed: bToGb(uint64(rdTrafficUsed)),
LibrarySize: allTorrents.Count(),
RepairQueueStr: repairQueueStr,
MemAlloc: bToMb(mem.Alloc),
@@ -87,7 +107,8 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
Paypal: "https://paypal.me/yowmamasita",
},
Config: zr.cfg.GetConfig(),
OnceDoneBin: zr.torMgr.OnceDoneBin.ToSlice(),
IDsToDelete: sortedIDs,
Hosts: zr.hosts,
}
out := `<table border="1px">
@@ -126,10 +147,6 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td>Library Size</td>
<td colspan="2">%d items</td>
</tr>
<tr>
<td>Repair Queue</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>Memory Allocation</td>
<td colspan="2">%d MB</td>
@@ -150,6 +167,10 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td>Process ID</td>
<td colspan="2">%d</td>
</tr>
<tr>
<td>RD Traffic Used</td>
<td colspan="2">%d GB</td>
</tr>
<tr>
<td rowspan="3">Sponsor Zurg</td>
<td>Patreon</td>
@@ -189,7 +210,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td>%s</td>
</tr>
<tr>
<td rowspan="20">Config</td>
<td rowspan="23">Config</td>
<td>Version</td>
<td>%s</td>
</tr>
@@ -253,6 +274,18 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td>Retries Until Failed</td>
<td>%d</td>
</tr>
<tr>
<td>Auto-Analyze New Torrents</td>
<td>%t</td>
</tr>
<tr>
<td>Cache Network Test Results</td>
<td>%t</td>
</tr>
<tr>
<td>Additional Playable Extensions</td>
<td>%s</td>
</tr>
<tr>
<td>Network Buffer Size</td>
<td>%d bytes</td>
@@ -273,6 +306,14 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td>IDs to be deleted</td>
<td colspan="2">%v</td>
</tr>
<tr>
<td>Repair Queue</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>Hosts</td>
<td colspan="2">%v</td>
</tr>
<tr>
<td>Utilities</td>
<td colspan="2">
@@ -324,12 +365,12 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Logs,
response.Logs,
response.LibrarySize,
response.RepairQueueStr,
response.MemAlloc,
response.TotalAlloc,
response.Sys,
response.NumGC,
response.PID,
response.RDTrafficUsed,
response.Sponsor.Patreon,
response.Sponsor.Patreon,
response.Sponsor.Github,
@@ -360,11 +401,16 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Config.GetApiTimeoutSecs(),
response.Config.GetDownloadTimeoutSecs(),
response.Config.GetRetriesUntilFailed(),
response.Config.ShouldAutoAnalyzeNewTorrents(),
response.Config.ShouldCacheNetworkTestResults(),
response.Config.GetPlayableExtensions(),
response.Config.GetNetworkBufferSize(),
response.Config.ShouldServeFromRclone(),
response.Config.ShouldForceIPv6(),
response.Config.GetOnLibraryUpdate(),
response.OnceDoneBin,
response.IDsToDelete,
response.RepairQueueStr,
response.Hosts,
)
fmt.Fprint(resp, out)
@@ -373,3 +419,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func bToGb(b uint64) uint64 {
return b / 1024 / 1024 / 1024
}