SaveRandomForOutputResolutions

This commit is contained in:
2022-12-29 15:24:23 -07:00
parent 26edd826d5
commit 06a1207285
9 changed files with 209 additions and 109 deletions

View File

@ -43,46 +43,74 @@ internal class F_Random
return result;
}
internal void Random(Property.Models.Configuration configuration, string outputResolution, List<Shared.Models.MappingFromItem> mappingFromItemCollection)
private static Dictionary<string, List<string>> Get(Map.Models.MapLogic mapLogic, Shared.Models.Mapping[] mappingCollection, string dateFormat)
{
Dictionary<string, List<string>> results = new();
string key;
DateTime dateTime;
List<long>? personKeys;
List<string>? relativePaths;
Dictionary<int, List<long>> idToPersonKeys = mapLogic.GetIdToPeronKeys();
foreach (Shared.Models.Mapping mapping in mappingCollection)
{
if (mapping.MappingFromItem.ImageFileHolder.DirectoryName is null || mapping.MappingFromPerson is null)
continue;
if (!idToPersonKeys.TryGetValue(mapping.MappingFromItem.Id, out personKeys))
continue;
if (!personKeys.Contains(mapping.MappingFromPerson.PersonBirthday.Value.Ticks))
continue;
dateTime = new(mapping.MappingFromPerson.PersonBirthday.Value.Ticks);
key = dateTime.ToString(dateFormat);
if (!results.TryGetValue(key, out relativePaths))
{
results.Add(key, new());
if (!results.TryGetValue(key, out relativePaths))
throw new Exception();
}
relativePaths.Add(mapping.MappingFromItem.RelativePath);
}
return results;
}
internal void Random(Property.Models.Configuration configuration, Map.Models.MapLogic mapLogic, string outputResolution, Shared.Models.Mapping[] mappingCollection)
{
string key;
string json;
string jsonFile;
Random random = new();
List<string>? collection;
string dateFormat = "MM-dd";
List<string> relativePaths = new();
List<string> ignoreRelativePaths = new();
DateTime dateTime = new(2024, 1, 1); //Leap year
DateTime dateTime = new(2024, 1, 1); //Leap year
Dictionary<string, List<string>> dayToRelativePaths = Get(mapLogic, mappingCollection, dateFormat);
string fRandomCollectionDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(F_Random), "[]");
string[] files = Directory.GetFiles(fRandomCollectionDirectory, "*", SearchOption.TopDirectoryOnly);
foreach (string file in files)
File.Delete(file);
foreach (Shared.Models.MappingFromItem mappingFromItem in mappingFromItemCollection)
foreach (Shared.Models.Mapping mapping in mappingCollection)
{
if (mappingFromItem.ImageFileHolder.DirectoryName is null)
if (mapping.MappingFromItem.ImageFileHolder.DirectoryName is null)
continue;
if (!_Configuration.IgnoreRelativePaths.Any(l => mappingFromItem.ImageFileHolder.DirectoryName.Contains(l)) || !IsIgnoreRelativePath(mappingFromItem.ImageFileHolder.DirectoryName))
relativePaths.Add(mappingFromItem.RelativePath);
else
ignoreRelativePaths.Add(mappingFromItem.RelativePath);
if (_Configuration.IgnoreRelativePaths.Any(l => mapping.MappingFromItem.ImageFileHolder.DirectoryName.Contains(l)) && IsIgnoreRelativePath(mapping.MappingFromItem.ImageFileHolder.DirectoryName))
continue;
relativePaths.Add(mapping.MappingFromItem.RelativePath);
}
if (relativePaths.Any())
{
for (int i = 0; i < 366; i++)
{
relativePaths = (from l in relativePaths orderby random.NextDouble() select l).ToList();
jsonFile = Path.Combine(fRandomCollectionDirectory, $"{dateTime.AddDays(i):MM-dd}.json");
json = JsonSerializer.Serialize(relativePaths, _WriteIndentedJsonSerializerOptions);
key = dateTime.AddDays(i).ToString(dateFormat);
if (dayToRelativePaths.TryGetValue(key, out collection) && collection.Count > 10)
collection = (from l in collection orderby random.NextDouble() select l).ToList();
else
collection = (from l in relativePaths orderby random.NextDouble() select l).ToList();
jsonFile = Path.Combine(fRandomCollectionDirectory, $"{key}.json");
json = JsonSerializer.Serialize(collection, _WriteIndentedJsonSerializerOptions);
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(jsonFile, json, updateDateWhenMatches: false, compareBeforeWrite: false);
if (!_Configuration.SaveFullYearOfRandomFiles)
break;
}
}
if (ignoreRelativePaths.Any())
{
ignoreRelativePaths = (from l in ignoreRelativePaths orderby random.NextDouble() select l).ToList();
jsonFile = Path.Combine(fRandomCollectionDirectory, "01-01.txt");
json = JsonSerializer.Serialize(ignoreRelativePaths, _WriteIndentedJsonSerializerOptions);
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(jsonFile, json, updateDateWhenMatches: false, compareBeforeWrite: false);
}
}
}