462 lines
21 KiB
C#
462 lines
21 KiB
C#
using Adaptation.Eaf.Management.ConfigurationData.CellAutomation;
|
|
using Adaptation.FileHandlers.json.WorkItems;
|
|
using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration;
|
|
using Adaptation.Shared;
|
|
using Adaptation.Shared.Duplicator;
|
|
using Adaptation.Shared.Methods;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
|
|
namespace Adaptation.FileHandlers.json;
|
|
|
|
public class FileRead : Shared.FileRead, IFileRead
|
|
{
|
|
|
|
private readonly Timer _Timer;
|
|
private readonly ReadOnlyCollection<string> _WorkItemTypes;
|
|
|
|
public FileRead(ISMTP smtp, Dictionary<string, string> fileParameter, string cellInstanceName, int? connectionCount, string cellInstanceConnectionName, FileConnectorConfiguration fileConnectorConfiguration, string equipmentTypeName, string parameterizedModelObjectDefinitionType, IList<ModelObjectParameterDefinition> modelObjectParameters, string equipmentDictionaryName, Dictionary<string, List<long>> dummyRuns, Dictionary<long, List<string>> staticRuns, bool useCyclicalForDescription, bool isEAFHosted) :
|
|
base(new Description(), false, smtp, fileParameter, cellInstanceName, connectionCount, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, dummyRuns, staticRuns, useCyclicalForDescription, isEAFHosted: connectionCount is null)
|
|
{
|
|
_MinFileLength = 10;
|
|
_NullData = string.Empty;
|
|
_Logistics = new(this);
|
|
if (_FileParameter is null)
|
|
throw new Exception(cellInstanceConnectionName);
|
|
if (_ModelObjectParameterDefinitions is null)
|
|
throw new Exception(cellInstanceConnectionName);
|
|
if (_IsDuplicator)
|
|
throw new Exception(cellInstanceConnectionName);
|
|
string cellInstanceNamed = string.Concat("CellInstance.", _EquipmentType);
|
|
string workItemTypes = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.WorkItemTypes");
|
|
_WorkItemTypes = new(workItemTypes.Split('|'));
|
|
if (!Debugger.IsAttached && fileConnectorConfiguration.PreProcessingMode != FileConnectorConfiguration.PreProcessingModeEnum.Process)
|
|
_Timer = new Timer(Callback, null, (int)(fileConnectorConfiguration.FileScanningIntervalInSeconds * 1000), Timeout.Infinite);
|
|
else
|
|
{
|
|
_Timer = new Timer(Callback, null, Timeout.Infinite, Timeout.Infinite);
|
|
Callback(null);
|
|
}
|
|
}
|
|
|
|
void IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> extractResults, Exception exception) => Move(extractResults);
|
|
|
|
void IFileRead.WaitForThread() => WaitForThread(thread: null, threadExceptions: null);
|
|
|
|
string IFileRead.GetEventDescription()
|
|
{
|
|
string result = _Description.GetEventDescription();
|
|
return result;
|
|
}
|
|
|
|
List<string> IFileRead.GetHeaderNames()
|
|
{
|
|
List<string> results = _Description.GetHeaderNames();
|
|
return results;
|
|
}
|
|
|
|
string[] IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> extractResults, string to, string from, string resolvedFileLocation, Exception exception)
|
|
{
|
|
string[] results = Move(extractResults, to, from, resolvedFileLocation, exception);
|
|
return results;
|
|
}
|
|
|
|
JsonProperty[] IFileRead.GetDefault()
|
|
{
|
|
JsonProperty[] results = _Description.GetDefault(this, _Logistics);
|
|
return results;
|
|
}
|
|
|
|
Dictionary<string, string> IFileRead.GetDisplayNamesJsonElement()
|
|
{
|
|
Dictionary<string, string> results = _Description.GetDisplayNamesJsonElement(this);
|
|
return results;
|
|
}
|
|
|
|
List<IDescription> IFileRead.GetDescriptions(IFileRead fileRead, List<Test> tests, IProcessData processData)
|
|
{
|
|
List<IDescription> results = _Description.GetDescriptions(fileRead, _Logistics, tests, processData);
|
|
return results;
|
|
}
|
|
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.GetExtractResult(string reportFullPath, string eventName)
|
|
{
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
|
|
if (string.IsNullOrEmpty(eventName))
|
|
throw new Exception();
|
|
_ReportFullPath = reportFullPath;
|
|
DateTime dateTime = DateTime.Now;
|
|
results = GetExtractResult(reportFullPath, dateTime);
|
|
if (results.Item3 is null)
|
|
results = new Tuple<string, Test[], JsonElement[], List<FileInfo>>(results.Item1, Array.Empty<Test>(), JsonSerializer.Deserialize<JsonElement[]>("[]"), results.Item4);
|
|
if (results.Item3.Length > 0 && _IsEAFHosted)
|
|
WritePDSF(this, results.Item3);
|
|
UpdateLastTicksDuration(DateTime.Now.Ticks - dateTime.Ticks);
|
|
return results;
|
|
}
|
|
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.ReExtract()
|
|
{
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
|
|
List<string> headerNames = _Description.GetHeaderNames();
|
|
Dictionary<string, string> keyValuePairs = _Description.GetDisplayNamesJsonElement(this);
|
|
results = ReExtract(this, headerNames, keyValuePairs);
|
|
return results;
|
|
}
|
|
|
|
#nullable enable
|
|
|
|
private static void ParseWorkItemsAsync(FileConnectorConfiguration fileConnectorConfiguration, string[] alternateTargetFolders, ReadOnlyCollection<WorkItem> workItems)
|
|
{
|
|
string checkFile;
|
|
string? pathRoot;
|
|
JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true };
|
|
string json = JsonSerializer.Serialize(workItems, jsonSerializerOptions);
|
|
foreach (string alternateTargetFolder in alternateTargetFolders)
|
|
{
|
|
if (alternateTargetFolder == fileConnectorConfiguration.TargetFileLocation)
|
|
continue;
|
|
pathRoot = Path.GetPathRoot(alternateTargetFolder);
|
|
if (string.IsNullOrEmpty(pathRoot))
|
|
continue;
|
|
try
|
|
{
|
|
if (!Directory.Exists(pathRoot))
|
|
continue;
|
|
}
|
|
catch (Exception)
|
|
{ continue; }
|
|
if (!Directory.Exists(alternateTargetFolder))
|
|
_ = Directory.CreateDirectory(alternateTargetFolder);
|
|
checkFile = Path.Combine(alternateTargetFolder, fileConnectorConfiguration.TargetFileName);
|
|
if (File.Exists(checkFile))
|
|
{
|
|
string old = File.ReadAllText(checkFile);
|
|
if (old == json)
|
|
continue;
|
|
}
|
|
File.WriteAllText(checkFile, json);
|
|
}
|
|
}
|
|
|
|
private static ReadOnlyCollection<WorkItem> GetWorkItems(ReadOnlyCollection<Value> valueWithReqCollection)
|
|
{
|
|
List<WorkItem> results = new();
|
|
Fields fields;
|
|
WorkItem workItem;
|
|
foreach (Value value in valueWithReqCollection)
|
|
{
|
|
fields = value.Fields;
|
|
workItem = new(fields.SystemAreaPath,
|
|
fields.SystemAssignedTo?.DisplayName,
|
|
fields.MicrosoftVSTSCommonBusinessValue == 0 ? null : fields.MicrosoftVSTSCommonBusinessValue,
|
|
fields.SystemChangedDate,
|
|
fields.MicrosoftVSTSCommonClosedDate == DateTime.MinValue ? null : fields.MicrosoftVSTSCommonClosedDate,
|
|
fields.SystemCommentCount,
|
|
fields.SystemCreatedDate,
|
|
fields.SystemDescription,
|
|
fields.MicrosoftVSTSSchedulingEffort == 0 ? null : fields.MicrosoftVSTSSchedulingEffort,
|
|
value.Id,
|
|
fields.SystemIterationPath,
|
|
fields.SystemParent == 0 ? null : fields.SystemParent,
|
|
fields.MicrosoftVSTSCommonPriority == 0 ? null : fields.MicrosoftVSTSCommonPriority,
|
|
value.Relations,
|
|
fields.CustomRequester?.DisplayName,
|
|
fields.MicrosoftVSTSCommonResolvedDate == DateTime.MinValue ? null : fields.MicrosoftVSTSCommonResolvedDate,
|
|
value.Rev,
|
|
fields.CustomRRminusOE == 0 ? null : fields.CustomRRminusOE,
|
|
fields.MicrosoftVSTSSchedulingStartDate == DateTime.MinValue ? null : fields.MicrosoftVSTSSchedulingStartDate,
|
|
fields.SystemState,
|
|
fields.SystemTags,
|
|
fields.MicrosoftVSTSSchedulingTargetDate == DateTime.MinValue ? null : fields.MicrosoftVSTSSchedulingTargetDate,
|
|
fields.MicrosoftVSTSCommonTimeCriticality == 0 ? null : fields.MicrosoftVSTSCommonTimeCriticality,
|
|
fields.SystemTitle,
|
|
fields.SystemWorkItemType,
|
|
fields.CustomWSJF == 0 ? null : fields.CustomWSJF);
|
|
results.Add(workItem);
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
private static ReadOnlyCollection<Value> GetWorkItems(string[] files)
|
|
{
|
|
List<Value> results = new();
|
|
string json;
|
|
Value? value;
|
|
foreach (string file in from l in files orderby l.Length, l select l)
|
|
{
|
|
json = File.ReadAllText(file);
|
|
value = JsonSerializer.Deserialize<Value>(json);
|
|
if (value is null)
|
|
continue;
|
|
results.Add(value);
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
private static ReadOnlyDictionary<int, WorkItem> GetWorkItems(ReadOnlyCollection<WorkItem> workItems)
|
|
{
|
|
Dictionary<int, WorkItem> results = new();
|
|
foreach (WorkItem workItem in workItems)
|
|
results.Add(workItem.Id, workItem);
|
|
return new(results);
|
|
}
|
|
|
|
private static int? GetIdFromUrlIfChild(Relation relation)
|
|
{
|
|
int? result;
|
|
string[] segments = relation?.Attributes is null || relation.Attributes.Name != "Child" ? Array.Empty<string>() : relation.URL.Split('/');
|
|
if (segments.Length < 2)
|
|
result = null;
|
|
else
|
|
{
|
|
if (!int.TryParse(segments[segments.Length - 1], out int id))
|
|
result = null;
|
|
else
|
|
result = id;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static Dictionary<int, Record> GetKeyValuePairs(ReadOnlyDictionary<int, WorkItem> workItems, WorkItem workItem)
|
|
{
|
|
Dictionary<int, Record> results = new();
|
|
int? childId;
|
|
WorkItem? childWorkItem;
|
|
List<WorkItem> collection = new();
|
|
Dictionary<int, Record> keyValuePairs;
|
|
if (workItem.Relations is not null && workItem.Relations.Length > 0)
|
|
{
|
|
collection.Clear();
|
|
foreach (Relation relation in workItem.Relations)
|
|
{
|
|
childId = GetIdFromUrlIfChild(relation);
|
|
if (childId is null || !workItems.TryGetValue(childId.Value, out childWorkItem))
|
|
continue;
|
|
collection.Add(childWorkItem);
|
|
}
|
|
collection = (from l in collection orderby l.State != "Closed", l.Id select l).ToList();
|
|
foreach (WorkItem item in collection)
|
|
{
|
|
keyValuePairs = GetKeyValuePairs(workItems, item);
|
|
results.Add(item.Id, new(item, new(keyValuePairs)));
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static ReadOnlyDictionary<int, Record> GetWorkItemAndChildren(ReadOnlyDictionary<int, WorkItem> workItems)
|
|
{
|
|
Dictionary<int, Record> results = new();
|
|
Dictionary<int, Record> keyValuePairs;
|
|
foreach (KeyValuePair<int, WorkItem> keyValuePair in workItems)
|
|
{
|
|
// if (keyValuePair.Key != 119185)
|
|
// continue;
|
|
keyValuePairs = GetKeyValuePairs(workItems, keyValuePair.Value);
|
|
results.Add(keyValuePair.Key, new(keyValuePair.Value, new(keyValuePairs)));
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
private static string GetClosed(WorkItem workItem)
|
|
{
|
|
string result = workItem.State != "Closed" ? "[ ]" : "[x]";
|
|
return result;
|
|
}
|
|
|
|
private static string GetLine(List<char> spaces, WorkItem workItem, KeyValuePair<int, Record> keyValuePair, bool condensed, bool sprintOnly) =>
|
|
sprintOnly ? $"\t- [ ] {workItem.IterationPath}" :
|
|
condensed ? $"{new string(spaces.Skip(1).ToArray())}- {GetClosed(workItem)} {keyValuePair.Key} - {workItem.Title}" :
|
|
$"{new string(spaces.Skip(1).ToArray())}- {GetClosed(workItem)} {keyValuePair.Key} - {workItem.Title} ~~~ {workItem.AssignedTo} - {workItem.IterationPath.Replace('\\', '-')} - {workItem.CreatedDate} --- {workItem.ClosedDate}";
|
|
|
|
private static void AppendLines(List<char> spaces, List<string> lines, Record record, bool condensed, bool sprintOnly)
|
|
{
|
|
spaces.Add('\t');
|
|
WorkItem workItem;
|
|
foreach (KeyValuePair<int, Record> keyValuePair in record.Children)
|
|
{
|
|
workItem = keyValuePair.Value.WorkItem;
|
|
lines.Add(GetLine(spaces, workItem, keyValuePair, condensed, sprintOnly));
|
|
AppendLines(spaces, lines, keyValuePair.Value, condensed, sprintOnly);
|
|
}
|
|
spaces.RemoveAt(0);
|
|
}
|
|
|
|
private static void AppendLines(List<char> spaces, List<string> lines, ReadOnlyDictionary<int, Record> workItemAndChildren, string workItemType)
|
|
{
|
|
WorkItem workItem;
|
|
List<string> distinct = new();
|
|
foreach (KeyValuePair<int, Record> keyValuePair in workItemAndChildren)
|
|
{
|
|
workItem = keyValuePair.Value.WorkItem;
|
|
if (workItem.WorkItemType != workItemType)
|
|
continue;
|
|
lines.Add($"## {workItem.AssignedTo} - {workItem.Id} - {workItem.Title}");
|
|
lines.Add(string.Empty);
|
|
lines.Add($"- [{workItem.Id}](https://tfs.intra.infineon.com/tfs/FactoryIntegration/ART%20SPS/_workitems/edit/{workItem.Id})");
|
|
lines.Add(string.Empty);
|
|
if (keyValuePair.Value.Children.Count > 0)
|
|
{
|
|
AppendLines(spaces, lines, keyValuePair.Value, condensed: true, sprintOnly: false);
|
|
lines.Add(string.Empty);
|
|
distinct.Clear();
|
|
lines.Add($"## Distinct Iteration Path(s) - {workItem.WorkItemType} - {workItem.AssignedTo} - {workItem.Id} - {workItem.Title} - {workItem.IterationPath}");
|
|
lines.Add(string.Empty);
|
|
lines.Add($"- [{workItem.Id}](https://tfs.intra.infineon.com/tfs/FactoryIntegration/ART%20SPS/_workitems/edit/{workItem.Id})");
|
|
lines.Add(string.Empty);
|
|
AppendLines(spaces, distinct, keyValuePair.Value, condensed: false, sprintOnly: true);
|
|
distinct.Sort();
|
|
lines.AddRange(distinct.Distinct());
|
|
lines.Add(string.Empty);
|
|
lines.Add($"## Extended - {workItem.Id} - {workItem.Title}");
|
|
lines.Add(string.Empty);
|
|
AppendLines(spaces, lines, keyValuePair.Value, condensed: false, sprintOnly: false);
|
|
lines.Add(string.Empty);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void WriteMarkdownFile(FileConnectorConfiguration fileConnectorConfiguration, string[] alternateTargetFolders, ReadOnlyCollection<WorkItem> workItems, ReadOnlyCollection<string> workItemTypes)
|
|
{
|
|
string old;
|
|
string html;
|
|
string text;
|
|
string json;
|
|
string checkFile;
|
|
string? pathRoot;
|
|
List<char> spaces = new();
|
|
List<string> lines = new();
|
|
lines.Add("# WorkItems");
|
|
ReadOnlyDictionary<int, WorkItem> keyValuePairs = GetWorkItems(workItems);
|
|
ReadOnlyDictionary<int, Record> workItemAndChildren = GetWorkItemAndChildren(keyValuePairs);
|
|
foreach (string alternateTargetFolder in alternateTargetFolders)
|
|
{
|
|
if (alternateTargetFolder == fileConnectorConfiguration.TargetFileLocation)
|
|
continue;
|
|
pathRoot = Path.GetPathRoot(alternateTargetFolder);
|
|
if (string.IsNullOrEmpty(pathRoot))
|
|
continue;
|
|
try
|
|
{
|
|
if (!Directory.Exists(pathRoot))
|
|
continue;
|
|
}
|
|
catch (Exception)
|
|
{ continue; }
|
|
if (!Directory.Exists(alternateTargetFolder))
|
|
_ = Directory.CreateDirectory(alternateTargetFolder);
|
|
if (workItemAndChildren.Count == -1)
|
|
{
|
|
json = JsonSerializer.Serialize(workItemAndChildren, new JsonSerializerOptions() { WriteIndented = true });
|
|
File.WriteAllText(Path.Combine(alternateTargetFolder, ".json"), json);
|
|
}
|
|
foreach (string workItemType in workItemTypes)
|
|
{
|
|
AppendLines(spaces, lines, workItemAndChildren, workItemType);
|
|
checkFile = Path.Combine(alternateTargetFolder, $"{workItemType}.md");
|
|
text = string.Join(Environment.NewLine, lines);
|
|
old = !File.Exists(checkFile) ? string.Empty : File.ReadAllText(checkFile);
|
|
if (text != old)
|
|
File.WriteAllText(checkFile, text);
|
|
checkFile = Path.Combine(alternateTargetFolder, $"{workItemType}.html");
|
|
html = CommonMark.CommonMarkConverter.Convert(text);
|
|
old = !File.Exists(checkFile) ? string.Empty : File.ReadAllText(checkFile);
|
|
if (html != old)
|
|
File.WriteAllText(checkFile, html);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void ParseWorkItemsAsync(FileConnectorConfiguration fileConnectorConfiguration, string[] alternateTargetFolders, ReadOnlyCollection<string> workItemTypes)
|
|
{
|
|
if (!Directory.Exists(fileConnectorConfiguration.TargetFileLocation))
|
|
_ = Directory.CreateDirectory(fileConnectorConfiguration.TargetFileLocation);
|
|
string[] files = Directory.GetFiles(fileConnectorConfiguration.TargetFileLocation, fileConnectorConfiguration.SourceFileFilter, SearchOption.TopDirectoryOnly);
|
|
ReadOnlyCollection<Value> collection = GetWorkItems(files);
|
|
ReadOnlyCollection<WorkItem> workItems = GetWorkItems(collection);
|
|
if (workItems.Count > 0)
|
|
{
|
|
ParseWorkItemsAsync(fileConnectorConfiguration, alternateTargetFolders, workItems);
|
|
WriteMarkdownFile(fileConnectorConfiguration, alternateTargetFolders, workItems, workItemTypes);
|
|
}
|
|
}
|
|
|
|
private static void ParseWorkItemsAsync(FileConnectorConfiguration fileConnectorConfiguration, ReadOnlyCollection<string> workItemTypes)
|
|
{
|
|
string[] alternateTargetFolders = fileConnectorConfiguration.AlternateTargetFolder.Split('|');
|
|
if (alternateTargetFolders.Length > 0)
|
|
ParseWorkItemsAsync(fileConnectorConfiguration, alternateTargetFolders, workItemTypes);
|
|
}
|
|
|
|
private void Callback(object state)
|
|
{
|
|
try
|
|
{
|
|
if (_IsEAFHosted)
|
|
ParseWorkItemsAsync(_FileConnectorConfiguration, _WorkItemTypes);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
string subject = string.Concat("Exception:", _CellInstanceConnectionName);
|
|
string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace);
|
|
try
|
|
{ _SMTP.SendHighPriorityEmailMessage(subject, body); }
|
|
catch (Exception) { }
|
|
}
|
|
try
|
|
{
|
|
if (_FileConnectorConfiguration?.FileScanningIntervalInSeconds is null)
|
|
throw new Exception();
|
|
TimeSpan timeSpan = new(DateTime.Now.AddSeconds(_FileConnectorConfiguration.FileScanningIntervalInSeconds.Value).Ticks - DateTime.Now.Ticks);
|
|
_ = _Timer.Change((long)timeSpan.TotalMilliseconds, Timeout.Infinite);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
string subject = string.Concat("Exception:", _CellInstanceConnectionName);
|
|
string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace);
|
|
try
|
|
{ _SMTP.SendHighPriorityEmailMessage(subject, body); }
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
|
|
#pragma warning disable IDE0060
|
|
private void MoveJson(string reportFullPath, DateTime dateTime)
|
|
#pragma warning restore IDE0060
|
|
{
|
|
bool moveFile = true;
|
|
string json = File.ReadAllText(reportFullPath);
|
|
Value? value = JsonSerializer.Deserialize<Value>(json);
|
|
if (value is null)
|
|
throw new Exception(nameof(value));
|
|
string checkFile = Path.Combine(_FileConnectorConfiguration.TargetFileLocation, $"{value.Id}.json");
|
|
if (File.Exists(checkFile))
|
|
{
|
|
string old = File.ReadAllText(checkFile);
|
|
if (json == old)
|
|
moveFile = false;
|
|
else
|
|
File.Delete(checkFile);
|
|
}
|
|
if (moveFile)
|
|
File.Move(reportFullPath, checkFile);
|
|
}
|
|
|
|
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
|
|
{
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
|
|
_Logistics = new Logistics(reportFullPath, $"LOGISTICS_1{'\t'}A_JOBID={"BACKLOG"};A_MES_ENTITY={"BACKLOG"};");
|
|
if (_IsEAFHosted && _FileConnectorConfiguration.FileScanningIntervalInSeconds > 0)
|
|
MoveJson(reportFullPath, dateTime);
|
|
results = new(_Logistics.Logistics1[0], Array.Empty<Test>(), Array.Empty<JsonElement>(), new List<FileInfo>());
|
|
return results;
|
|
}
|
|
|
|
} |