Increment traffic monitors async

This commit is contained in:
Ben Adrian Sarmiento
2024-06-25 02:19:54 +02:00
parent 7fc8a4e0c5
commit d095bf2dbf
3 changed files with 40 additions and 26 deletions

View File

@@ -53,12 +53,6 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
return nil, err
}
trafficDetails, err := zr.api.GetTrafficDetails()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return nil, err
}
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
@@ -83,7 +77,11 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
sortedIDs := zr.torMgr.OnceDoneBin.ToSlice()
sort.Strings(sortedIDs)
// check if real-debrid.com is in the traffic details
trafficDetails, err := zr.api.GetTrafficDetails()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return nil, err
}
var trafficLogged int64
trafficLogged = 0
if _, ok := trafficDetails["real-debrid.com"]; ok {
@@ -227,7 +225,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
</tr>
<tr>
<td>Traffic Logged</td>
<td colspan="2">%d MB</td>
<td colspan="2">%d MB (%d MB added)</td>
</tr>
<tr>
<td>Traffic Requested</td>
@@ -248,6 +246,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.NumGC,
response.PID,
response.TrafficLogged,
response.TrafficLogged-bToMb(uint64(zr.initialTraffic)),
response.RequestedMB,
response.ServedMB,
efficiency,

View File

@@ -20,13 +20,14 @@ import (
)
type Handlers struct {
downloader *universal.Downloader
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
workerPool *ants.Pool
hosts []string
log *logutil.Logger
downloader *universal.Downloader
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
workerPool *ants.Pool
hosts []string
initialTraffic int64
log *logutil.Logger
}
func init() {
@@ -46,6 +47,16 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
log: log,
}
trafficDetails, err := api.GetTrafficDetails()
if err != nil {
log.Errorf("Failed to get traffic details: %v", err)
trafficDetails = make(map[string]int64)
}
hs.initialTraffic = 0
if _, ok := trafficDetails["real-debrid.com"]; ok {
hs.initialTraffic = trafficDetails["real-debrid.com"]
}
if cfg.GetUsername() != "" {
router.Use(hs.basicAuth)
}