Refactor FileRead and WSRequest classes to use unique sequence and ID generation; enhance logistics handling across multiple file handlers
This commit is contained in:
@ -267,11 +267,12 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
List<Post> results = new();
|
||||
Post post;
|
||||
long preWait;
|
||||
long uniqueSequence = Logistics.GetUniqueSequence(_Logistics);
|
||||
foreach (PreWith preWith in preWithCollection)
|
||||
{
|
||||
if (!_IsEAFHosted)
|
||||
continue;
|
||||
if (!_StaticRuns.TryGetValue(_Logistics.Sequence, out List<Shared.Metrology.WS.Results>? wsResults))
|
||||
if (!_StaticRuns.TryGetValue(uniqueSequence, out List<Shared.Metrology.WS.Results>? wsResults))
|
||||
wsResults = null;
|
||||
if (processDataStandardFormat.InputPDSF is null)
|
||||
File.Move(preWith.MatchingFile, preWith.CheckFile);
|
||||
|
||||
@ -125,13 +125,14 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
{
|
||||
long? subgroupId;
|
||||
string fileName = Path.GetFileName(reportFullPath);
|
||||
long uniqueSequence = Logistics.GetUniqueSequence(_Logistics);
|
||||
long breakAfter = dateTime.AddSeconds(_BreakAfterSeconds).Ticks;
|
||||
long preWait = _FileConnectorConfiguration?.FileHandleWaitTime is null ? dateTime.AddMilliseconds(1234).Ticks : dateTime.AddMilliseconds(_FileConnectorConfiguration.FileHandleWaitTime.Value).Ticks;
|
||||
if (string.IsNullOrEmpty(descriptions[0].Reactor) || string.IsNullOrEmpty(descriptions[0].PSN))
|
||||
subgroupId = null;
|
||||
else
|
||||
(subgroupId, int? _, string _) = FromIQS.GetCommandText(_IqsConnectionString, _Logistics, descriptions[0], breakAfter, preWait);
|
||||
if (_StaticRuns.TryGetValue(_Logistics.Sequence, out List<WS.Results> wsResults))
|
||||
if (_StaticRuns.TryGetValue(uniqueSequence, out List<WS.Results> wsResults))
|
||||
{
|
||||
if (wsResults is null || wsResults.Count != 1)
|
||||
throw new NullReferenceException($"{nameof(wsResults)} {wsResults?.Count} != 1 {_Logistics.Sequence}!");
|
||||
|
||||
@ -113,6 +113,7 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
private void SendData(string reportFullPath, DateTime dateTime, JsonElement[] jsonElements, List<RsM.Description> descriptions)
|
||||
{
|
||||
string checkDirectory;
|
||||
long uniqueSequence = Logistics.GetUniqueSequence(_Logistics);
|
||||
WSRequest wsRequest = new(this, _Logistics, jsonElements, descriptions);
|
||||
int weekOfYear = _Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
|
||||
string directory = Path.Combine(_OpenInsightMetrologyViewerFileShare, dateTime.Year.ToString(), $"WW{weekOfYear:00}");
|
||||
@ -126,9 +127,9 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
_Log.Debug(wsResults.HeaderId);
|
||||
lock (_StaticRuns)
|
||||
{
|
||||
if (!_StaticRuns.ContainsKey(_Logistics.Sequence))
|
||||
_StaticRuns.Add(_Logistics.Sequence, new());
|
||||
_StaticRuns[_Logistics.Sequence].Add(wsResults);
|
||||
if (!_StaticRuns.ContainsKey(uniqueSequence))
|
||||
_StaticRuns.Add(uniqueSequence, new());
|
||||
_StaticRuns[uniqueSequence].Add(wsResults);
|
||||
}
|
||||
checkDirectory = Path.Combine(directory, $"-{wsResults.HeaderId}");
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
|
||||
@ -100,11 +100,12 @@ public class WSRequest
|
||||
Details.Add(detail);
|
||||
}
|
||||
Date ??= logistics.DateTimeFromSequence.ToString();
|
||||
UniqueId = $"{logistics.JobID}_{logistics.MID}_{logistics.DateTimeFromSequence:yyyyMMddHHmmssffff}";
|
||||
string uniqueId = Logistics.GetUniqueId(logistics);
|
||||
UniqueId = uniqueId;
|
||||
for (int i = 0; i < Details.Count; i++)
|
||||
{
|
||||
Details[i].HeaderUniqueId = UniqueId;
|
||||
Details[i].UniqueId = $"{logistics.JobID}_{logistics.MID}_{logistics.DateTimeFromSequence:yyyyMMddHHmmssffff}_Item-{i + 1}";
|
||||
Details[i].UniqueId = $"{uniqueId}_Item-{i + 1}";
|
||||
}
|
||||
string onlyWSRequest = string.Empty;
|
||||
FilePath = onlyWSRequest;
|
||||
@ -131,6 +132,7 @@ public class WSRequest
|
||||
internal static void PostOpenInsightMetrologyViewerAttachments(IFileRead fileRead, Logistics logistics, string openInsightMetrologyViewerAPI, List<RsM.Description> descriptions, string matchDirectory, WS.Results results, string headerIdDirectory)
|
||||
#pragma warning restore IDE0060
|
||||
{
|
||||
string uniqueId = Logistics.GetUniqueId(logistics);
|
||||
string[] txtFiles = Directory.GetFiles(matchDirectory, "*.txt", SearchOption.TopDirectoryOnly);
|
||||
if (txtFiles.Length != 1)
|
||||
throw new Exception($"Invalid source file count for <{results.HeaderId}>!");
|
||||
@ -147,7 +149,7 @@ public class WSRequest
|
||||
throw new Exception("Invalid *.pdf file count!");
|
||||
List<WS.Attachment> headerAttachments = new()
|
||||
{
|
||||
new WS.Attachment(results, headerIdDirectory, $"{logistics.JobID}_{logistics.MID}_{logistics.DateTimeFromSequence:yyyyMMddHHmmssffff}", "Data.pdf", pdfFiles[0])
|
||||
new WS.Attachment(results, headerIdDirectory, uniqueId, "Data.pdf", pdfFiles[0])
|
||||
};
|
||||
WS.AttachFiles(openInsightMetrologyViewerAPI, headerAttachments, dataAttachments: null);
|
||||
}
|
||||
|
||||
@ -141,8 +141,9 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
string jobIdDirectory = Path.Combine(Path.GetDirectoryName(_FileConnectorConfiguration.AlternateTargetFolder) ?? throw new Exception(), _Logistics.JobID);
|
||||
if (!Directory.Exists(jobIdDirectory))
|
||||
_ = Directory.CreateDirectory(jobIdDirectory);
|
||||
long uniqueSequence = Logistics.GetUniqueSequence(_Logistics);
|
||||
string[] matchDirectories = GetInProcessDirectory(jobIdDirectory);
|
||||
if (!_StaticRuns.TryGetValue(_Logistics.Sequence, out List<Shared.Metrology.WS.Results>? wsResults))
|
||||
if (!_StaticRuns.TryGetValue(uniqueSequence, out List<Shared.Metrology.WS.Results>? wsResults))
|
||||
results = null;
|
||||
else
|
||||
{
|
||||
|
||||
@ -114,6 +114,7 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
throw new ArgumentNullException(nameof(dateTime));
|
||||
FileInfo fileInfo = new(reportFullPath);
|
||||
string logisticsSequence = _Logistics.Sequence.ToString();
|
||||
long uniqueSequence = Logistics.GetUniqueSequence(_Logistics);
|
||||
string jobIdDirectory = Path.Combine(_JobIdParentDirectory, _Logistics.JobID);
|
||||
if (!Directory.Exists(jobIdDirectory))
|
||||
_ = Directory.CreateDirectory(jobIdDirectory);
|
||||
@ -136,7 +137,7 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
File.Copy(reportFullPath, Path.Combine(sequenceDirectory, Path.GetFileName(reportFullPath)), overwrite: true);
|
||||
File.WriteAllText(jsonFileName, json);
|
||||
lock (_StaticRuns)
|
||||
_ = _StaticRuns.Remove(_Logistics.Sequence);
|
||||
_ = _StaticRuns.Remove(uniqueSequence);
|
||||
}
|
||||
|
||||
private static void MoveMatchingFile(string jobIdDirectory, string matchDirectory)
|
||||
|
||||
@ -199,4 +199,31 @@ public class Logistics : ILogistics
|
||||
_ProcessJobID = processJobID;
|
||||
}
|
||||
|
||||
private static int GetCountFromFileName(Logistics logistics)
|
||||
{
|
||||
int result;
|
||||
string[] segments = logistics.FileInfo.Name.Split('.');
|
||||
string[] segmentsB = segments[0].Split('_');
|
||||
string countFromFileName = segmentsB.Length < 3 ? "0" : segmentsB[2];
|
||||
if (!int.TryParse(countFromFileName, out result))
|
||||
result = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static long GetUniqueSequence(Logistics logistics)
|
||||
{
|
||||
long result;
|
||||
int countFromFileName = GetCountFromFileName(logistics);
|
||||
result = (logistics.Sequence * 10) + countFromFileName;
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string GetUniqueId(Logistics logistics)
|
||||
{
|
||||
string result;
|
||||
int countFromFileName = GetCountFromFileName(logistics);
|
||||
result = $"{logistics.JobID}_{logistics.MID}_{logistics.DateTimeFromSequence:yyyyMMddHHmmssffff}_{countFromFileName}";
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@ -665,7 +665,7 @@ internal class ProcessDataStandardFormat
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static string GetPDSFText(IFileRead fileRead, Logistics logistics, JsonElement[] jsonElements, string logisticsText)
|
||||
internal static string GetPDSFText(IFileRead fileRead, Logistics logistics, JsonElement[] jsonElements, string? logisticsText)
|
||||
{
|
||||
string result;
|
||||
if (jsonElements.Length == 0)
|
||||
@ -850,33 +850,6 @@ internal class ProcessDataStandardFormat
|
||||
return string.Concat(((int)searchFor).ToString().PadLeft(2, '0'), separator, searchFor.ToString().Replace("In", string.Concat(separator, "In")).Replace("Ex", string.Concat(separator, "Ex")));
|
||||
}
|
||||
|
||||
private static int? TryGetPropertyIndex(JsonProperty[] jsonProperties, string propertyName)
|
||||
{
|
||||
int? result = null;
|
||||
for (int i = 0; i < jsonProperties.Length; i++)
|
||||
{
|
||||
if (jsonProperties[i].Name != propertyName)
|
||||
continue;
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
if (result is null)
|
||||
{
|
||||
for (int i = 0; i < jsonProperties.Length; i++)
|
||||
{
|
||||
if (jsonProperties[i].Name[0] != propertyName[0])
|
||||
continue;
|
||||
if (jsonProperties[i].Name.Length != propertyName.Length)
|
||||
continue;
|
||||
if (jsonProperties[i].Name != propertyName)
|
||||
continue;
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string GetXml(ProcessDataStandardFormat processDataStandardFormat)
|
||||
{
|
||||
string result;
|
||||
|
||||
Reference in New Issue
Block a user