diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-04-27 22:13:30 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-04-27 22:13:30 -0500 |
| commit | 2b39b7ee61610ca4526269d321f0ece0be6bb521 (patch) | |
| tree | 6bbb4bf559480accc3b1664d0fc4b4d0382e14f1 | |
| parent | 6daeed10807243ca98718eefe6eeb70e952e553c (diff) | |
| download | slskd-2b39b7ee61610ca4526269d321f0ece0be6bb521.tar.gz | |
make state derivation static
| -rw-r--r-- | src/slskd/Transfers/Types/Batch.cs | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/slskd/Transfers/Types/Batch.cs b/src/slskd/Transfers/Types/Batch.cs index e15ecde3..6b3cf15c 100644 --- a/src/slskd/Transfers/Types/Batch.cs +++ b/src/slskd/Transfers/Types/Batch.cs @@ -48,14 +48,19 @@ public record Batch public Guid Id { get; init; } public string Username { get; init; } public TransferDirection Direction { get; init; } = TransferDirection.Download; - public string Destination { get; init; } public int Files { get; init; } public long Size { get; init; } + public string Destination { get; init; } + public DateTime CreatedAt { get; init; } = DateTime.UtcNow; - [NotMapped] - public TransferStates State { get; init; } + /// <summary> + /// Gets the current batch state. + /// </summary> + /// <remarks> + /// If the database value is null, value must be derived from the associated transfers. + /// </remarks> + public TransferStates? State { get; set; } // todo: set when the last file is completed - public DateTime CreatedAt { get; init; } = DateTime.UtcNow; public DateTime? EndedAt { get; set; } // todo: set when the last file is completed, for performance reasons /// <summary> @@ -84,30 +89,30 @@ public record Batch [JsonIgnore] public bool Removed { get; init; } - private TransferStates ComputeState() + public static TransferStates DeriveState(IEnumerable<Transfer> transfers) { // if there's a transfer in progress, the batch is in progress - if (Transfers.Any(t => TransferStateCategories.InProgress.Contains(t.State))) + 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))) + 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))) + 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))) + if (transfers.Any(t => TransferStateCategories.Failed.Contains(t.State))) { return TransferStates.Completed | TransferStates.Errored; } |
