Add bandwidth tracking

This commit is contained in:
Ben Adrian Sarmiento
2024-06-24 23:04:31 +02:00
parent ec25c1ef17
commit e9e31759c1
3 changed files with 131 additions and 19 deletions

View File

@@ -29,7 +29,9 @@ type RootResponse struct {
Infuse string `json:"infuse"`
Logs string `json:"logs"`
UserInfo *realdebrid.User `json:"user_info"`
RDTrafficUsed uint64 `json:"rd_traffic_used"`
TrafficLogged uint64 `json:"traffic_logged"`
RequestedMB uint64 `json:"requested_mb"`
ServedMB uint64 `json:"served_mb"`
LibrarySize int `json:"library_size"` // Number of torrents in the library
TorrentsToRepair string `json:"repair_queue"` // List of torrents in the repair queue
MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB
@@ -82,10 +84,10 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
sort.Strings(sortedIDs)
// check if real-debrid.com is in the traffic details
var rdTrafficUsed int64
rdTrafficUsed = 0
var trafficLogged int64
trafficLogged = 0
if _, ok := trafficDetails["real-debrid.com"]; ok {
rdTrafficUsed = trafficDetails["real-debrid.com"]
trafficLogged = trafficDetails["real-debrid.com"]
}
userInfo.Premium = userInfo.Premium / 86400
@@ -101,7 +103,9 @@ func (zr *Handlers) generateResponse(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)),
TrafficLogged: bToMb(uint64(trafficLogged)),
RequestedMB: bToMb(zr.downloader.RequestedBytes.Load()),
ServedMB: bToMb(zr.downloader.TotalBytes.Load()),
LibrarySize: allTorrents.Count(),
TorrentsToRepair: repairQueueStr,
MemAlloc: bToMb(mem.Alloc),
@@ -190,6 +194,12 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Logs,
)
denominator := response.RequestedMB
if denominator == 0 {
denominator = 1
}
efficiency := response.ServedMB / denominator
out += fmt.Sprintf(`
<tr>
<td>Library Size</td>
@@ -216,8 +226,20 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td colspan="2">%d</td>
</tr>
<tr>
<td>RD Traffic Used</td>
<td colspan="2">%d GB</td>
<td>Traffic Logged</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>Traffic Requested</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>Traffic Served</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>Traffic Efficiency</td>
<td colspan="2">%d%% (wasted %d MB)</td>
</tr>`,
response.LibrarySize,
response.MemAlloc,
@@ -225,7 +247,11 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Sys,
response.NumGC,
response.PID,
response.RDTrafficUsed,
response.TrafficLogged,
response.RequestedMB,
response.ServedMB,
efficiency,
response.RequestedMB-response.ServedMB,
)
out += fmt.Sprintf(`
@@ -467,7 +493,3 @@ 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
}