diff --git a/Day/2024-Q3/Helper-2024-07-24.cs b/Day/2024-Q3/Helper-2024-07-24.cs index 27f20fd..b3f5759 100644 --- a/Day/2024-Q3/Helper-2024-07-24.cs +++ b/Day/2024-Q3/Helper-2024-07-24.cs @@ -1,13 +1,186 @@ using Microsoft.Extensions.Logging; +using System.Collections.ObjectModel; using System.Diagnostics; +using System.Globalization; +using System.Text.Json; namespace File_Folder_Helper.Day; internal static partial class Helper20240724 { + private record NginxFileSystem(string Name, + string Type, + string MTime, + float Size); + + private record FileConnectorConfigurationSystem(string AlternateTargetFolder, + string FileAgeThreshold, + string[] SourceFileFilters, + string TargetFileLocation); + +#pragma warning disable IDE0028, IDE0056, IDE0300, IDE0240, IDE0241 + + private static readonly HttpClient _HttpClient = new(); + private static readonly string _StaticFileServer = "localhost:5054"; + private static readonly FileConnectorConfigurationSystem _FileConnectorConfiguration = new( + "D:/Tmp/Phares/AlternateTargetFolder", + "000:20:00:01", + [".txt"], + "D:/Tmp/Phares/TargetFileLocation"); + + private static DateTime GetFileAgeThresholdDateTime(string fileAgeThreshold) + { + DateTime result = DateTime.Now; + string[] segments = fileAgeThreshold.Split(':'); + for (int i = 0; i < segments.Length; i++) + { + result = i switch + { + 0 => result.AddDays(double.Parse(segments[i]) * -1), + 1 => result.AddHours(double.Parse(segments[i]) * -1), + 2 => result.AddMinutes(double.Parse(segments[i]) * -1), + 3 => result.AddSeconds(double.Parse(segments[i]) * -1), + _ => throw new Exception(), + }; + } + return result; + } + + private static string[] GetValidWeeks(DateTime fileAgeThresholdDateTime) + { + DateTime dateTime = DateTime.Now; + Calendar calendar = new CultureInfo("en-US").Calendar; + string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}"; + string lastWeekOfYear = $"{fileAgeThresholdDateTime:yyyy}_Week_{calendar.GetWeekOfYear(fileAgeThresholdDateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}"; + return new string[] { weekOfYear, lastWeekOfYear }.Distinct().ToArray(); + } + + private static string[] GetValidDays(DateTime fileAgeThresholdDateTime) + { + DateTime dateTime = DateTime.Now; + return new string[] { dateTime.ToString("yyyy-MM-dd"), fileAgeThresholdDateTime.ToString("yyyy-MM-dd") }.Distinct().ToArray(); + } + + private static ReadOnlyCollection GetDayNginxFileSystemCollection(DateTime fileAgeThresholdDateTime, string week, string day, string dayUrl, NginxFileSystem[] dayNginxFileSystemCollection) + { + List results = new(); + DateTime dateTime; + string nginxFormat = "ddd, dd MMM yyyy HH:mm:ss zzz"; + foreach (NginxFileSystem dayNginxFileSystem in dayNginxFileSystemCollection) + { + if (!DateTime.TryParseExact(dayNginxFileSystem.MTime.Replace("GMT", "+00:00"), nginxFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) + continue; + if (dateTime < fileAgeThresholdDateTime) + continue; + results.Add(new( + Path.GetFullPath(Path.Combine(_FileConnectorConfiguration.TargetFileLocation, week, day, dayNginxFileSystem.Name)), + string.Concat(dayUrl, '/', dayNginxFileSystem.Name), + dateTime.ToString(), + dayNginxFileSystem.Size)); + } + return results.AsReadOnly(); + } + + private static ReadOnlyCollection GetDayNginxFileSystemCollection(DateTime fileAgeThresholdDateTime) + { +#nullable enable + List results = new(); + string dayUrl; + string dayJson; + string weekJson; + string checkWeek; + Task task; + NginxFileSystem[]? dayNginxFileSystemCollection; + NginxFileSystem[]? weekNginxFileSystemCollection; + string[] days = GetValidDays(fileAgeThresholdDateTime); + string[] weeks = GetValidWeeks(fileAgeThresholdDateTime); + JsonSerializerOptions propertyNameCaseInsensitiveJsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; + foreach (string week in weeks) + { + checkWeek = string.Concat("http://", _StaticFileServer, '/', week); + task = _HttpClient.GetAsync(checkWeek); + task.Wait(); + if (!task.Result.IsSuccessStatusCode) + continue; + weekJson = _HttpClient.GetStringAsync(checkWeek).Result; + weekNginxFileSystemCollection = JsonSerializer.Deserialize(weekJson, propertyNameCaseInsensitiveJsonSerializerOptions); + if (weekNginxFileSystemCollection is null) + continue; + foreach (NginxFileSystem weekNginxFileSystem in weekNginxFileSystemCollection) + { + if (!(from l in days where weekNginxFileSystem.Name == l select false).Any()) + continue; + dayUrl = string.Concat(checkWeek, '/', weekNginxFileSystem.Name); + dayJson = _HttpClient.GetStringAsync(dayUrl).Result; + dayNginxFileSystemCollection = JsonSerializer.Deserialize(dayJson, propertyNameCaseInsensitiveJsonSerializerOptions); + if (dayNginxFileSystemCollection is null) + continue; + results.AddRange(GetDayNginxFileSystemCollection(fileAgeThresholdDateTime, week, weekNginxFileSystem.Name, dayUrl, dayNginxFileSystemCollection)); + } + } + return results.AsReadOnly(); +#nullable disable + } + + private static ReadOnlyCollection> GetPossible() + { + List> results = new(); + DateTime dateTime; + FileInfo targetFileInfo; + FileInfo alternateFileInfo; + DateTime fileAgeThresholdDateTime = GetFileAgeThresholdDateTime(_FileConnectorConfiguration.FileAgeThreshold); + ReadOnlyCollection dayNginxFileSystemCollection = GetDayNginxFileSystemCollection(fileAgeThresholdDateTime); + foreach (NginxFileSystem nginxFileSystem in dayNginxFileSystemCollection) + { + targetFileInfo = new FileInfo(nginxFileSystem.Name); + if (targetFileInfo.Directory is null) + continue; + if (!Directory.Exists(targetFileInfo.Directory.FullName)) + _ = Directory.CreateDirectory(targetFileInfo.Directory.FullName); + if (!DateTime.TryParse(nginxFileSystem.MTime, out dateTime)) + continue; + if (targetFileInfo.Exists && targetFileInfo.LastWriteTime == dateTime) + continue; + alternateFileInfo = new(Path.Combine(_FileConnectorConfiguration.AlternateTargetFolder, nginxFileSystem.Name)); + results.Add(new(dateTime, targetFileInfo, alternateFileInfo, nginxFileSystem.Type)); + } + return (from l in results orderby l.Item1 select l).ToList().AsReadOnly(); + } + + private static void Test() + { +#nullable enable + if (_HttpClient is null) + throw new Exception(); + if (string.IsNullOrEmpty(_StaticFileServer)) + throw new Exception(); + if (string.IsNullOrEmpty(_StaticFileServer)) + { + ReadOnlyCollection> possibleDownload = GetPossible(); + if (possibleDownload.Count > 0) + { + string targetFileName = possibleDownload[0].Item4; + FileInfo targetFileInfo = possibleDownload[0].Item2; + FileInfo alternateFileInfo = possibleDownload[0].Item3; + DateTime matchNginxFileSystemDateTime = possibleDownload[0].Item1; + // if (alternateFileInfo.Exists) + // File.Delete(alternateFileInfo.FullName); + if (targetFileInfo.Exists) + File.Delete(targetFileInfo.FullName); + string targetJson = _HttpClient.GetStringAsync(targetFileName).Result; + File.WriteAllText(targetFileInfo.FullName, targetJson); + targetFileInfo.LastWriteTime = matchNginxFileSystemDateTime; + // File.Copy(targetFileInfo.FullName, alternateFileInfo.FullName); + File.AppendAllText(alternateFileInfo.FullName, targetJson); + } + } +#nullable disable + } + internal static void CopyDirectories(ILogger logger, List args) { + Test(); string[] files; Process process; string checkDirectory;