Try Catch on Serialize

Better Relation Data
This commit is contained in:
2024-11-13 14:09:12 -07:00
parent 3fa7998ff6
commit 0505330ac5
17 changed files with 322 additions and 76 deletions

View File

@ -11,7 +11,9 @@ using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
namespace Adaptation.FileHandlers.json;
@ -111,14 +113,54 @@ public class FileRead : Shared.FileRead, IFileRead
#nullable enable
private static ReadOnlyCollection<WorkItem> GetWorkItems(ReadOnlyCollection<Value> valueWithReqCollection)
private static string GetParamCase(string value)
{
List<WorkItem> results = new();
string result;
StringBuilder stringBuilder = new(value);
List<Match> matches = new();
MatchCollection matchCollection = Regex.Matches(value, "([A-Z]+(.))");
foreach (object? item in matchCollection)
{
if (item is not Match match)
continue;
matches.Add(match);
}
for (int i = matches.Count - 1; i > -1; i--)
_ = stringBuilder.Insert(matches[i].Index, '-');
string[] segments = Regex.Split(stringBuilder.ToString().ToLower(), "[\\s!?.,@:;|\\\\/\"'`£$%\\^&*{}[\\]()<>~#+\\-=_¬]+");
result = string.Join("-", segments).Trim('-');
return result;
}
private static ReadOnlyDictionary<int, WorkItem> GetKeyValuePairs(ReadOnlyCollection<Value> valueWithReqCollection)
{
Dictionary<int, WorkItem> results = new();
Fields fields;
WorkItem workItem;
Relation relation;
string[] segments;
List<Relation> relations;
WorkItems.Attribute attributes;
foreach (Value value in valueWithReqCollection)
{
relations = new();
fields = value.Fields;
if (value.Relations is not null)
{
foreach (WIQL.Relation r in value.Relations)
{
segments = r?.Attributes is null ? Array.Empty<string>() : r.URL.Split('/');
if (segments.Length < 2)
continue;
if (r?.Attributes is null)
continue;
if (!int.TryParse(segments[segments.Length - 1], out int id))
continue;
attributes = new(r.Attributes.IsLocked, r.Attributes.Name, null, null, null);
relation = new(attributes, id, r.Rel);
relations.Add(relation);
}
}
workItem = new(fields.MicrosoftVSTSCommonActivatedDate == DateTime.MinValue ? null : fields.MicrosoftVSTSCommonActivatedDate,
fields.SystemAreaPath,
fields.SystemAssignedTo?.DisplayName,
@ -133,7 +175,7 @@ public class FileRead : Shared.FileRead, IFileRead
fields.SystemIterationPath,
fields.SystemParent == 0 ? null : fields.SystemParent,
fields.MicrosoftVSTSCommonPriority == 0 ? null : fields.MicrosoftVSTSCommonPriority,
value.Relations,
relations.ToArray(),
fields.CustomRequester?.DisplayName,
fields.MicrosoftVSTSCommonResolvedDate == DateTime.MinValue ? null : fields.MicrosoftVSTSCommonResolvedDate,
value.Rev,
@ -143,10 +185,41 @@ public class FileRead : Shared.FileRead, IFileRead
fields.SystemTags,
fields.MicrosoftVSTSSchedulingTargetDate == DateTime.MinValue ? null : fields.MicrosoftVSTSSchedulingTargetDate,
fields.MicrosoftVSTSCommonTimeCriticality is null or 0 ? null : (long)fields.MicrosoftVSTSCommonTimeCriticality,
fields.SystemTitle,
fields.SystemTitle.Trim(),
null,
fields.CustomWSJF is null or 0 ? null : (long)fields.CustomWSJF,
fields.SystemWorkItemType);
results.Add(workItem.Id, workItem);
}
return new(results);
}
private static ReadOnlyCollection<WorkItem> GetWorkItems(ReadOnlyCollection<Value> valueWithReqCollection)
{
List<WorkItem> results = new();
WorkItem workItem;
Relation relation;
string parameterTitle;
List<Relation> relations;
WorkItem? relationWorkItem;
WorkItems.Attribute attributes;
ReadOnlyDictionary<int, WorkItem> keyValuePairs = GetKeyValuePairs(valueWithReqCollection);
foreach (KeyValuePair<int, WorkItem> keyValuePair in keyValuePairs)
{
relations = new();
if (keyValuePair.Value.Relations is not null)
{
foreach (Relation r in keyValuePair.Value.Relations)
{
if (!keyValuePairs.TryGetValue(r.Id, out relationWorkItem))
continue;
parameterTitle = GetParamCase(relationWorkItem.Title);
attributes = new(r.Attributes.IsLocked, r.Attributes.Name, parameterTitle, relationWorkItem.State, relationWorkItem.WorkItemType);
relation = new(attributes, r.Id, r.Rel);
relations.Add(relation);
}
}
workItem = WorkItem.Get(keyValuePair.Value, relations.ToArray());
results.Add(workItem);
}
return new(results);
@ -237,7 +310,10 @@ public class FileRead : Shared.FileRead, IFileRead
string subject = string.Concat("Exception:", _CellInstanceConnectionName);
string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace);
try
{ _SMTP.SendHighPriorityEmailMessage(subject, body); }
{
_SMTP.SendHighPriorityEmailMessage(subject, body);
File.WriteAllText(".email", body);
}
catch (Exception) { }
}
try
@ -252,7 +328,10 @@ public class FileRead : Shared.FileRead, IFileRead
string subject = string.Concat("Exception:", _CellInstanceConnectionName);
string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace);
try
{ _SMTP.SendHighPriorityEmailMessage(subject, body); }
{
_SMTP.SendHighPriorityEmailMessage(subject, body);
File.WriteAllText(".email", body);
}
catch (Exception) { }
}
}

View File

@ -0,0 +1,25 @@
using System.Text.Json.Serialization;
namespace Adaptation.FileHandlers.json.WIQL;
internal class Attribute
{
[JsonConstructor]
public Attribute(bool isLocked,
string name)
{
IsLocked = isLocked;
Name = name;
}
[JsonPropertyName("isLocked")] public bool IsLocked { get; }
[JsonPropertyName("name")] public string Name { get; }
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Attribute))]
internal partial class WIQLAttributeSourceGenerationContext : JsonSerializerContext
{
}

View File

@ -0,0 +1,35 @@
using System.Text.Json.Serialization;
namespace Adaptation.FileHandlers.json.WIQL;
internal class Relation
{
[JsonConstructor]
public Relation(string rel,
string url,
Attribute attributes)
{
Rel = rel;
Rel = rel;
URL = url;
Attributes = attributes;
}
[JsonPropertyName("attributes")] public Attribute Attributes { get; set; }
[JsonPropertyName("rel")] public string Rel { get; set; }
[JsonPropertyName("url")] public string URL { get; set; }
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Relation))]
internal partial class WIQLRelationSourceGenerationContext : JsonSerializerContext
{
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Relation[]))]
internal partial class WIQLRelationCollectionSourceGenerationContext : JsonSerializerContext
{
}

View File

@ -5,16 +5,27 @@ namespace Adaptation.FileHandlers.json.WorkItems;
internal class Attribute
{
#nullable enable
[JsonConstructor]
public Attribute(bool isLocked,
string name)
string name,
string? parameterTitle,
string? state,
string? workItemType)
{
IsLocked = isLocked;
Name = name;
ParameterTitle = parameterTitle;
State = state;
WorkItemType = workItemType;
}
[JsonPropertyName("isLocked")] public bool IsLocked { get; }
[JsonPropertyName("name")] public string Name { get; }
[JsonPropertyName("parameterTitle")] public string? ParameterTitle { get; set; }
[JsonPropertyName("state")] public string? State { get; set; }
[JsonPropertyName("workItemType")] public string? WorkItemType { get; set; }
}

View File

@ -88,30 +88,13 @@ internal class Record
return result;
}
private static int? GetIdFromUrl(string relationName, Relation relation)
{
int? result;
string[] segments = relation?.Attributes is null || relation.Attributes.Name != relationName ? 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;
}
internal static ReadOnlyCollection<Record> GetKeyValuePairs(ReadOnlyDictionary<int, WorkItem> keyValuePairs, WorkItem workItem, string relationName, List<bool> nests, bool keepRelations)
{
List<Record> results = new();
int? childId;
Record record;
nests.Add(true);
WorkItem? childWorkItem;
WorkItem? parentWorkItem;
WorkItem? relationWorkItem;
List<WorkItem> collection = new();
ReadOnlyCollection<Record> childRecords;
ReadOnlyCollection<Record> relatedRecords;
@ -121,12 +104,13 @@ internal class Record
collection.Clear();
foreach (Relation relation in workItem.Relations)
{
childId = GetIdFromUrl(relationName, relation);
if (childId is not null && workItem.Parent is not null && relation?.URL is not null && relation.URL.Contains(workItem.Parent.Value.ToString()))
if (relation.Attributes.Name != relationName)
continue;
if (childId is null || !keyValuePairs.TryGetValue(childId.Value, out childWorkItem))
if (workItem.Parent is not null && relation.Id == workItem.Parent.Value)
continue;
collection.Add(childWorkItem);
if (!keyValuePairs.TryGetValue(relation.Id, out relationWorkItem))
continue;
collection.Add(relationWorkItem);
}
collection = (from l in collection orderby l.State != "Closed", l.Id select l).ToList();
foreach (WorkItem w in collection)
@ -138,14 +122,9 @@ internal class Record
else
_ = keyValuePairs.TryGetValue(w.Parent.Value, out parentWorkItem);
childRecords = GetKeyValuePairs(keyValuePairs, w, "Child", nests, keepRelations); // Forward
// records = GetKeyValuePairs(keyValuePairs, w, "Predecessor", nests); // Reverse
// successorRecords = GetKeyValuePairs(keyValuePairs, w, "Successor", nests); // Forward
// if (successorRecords.Count > 0)
// {
// if (successorRecords.Count > 0)
// { }
// }
successorRecords = new(Array.Empty<Record>());
// predecessorRecords = GetKeyValuePairs(keyValuePairs, w, "Predecessor", nests); // Reverse
relatedRecords = GetKeyValuePairs(keyValuePairs, w, "Related", nests, keepRelations); // Related
record = Get(w, parentWorkItem, childRecords, relatedRecords, successorRecords, keepRelations);
results.Add(record);
@ -160,4 +139,10 @@ internal class Record
[JsonSerializable(typeof(Record))]
internal partial class RecordSourceGenerationContext : JsonSerializerContext
{
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Record[]))]
internal partial class RecordCollectionSourceGenerationContext : JsonSerializerContext
{
}

View File

@ -6,18 +6,18 @@ internal class Relation
{
[JsonConstructor]
public Relation(string rel,
string url,
Attribute attributes)
public Relation(Attribute attributes,
int id,
string rel)
{
Rel = rel;
URL = url;
Attributes = attributes;
Id = id;
Rel = rel;
}
[JsonPropertyName("attributes")] public Attribute Attributes { get; set; }
[JsonPropertyName("id")] public int Id { get; set; }
[JsonPropertyName("rel")] public string Rel { get; set; }
[JsonPropertyName("url")] public string URL { get; set; }
}

View File

@ -10,7 +10,7 @@ internal class Value
int id,
int rev,
Fields fields,
Relation[] relations,
WIQL.Relation[] relations,
CommentVersionRef commentVersionRef,
string url
)
@ -26,7 +26,7 @@ internal class Value
[JsonPropertyName("commentVersionRef")] public CommentVersionRef CommentVersionRef { get; }
[JsonPropertyName("fields")] public Fields Fields { get; }
[JsonPropertyName("id")] public int Id { get; }
[JsonPropertyName("relations")] public Relation[] Relations { get; }
[JsonPropertyName("relations")] public WIQL.Relation[] Relations { get; }
[JsonPropertyName("rev")] public int Rev { get; }
[JsonPropertyName("url")] public string Url { get; }

View File

@ -70,6 +70,39 @@ internal class WorkItem
public override string ToString() => $"{Id} - {WorkItemType} - {Title}";
public static WorkItem Get(WorkItem workItem, Relation[] relations)
{
WorkItem result = new(workItem.ActivatedDate,
workItem.AreaPath,
workItem.AssignedTo,
workItem.BusinessValue,
workItem.ChangedDate,
workItem.ClosedDate,
workItem.CommentCount,
workItem.CreatedDate,
workItem.Description,
workItem.Effort,
workItem.Id,
workItem.IterationPath,
workItem.Parent,
workItem.Priority,
relations,
workItem.Requester,
workItem.ResolvedDate,
workItem.Revision,
workItem.RiskReductionMinusOpportunityEnablement,
workItem.StartDate,
workItem.State,
workItem.Tags,
workItem.TargetDate,
workItem.TimeCriticality,
workItem.Title,
workItem.Violation,
workItem.WeightedShortestJobFirst,
workItem.WorkItemType);
return result;
}
public static WorkItem? GetWithOutRelations(WorkItem? workItem)
{
WorkItem? result = workItem is null ? null : new(workItem.ActivatedDate,