Fix traffic computation and optimize open ended requests

This commit is contained in:
Ben Adrian Sarmiento
2024-07-04 03:39:53 +02:00
parent 6deeb45db0
commit 49432dd810
5 changed files with 100 additions and 106 deletions

View File

@@ -22,29 +22,28 @@ type SponsorResponse struct {
}
type RootResponse struct {
Version string `json:"version"`
BuiltAt string `json:"built_at"`
GitCommit string `json:"git_commit"`
Html string `json:"html"`
Dav string `json:"dav"`
Infuse string `json:"infuse"`
Logs string `json:"logs"`
UserInfo *realdebrid.User `json:"user_info"`
APITraffic uint64 `json:"traffic_from_api"`
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
TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB
Sys uint64 `json:"sys"` // System memory in MB
NumGC uint32 `json:"num_gc"` // Number of completed GC cycles
PID int `json:"pid"` // Process ID
Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links
Config config.ZurgConfig `json:"config"`
Token string `json:"token"`
DownloadTokens []string `json:"download_tokens"`
IDsToDelete []string `json:"ids_to_delete"`
Hosts []string `json:"hosts"`
Version string `json:"version"`
BuiltAt string `json:"built_at"`
GitCommit string `json:"git_commit"`
Html string `json:"html"`
Dav string `json:"dav"`
Infuse string `json:"infuse"`
Logs string `json:"logs"`
UserInfo *realdebrid.User `json:"user_info"`
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
TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB
Sys uint64 `json:"sys"` // System memory in MB
NumGC uint32 `json:"num_gc"` // Number of completed GC cycles
PID int `json:"pid"` // Process ID
Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links
Config config.ZurgConfig `json:"config"`
Token string `json:"token"`
DownloadTokens []string `json:"download_tokens"`
IDsToDelete []string `json:"ids_to_delete"`
Hosts []string `json:"hosts"`
TrafficServedPerAPI uint64 `json:"traffic_served_per_api"`
}
func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request) (*RootResponse, error) {
@@ -98,23 +97,22 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
}
return &RootResponse{
Version: version.GetVersion(),
BuiltAt: version.GetBuiltAt(),
GitCommit: version.GetGitCommit(),
Html: fmt.Sprintf("//%s/http/", req.Host),
Dav: fmt.Sprintf("//%s/dav/", req.Host),
Infuse: fmt.Sprintf("//%s/infuse/", req.Host),
Logs: fmt.Sprintf("//%s/logs/", req.Host),
UserInfo: userInfo,
APITraffic: uint64(trafficFromAPI),
ServedMB: bToMb(zr.downloader.TotalBytes.Load()),
LibrarySize: allTorrents.Count(),
TorrentsToRepair: repairQueueStr,
MemAlloc: bToMb(mem.Alloc),
TotalAlloc: bToMb(mem.TotalAlloc),
Sys: bToMb(mem.Sys),
NumGC: mem.NumGC,
PID: os.Getpid(),
Version: version.GetVersion(),
BuiltAt: version.GetBuiltAt(),
GitCommit: version.GetGitCommit(),
Html: fmt.Sprintf("//%s/http/", req.Host),
Dav: fmt.Sprintf("//%s/dav/", req.Host),
Infuse: fmt.Sprintf("//%s/infuse/", req.Host),
Logs: fmt.Sprintf("//%s/logs/", req.Host),
UserInfo: userInfo,
TrafficServedPerAPI: uint64(trafficFromAPI),
LibrarySize: allTorrents.Count(),
TorrentsToRepair: repairQueueStr,
MemAlloc: bToMb(mem.Alloc),
TotalAlloc: bToMb(mem.TotalAlloc),
Sys: bToMb(mem.Sys),
NumGC: mem.NumGC,
PID: os.Getpid(),
Sponsor: SponsorResponse{
Patreon: "https://www.patreon.com/debridmediamanager",
Github: "https://github.com/sponsors/debridmediamanager",
@@ -197,18 +195,6 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Logs,
)
denominator := bToMb(response.APITraffic - zr.trafficOnStartup.Load())
if denominator == 0 {
denominator = 1
}
efficiency := response.ServedMB * 100 / denominator
if zr.trafficOnStartup.Load() > response.APITraffic {
// it cannot be bigger than traffic logged
// so it must be a reset back to 0
zr.trafficOnStartup.Store(0)
}
out += fmt.Sprintf(`
<tr>
<td>Library Size</td>
@@ -235,16 +221,12 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td colspan="2">%d</td>
</tr>
<tr>
<td>Traffic Logged</td>
<td colspan="2">%d MB (%d MB added)</td>
<td>Traffic Served (main token)</td>
<td colspan="2">%d MB (%d MB since startup)</td>
</tr>
<tr>
<td>Traffic Served</td>
<td>Traffic Served (zurg)</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>Traffic Efficiency</td>
<td colspan="2">%d%% (wasted %d MB) disclaimer: this assumes all RD traffic comes from this zurg instance</td>
</tr>`,
response.LibrarySize,
response.MemAlloc,
@@ -252,11 +234,9 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Sys,
response.NumGC,
response.PID,
bToMb(response.APITraffic),
bToMb(response.APITraffic-zr.trafficOnStartup.Load()),
response.ServedMB,
efficiency,
bToMb(response.APITraffic-zr.trafficOnStartup.Load())-response.ServedMB,
bToMb(response.TrafficServedPerAPI), // traffic served *api*
bToMb(response.TrafficServedPerAPI-zr.downloader.TrafficOnStartup.Load()), // traffic served *api* since startup
bToMb(zr.downloader.TrafficServed.Load()), // traffic served *zurg*
)
out += fmt.Sprintf(`