- Add more indepth stats like number of torrents, profile details etc
- Add torrent ingest endpoints - Add issue template
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/sirrobot01/decypharr/internal/request"
|
||||
debridTypes "github.com/sirrobot01/decypharr/pkg/debrid/types"
|
||||
"github.com/sirrobot01/decypharr/pkg/service"
|
||||
"net/http"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func (s *Server) handleIngests(w http.ResponseWriter, r *http.Request) {
|
||||
ingests := make([]debridTypes.IngestData, 0)
|
||||
svc := service.GetService()
|
||||
if svc.Debrid == nil {
|
||||
http.Error(w, "Debrid service is not enabled", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
for _, cache := range svc.Debrid.Caches {
|
||||
if cache == nil {
|
||||
s.logger.Error().Msg("Debrid cache is nil, skipping")
|
||||
continue
|
||||
}
|
||||
data, err := cache.GetIngests()
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to get ingests from debrid cache")
|
||||
http.Error(w, "Failed to get ingests: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
ingests = append(ingests, data...)
|
||||
}
|
||||
|
||||
request.JSONResponse(w, ingests, 200)
|
||||
}
|
||||
|
||||
func (s *Server) handleIngestsByDebrid(w http.ResponseWriter, r *http.Request) {
|
||||
debridName := chi.URLParam(r, "debrid")
|
||||
if debridName == "" {
|
||||
http.Error(w, "Debrid name is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
svc := service.GetService()
|
||||
if svc.Debrid == nil {
|
||||
http.Error(w, "Debrid service is not enabled", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
cache, exists := svc.Debrid.Caches[debridName]
|
||||
if !exists {
|
||||
http.Error(w, "Debrid cache not found: "+debridName, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := cache.GetIngests()
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to get ingests from debrid cache")
|
||||
http.Error(w, "Failed to get ingests: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
request.JSONResponse(w, data, 200)
|
||||
}
|
||||
|
||||
func (s *Server) handleStats(w http.ResponseWriter, r *http.Request) {
|
||||
var memStats runtime.MemStats
|
||||
runtime.ReadMemStats(&memStats)
|
||||
|
||||
stats := map[string]any{
|
||||
// Memory stats
|
||||
"heap_alloc_mb": fmt.Sprintf("%.2fMB", float64(memStats.HeapAlloc)/1024/1024),
|
||||
"total_alloc_mb": fmt.Sprintf("%.2fMB", float64(memStats.TotalAlloc)/1024/1024),
|
||||
"memory_used": fmt.Sprintf("%.2fMB", float64(memStats.Sys)/1024/1024),
|
||||
|
||||
// GC stats
|
||||
"gc_cycles": memStats.NumGC,
|
||||
// Goroutine stats
|
||||
"goroutines": runtime.NumGoroutine(),
|
||||
|
||||
// System info
|
||||
"num_cpu": runtime.NumCPU(),
|
||||
|
||||
// OS info
|
||||
"os": runtime.GOOS,
|
||||
"arch": runtime.GOARCH,
|
||||
"go_version": runtime.Version(),
|
||||
}
|
||||
|
||||
svc := service.GetService()
|
||||
if svc.Debrid == nil {
|
||||
request.JSONResponse(w, stats, http.StatusOK)
|
||||
return
|
||||
}
|
||||
clients := svc.Debrid.GetDebrids()
|
||||
profiles := make([]*debridTypes.Profile, 0)
|
||||
for debridName, client := range clients {
|
||||
profile, err := client.GetProfile()
|
||||
profile.Name = debridName
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Msg("Failed to get debrid profile")
|
||||
continue
|
||||
}
|
||||
cache, ok := svc.Debrid.Caches[debridName]
|
||||
if ok {
|
||||
// Get torrent data
|
||||
profile.LibrarySize = len(cache.GetTorrents())
|
||||
profile.BadTorrents = len(cache.GetListing("__bad__"))
|
||||
profile.ActiveLinks = cache.GetTotalActiveDownloadLinks()
|
||||
|
||||
}
|
||||
profiles = append(profiles, profile)
|
||||
}
|
||||
stats["debrids"] = profiles
|
||||
request.JSONResponse(w, stats, http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user