diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-04-26 19:19:12 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-04-26 19:19:12 -0500 |
| commit | 169cd154ba14c97073899b3b6db947419ecf267e (patch) | |
| tree | 1dcb37378517c83e068a19580769e1c3e598e8c1 /src | |
| parent | da732237ce6e87b40ccd0b042255e19e40d27682 (diff) | |
| download | slskd-169cd154ba14c97073899b3b6db947419ecf267e.tar.gz | |
rough in Batch
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Types/Batch.cs | 72 |
1 files changed, 70 insertions, 2 deletions
diff --git a/src/slskd/Transfers/Types/Batch.cs b/src/slskd/Transfers/Types/Batch.cs index 820db66e..30633cc9 100644 --- a/src/slskd/Transfers/Types/Batch.cs +++ b/src/slskd/Transfers/Types/Batch.cs @@ -35,6 +35,9 @@ namespace slskd.Transfers; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using Soulseek; public class Batch { @@ -42,8 +45,73 @@ public class Batch [Key] public Guid Id { get; init; } - public string Destination { get; init; } + public string Username { get; init; } + public TransferDirection Direction { get; } = TransferDirection.Download; + + [NotMapped] + public TransferStates State => ComputeState(); + public DateTime CreatedAt { get; init; } = DateTime.UtcNow; - public ICollection<Transfer> Transfers { get; set; } + [NotMapped] + public DateTime? EndedAt => Transfers.Max(t => t.EndedAt); + + public ICollection<Transfer> Transfers { get; set; } = []; + + public string Destination { get; init; } + + [NotMapped] + public long BytesTransferred => Transfers.Sum(t => t.BytesTransferred); + [NotMapped] + public int Files => Transfers.Count; + [NotMapped] + public long Size => Transfers.Sum(t => t.Size); + [NotMapped] + public long BytesRemaining => Size - BytesTransferred; + [NotMapped] + public TimeSpan ElapsedTime => DateTime.UtcNow - CreatedAt; + [NotMapped] + public double PercentComplete => Size == 0 ? 0 : (BytesTransferred / (double)Size) * 100; + + [NotMapped] + public double AverageSpeed => Transfers + .Where(t => TransferStateCategories.Successful.Contains(t.State)) + .Select(t => t.AverageSpeed) + .DefaultIfEmpty(0) + .Average(); + + [NotMapped] + public bool Removed => Transfers.All(t => t.Removed); + + private TransferStates ComputeState() + { + // if there's a transfer in progress, the batch is in progress + if (Transfers.Any(t => TransferStateCategories.InProgress.Contains(t.State))) + { + return TransferStates.InProgress; + } + + // if no transfers are in progress but at least one is queued (doesn't matter locally or remotely), + // the batch is queued. it doesn't matter if one or more are errored, they might be retried + if (Transfers.Any(t => TransferStateCategories.Queued.Contains(t.State))) + { + return TransferStates.Queued; + } + + // if all transfers completed successfully, the batch did too + if (Transfers.All(t => TransferStateCategories.Successful.Contains(t.State))) + { + return TransferStates.Completed | TransferStates.Succeeded; + } + + // if one or more transfers failed and there are no more enqueued files, + // the batch errored and it will not recover on its own + if (Transfers.Any(t => TransferStateCategories.Failed.Contains(t.State))) + { + return TransferStates.Completed | TransferStates.Errored; + } + + // unclear how we would get here + return TransferStates.None; + } }
\ No newline at end of file |
