Back to comma

This commit is contained in:
Mike Phares 2025-02-05 16:27:37 -07:00
parent 33020be9ec
commit 2c5f68e8bc
2 changed files with 16 additions and 29 deletions

View File

@ -77,17 +77,22 @@ internal class Run
fileInfoCollection.Add(fileInfo); fileInfoCollection.Add(fileInfo);
} }
private static ReadOnlyCollection<string> GetLines(JsonElement[]? jsonElements) private static ReadOnlyCollection<string> GetLines(Logistics logistics, JsonElement[]? jsonElements)
{ {
List<string> results = new(); List<string> results = new();
int columns = 0; int columns = 0;
StringBuilder stringBuilder = new(); StringBuilder stringBuilder = new();
results.Add($"\"Count\",{jsonElements?.Length}");
results.Add($"\"{nameof(logistics.Sequence)}\",{logistics.Sequence}");
results.Add($"\"{nameof(logistics.MesEntity)}\",\"{logistics.MesEntity}\"");
string dateTimeFromSequence = logistics.DateTimeFromSequence.ToString("MM/dd/yyyy hh:mm:ss tt");
for (int i = 0; i < jsonElements?.Length;) for (int i = 0; i < jsonElements?.Length;)
{ {
_ = stringBuilder.Append('"').Append(nameof(logistics.DateTimeFromSequence)).Append('"').Append(',');
foreach (JsonProperty jsonProperty in jsonElements[0].EnumerateObject()) foreach (JsonProperty jsonProperty in jsonElements[0].EnumerateObject())
{ {
columns += 1; columns += 1;
_ = stringBuilder.Append('"').Append(jsonProperty.Name).Append('"').Append('\t'); _ = stringBuilder.Append('"').Append(jsonProperty.Name).Append('"').Append(',');
} }
break; break;
} }
@ -97,14 +102,15 @@ internal class Run
for (int i = 0; i < jsonElements?.Length; i++) for (int i = 0; i < jsonElements?.Length; i++)
{ {
_ = stringBuilder.Clear(); _ = stringBuilder.Clear();
_ = stringBuilder.Append('"').Append(dateTimeFromSequence).Append('"').Append(',');
foreach (JsonProperty jsonProperty in jsonElements[i].EnumerateObject()) foreach (JsonProperty jsonProperty in jsonElements[i].EnumerateObject())
{ {
if (jsonProperty.Value.ValueKind == JsonValueKind.Object) if (jsonProperty.Value.ValueKind == JsonValueKind.Object)
_ = stringBuilder.Append('\t'); _ = stringBuilder.Append(',');
else if (jsonProperty.Value.ValueKind != JsonValueKind.String) else if (jsonProperty.Value.ValueKind != JsonValueKind.String)
_ = stringBuilder.Append(jsonProperty.Value).Append('\t'); _ = stringBuilder.Append(jsonProperty.Value).Append(',');
else else
_ = stringBuilder.Append('"').Append(jsonProperty.Value).Append('"').Append('\t'); _ = stringBuilder.Append('"').Append(jsonProperty.Value).Append('"').Append(',');
} }
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1); _ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
results.Add(stringBuilder.ToString()); results.Add(stringBuilder.ToString());
@ -112,14 +118,10 @@ internal class Run
return results.AsReadOnly(); return results.AsReadOnly();
} }
private static ReadOnlyCollection<string> GetLines(JsonElement? jsonElement) => private static void WriteCommaSeparatedValues(Logistics logistics, Run run)
jsonElement is null ? new(Array.Empty<string>()) : GetLines(new JsonElement[] { jsonElement.Value });
private static void WriteTabSeparatedValues(Logistics logistics, List<FileInfo> fileInfoCollection, Run run)
{ {
List<Row> results = new(); List<Row> results = new();
Row row; Row row;
FileInfo fileInfo = new($"{logistics.ReportFullPath}.tsv");
for (int i = 0; i < run.Sites.Count; i++) for (int i = 0; i < run.Sites.Count; i++)
{ {
row = new(run, i); row = new(run, i);
@ -127,16 +129,8 @@ internal class Run
} }
string json = JsonSerializer.Serialize(results); string json = JsonSerializer.Serialize(results);
JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json); JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
ReadOnlyCollection<string> lines = GetLines(jsonElements); ReadOnlyCollection<string> lines = GetLines(logistics, jsonElements);
File.WriteAllText(fileInfo.FullName, string.Join(Environment.NewLine, lines)); File.WriteAllText($"{logistics.ReportFullPath}.csv", string.Join(Environment.NewLine, lines));
File.SetLastWriteTime(fileInfo.FullName, logistics.DateTimeFromSequence);
fileInfoCollection.Add(fileInfo);
}
private static void WriteException(Logistics logistics, Exception ex)
{
FileInfo fileInfo = new($"{logistics.ReportFullPath}.{nameof(Exception)}.txt");
File.WriteAllText(fileInfo.FullName, $"{ex.Message}{Environment.NewLine}{ex.StackTrace}");
} }
internal static Run? Get(Logistics logistics, List<FileInfo> fileInfoCollection, Header[] lastHeader) internal static Run? Get(Logistics logistics, List<FileInfo> fileInfoCollection, Header[] lastHeader)
@ -166,14 +160,7 @@ internal class Run
{ {
result = new(header, sites, body, footer); result = new(header, sites, body, footer);
WriteJson(logistics, fileInfoCollection, result); WriteJson(logistics, fileInfoCollection, result);
try WriteCommaSeparatedValues(logistics, result);
{
WriteTabSeparatedValues(logistics, fileInfoCollection, result);
}
catch (Exception ex)
{
WriteException(logistics, ex);
}
} }
} }
} }

View File

@ -88,7 +88,7 @@ public partial class FileRead : FileReaderHandler, ISMTP
try try
{ {
extractResults = _FileRead.GetExtractResult(reportFullPath, eventName); extractResults = _FileRead.GetExtractResult(reportFullPath, eventName);
if (extractResults.Item3.Length == 0 && !string.IsNullOrEmpty(extractResults.Item1) && !extractResults.Item1.Contains(Environment.NewLine)) if (extractResults.Item3.Length == 0 && !string.IsNullOrEmpty(extractResults.Item1) && !extractResults.Item1.Contains("LOGISTICS_1"))
throw new Exception(extractResults.Item1); throw new Exception(extractResults.Item1);
TriggerEvents(extractResults); TriggerEvents(extractResults);
_FileRead.Move(extractResults); _FileRead.Move(extractResults);