diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-01-07 18:41:27 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2024-01-07 18:41:27 -0600 |
| commit | 9f4b853959435b69df02f703f95fa112e571896e (patch) | |
| tree | 81bc7d315a00f223020e899d88350581bdf5fa15 /src | |
| parent | f41badad5074668ac36f07a58bbf971e01678ce6 (diff) | |
| download | slskd-9f4b853959435b69df02f703f95fa112e571896e.tar.gz | |
implement Summarize()
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 59 |
1 files changed, 53 insertions, 6 deletions
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 @@ -95,6 +95,17 @@ namespace slskd.Transfers.Uploads void Remove(Guid id); /// <summary> + /// Returns a summary of the uploads matching the specified <paramref name="expression"/>. This can be expensive; + /// consider caching. + /// </summary> + /// <param name="expression">The expression used to select uploads for summarization.</param> + /// <returns> + /// The generated summary, including the total number of transfers, the number of unique files and directories, and + /// total size in bytes. + /// </returns> + (int Transfers, int Files, int Directories, long Bytes) Summarize(Expression<Func<Transfer, bool>> expression); + + /// <summary> /// Cancels the upload matching the specified <paramref name="id"/>, if it is in progress. /// </summary> /// <param name="id">The unique identifier for the upload.</param> @@ -146,10 +157,10 @@ namespace slskd.Transfers.Uploads private ISoulseekClient Client { get; set; } private IDbContextFactory<TransfersDbContext> ContextFactory { get; } private ILogger Log { get; } = Serilog.Log.ForContext<UploadService>(); - private IShareService Shares { get; set; } - private IUserService Users { get; set; } private IOptionsMonitor<Options> OptionsMonitor { get; } private IRelayService Relay { get; } + private IShareService Shares { get; set; } + private IUserService Users { get; set; } /// <summary> /// Adds the specified <paramref name="transfer"/>. 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 @@ -537,6 +548,42 @@ namespace slskd.Transfers.Uploads } /// <summary> + /// Returns a summary of the uploads matching the specified <paramref name="expression"/>. This can be expensive; + /// consider caching. + /// </summary> + /// <param name="expression">The expression used to select uploads for summarization.</param> + /// <returns> + /// The generated summary, including the total number of transfers, the number of unique files and directories, and + /// total size in bytes. + /// </returns> + public (int Transfers, int Files, int Directories, long Bytes) Summarize(Expression<Func<Transfer, bool>> 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<string>(); + var directories = new HashSet<string>(); + + 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); + } + + /// <summary> /// Cancels the upload matching the specified <paramref name="id"/>, if it is in progress. /// </summary> /// <param name="id">The unique identifier for the upload.</param> |
