-
Notifications
You must be signed in to change notification settings - Fork 84
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
Open
Qoen1
wants to merge
3
commits into
immichFrame:main
Choose a base branch
from
Qoen1:feature/avoid-duplicates-server-side
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
ImmichFrame.Core.Tests/Logic/QueueMutator/DuplicateAvoidingQueueMutatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Show resolved
Hide resolved
Qoen1 marked this conversation as resolved.
Show resolved
Hide 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)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
ImmichFrame.Core/Logic/QueueMutator/DuplicateAvoidingQueueMutator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.