ADO
This commit is contained in:
@ -1,86 +1,44 @@
|
||||
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 Microsoft.TeamFoundation.WorkItemTracking.WebApi;
|
||||
using Microsoft.VisualStudio.Services.Common;
|
||||
using Microsoft.VisualStudio.Services.WebApi;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
|
||||
namespace Adaptation.FileHandlers.json;
|
||||
|
||||
public class FileRead : Shared.FileRead, IFileRead
|
||||
{
|
||||
|
||||
internal static string Json { get; private set; }
|
||||
|
||||
private readonly string _API;
|
||||
private readonly string _Query;
|
||||
private readonly string _Project;
|
||||
private readonly string _BasePage;
|
||||
private readonly HttpClient _HttpClient;
|
||||
private string _LastDateForcedUpdatedBy;
|
||||
private string _LastDateDeleteForcedUpdatedBy;
|
||||
private readonly WorkItemTrackingHttpClient _WorkItemTrackingHttpClient;
|
||||
private readonly ReadOnlyDictionary<string, string> _RequestorNameToUser;
|
||||
private readonly ReadOnlyDictionary<string, string> _AssignedToNameToUser;
|
||||
private readonly ReadOnlyDictionary<string, string> _AssignedToNameToEncodedPAT;
|
||||
private readonly Timer _Timer;
|
||||
|
||||
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;
|
||||
Json = string.Empty;
|
||||
_NullData = string.Empty;
|
||||
_Logistics = new(this);
|
||||
if (_FileParameter is null)
|
||||
throw new Exception(cellInstanceConnectionName);
|
||||
if (_ModelObjectParameterDefinitions is null)
|
||||
throw new Exception(cellInstanceConnectionName);
|
||||
if (!_IsDuplicator)
|
||||
if (_IsDuplicator)
|
||||
throw new Exception(cellInstanceConnectionName);
|
||||
_LastDateForcedUpdatedBy = string.Empty;
|
||||
_LastDateDeleteForcedUpdatedBy = string.Empty;
|
||||
Dictionary<string, string> requestorNameToUser = new();
|
||||
Dictionary<string, string> assignedToNameToUser = new();
|
||||
Dictionary<string, string> assignedToNameToEncodedPAT = new();
|
||||
string cellInstanceNamed = string.Concat("CellInstance.", cellInstanceName);
|
||||
MediaTypeWithQualityHeaderValue mediaTypeWithQualityHeaderValue = new("application/json");
|
||||
_API = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.API");
|
||||
_Query = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.Query");
|
||||
string pat = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.PAT");
|
||||
_Project = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.Project");
|
||||
string basePage = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.BasePage");
|
||||
string baseAddress = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.BaseAddress");
|
||||
ModelObjectParameterDefinition[] assignedTo = GetProperties(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.Assinged.To.");
|
||||
foreach (ModelObjectParameterDefinition modelObjectParameterDefinition in assignedTo)
|
||||
assignedToNameToUser.Add(modelObjectParameterDefinition.Name.Split('.')[4], modelObjectParameterDefinition.Value);
|
||||
_AssignedToNameToUser = new(assignedToNameToUser);
|
||||
ModelObjectParameterDefinition[] requestor = GetProperties(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.Requestor.");
|
||||
foreach (ModelObjectParameterDefinition modelObjectParameterDefinition in requestor)
|
||||
requestorNameToUser.Add(modelObjectParameterDefinition.Name.Split('.')[3], modelObjectParameterDefinition.Value);
|
||||
_RequestorNameToUser = new(requestorNameToUser);
|
||||
ModelObjectParameterDefinition[] encodedPAT = GetProperties(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.Encoded.PAT.");
|
||||
foreach (ModelObjectParameterDefinition modelObjectParameterDefinition in encodedPAT)
|
||||
assignedToNameToEncodedPAT.Add(modelObjectParameterDefinition.Name.Split('.')[4], modelObjectParameterDefinition.Value);
|
||||
_AssignedToNameToEncodedPAT = new(assignedToNameToEncodedPAT);
|
||||
byte[] bytes = Encoding.ASCII.GetBytes($":{pat}");
|
||||
string base64 = Convert.ToBase64String(bytes);
|
||||
_HttpClient = new(new HttpClientHandler() { UseDefaultCredentials = true }) { BaseAddress = new(baseAddress) };
|
||||
_HttpClient.DefaultRequestHeaders.Authorization = new("Basic", base64);
|
||||
_HttpClient.DefaultRequestHeaders.Accept.Add(mediaTypeWithQualityHeaderValue);
|
||||
VssBasicCredential credential = new("", pat);
|
||||
VssConnection connection = new(new(string.Concat(baseAddress, basePage)), credential);
|
||||
_WorkItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();
|
||||
_BasePage = basePage;
|
||||
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);
|
||||
@ -148,44 +106,175 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
return results;
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
|
||||
private static void ParseWorkItemsAsync(FileConnectorConfiguration fileConnectorConfiguration, string[] alternateTargetFolders, ReadOnlyCollection<WorkItem> workItems)
|
||||
{
|
||||
bool writeFile;
|
||||
string checkFile;
|
||||
string? pathRoot;
|
||||
JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true };
|
||||
string json = JsonSerializer.Serialize(workItems, jsonSerializerOptions);
|
||||
foreach (string alternateTargetFolder in alternateTargetFolders)
|
||||
{
|
||||
writeFile = true;
|
||||
if (alternateTargetFolder == fileConnectorConfiguration.TargetFileLocation)
|
||||
continue;
|
||||
pathRoot = Path.GetPathRoot(alternateTargetFolder);
|
||||
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)
|
||||
writeFile = false;
|
||||
}
|
||||
if (writeFile)
|
||||
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.MicrosoftVSTSCommonPriority == 0 ? null : fields.MicrosoftVSTSCommonPriority,
|
||||
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 void ParseWorkItemsAsync(FileConnectorConfiguration fileConnectorConfiguration, string[] alternateTargetFolders)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
private static void ParseWorkItemsAsync(FileConnectorConfiguration fileConnectorConfiguration)
|
||||
{
|
||||
string[] alternateTargetFolders = fileConnectorConfiguration.AlternateTargetFolder.Split('|');
|
||||
if (alternateTargetFolders.Length > 0)
|
||||
ParseWorkItemsAsync(fileConnectorConfiguration, alternateTargetFolders);
|
||||
}
|
||||
|
||||
private void Callback(object state)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_IsEAFHosted)
|
||||
ParseWorkItemsAsync(_FileConnectorConfiguration);
|
||||
}
|
||||
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;
|
||||
File.Delete(reportFullPath);
|
||||
}
|
||||
}
|
||||
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 = new(string.Empty, null, null, new List<FileInfo>());
|
||||
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
|
||||
_Logistics = new Logistics(reportFullPath, $"LOGISTICS_1{'\t'}A_JOBID={"BACKLOG"};A_MES_ENTITY={"BACKLOG"};");
|
||||
string json = File.ReadAllText(reportFullPath);
|
||||
if (Json == json)
|
||||
results.Item4.Add(_Logistics.FileInfo);
|
||||
else
|
||||
{
|
||||
string formattedDateTime = dateTime.ToString("yyyy-MM-dd");
|
||||
bool forceUpdatedBy = false; // !_IsEAFHosted || (dateTime.DayOfWeek == DayOfWeek.Thursday && dateTime.Hour == 12 && _LastDateForcedUpdatedBy != formattedDateTime);
|
||||
if (forceUpdatedBy)
|
||||
_LastDateForcedUpdatedBy = formattedDateTime;
|
||||
bool forceDeleteUpdatedBy = false; // !_IsEAFHosted || (dateTime.DayOfWeek == DayOfWeek.Thursday && dateTime.Hour == 18 && _LastDateDeleteForcedUpdatedBy != formattedDateTime);
|
||||
if (forceDeleteUpdatedBy)
|
||||
_LastDateDeleteForcedUpdatedBy = formattedDateTime;
|
||||
IProcessData iProcessData = new ProcessData(this,
|
||||
_Logistics,
|
||||
results.Item4,
|
||||
_HttpClient,
|
||||
_BasePage,
|
||||
_API,
|
||||
_Query,
|
||||
_WorkItemTrackingHttpClient,
|
||||
_Project,
|
||||
_AssignedToNameToEncodedPAT,
|
||||
_AssignedToNameToUser,
|
||||
_RequestorNameToUser,
|
||||
json,
|
||||
forceUpdatedBy,
|
||||
forceDeleteUpdatedBy);
|
||||
if (iProcessData is not ProcessData _)
|
||||
throw new Exception(string.Concat("A) No Data - ", dateTime.Ticks));
|
||||
if (iProcessData.Details.Count == 0)
|
||||
throw new Exception(string.Concat("B) No Data - ", dateTime.Ticks));
|
||||
results = iProcessData.GetResults(this, _Logistics, results.Item4);
|
||||
Json = json;
|
||||
}
|
||||
if (_IsEAFHosted && _FileConnectorConfiguration.FileScanningIntervalInSeconds > 0)
|
||||
MoveJson(reportFullPath, dateTime);
|
||||
results = new(_Logistics.Logistics1[0], Array.Empty<Test>(), Array.Empty<JsonElement>(), new List<FileInfo>());
|
||||
return results;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user