added status API endpoint

This commit is contained in:
2024-12-19 13:08:25 +01:00
parent 17fb608f45
commit f9e829dce6
8 changed files with 230 additions and 5 deletions

View File

@@ -0,0 +1,65 @@
package repository
import (
"context"
"slices"
"github.com/marcopeocchi/yt-dlp-web-ui/v3/server/internal"
"github.com/marcopeocchi/yt-dlp-web-ui/v3/server/status/domain"
)
type Repository struct {
mdb *internal.MemoryDB
}
// DownloadSpeed implements domain.Repository.
func (r *Repository) DownloadSpeed(ctx context.Context) int64 {
processes := r.mdb.All()
var downloadSpeed float64
for _, p := range *processes {
downloadSpeed += p.Progress.Speed
}
return int64(downloadSpeed)
}
// Completed implements domain.Repository.
func (r *Repository) Completed(ctx context.Context) int {
processes := r.mdb.All()
completed := slices.DeleteFunc(*processes, func(p internal.ProcessResponse) bool {
return p.Progress.Status != internal.StatusCompleted
})
return len(completed)
}
// Downloading implements domain.Repository.
func (r *Repository) Downloading(ctx context.Context) int {
processes := r.mdb.All()
downloading := slices.DeleteFunc(*processes, func(p internal.ProcessResponse) bool {
return p.Progress.Status != internal.StatusDownloading
})
return len(downloading)
}
// Pending implements domain.Repository.
func (r *Repository) Pending(ctx context.Context) int {
processes := r.mdb.All()
pending := slices.DeleteFunc(*processes, func(p internal.ProcessResponse) bool {
return p.Progress.Status != internal.StatusPending
})
return len(pending)
}
func New(mdb *internal.MemoryDB) domain.Repository {
return &Repository{
mdb: mdb,
}
}