diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-01-15 18:45:50 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2024-01-15 18:45:50 -0600 |
| commit | 9c76af6ec66ccc16c6e956203fbdfb8fc2a4a05f (patch) | |
| tree | 59271fdc5bcab74e877042d14b756aced31f35dd | |
| parent | b427be45760c693bb7e0b39237e78183968f8eac (diff) | |
| download | slskd-9c76af6ec66ccc16c6e956203fbdfb8fc2a4a05f.tar.gz | |
go back to summary, simplify greatly
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 55 |
1 files changed, 24 insertions, 31 deletions
diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 6f01219e..6f03526c 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -76,12 +76,10 @@ namespace slskd.Transfers.Uploads /// consider caching. /// </summary> /// <param name="expression">The expression used to select uploads for summarization.</param> - /// <param name="hypothetical">The hypotheticaly new transfer.</param> /// <returns> - /// The generated summary, including the total number of transfers, the number of unique files and directories, and - /// total size in bytes. + /// The generated summary, including the number of files and total size in bytes. /// </returns> - (int Transfers, int Files, int Directories, long Bytes) Hypothesize(Expression<Func<Transfer, bool>> expression, Transfer hypothetical); + (int Files, long Bytes) Summarize(Expression<Func<Transfer, bool>> expression); /// <summary> /// Returns a list of all uploads matching the optional <paramref name="expression"/>. @@ -449,41 +447,36 @@ namespace slskd.Transfers.Uploads /// consider caching. /// </summary> /// <param name="expression">The expression used to select uploads for summarization.</param> - /// <param name="hypothetical">The hypotheticaly new transfer.</param> /// <returns> - /// The generated summary, including the total number of transfers, the number of unique files and directories, and - /// total size in bytes. + /// The generated summary, including the number of files and total size in bytes. /// </returns> - public (int Transfers, int Files, int Directories, long Bytes) Hypothesize(Expression<Func<Transfer, bool>> expression, Transfer hypothetical) + public (int Files, 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); + expression ??= t => true; - records.Add(hypothetical); + try + { + using var context = ContextFactory.CreateDbContext(); - int transfers = 0; - long bytes = 0; - var files = new HashSet<string>(); - var directories = new HashSet<string>(); + var stats = context.Transfers + .AsNoTracking() + .Where(t => t.Direction == TransferDirection.Upload) + .Where(expression) + .GroupBy(t => true) // https://stackoverflow.com/a/25489456 + .Select(t => new + { + Files = t.Count(), + Bytes = t.Sum(x => x.Size), + }) + .FirstOrDefault(); - foreach (var t in records) + return (stats?.Files ?? 0, stats?.Bytes ?? 0); + } + catch (Exception ex) { - 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()); + Log.Error(ex, "Failed to list uploads: {Message}", ex.Message); + throw; } - - Log.Warning("Directories: {@Directories}", directories); - Log.Warning("Files: {@Files}", files); - - return (transfers, files.Count, directories.Count, bytes); } /// <summary> |
