From 580c7132c60105c5b60578237ee72112d20237d3 Mon Sep 17 00:00:00 2001 From: Roger Far Date: Wed, 11 Feb 2026 20:22:28 -0700 Subject: [PATCH] Cleanup. --- server/RdtClient.Data/Data/TorrentData.cs | 8 +- .../Models/Internal/TorrentDto.cs | 1 - .../BackgroundServices/WebsocketsUpdater.cs | 8 +- .../RdtClient.Service/Services/QBittorrent.cs | 9 +- .../Services/RemoteService.cs | 120 ++++++------- .../Controllers/TorrentsController.cs | 159 +++++++++--------- 6 files changed, 156 insertions(+), 149 deletions(-) diff --git a/server/RdtClient.Data/Data/TorrentData.cs b/server/RdtClient.Data/Data/TorrentData.cs index c42e07f..73d9697 100644 --- a/server/RdtClient.Data/Data/TorrentData.cs +++ b/server/RdtClient.Data/Data/TorrentData.cs @@ -17,10 +17,10 @@ public class TorrentData(DataContext dataContext) : ITorrentData try { _torrentCache ??= await dataContext.Torrents - .AsNoTracking() - .AsSplitQuery() - .Include(m => m.Downloads) - .ToListAsync(); + .AsNoTracking() + .AsSplitQuery() + .Include(m => m.Downloads) + .ToListAsync(); return [.. _torrentCache.OrderBy(m => m.Priority ?? 9999).ThenBy(m => m.Added)]; } diff --git a/server/RdtClient.Data/Models/Internal/TorrentDto.cs b/server/RdtClient.Data/Models/Internal/TorrentDto.cs index d04f64e..ba51494 100644 --- a/server/RdtClient.Data/Models/Internal/TorrentDto.cs +++ b/server/RdtClient.Data/Models/Internal/TorrentDto.cs @@ -1,5 +1,4 @@ using RdtClient.Data.Enums; -using RdtClient.Data.Models.Data; using RdtClient.Data.Models.TorrentClient; namespace RdtClient.Data.Models.Internal; diff --git a/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs b/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs index 1b58fc1..d78f8ec 100644 --- a/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs +++ b/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs @@ -22,7 +22,7 @@ public class WebsocketsUpdater(ILogger logger, IServiceProvid { using var scope = serviceProvider.CreateScope(); var remoteService = scope.ServiceProvider.GetRequiredService(); - + await remoteService.Update(); } catch (Exception ex) @@ -30,10 +30,10 @@ public class WebsocketsUpdater(ILogger logger, IServiceProvid logger.LogError(ex, $"Unexpected error occurred in WebsocketsUpdater: {ex.Message}"); } - var delay = RdtHub.HasConnections - ? TimeSpan.FromSeconds(1) + var delay = RdtHub.HasConnections + ? TimeSpan.FromSeconds(1) : TimeSpan.FromSeconds(5); - + await Task.Delay(delay, stoppingToken); } diff --git a/server/RdtClient.Service/Services/QBittorrent.cs b/server/RdtClient.Service/Services/QBittorrent.cs index 2525afe..580784b 100644 --- a/server/RdtClient.Service/Services/QBittorrent.cs +++ b/server/RdtClient.Service/Services/QBittorrent.cs @@ -221,23 +221,26 @@ public class QBittorrent(ILogger logger, Settings settings, Authent var rdProgress = Math.Clamp(torrent.RdProgress ?? 0.0, 0.0, 100.0) / 100.0; var bytesTotal = torrent.RdSize ?? 0; var speed = torrent.RdSpeed ?? 0; - var bytesDone = (Int64) (bytesTotal * rdProgress); + var bytesDone = (Int64)(bytesTotal * rdProgress); Double downloadProgress = 0; + if (torrent.Downloads is { Count: > 0 }) { var dlBytesDone = torrent.Downloads.Sum(m => m.BytesDone); var dlBytesTotal = torrent.Downloads.Sum(m => m.BytesTotal); - speed = (Int32) torrent.Downloads.Average(m => m.Speed); - downloadProgress = bytesTotal > 0 ? Math.Clamp((Double) dlBytesDone / dlBytesTotal, 0.0, 1.0) : 0; + speed = (Int32)torrent.Downloads.Average(m => m.Speed); + downloadProgress = bytesTotal > 0 ? Math.Clamp((Double)dlBytesDone / dlBytesTotal, 0.0, 1.0) : 0; } var progress = (rdProgress + downloadProgress) / 2.0; var remaining = TimeSpan.Zero; var bytesRemaining = bytesTotal - bytesDone; + if (speed > 0 && bytesRemaining > 0) { remaining = TimeSpan.FromSeconds(bytesRemaining / (Double)speed); + // In case there is clock skew if (remaining < TimeSpan.Zero) { diff --git a/server/RdtClient.Service/Services/RemoteService.cs b/server/RdtClient.Service/Services/RemoteService.cs index b0171dd..d778c85 100644 --- a/server/RdtClient.Service/Services/RemoteService.cs +++ b/server/RdtClient.Service/Services/RemoteService.cs @@ -10,65 +10,67 @@ public class RemoteService(IHubContext hub, Torrents torrents) var allTorrents = await torrents.Get(); var torrentDtos = allTorrents.Select(torrent => new TorrentDto - { - TorrentId = torrent.TorrentId, - Hash = torrent.Hash, - Category = torrent.Category, - DownloadAction = torrent.DownloadAction, - FinishedAction = torrent.FinishedAction, - FinishedActionDelay = torrent.FinishedActionDelay, - HostDownloadAction = torrent.HostDownloadAction, - DownloadMinSize = torrent.DownloadMinSize, - IncludeRegex = torrent.IncludeRegex, - ExcludeRegex = torrent.ExcludeRegex, - DownloadManualFiles = torrent.DownloadManualFiles, - DownloadClient = torrent.DownloadClient, - Added = torrent.Added, - FilesSelected = torrent.FilesSelected, - Completed = torrent.Completed, - IsFile = torrent.IsFile, - Priority = torrent.Priority, - RetryCount = torrent.RetryCount, - DownloadRetryAttempts = torrent.DownloadRetryAttempts, - TorrentRetryAttempts = torrent.TorrentRetryAttempts, - DeleteOnError = torrent.DeleteOnError, - Lifetime = torrent.Lifetime, - Error = torrent.Error, - RdId = torrent.RdId, - RdName = torrent.RdName, - RdSize = torrent.RdSize, - RdHost = torrent.RdHost, - RdSplit = torrent.RdSplit, - RdProgress = torrent.RdProgress, - RdStatus = torrent.RdStatus, - RdStatusRaw = torrent.RdStatusRaw, - RdAdded = torrent.RdAdded, - RdEnded = torrent.RdEnded, - RdSpeed = torrent.RdSpeed, - RdSeeders = torrent.RdSeeders, - Files = torrent.Files, - Downloads = torrent.Downloads.Select(download => new DownloadDto - { - DownloadId = download.DownloadId, - TorrentId = download.TorrentId, - Path = download.Path, - Link = download.Link, - Added = download.Added, - DownloadQueued = download.DownloadQueued, - DownloadStarted = download.DownloadStarted, - DownloadFinished = download.DownloadFinished, - UnpackingQueued = download.UnpackingQueued, - UnpackingStarted = download.UnpackingStarted, - UnpackingFinished = download.UnpackingFinished, - Completed = download.Completed, - RetryCount = download.RetryCount, - Error = download.Error, - BytesTotal = download.BytesTotal, - BytesDone = download.BytesDone, - Speed = download.Speed - }).ToList() - }).ToList(); - + { + TorrentId = torrent.TorrentId, + Hash = torrent.Hash, + Category = torrent.Category, + DownloadAction = torrent.DownloadAction, + FinishedAction = torrent.FinishedAction, + FinishedActionDelay = torrent.FinishedActionDelay, + HostDownloadAction = torrent.HostDownloadAction, + DownloadMinSize = torrent.DownloadMinSize, + IncludeRegex = torrent.IncludeRegex, + ExcludeRegex = torrent.ExcludeRegex, + DownloadManualFiles = torrent.DownloadManualFiles, + DownloadClient = torrent.DownloadClient, + Added = torrent.Added, + FilesSelected = torrent.FilesSelected, + Completed = torrent.Completed, + IsFile = torrent.IsFile, + Priority = torrent.Priority, + RetryCount = torrent.RetryCount, + DownloadRetryAttempts = torrent.DownloadRetryAttempts, + TorrentRetryAttempts = torrent.TorrentRetryAttempts, + DeleteOnError = torrent.DeleteOnError, + Lifetime = torrent.Lifetime, + Error = torrent.Error, + RdId = torrent.RdId, + RdName = torrent.RdName, + RdSize = torrent.RdSize, + RdHost = torrent.RdHost, + RdSplit = torrent.RdSplit, + RdProgress = torrent.RdProgress, + RdStatus = torrent.RdStatus, + RdStatusRaw = torrent.RdStatusRaw, + RdAdded = torrent.RdAdded, + RdEnded = torrent.RdEnded, + RdSpeed = torrent.RdSpeed, + RdSeeders = torrent.RdSeeders, + Files = torrent.Files, + Downloads = torrent.Downloads.Select(download => new DownloadDto + { + DownloadId = download.DownloadId, + TorrentId = download.TorrentId, + Path = download.Path, + Link = download.Link, + Added = download.Added, + DownloadQueued = download.DownloadQueued, + DownloadStarted = download.DownloadStarted, + DownloadFinished = download.DownloadFinished, + UnpackingQueued = download.UnpackingQueued, + UnpackingStarted = download.UnpackingStarted, + UnpackingFinished = download.UnpackingFinished, + Completed = download.Completed, + RetryCount = download.RetryCount, + Error = download.Error, + BytesTotal = download.BytesTotal, + BytesDone = download.BytesDone, + Speed = download.Speed + }) + .ToList() + }) + .ToList(); + await hub.Clients.All.SendCoreAsync("update", [ torrentDtos diff --git a/server/RdtClient.Web/Controllers/TorrentsController.cs b/server/RdtClient.Web/Controllers/TorrentsController.cs index 5edadef..db6840c 100644 --- a/server/RdtClient.Web/Controllers/TorrentsController.cs +++ b/server/RdtClient.Web/Controllers/TorrentsController.cs @@ -22,64 +22,66 @@ public class TorrentsController(ILogger logger, Torrents tor var results = await torrents.Get(); var torrentDtos = results.Select(torrent => new TorrentDto - { - TorrentId = torrent.TorrentId, - Hash = torrent.Hash, - Category = torrent.Category, - DownloadAction = torrent.DownloadAction, - FinishedAction = torrent.FinishedAction, - FinishedActionDelay = torrent.FinishedActionDelay, - HostDownloadAction = torrent.HostDownloadAction, - DownloadMinSize = torrent.DownloadMinSize, - IncludeRegex = torrent.IncludeRegex, - ExcludeRegex = torrent.ExcludeRegex, - DownloadManualFiles = torrent.DownloadManualFiles, - DownloadClient = torrent.DownloadClient, - Added = torrent.Added, - FilesSelected = torrent.FilesSelected, - Completed = torrent.Completed, - IsFile = torrent.IsFile, - Priority = torrent.Priority, - RetryCount = torrent.RetryCount, - DownloadRetryAttempts = torrent.DownloadRetryAttempts, - TorrentRetryAttempts = torrent.TorrentRetryAttempts, - DeleteOnError = torrent.DeleteOnError, - Lifetime = torrent.Lifetime, - Error = torrent.Error, - RdId = torrent.RdId, - RdName = torrent.RdName, - RdSize = torrent.RdSize, - RdHost = torrent.RdHost, - RdSplit = torrent.RdSplit, - RdProgress = torrent.RdProgress, - RdStatus = torrent.RdStatus, - RdStatusRaw = torrent.RdStatusRaw, - RdAdded = torrent.RdAdded, - RdEnded = torrent.RdEnded, - RdSpeed = torrent.RdSpeed, - RdSeeders = torrent.RdSeeders, - Files = torrent.Files, - Downloads = torrent.Downloads.Select(download => new DownloadDto - { - DownloadId = download.DownloadId, - TorrentId = download.TorrentId, - Path = download.Path, - Link = download.Link, - Added = download.Added, - DownloadQueued = download.DownloadQueued, - DownloadStarted = download.DownloadStarted, - DownloadFinished = download.DownloadFinished, - UnpackingQueued = download.UnpackingQueued, - UnpackingStarted = download.UnpackingStarted, - UnpackingFinished = download.UnpackingFinished, - Completed = download.Completed, - RetryCount = download.RetryCount, - Error = download.Error, - BytesTotal = download.BytesTotal, - BytesDone = download.BytesDone, - Speed = download.Speed - }).ToList() - }).ToList(); + { + TorrentId = torrent.TorrentId, + Hash = torrent.Hash, + Category = torrent.Category, + DownloadAction = torrent.DownloadAction, + FinishedAction = torrent.FinishedAction, + FinishedActionDelay = torrent.FinishedActionDelay, + HostDownloadAction = torrent.HostDownloadAction, + DownloadMinSize = torrent.DownloadMinSize, + IncludeRegex = torrent.IncludeRegex, + ExcludeRegex = torrent.ExcludeRegex, + DownloadManualFiles = torrent.DownloadManualFiles, + DownloadClient = torrent.DownloadClient, + Added = torrent.Added, + FilesSelected = torrent.FilesSelected, + Completed = torrent.Completed, + IsFile = torrent.IsFile, + Priority = torrent.Priority, + RetryCount = torrent.RetryCount, + DownloadRetryAttempts = torrent.DownloadRetryAttempts, + TorrentRetryAttempts = torrent.TorrentRetryAttempts, + DeleteOnError = torrent.DeleteOnError, + Lifetime = torrent.Lifetime, + Error = torrent.Error, + RdId = torrent.RdId, + RdName = torrent.RdName, + RdSize = torrent.RdSize, + RdHost = torrent.RdHost, + RdSplit = torrent.RdSplit, + RdProgress = torrent.RdProgress, + RdStatus = torrent.RdStatus, + RdStatusRaw = torrent.RdStatusRaw, + RdAdded = torrent.RdAdded, + RdEnded = torrent.RdEnded, + RdSpeed = torrent.RdSpeed, + RdSeeders = torrent.RdSeeders, + Files = torrent.Files, + Downloads = torrent.Downloads.Select(download => new DownloadDto + { + DownloadId = download.DownloadId, + TorrentId = download.TorrentId, + Path = download.Path, + Link = download.Link, + Added = download.Added, + DownloadQueued = download.DownloadQueued, + DownloadStarted = download.DownloadStarted, + DownloadFinished = download.DownloadFinished, + UnpackingQueued = download.UnpackingQueued, + UnpackingStarted = download.UnpackingStarted, + UnpackingFinished = download.UnpackingFinished, + Completed = download.Completed, + RetryCount = download.RetryCount, + Error = download.Error, + BytesTotal = download.BytesTotal, + BytesDone = download.BytesDone, + Speed = download.Speed + }) + .ToList() + }) + .ToList(); return Ok(torrentDtos); } @@ -138,26 +140,27 @@ public class TorrentsController(ILogger logger, Torrents tor RdSpeed = torrent.RdSpeed, RdSeeders = torrent.RdSeeders, Files = torrent.Files, - Downloads = (torrent.Downloads).Select(download => new DownloadDto - { - DownloadId = download.DownloadId, - TorrentId = download.TorrentId, - Path = download.Path, - Link = download.Link, - Added = download.Added, - DownloadQueued = download.DownloadQueued, - DownloadStarted = download.DownloadStarted, - DownloadFinished = download.DownloadFinished, - UnpackingQueued = download.UnpackingQueued, - UnpackingStarted = download.UnpackingStarted, - UnpackingFinished = download.UnpackingFinished, - Completed = download.Completed, - RetryCount = download.RetryCount, - Error = download.Error, - BytesTotal = download.BytesTotal, - BytesDone = download.BytesDone, - Speed = download.Speed - }).ToList() + Downloads = torrent.Downloads.Select(download => new DownloadDto + { + DownloadId = download.DownloadId, + TorrentId = download.TorrentId, + Path = download.Path, + Link = download.Link, + Added = download.Added, + DownloadQueued = download.DownloadQueued, + DownloadStarted = download.DownloadStarted, + DownloadFinished = download.DownloadFinished, + UnpackingQueued = download.UnpackingQueued, + UnpackingStarted = download.UnpackingStarted, + UnpackingFinished = download.UnpackingFinished, + Completed = download.Completed, + RetryCount = download.RetryCount, + Error = download.Error, + BytesTotal = download.BytesTotal, + BytesDone = download.BytesDone, + Speed = download.Speed + }) + .ToList() }; return Ok(torrentDto);