From 2f5d309ed18f10c659407d5539cb76d89e503376 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Sat, 12 Aug 2023 08:30:08 -0700 Subject: [PATCH] Fix random logic --- Distance/Models/_E_Distance.cs | 2 +- Instance/DlibDotNet.cs | 11 ++++++--- Instance/Models/_F_Random.cs | 13 +++++----- Map/Models/MapLogic.cs | 20 +++++++++------ Map/Models/Stateless/MapLogic.cs | 30 ++++++++++++++++------- Map/Models/Stateless/Methods/IMapLogic.cs | 4 +-- Resize/Models/_C_Resize.cs | 10 ++++---- Shared/.kanbn/index.md | 1 + Shared/.kanbn/tasks/fix-random-logic.md | 11 +++++++++ Shared/Models/Face.cs | 11 --------- 10 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 Shared/.kanbn/tasks/fix-random-logic.md diff --git a/Distance/Models/_E_Distance.cs b/Distance/Models/_E_Distance.cs index 98b3490..e1ad047 100644 --- a/Distance/Models/_E_Distance.cs +++ b/Distance/Models/_E_Distance.cs @@ -194,7 +194,7 @@ public partial class E_Distance : IDistance collection = collection.OrderBy(l => l.Length).ToList(); for (int i = 0; i < collection.Count - 1; i++) { - checkFile = string.Concat(collection[i].FullName, ".dup"); + checkFile = $"{collection[i].FullName}.dup"; if (File.Exists(checkFile)) continue; File.Move(collection[i].FullName, checkFile); diff --git a/Instance/DlibDotNet.cs b/Instance/DlibDotNet.cs index 655816b..13d7bf0 100644 --- a/Instance/DlibDotNet.cs +++ b/Instance/DlibDotNet.cs @@ -438,7 +438,7 @@ public partial class DlibDotNet string[] changesFrom = new string[] { nameof(A_Property) }; FileHolder resizedFileHolder = _Resize.GetResizedFileHolder(cResultsFullGroupDirectory, item, outputResolutionHasNumber); ReadOnlyCollection> locationContainers = mapLogic.GetLocationContainers(item); - if (item.Property is null || item.Property.Id is null || item.Any()) + if (item.Property is null || item.Property.Id is null || !item.SourceDirectoryFileHolder.Exists || item.SourceDirectoryFileHolder.CreationTime is null || item.SourceDirectoryFileHolder.LastWriteTime is null || item.Any()) { LogItemPropertyIsNull(item); int? propertyHashCode = item.Property?.GetHashCode(); @@ -458,7 +458,12 @@ public partial class DlibDotNet else { property = item.Property; - if (item.SourceDirectoryFileHolder.LastWriteTime is not null) + if (_Configuration.PropertyConfiguration.ForcePropertyLastWriteTimeToCreationTime && item.SourceDirectoryFileHolder.LastWriteTime.Value != item.SourceDirectoryFileHolder.CreationTime.Value) + { + File.SetLastWriteTime(item.SourceDirectoryFileHolder.FullName, item.SourceDirectoryFileHolder.CreationTime.Value); + subFileTuples.Add(new Tuple(nameof(A_Property), item.SourceDirectoryFileHolder.CreationTime.Value)); + } + else if (item.SourceDirectoryFileHolder.LastWriteTime is not null) subFileTuples.Add(new Tuple(nameof(A_Property), item.SourceDirectoryFileHolder.LastWriteTime.Value)); else subFileTuples.Add(new Tuple(nameof(A_Property), new FileInfo(item.SourceDirectoryFileHolder.FullName).LastWriteTime)); @@ -1107,8 +1112,8 @@ public partial class DlibDotNet bool filesCollectionCountIsOne = false; List? filesCollection = null; const string directorySearchFilter = "*"; - Dictionary> personKeyToIds; bool configurationOutputResolutionsHas = false; + ReadOnlyDictionary> personKeyToIds; bool runToDoCollectionFirst = GetRunToDoCollectionFirst(ticks); Dictionary> fileNameToCollection; (aResultsFullGroupDirectory, bResultsFullGroupDirectory) = GetResultsFullGroupDirectories(); diff --git a/Instance/Models/_F_Random.cs b/Instance/Models/_F_Random.cs index 2ec2a16..6ad673f 100644 --- a/Instance/Models/_F_Random.cs +++ b/Instance/Models/_F_Random.cs @@ -26,7 +26,7 @@ internal class F_Random return result; } - private static Dictionary> Get(Dictionary> personKeyToIds, ReadOnlyCollection mappingCollection, string dateFormat) + private static ReadOnlyDictionary> GetDayToRelativePaths(ReadOnlyCollection mappingCollection, string dateFormat, ReadOnlyDictionary> idToPersonKeys) { Dictionary> results = new(); string key; @@ -34,7 +34,6 @@ internal class F_Random DateTime dateTime; List? personKeys; List? relativePaths; - Dictionary> idToPersonKeys = Map.Models.Stateless.Methods.IMapLogic.GetIdToPersonKeys(personKeyToIds); foreach (Shared.Models.Mapping mapping in mappingCollection) { if (mapping.MappingFromItem.ImageFileHolder.DirectoryName is null || mapping.MappingFromPerson is null) @@ -56,10 +55,10 @@ internal class F_Random } relativePaths.Add(mapping.MappingFromItem.RelativePath); } - return results; + return new(results); } - internal void Random(Property.Models.Configuration configuration, string[] validKeyWordsToIgnoreInRandom, string outputResolution, Dictionary> personKeyToIds, ReadOnlyCollection mappingCollection) + internal void Random(Property.Models.Configuration configuration, string[] validKeyWordsToIgnoreInRandom, string outputResolution, ReadOnlyDictionary> personKeyToIds, ReadOnlyCollection mappingCollection) { string key; string json; @@ -70,7 +69,8 @@ internal class F_Random List relativePaths = new(); List distinctCollection = new(); DateTime dateTime = new(2024, 1, 1); //Leap year - Dictionary> dayToRelativePaths = Get(personKeyToIds, mappingCollection, dateFormat); + ReadOnlyDictionary> idToPersonKeys = Map.Models.Stateless.Methods.IMapLogic.GetIdToPersonKeys(personKeyToIds); + ReadOnlyDictionary> dayToRelativePaths = GetDayToRelativePaths(mappingCollection, dateFormat, idToPersonKeys); string fRandomCollectionDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(F_Random), "[]"); string[] files = Directory.GetFiles(fRandomCollectionDirectory, "*", SearchOption.TopDirectoryOnly); foreach (string file in files) @@ -90,8 +90,9 @@ internal class F_Random { for (int i = 0; i < 366; i++) { + random = new(i); key = dateTime.AddDays(i).ToString(dateFormat); - if (dayToRelativePaths.TryGetValue(key, out collection) && collection.Count > 10) + if (dayToRelativePaths.TryGetValue(key, out collection) && collection.Count > 100) collection = (from l in collection orderby random.NextDouble() select l).ToList(); else collection = (from l in relativePaths orderby random.NextDouble() select l).ToList(); diff --git a/Map/Models/MapLogic.cs b/Map/Models/MapLogic.cs index ca79a78..11148bc 100644 --- a/Map/Models/MapLogic.cs +++ b/Map/Models/MapLogic.cs @@ -149,7 +149,7 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic return new(results); } - public Dictionary> GetPersonKeyToIds() + public ReadOnlyDictionary> GetPersonKeyToIds() { Dictionary> results = new(); long personKey; @@ -158,7 +158,9 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic PersonBirthday personBirthday; List shouldMove = new(); foreach (KeyValuePair>> idToCollection in _IdThenWholePercentagesToPersonContainers) + { foreach (KeyValuePair> wholePercentagesToPersonContainers in idToCollection.Value) + { foreach (PersonContainer personContainer in wholePercentagesToPersonContainers.Value) { if (personContainer.Key is null || personContainer.Birthdays is null || personContainer.Birthdays.Length == 0) @@ -177,9 +179,11 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic continue; collection.Add(idToCollection.Key); } + } + } if (shouldMove.Count > 0) throw new Exception(string.Join(Environment.NewLine, shouldMove)); - return results; + return new(results); } (bool, ReadOnlyDictionary>?) Shared.Models.Methods.IMapLogic.GetWholePercentagesToPersonContainers(int id) @@ -759,7 +763,7 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic return new(personKeyFormatted, personBirthday); } - private List GetPersonKeyFormattedCollection(string[] jLinks, string a2PeopleContentDirectory, ReadOnlyCollection personContainers, Dictionary> personKeyToIds) + private List GetPersonKeyFormattedCollection(string[] jLinks, string a2PeopleContentDirectory, ReadOnlyCollection personContainers, ReadOnlyDictionary> personKeyToIds) { if (_Configuration is null) throw new NullReferenceException(nameof(_Configuration)); @@ -817,7 +821,7 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic return results; } - private (int, FileHolder, int, string, string, string, string)[] GetCollectionForSaveFilteredOriginalImagesFromJLinks(string[] jLinks, string a2PeopleContentDirectory, ReadOnlyCollection personContainers, ReadOnlyCollection mappingCollection, Dictionary> personKeyToIds) + private (int, FileHolder, int, string, string, string, string)[] GetCollectionForSaveFilteredOriginalImagesFromJLinks(string[] jLinks, string a2PeopleContentDirectory, ReadOnlyCollection personContainers, ReadOnlyCollection mappingCollection, ReadOnlyDictionary> personKeyToIds) { if (_Configuration is null) throw new NullReferenceException(nameof(_Configuration)); @@ -878,7 +882,7 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic return results; } - public void SaveFilteredOriginalImagesFromJLinks(string[] jLinks, ReadOnlyCollection personContainers, string a2PeopleContentDirectory, Dictionary> personKeyToIds, ReadOnlyCollection mappingCollection) + public void SaveFilteredOriginalImagesFromJLinks(string[] jLinks, ReadOnlyCollection personContainers, string a2PeopleContentDirectory, ReadOnlyDictionary> personKeyToIds, ReadOnlyCollection mappingCollection) { if (_Configuration is null) throw new NullReferenceException(nameof(_Configuration)); @@ -896,7 +900,7 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic SaveContainers(saveIndividually, null, saveContainers); } - private List GetCollectionForSaveShortcutsForOutputResolutionsPreMapLogic(string eDistanceContentDirectory, Dictionary> personKeyToIds, ReadOnlyCollection mappingCollection) + private List GetCollectionForSaveShortcutsForOutputResolutionsPreMapLogic(string eDistanceContentDirectory, ReadOnlyDictionary> personKeyToIds, ReadOnlyCollection mappingCollection) { List results = new(); if (_Configuration is null) @@ -912,7 +916,7 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic List? personKeys; string personKeyFormatted; Calendar calendar = new CultureInfo("en-US").Calendar; - Dictionary> idToPersonKeys = Stateless.Methods.IMapLogic.GetIdToPersonKeys(personKeyToIds); + ReadOnlyDictionary> idToPersonKeys = Stateless.Methods.IMapLogic.GetIdToPersonKeys(personKeyToIds); foreach (Mapping mapping in mappingCollection) { dateTime = mapping.MappingFromItem.GetDateTimeOriginalThenMinimumDateTime(); @@ -952,7 +956,7 @@ public partial class MapLogic : Shared.Models.Methods.IMapLogic return results; } - public void SaveShortcutsForOutputResolutionsPreMapLogic(string eDistanceContentDirectory, Dictionary> personKeyToIds, ReadOnlyCollection mappingCollection) + public void SaveShortcutsForOutputResolutionsPreMapLogic(string eDistanceContentDirectory, ReadOnlyDictionary> personKeyToIds, ReadOnlyCollection mappingCollection) { string hiddenFile; WindowsShortcut windowsShortcut; diff --git a/Map/Models/Stateless/MapLogic.cs b/Map/Models/Stateless/MapLogic.cs index 3c1fc86..8f700d6 100644 --- a/Map/Models/Stateless/MapLogic.cs +++ b/Map/Models/Stateless/MapLogic.cs @@ -37,6 +37,7 @@ internal abstract class MapLogic { int? id; string fileName; + string checkFile; int? wholePercentages; List distinctFiles = new(); List distinctFileName = new(); @@ -58,8 +59,10 @@ internal abstract class MapLogic fileName = Path.GetFileName(distinctFile); if (distinctFileName.Contains(fileName)) { - if (!distinctFile.EndsWith(".dup") && !File.Exists($"{distinctFile}.dup")) - File.Move(distinctFile, $"{distinctFile}.dup"); + checkFile = $"{distinctFile}.dup"; + if (File.Exists(checkFile)) + continue; + File.Move(distinctFile, checkFile); continue; } (id, wholePercentages) = IMapping.GetConverted(configuration.FacesFileNameExtension, distinctFile); @@ -256,6 +259,7 @@ internal abstract class MapLogic string[] files; string fileName; int totalSeconds; + string checkFile; DateTime dateTime; TimeSpan timeSpan; int directoryNumber; @@ -406,8 +410,10 @@ internal abstract class MapLogic fileName = Path.GetFileName(mappedFaceFile); if (distinct.Contains(fileName)) { - if (!mappedFaceFile.EndsWith(".dup") && !File.Exists($"{mappedFaceFile}.dup")) - File.Move(mappedFaceFile, $"{mappedFaceFile}.dup"); + checkFile = $"{mappedFaceFile}.dup"; + if (File.Exists(checkFile)) + continue; + File.Move(mappedFaceFile, checkFile); continue; } distinct.Add(fileName); @@ -818,9 +824,11 @@ internal abstract class MapLogic Directory.SetLastWriteTime(ticksDirectory, new DateTime(directoryTicks)); totalDays = lastDirectoryTicks is null || new TimeSpan(dateTime.Ticks - directoryTicks).TotalDays < 1 ? null : (float)new TimeSpan(directoryTicks - lastDirectoryTicks.Value).TotalDays; results.Add(new(ticksDirectory, ticksDirectoryName, new(directoryTicks), new DateTime(directoryDateTime.Year, directoryDateTime.Month, directoryDateTime.Day + 1), totalDays)); + if (directoryDateTime.Hour == 0 && directoryDateTime.Minute == 0 && directoryDateTime.Second == 0) + continue; lastDirectoryTicks = directoryTicks; } - string[] compare = (from l in results where l.DirectoryDateTime.Second != 0 && l.TotalDays is not null and < 3.95f select l.Directory).ToArray(); + string[] compare = (from l in results where l.TotalDays is not null and < 9.95f select l.Directory).ToArray(); if (compare.Length > 0) throw new Exception($"Please Consolidate <{string.Join(Environment.NewLine, compare)}>"); return results; @@ -890,6 +898,7 @@ internal abstract class MapLogic private static void ParallelFor(Configuration configuration, string eDistanceContentDirectory, Dictionary> skipCollection, List> locationContainers, long personKey, int? directoryNumber, string file) { + string checkFile; string[] fileMatches; const string lnk = ".lnk"; int? id, wholePercentages; @@ -909,8 +918,11 @@ internal abstract class MapLogic { if (string.IsNullOrEmpty(fileMatch) || !File.Exists(fileMatch)) continue; - if (!fileMatch.EndsWith(".dup") && !File.Exists($"{fileMatch}.dup")) - File.Move(fileMatch, $"{fileMatch}.dup"); + checkFile = $"{fileMatch}.dup"; + if (File.Exists(checkFile)) + continue; + File.Move(fileMatch, checkFile); + continue; } } if (file.EndsWith(lnk) || (!configuration.DistanceMoveUnableToMatch && !configuration.DistanceRenameToMatch) || !File.Exists(file)) @@ -1255,7 +1267,7 @@ internal abstract class MapLogic } } - internal static Dictionary> GetIdToPersonKeys(Dictionary> personKeyToIds) + internal static ReadOnlyDictionary> GetIdToPersonKeys(ReadOnlyDictionary> personKeyToIds) { Dictionary> results = new(); List? collection; @@ -1274,7 +1286,7 @@ internal abstract class MapLogic collection.Add(keyValuePair.Key); } } - return results; + return new(results); } internal static Mapping[] GetSelectedMappingCollection(ReadOnlyCollection faces) diff --git a/Map/Models/Stateless/Methods/IMapLogic.cs b/Map/Models/Stateless/Methods/IMapLogic.cs index 56bb680..87e3ce4 100644 --- a/Map/Models/Stateless/Methods/IMapLogic.cs +++ b/Map/Models/Stateless/Methods/IMapLogic.cs @@ -5,9 +5,9 @@ namespace View_by_Distance.Map.Models.Stateless.Methods; public interface IMapLogic { - Dictionary> TestStatic_GetIdToPersonKeys(Dictionary> personKeyToIds) => + ReadOnlyDictionary> TestStatic_GetIdToPersonKeys(ReadOnlyDictionary> personKeyToIds) => GetIdToPersonKeys(personKeyToIds); - static Dictionary> GetIdToPersonKeys(Dictionary> personKeyToIds) => + static ReadOnlyDictionary> GetIdToPersonKeys(ReadOnlyDictionary> personKeyToIds) => MapLogic.GetIdToPersonKeys(personKeyToIds); ReadOnlyCollection TestStatic_GetFaces(ReadOnlyCollection items) => diff --git a/Resize/Models/_C_Resize.cs b/Resize/Models/_C_Resize.cs index d494a81..e81befd 100644 --- a/Resize/Models/_C_Resize.cs +++ b/Resize/Models/_C_Resize.cs @@ -335,11 +335,11 @@ public class C_Resize check = true; if (check) { - if (fileInfo.Exists) - File.Delete(fileInfo.FullName); - File.Copy(mappingFromItem.ImageFileHolder.FullName, fileInfo.FullName); - item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder)); - subFileTuples.Add(new Tuple(nameof(C_Resize), DateTime.Now)); + // if (fileInfo.Exists) + // File.Delete(fileInfo.FullName); + // File.Copy(mappingFromItem.ImageFileHolder.FullName, fileInfo.FullName); + // item.SetResizedFileHolder(_FileNameExtension, Shared.Models.Stateless.Methods.IFileHolder.Refresh(mappingFromItem.ResizedFileHolder)); + // subFileTuples.Add(new Tuple(nameof(C_Resize), DateTime.Now)); } } else diff --git a/Shared/.kanbn/index.md b/Shared/.kanbn/index.md index dc139e7..b637ae7 100644 --- a/Shared/.kanbn/index.md +++ b/Shared/.kanbn/index.md @@ -44,6 +44,7 @@ taskTemplate: '^+^_${overdue ? ''^R'' : ''''}${name}^: ${relations ? (''\n^-^/^g - [eof-error](tasks/eof-error.md) - [shrink-percent](tasks/shrink-percent.md) - [setup-photo-prism-again-in-wsl-docker](tasks/setup-photo-prism-again-in-wsl-docker.md) +- [fix-random-logic](tasks/fix-random-logic.md) - [move-copy-manual-files-to-get-display-directory-all-files](tasks/move-copy-manual-files-to-get-display-directory-all-files.md) - [rename-files-to-padded-number-string](tasks/rename-files-to-padded-number-string.md) - [genealogical-data-communication-as-golden](tasks/genealogical-data-communication-as-golden.md) diff --git a/Shared/.kanbn/tasks/fix-random-logic.md b/Shared/.kanbn/tasks/fix-random-logic.md new file mode 100644 index 0000000..92f3f12 --- /dev/null +++ b/Shared/.kanbn/tasks/fix-random-logic.md @@ -0,0 +1,11 @@ +--- +created: 2023-08-12T04:53:43.442Z +updated: 2023-08-12T15:26:41.597Z +assigned: "" +progress: 0 +tags: [] +started: 2023-08-12T04:53:43.442Z +completed: 2023-08-12T15:26:41.598Z +--- + +# Fix random logic diff --git a/Shared/Models/Face.cs b/Shared/Models/Face.cs index 039180d..0af7482 100644 --- a/Shared/Models/Face.cs +++ b/Shared/Models/Face.cs @@ -33,13 +33,6 @@ public class Face : Properties.IFace _OutputResolution = outputResolution; } - public Face(int locationDigits, int locationFactor, int facesCount, Face face) : - this(face.DateTime, null, face.FaceEncoding, face.FaceParts, face.Location, null, face.OutputResolution) - { - if (face.Location?.Confidence is not null && face.OutputResolution is not null) - _Location = new(face.Location.Confidence, face.OutputResolution.Height, face.Location, locationDigits, locationFactor, face.OutputResolution.Width, facesCount); - } - public Face(Property property, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, Location? location) : this(DateTime.MinValue, null, null, null, location, null, null) { @@ -49,10 +42,6 @@ public class Face : Properties.IFace _DateTime = (from l in dateTimes where l.HasValue select l.Value).Min(); } - public Face(Face face, int height, Location location, int locationDigits, int locationFactor, int width, int zCount) : - this(face.DateTime, face.FaceDistance, face.FaceEncoding, face.FaceParts, new(height, location, locationDigits, locationFactor, width, zCount), face.Mapping, face.OutputResolution) - { } - public override string ToString() { string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });