From 9f4b853959435b69df02f703f95fa112e571896e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 7 Jan 2024 18:41:27 -0600 Subject: implement Summarize() --- src/slskd/Transfers/Uploads/UploadService.cs | 59 +++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 56bf3751..ba3dea77 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -94,6 +94,17 @@ namespace slskd.Transfers.Uploads /// The unique identifier of the upload. void Remove(Guid id); + /// + /// Returns a summary of the uploads matching the specified . This can be expensive; + /// consider caching. + /// + /// The expression used to select uploads for summarization. + /// + /// The generated summary, including the total number of transfers, the number of unique files and directories, and + /// total size in bytes. + /// + (int Transfers, int Files, int Directories, long Bytes) Summarize(Expression> expression); + /// /// Cancels the upload matching the specified , if it is in progress. /// @@ -146,10 +157,10 @@ namespace slskd.Transfers.Uploads private ISoulseekClient Client { get; set; } private IDbContextFactory ContextFactory { get; } private ILogger Log { get; } = Serilog.Log.ForContext(); - private IShareService Shares { get; set; } - private IUserService Users { get; set; } private IOptionsMonitor OptionsMonitor { get; } private IRelayService Relay { get; } + private IShareService Shares { get; set; } + private IUserService Users { get; set; } /// /// Adds the specified . Supersedes any existing record for the same file and username. @@ -199,8 +210,8 @@ namespace slskd.Transfers.Uploads if (host == Program.LocalHostName) { - // if it's local, do a quick check to see if it exists to spare the caller from - // queueing up if the transfer is doomed to fail. for remote files, take a leap of faith. + // if it's local, do a quick check to see if it exists to spare the caller from queueing up if the transfer is + // doomed to fail. for remote files, take a leap of faith. var info = new FileInfo(localFilename); if (!info.Exists) @@ -360,8 +371,8 @@ namespace slskd.Transfers.Uploads transfer = transfer.WithSoulseekTransfer(completedTransfer); } - // explicitly dispose the rate limiter to prevent updates from it - // beyond this point, which may overwrite the final state + // explicitly dispose the rate limiter to prevent updates from it beyond this point, which may overwrite the + // final state rateLimiter.Dispose(); // todo: broadcast @@ -536,6 +547,42 @@ namespace slskd.Transfers.Uploads } } + /// + /// Returns a summary of the uploads matching the specified . This can be expensive; + /// consider caching. + /// + /// The expression used to select uploads for summarization. + /// + /// The generated summary, including the total number of transfers, the number of unique files and directories, and + /// total size in bytes. + /// + public (int Transfers, int Files, int Directories, long Bytes) Summarize(Expression> expression) + { + // get all matching transfers, regardless of whether they have been removed from the UI + // note that this could be a raw SQL query, and if it were the performance would be much better + // however, passing an expression wouldn't work. if performance becomes an issue, consider passing in + // a query object instead of an expression and then converting this to a raw query. + var records = List(expression, includeRemoved: true); + + int transfers = 0; + long bytes = 0; + var files = new HashSet(); + var directories = new HashSet(); + + foreach (var t in records) + { + transfers++; + bytes += t.Size; + files.Add(t.Filename); + + // this sucks, but this ensures history works even in cases where someone moves the app + data between windows and + // linux. if this causes a huge performance problem, i'll revisit. + directories.Add(t.Filename.NormalizePathForSoulseek().GetNormalizedDirectoryName()); + } + + return (transfers, files.Count, directories.Count, bytes); + } + /// /// Cancels the upload matching the specified , if it is in progress. /// -- cgit v1.2.3