-
Notifications
You must be signed in to change notification settings - Fork 106
avoid duplicates in split view #616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using ImmichFrame.Core.Api; | ||
| using ImmichFrame.Core.Logic.QueueMutator; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace ImmichFrame.Core.Tests.Logic.QueueMutator; | ||
|
|
||
| [TestFixture] | ||
| public class DuplicateAvoidingQueueMutatorTests | ||
| { | ||
| private IQueueMutator<AssetResponseDto> _queueMutator; | ||
|
|
||
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| _queueMutator = new DuplicateAvoidingQueueMutator(2, new NullLogger<DuplicateAvoidingQueueMutator>()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Mutate_NoItems_Succeeds() | ||
| { | ||
| IEnumerable<AssetResponseDto> result = [new()]; | ||
|
|
||
| Assert.DoesNotThrow(() => result = _queueMutator.Mutate([])); | ||
| Assert.That(result, Is.Empty); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Mutate_NoDuplicates_NoChange() | ||
| { | ||
| List<AssetResponseDto> original = [ | ||
| new(){Id = "aap"}, | ||
| new(){Id = "noot"}, | ||
| new(){Id = "mies"}, | ||
| new(){Id = "aardappel"}, | ||
| ]; | ||
| IEnumerable<AssetResponseDto> result = []; | ||
| IEnumerable<AssetResponseDto> input = new List<AssetResponseDto>(original); | ||
|
|
||
| Assert.DoesNotThrow(() => result = _queueMutator.Mutate(input)); | ||
| Assert.That(result, Is.EqualTo(original)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Mutate_SeparatedDuplicates_NoChange() | ||
| { | ||
| List<AssetResponseDto> original = [ | ||
| new(){Id = "aap"}, | ||
| new(){Id = "mies"}, | ||
| new(){Id = "noot"}, | ||
| new(){Id = "mies"}, | ||
| new(){Id = "aardappel"}, | ||
| new(){Id = "mies"}, | ||
| ]; | ||
| IEnumerable<AssetResponseDto> result = []; | ||
| IEnumerable<AssetResponseDto> input = new List<AssetResponseDto>(original); | ||
|
|
||
| Assert.DoesNotThrow(() => result = _queueMutator.Mutate(input)); | ||
| Assert.That(result, Is.EqualTo(original)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Mutate_AdjacentDuplicates_ShouldSeparate() | ||
| { | ||
| List<AssetResponseDto> original = [ | ||
| new(){Id = "aap"}, | ||
| new(){Id = "aap"}, | ||
| new(){Id = "noot"}, | ||
| new(){Id = "mies"}, | ||
| new(){Id = "aardappel"}, | ||
| ]; | ||
| IEnumerable<AssetResponseDto> result = []; | ||
| IEnumerable<AssetResponseDto> input = new List<AssetResponseDto>(original); | ||
|
|
||
| Assert.DoesNotThrow(() => result = _queueMutator.Mutate(input)); | ||
| var assetResponseDtos = result as AssetResponseDto[] ?? result.ToArray(); | ||
| for(int i = 0; i < assetResponseDtos.Length - 1; i++) | ||
| { | ||
| Assert.That(assetResponseDtos.ElementAt(i).Id, Is.Not.EqualTo(assetResponseDtos.ElementAt(i + 1).Id)); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Qoen1 marked this conversation as resolved.
|
||
| [Test] | ||
| public void Mutate_AdjacentDuplicates_differentBatch_NoChange() | ||
| { | ||
| List<AssetResponseDto> original = [ | ||
| new(){Id = "aap"}, | ||
| new(){Id = "noot"}, | ||
| new(){Id = "noot"}, | ||
| new(){Id = "mies"}, | ||
| new(){Id = "aardappel"}, | ||
| ]; | ||
| IEnumerable<AssetResponseDto> result = []; | ||
| IEnumerable<AssetResponseDto> input = new List<AssetResponseDto>(original); | ||
|
|
||
| Assert.DoesNotThrow(() => result = _queueMutator.Mutate(input)); | ||
| Assert.That(result, Is.EqualTo(original)); | ||
| } | ||
| [Test] | ||
| public void Mutate_LandscapeDuplicates_adjacent_NoChange() | ||
| { | ||
| List<AssetResponseDto> original = [ | ||
| new(){Id = "aap"}, | ||
| new(){Id = "peter"}, | ||
| new(){Id = "noot", ExifInfo =new(){Orientation = "1", ExifImageWidth = 200, ExifImageHeight = 100}}, | ||
| new(){Id = "noot", ExifInfo =new(){Orientation = "1", ExifImageWidth = 200, ExifImageHeight = 100}}, | ||
| new(){Id = "mies"}, | ||
| new(){Id = "aardappel"}, | ||
| ]; | ||
| IEnumerable<AssetResponseDto> result = []; | ||
| IEnumerable<AssetResponseDto> input = new List<AssetResponseDto>(original); | ||
|
|
||
| Assert.DoesNotThrow(() => result = _queueMutator.Mutate(input)); | ||
| Assert.That(result, Is.EqualTo(original)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,5 +14,40 @@ public static Stream SaveDataUrlToStream(string dataUrl) | |
|
|
||
| return new MemoryStream(binData); | ||
| } | ||
|
|
||
| public static double GetAspectRatioFloat(double width, double height) | ||
| { | ||
| if (width <= 0 || height <= 0) | ||
| throw new ArgumentException("Width and height must be positive."); | ||
|
|
||
| return width / height; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| public static bool IsLandscape(double exifWidth, double exifHeight, string exifOrientation) | ||
| { | ||
| double width; | ||
| double height; | ||
| if (exifOrientation == "1" | ||
| || exifOrientation == "2" | ||
| || exifOrientation == "3" | ||
| || exifOrientation == "4") | ||
| { | ||
| width = exifWidth; | ||
| height = exifHeight; | ||
| }else if (exifOrientation == "5" | ||
| || exifOrientation == "6" | ||
| || exifOrientation == "7" | ||
| || exifOrientation == "8") | ||
| { | ||
| height = exifWidth; | ||
| width = exifHeight; | ||
| } | ||
| else | ||
| { | ||
| throw new ArgumentException(nameof(exifOrientation)); | ||
| } | ||
|
|
||
| return GetAspectRatioFloat(width, height) > 1; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The client gates split view on |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using ImmichFrame.Core.Api; | ||
| using ImmichFrame.Core.Helpers; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace ImmichFrame.Core.Logic.QueueMutator; | ||
|
|
||
| public class DuplicateAvoidingQueueMutator: QueueMutatorBase<AssetResponseDto> | ||
| { | ||
| private readonly int _batchSize; | ||
| private readonly ILogger<DuplicateAvoidingQueueMutator> _logger; | ||
|
|
||
| public DuplicateAvoidingQueueMutator(int batchSize, ILogger<DuplicateAvoidingQueueMutator> logger) | ||
| { | ||
| if(batchSize <= 0) throw new ArgumentException("Batch size must be greater than 0", nameof(batchSize)); | ||
| _batchSize = batchSize; | ||
| _logger = logger; | ||
| } | ||
| public override IEnumerable<AssetResponseDto> Mutate(IEnumerable<AssetResponseDto> source) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The server-side logic ignores videos and also does fixed batches of two, this has to be changed to match the client-logic. (See my other comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel trying to keep the server-side and client side logic in sync like this is fragile as it could accidentally diverge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we instead have this PR just prevent duplicates in a single asset batch. That way we don't need all the isPortrait/isLandscape logic which can go out of sync with the client There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this would mean that the batch sizes are non deterministic, not sure if that could cause any issues. I don't think it will. what do you think of this @JW-CH updating this to a DuplicateRemovingQueueMutator |
||
| { | ||
| var remaining = new Queue<AssetResponseDto>(source); | ||
| List<AssetResponseDto> result = new(remaining.Count); | ||
| var batch = new List<AssetResponseDto>(_batchSize); | ||
|
|
||
| int consecutiveDuplicates = 0; | ||
| while (remaining.Count > 0) | ||
| { | ||
| var currentItem = remaining.Dequeue(); | ||
|
|
||
| bool landscape = false; | ||
| try | ||
| { | ||
|
|
||
| landscape = ImageHelper.IsLandscape(currentItem.ExifInfo.ExifImageWidth!.Value, | ||
| currentItem.ExifInfo.ExifImageHeight!.Value, currentItem.ExifInfo.Orientation); | ||
| } | ||
| catch | ||
| { | ||
| _logger.LogWarning("Failed to determine orientation for asset {AssetId}, defaulting to portrait", currentItem.Id); | ||
| } | ||
|
|
||
|
|
||
| if (!landscape && batch.Any(x => x.Id == currentItem.Id)) | ||
| { | ||
| remaining.Enqueue(currentItem); | ||
| consecutiveDuplicates++; | ||
| // no other items to process, break to avoid infinite loop | ||
| if (consecutiveDuplicates >= remaining.Count) break; | ||
| continue; | ||
| } | ||
|
|
||
| batch.Add(currentItem); | ||
|
|
||
| if (batch.Count >= _batchSize) | ||
| { | ||
| result.AddRange(batch); | ||
| batch.Clear(); | ||
| } | ||
|
|
||
| consecutiveDuplicates = 0; | ||
| } | ||
|
|
||
| if (batch.Count > 0) | ||
| { | ||
| result.AddRange(batch); | ||
| } | ||
|
|
||
| return Next?.Mutate(result) ?? result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using System.Diagnostics.Contracts; | ||
|
|
||
| namespace ImmichFrame.Core.Logic.QueueMutator; | ||
|
|
||
| public interface IQueueMutator<T> | ||
| { | ||
| [Pure] | ||
| IEnumerable<T> Mutate(IEnumerable<T> source); | ||
| void SetNext(IQueueMutator<T> next); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace ImmichFrame.Core.Logic.QueueMutator; | ||
|
|
||
| public abstract class QueueMutatorBase<T>: IQueueMutator<T> | ||
| { | ||
| protected IQueueMutator<T>? Next; | ||
|
|
||
| public abstract IEnumerable<T> Mutate(IEnumerable<T> source); | ||
|
|
||
| public void SetNext(IQueueMutator<T> next) | ||
| { | ||
| Next = next; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace ImmichFrame.Core.Logic.QueueMutator; | ||
|
|
||
| public class RandomQueueMutator<T>: QueueMutatorBase<T> | ||
| { | ||
| private readonly Random _random = new(); | ||
| public override IEnumerable<T> Mutate(IEnumerable<T> source) | ||
| { | ||
| var result = source.OrderBy(_ => _random.Next()); | ||
| return Next?.Mutate(result) ?? result; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.