From 9c76af6ec66ccc16c6e956203fbdfb8fc2a4a05f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 15 Jan 2024 18:45:50 -0600 Subject: go back to summary, simplify greatly --- src/slskd/Transfers/Uploads/UploadService.cs | 55 ++++++++++++---------------- 1 file changed, 24 insertions(+), 31 deletions(-) (limited to 'src') 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. /// /// The expression used to select uploads for summarization. - /// The hypotheticaly new transfer. /// - /// 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. /// - (int Transfers, int Files, int Directories, long Bytes) Hypothesize(Expression> expression, Transfer hypothetical); + (int Files, long Bytes) Summarize(Expression> expression); /// /// Returns a list of all uploads matching the optional . @@ -449,41 +447,36 @@ namespace slskd.Transfers.Uploads /// consider caching. /// /// The expression used to select uploads for summarization. - /// The hypotheticaly new transfer. /// - /// 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. /// - public (int Transfers, int Files, int Directories, long Bytes) Hypothesize(Expression> expression, Transfer hypothetical) + public (int Files, 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); + expression ??= t => true; - records.Add(hypothetical); + try + { + using var context = ContextFactory.CreateDbContext(); - int transfers = 0; - long bytes = 0; - var files = new HashSet(); - var directories = new HashSet(); + 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); } /// -- cgit v1.2.3