aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-04-30 23:44:56 -0500
committerJP Dillingham <jp@dillingham.me>2026-04-30 23:44:56 -0500
commiteba232fe9de343359b96ca4686c5414912d4b276 (patch)
tree3dd0962298c5608d3c473b69f8d48d872616405a /src
parent4323178f7e795e26bc3906411c6403746f2f8bb5 (diff)
downloadslskd-eba232fe9de343359b96ca4686c5414912d4b276.tar.gz
finalize batchservice
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Transfers/Batches/BatchService.cs79
-rw-r--r--src/slskd/Transfers/Types/Batch.cs18
2 files changed, 12 insertions, 85 deletions
diff --git a/src/slskd/Transfers/Batches/BatchService.cs b/src/slskd/Transfers/Batches/BatchService.cs
index daedbdc6..aadaa820 100644
--- a/src/slskd/Transfers/Batches/BatchService.cs
+++ b/src/slskd/Transfers/Batches/BatchService.cs
@@ -39,7 +39,6 @@ namespace slskd.Transfers
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Serilog;
- using Soulseek;
/// <summary>
/// Manages transfer batches.
@@ -129,33 +128,11 @@ namespace slskd.Transfers
using var context = ContextFactory.CreateDbContext();
- var batch = await context.Batches
+ return await context.Batches
.AsNoTracking()
.Include(b => b.Transfers)
.Where(expression)
.SingleOrDefaultAsync();
-
- if (batch is null)
- {
- return null;
- }
-
- // if the batch record State has a non-null state, it has ended and it is safe to use the
- // stored value. otherwise we must compute it from the associated transfers
- var state = batch.State ?? Batch.DeriveState(batch.Transfers);
- var endedAt = batch.EndedAt ?? batch.Transfers.Max(t => t.EndedAt);
-
- var bytesTransferred = batch.Transfers.Sum(t => t.BytesTransferred);
-
- return batch with
- {
- State = state,
- BytesTransferred = bytesTransferred,
- BytesRemaining = batch.Transfers.Sum(t => t.BytesRemaining),
- PercentComplete = batch.Size == 0 ? 0 : bytesTransferred / (double)batch.Size,
- AverageSpeed = batch.Transfers.Average(t => t.AverageSpeed),
- Removed = batch.Transfers.All(t => t.Removed),
- };
}
/// <summary>
@@ -169,61 +146,11 @@ namespace slskd.Transfers
using var context = ContextFactory.CreateDbContext();
- var batches = await context.Batches
+ return await context.Batches
.AsNoTracking()
+ .Include(b => b.Transfers)
.Where(expression)
.ToListAsync();
-
- if (batches.Count == 0)
- {
- return batches;
- }
-
- var batchIds = batches.Select(b => b.Id).ToList();
-
- var stats = (await context.Transfers
- .AsNoTracking()
- .Where(t => t.BatchId.HasValue && batchIds.Contains(t.BatchId.Value))
- .GroupBy(t => t.BatchId)
- .Select(g => new
- {
- BatchId = g.Key,
- EndedAt = g.Max(t => t.EndedAt),
- BytesTransferred = g.Sum(t => t.BytesTransferred),
- BytesRemaining = g.Sum(t => t.BytesRemaining),
- AverageSpeed = g.Average(t => t.AverageSpeed),
- Removed = g.All(t => t.Removed),
- AnyInProgress = g.Any(t => TransferStateCategories.InProgress.Contains(t.State)),
- AnyQueued = g.Any(t => TransferStateCategories.Queued.Contains(t.State)),
- AllSuccessful = g.All(t => TransferStateCategories.Successful.Contains(t.State)),
- AnyFailed = g.Any(t => TransferStateCategories.Failed.Contains(t.State)),
- })
- .ToListAsync())
- .ToDictionary(s => s.BatchId);
-
- return batches.Select(b =>
- {
- if (!stats.TryGetValue(b.Id, out var s))
- {
- return b;
- }
-
- var state = s.AnyInProgress ? TransferStates.InProgress
- : s.AnyQueued ? TransferStates.Queued
- : s.AllSuccessful ? TransferStates.Completed | TransferStates.Succeeded
- : s.AnyFailed ? TransferStates.Completed | TransferStates.Errored
- : TransferStates.None;
-
- return b with
- {
- BytesTransferred = s.BytesTransferred,
- BytesRemaining = s.BytesRemaining,
- PercentComplete = b.Size == 0 ? 0 : s.BytesTransferred / (double)b.Size,
- AverageSpeed = s.AverageSpeed,
- Removed = s.Removed,
- State = state,
- };
- }).ToList();
}
/// <summary>
diff --git a/src/slskd/Transfers/Types/Batch.cs b/src/slskd/Transfers/Types/Batch.cs
index 3fe692f2..ac7025fb 100644
--- a/src/slskd/Transfers/Types/Batch.cs
+++ b/src/slskd/Transfers/Types/Batch.cs
@@ -87,27 +87,27 @@ public record Batch
public long FileSize { get; init; }
/// <summary>
- /// Gets the timestamp at which the Batch was created.
+ /// Gets the time at which the Batch was created.
/// </summary>
public DateTime CreatedAt { get; init; } = DateTime.UtcNow;
/// <summary>
- /// Gets the optional destination directory for the files in the Batch, relative to the configured
- /// download directory.
+ /// Gets the Transfer records associated with the batch.
/// </summary>
/// <remarks>
- /// If specified, takes precedence over other configured placement rules.
+ /// Lazy loaded. If not explicitly loaded with the Batch record using Include(), the collection and
+ /// all of the (future?) statistics will be null.
/// </remarks>
- public string DestinationDirectory { get; init; }
+ public ICollection<Transfer> Transfers { get; init; } = null;
/// <summary>
- /// Gets the Transfer records associated with the batch.
+ /// Gets the optional destination directory for the files in the Batch, relative to the configured
+ /// download directory.
/// </summary>
/// <remarks>
- /// Lazy loaded. If not explicitly loaded with the Batch record using Include(), the collection and
- /// all of the (future?) statistics will be null.
+ /// If specified, takes precedence over other configured placement rules.
/// </remarks>
- public ICollection<Transfer> Transfers { get; init; } = null;
+ public string DestinationDirectory { get; init; }
/*
future: [NotMapped] properties that aggregate values from the associated Transfer records.