From 0fb9663e6fc537713c4ef6604fb73d496fe7784b Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Tue, 1 Jul 2025 19:39:11 -0700 Subject: [PATCH] process-data-standard-format-to-java-script-object-notation (Day-Helper-2025-07-01) --- .vscode/launch.json | 7 ++ ADO2025/PI5/Helper-2025-02-19.cs | 4 +- ADO2025/PI5/Helper-2025-03-06.cs | 2 +- ADO2025/PI6/Helper-2025-07-01.cs | 177 +++++++++++++++++++++++++++++++ Day/HelperDay.cs | 2 + 5 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 ADO2025/PI6/Helper-2025-07-01.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index e9eeead..694caa7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,6 +11,13 @@ "preLaunchTask": "build", "program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll", "args": [ + "s", + "X", + "\\\\mesfs.infineon.com\\EC_APC\\Production\\Traces\\DEP08CEPIEPSILON\\PollPath", + "Day-Helper-2025-07-01", + "*.pdsf", + "321321", + "\\\\mesfs.infineon.com\\EC_APC\\Production\\Traces\\DEP08CEPIEPSILON\\JavaScriptObjectNotation", "s", "X", "F:/0-ISO-A", diff --git a/ADO2025/PI5/Helper-2025-02-19.cs b/ADO2025/PI5/Helper-2025-02-19.cs index 12ceca9..7e69ae1 100644 --- a/ADO2025/PI5/Helper-2025-02-19.cs +++ b/ADO2025/PI5/Helper-2025-02-19.cs @@ -277,12 +277,12 @@ internal static partial class Helper20250219 { } if (!lookForNumbers) { for (int c = 0; c < segments.Length; c++) { - value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\"); + value = segments[c].Replace("\\", "\\\\").Replace("\"", "\\\""); _ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":\"").Append(value).Append("\","); } } else { for (int c = 0; c < segments.Length; c++) { - value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\"); + value = segments[c].Replace("\\", "\\\\").Replace("\"", "\\\""); if (string.IsNullOrEmpty(value)) _ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":").Append(value).Append("null,"); else if (value.All(char.IsDigit)) diff --git a/ADO2025/PI5/Helper-2025-03-06.cs b/ADO2025/PI5/Helper-2025-03-06.cs index 172892a..60b25a9 100644 --- a/ADO2025/PI5/Helper-2025-03-06.cs +++ b/ADO2025/PI5/Helper-2025-03-06.cs @@ -71,7 +71,7 @@ internal static partial class Helper20250306 { if (segments.Length != columnsLength) continue; for (int c = 1; c < segments.Length; c++) { - value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\"); + value = segments[c].Replace("\\", "\\\\").Replace("\"", "\\\""); line += '"' + columns[c].Trim('"') + '"' + ':' + '"' + value + '"' + ','; } line = line.Substring(0, line.Length - 1) + '}' + ',' + '\n'; diff --git a/ADO2025/PI6/Helper-2025-07-01.cs b/ADO2025/PI6/Helper-2025-07-01.cs new file mode 100644 index 0000000..e24f9c8 --- /dev/null +++ b/ADO2025/PI6/Helper-2025-07-01.cs @@ -0,0 +1,177 @@ + +using System.Text.Json; +using System.Text.Json.Serialization; + +using Microsoft.Extensions.Logging; + +namespace File_Folder_Helper.ADO2025.PI6; + +internal static partial class Helper20250701 { + + [JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] + [JsonSerializable(typeof(JsonElement))] + internal partial class JsonElementSourceGenerationContext : JsonSerializerContext { + } + + internal static void ProcessDataStandardFormatToJavaScriptObjectNotation(ILogger logger, List args) { + logger.LogInformation(args[0]); + logger.LogInformation(args[1]); + logger.LogInformation(args[2]); + string searchPattern = args[2]; + int sizeFilter = int.Parse(args[3]); + string sourceDirectory = Path.GetFullPath(args[0].Split('~')[0]); + string destinationDirectory = Path.GetFullPath(args[4].Split('~')[0]); + string[] directories = Directory.GetDirectories(sourceDirectory, "*", SearchOption.TopDirectoryOnly); + WriteJavaScriptObjectNotation(logger, directories, searchPattern, sizeFilter, destinationDirectory); + } + + private static void WriteJavaScriptObjectNotation(ILogger logger, string[] directories, string searchPattern, int sizeFilter, string destinationDirectory) { + string? json; + string[] files; + string markdown; + string checkFile; + FileInfo fileInfo; + string? pipeTable; + string directoryName; + string checkDirectory; + string fileNameWithoutExtension; + foreach (string directory in directories) { + files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly); + foreach (string file in files) { + fileInfo = new(file); + if (fileInfo.LastWriteTime > DateTime.Now.AddSeconds(-2)) { + continue; + } + directoryName = Path.GetFileName(fileInfo.DirectoryName); + if (fileInfo.Length > sizeFilter && !directoryName.StartsWith('Z')) { + checkDirectory = Path.Combine(fileInfo.DirectoryName, $"Z{directoryName}-Big"); + if (!Directory.Exists(checkDirectory)) { + _ = Directory.CreateDirectory(checkDirectory); + } + checkFile = Path.Combine(checkDirectory, fileInfo.Name); + if (File.Exists(checkFile)) { + continue; + } + File.Move(file, checkFile); + continue; + } + fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file); + checkDirectory = Path.Combine(destinationDirectory, directoryName); + if (!Directory.Exists(checkDirectory)) { + _ = Directory.CreateDirectory(checkDirectory); + } + checkFile = Path.Combine(checkDirectory, $"{fileNameWithoutExtension}.json"); + if (File.Exists(checkFile)) { + continue; + } + json = GetJavaScriptObjectNotation(logger, file); + if (string.IsNullOrEmpty(json)) { + logger.LogWarning("json is null"); + continue; + } + pipeTable = GetPipeTable(logger, json); + if (string.IsNullOrEmpty(pipeTable)) { + logger.LogWarning("pipeTable is null"); + continue; + } + markdown = $"# {fileNameWithoutExtension}{Environment.NewLine}{Environment.NewLine}{pipeTable}{Environment.NewLine}"; + File.WriteAllText(checkFile, json); + File.SetLastWriteTime(checkFile, fileInfo.LastAccessTime); + File.WriteAllText($"{checkFile}.md", markdown); + File.SetLastWriteTime($"{checkFile}.md", fileInfo.LastAccessTime); + logger.LogInformation("<{checkFile}> was written", checkFile); + } + } + } + + private static string? GetJavaScriptObjectNotation(ILogger logger, string file) { + string? result = null; + string[] lines = File.ReadAllLines(file); + int? columnTitlesLine = GetProcessDataStandardFormatColumnTitlesLine(lines); + if (columnTitlesLine is null) { + logger.LogWarning("<{columnTitlesLine}> is null", nameof(columnTitlesLine)); + } else { + if (lines.Length < columnTitlesLine.Value + 1) { + logger.LogWarning("<{lines}>(s)", lines.Length); + } else { + result = GetJavaScriptObjectNotation(columnTitlesLine.Value, [], lines); + } + } + return result; + } + + private static int? GetProcessDataStandardFormatColumnTitlesLine(string[] lines) { + int? result = null; + for (int i = 0; i < lines.Length; i++) { + if (lines[i].StartsWith("END_OFFSET") && i + 3 < lines.Length) { + result = i + 1; + break; + } + } + return result; + } + + private static string GetJavaScriptObjectNotation(int columnTitlesLine, string[] columns, string[] lines) { +#pragma warning disable CA1845, IDE0057 + string result = "[\n"; + string line; + string value; + string[] segments; + if (columns.Length == 0) { + columns = lines[columnTitlesLine].Split('\t'); + } + for (int i = columnTitlesLine + 1; i < lines.Length; i++) { + if (lines[i].StartsWith("NUM_DATA_ROWS")) + break; + line = "{"; + segments = lines[i].Split('\t'); + if (segments.Length > columns.Length) { + continue; + } + for (int c = 0; c < segments.Length; c++) { + value = segments[c].Replace("\\", "\\\\").Replace("\"", "\\\""); + line += '"' + columns[c].Trim('"') + '"' + ':' + '"' + value + '"' + ','; + } + line = line.Substring(0, line.Length - 1) + '}' + ',' + '\n'; + result += line; + } + result = result.Substring(0, result.Length - 2) + ']'; + return result; +#pragma warning restore CA1845, IDE0057 + } + + private static string? GetPipeTable(ILogger logger, string json) { + string? result = null; + string? value; + string[]? columns = null; + List values = []; + List results = []; + Dictionary keyValuePairs = []; + JsonElement jsonElement = JsonSerializer.Deserialize(json, JsonElementSourceGenerationContext.Default.JsonElement); + JsonElement[] jsonElements = jsonElement.EnumerateArray().ToArray(); + foreach (JsonElement j in jsonElements) { + values.Clear(); + keyValuePairs.Clear(); + foreach (JsonProperty jsonProperty in j.EnumerateObject().ToArray()) { + if (columns is null) { + JsonProperty[] jsonProperties = j.EnumerateObject().OrderBy(l => l.Name).ToArray(); + columns = jsonProperties.Select(l => l.Name).ToArray(); + results.Add($"|{string.Join('|', columns)}|"); + results.Add($"|{string.Join('|', columns.Select(l => '-'))}|"); + } + keyValuePairs.Add(jsonProperty.Name, jsonProperty.Value.ToString()); + } + foreach (string column in columns) { + if (!keyValuePairs.TryGetValue(column, out value)) { + values.Add(new string(' ', column.Length)); + } else { + values.Add(value.PadLeft(column.Length, ' ')); + } + } + results.Add($"|{string.Join('|', values)}|"); + } + if (results.Count > 0) + result = string.Join(Environment.NewLine, results); + return result; + } +} \ No newline at end of file diff --git a/Day/HelperDay.cs b/Day/HelperDay.cs index 820c597..65866a7 100644 --- a/Day/HelperDay.cs +++ b/Day/HelperDay.cs @@ -173,6 +173,8 @@ internal static class HelperDay ADO2025.PI6.Helper20250618.MoveAllButXOfEach(logger, args); else if (args[1] == "Day-Helper-2025-06-28") ADO2025.PI6.Helper20250628.LogIsoInformation(logger, args); + else if (args[1] == "Day-Helper-2025-07-01") + ADO2025.PI6.Helper20250701.ProcessDataStandardFormatToJavaScriptObjectNotation(logger, args); else throw new Exception(appSettings.Company); }