diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-12-28 16:39:39 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-12-28 16:39:39 -0600 |
| commit | 130449908748762449b41b93af45b884b5265255 (patch) | |
| tree | d7a9e8a5f5b3af6dc3667adf72c2088da8f1c009 /src | |
| parent | 7b2deffc16b632b084750cd0be535a509ff3d253 (diff) | |
| download | slskd-130449908748762449b41b93af45b884b5265255.tar.gz | |
add UserUploadStatistics and method to derive
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Types/UserUploadStatistics.cs | 35 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 51 |
2 files changed, 86 insertions, 0 deletions
diff --git a/src/slskd/Transfers/Types/UserUploadStatistics.cs b/src/slskd/Transfers/Types/UserUploadStatistics.cs new file mode 100644 index 00000000..945964f5 --- /dev/null +++ b/src/slskd/Transfers/Types/UserUploadStatistics.cs @@ -0,0 +1,35 @@ +// <copyright file="UserUploadStatistics.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd.Transfers; + +/// <summary> +/// Upload statistics, for use when evaluating incoming enqueue requests against configured limits. +/// </summary> +public record UserUploadStatistics +{ + public int QueuedFiles { get; set; } + public long QueuedBytes { get; set; } + + public int WeeklyFailedFiles { get; set; } + public int WeeklySucceededFiles { get; set; } + public long WeeklySucceededBytes { get; set; } + + public int DailyFailedFiles { get; set; } + public int DailySucceededFiles { get; set; } + public long DailySucceededBytes { get; set; } +}
\ No newline at end of file diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 67f71647..fe2621c8 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -96,6 +96,14 @@ namespace slskd.Transfers.Uploads (int Files, long Bytes) Summarize(Expression<Func<Transfer, bool>> expression); /// <summary> + /// Gets upload statistics for the specified <paramref name="username"/>. + /// </summary> + /// <param name="username">The username of the user.</param> + /// <param name="utcNow">An optional timestamp representing the current time in UTC.</param> + /// <returns>The gathered statistics.</returns> + UserUploadStatistics GetUserStatistics(string username, DateTime? utcNow = null); + + /// <summary> /// Returns a list of all uploads matching the optional <paramref name="expression"/>. /// </summary> /// <param name="expression">An optional expression used to match uploads.</param> @@ -919,6 +927,49 @@ namespace slskd.Transfers.Uploads context.SaveChanges(); } + /// <summary> + /// Gets upload statistics for the specified <paramref name="username"/>. + /// </summary> + /// <param name="username">The username of the user.</param> + /// <param name="utcNow">An optional timestamp representing the current time in UTC.</param> + /// <returns>The gathered statistics.</returns> + public UserUploadStatistics GetUserStatistics(string username, DateTime? utcNow = null) + { + utcNow ??= DateTime.UtcNow; + var dailyCutoff = utcNow.Value.AddDays(-1); + var weeklyCutoff = utcNow.Value.AddDays(-7); + + var failedStateIds = TransferStateCategories.Failed.ToList(); + var successState = TransferStates.Completed | TransferStates.Succeeded; + + using var context = ContextFactory.CreateDbContext(); + + var stats = context.Transfers + .AsNoTracking() + .Where(t => t.Username == username) + .Where(t => t.Direction == TransferDirection.Upload) + .Where(t => t.EndedAt == null || t.StartedAt >= weeklyCutoff) + .GroupBy(t => 1) // aggregate everything into one row + .Select(g => new UserUploadStatistics + { + QueuedFiles = g.Count(t => t.EndedAt == null), + QueuedBytes = g.Where(t => t.EndedAt == null).Sum(t => (long?)t.Size) ?? 0, + + WeeklyFailedFiles = g.Count(t => t.StartedAt >= weeklyCutoff && failedStateIds.Contains(t.State)), + + WeeklySucceededFiles = g.Count(t => t.StartedAt >= weeklyCutoff && (t.State == successState)), + WeeklySucceededBytes = g.Where(t => t.StartedAt >= weeklyCutoff && (t.State == successState)).Sum(t => (long?)t.Size) ?? 0, + + DailyFailedFiles = g.Count(t => t.StartedAt >= dailyCutoff && failedStateIds.Contains(t.State)), + + DailySucceededFiles = g.Count(t => t.StartedAt >= dailyCutoff && t.State == successState), + DailySucceededBytes = g.Where(t => t.StartedAt >= dailyCutoff && t.State == successState).Sum(t => (long?)t.Size) ?? 0, + }) + .FirstOrDefault(); + + return stats ?? new UserUploadStatistics(); + } + private bool TryFail(Guid id, string exception, TransferStates state) { var t = Find(t => t.Id == id); |
