This commit is contained in:
2025-04-10 20:00:02 -07:00
parent 2fc83bb54d
commit 906868540b
36 changed files with 547 additions and 496 deletions

View File

@ -45,6 +45,7 @@ public class Aggregation
private static ReadOnlyDictionary<int, Aggregation> GetKeyValuePairs(Settings settings, Dictionary<int, List<Notification>> keyValuePairs)
{
Dictionary<int, Aggregation> results = new();
int value;
int? inverseValue;
double inverseAverage;
Aggregation aggregation;
@ -60,7 +61,9 @@ public class Aggregation
fibonacciCollection.Clear();
foreach (Notification notification in keyValuePair.Value)
{
collection.Add(notification.Value);
if (!int.TryParse(notification.Value, out value))
continue;
collection.Add(value);
if (notification.Inverse is null)
continue;
inverseCollection.Add(notification.Inverse.Value);
@ -72,7 +75,7 @@ public class Aggregation
continue;
inverseAverage = Math.Round(inverseCollection.Average(), settings.Digits);
averageFromInverseCeiling = (int)Math.Ceiling(inverseAverage);
inverseValue = Notification.GetInverse(averageFromInverseCeiling);
inverseValue = Notification.GetInverse(averageFromInverseCeiling.ToString());
fibonacciAverage = Math.Round(fibonacciCollection.Average(), settings.Digits);
aggregation = new(inverseAverage: inverseAverage,
valueCount: collection.Count,
@ -107,9 +110,9 @@ public class Aggregation
if (string.IsNullOrEmpty(text) || text[0] == '[')
continue;
notification = JsonSerializer.Deserialize(text, NotificationSourceGenerationContext.Default.Notification);
if (notification is null || notification.Id == 0)
if (notification is null || string.IsNullOrEmpty(notification.Id))
continue;
key = !string.IsNullOrEmpty(notification.Username) ? notification.Username : notification.RemoteIpAddress;
key = !string.IsNullOrEmpty(notification.Username) ? notification.Username : throw new Exception();
if (string.IsNullOrEmpty(key))
continue;
if (!keyValuePairs.TryGetValue(key, out collection))
@ -126,7 +129,7 @@ public class Aggregation
results.Add(keyValuePair.Value[0]);
else
{
notification = keyValuePair.Value.Select(record => new KeyValuePair<long, Notification>(record.Time, record)).OrderBy(pair => pair.Key).Last().Value;
notification = keyValuePair.Value.Select(record => new KeyValuePair<string, Notification>(record.Time, record)).OrderBy(pair => pair.Key).Last().Value;
results.Add(notification);
}
}
@ -137,15 +140,18 @@ public class Aggregation
private static ReadOnlyDictionary<int, Aggregation> GetKeyValuePairs(Settings settings, string directory)
{
ReadOnlyDictionary<int, Aggregation> results;
int id;
List<Notification>? collection;
Dictionary<int, List<Notification>> keyValuePairs = new();
ReadOnlyCollection<Notification> notifications = GetNotifications(settings, directory);
foreach (Notification notification in notifications)
{
if (!keyValuePairs.TryGetValue(notification.Id, out collection))
if (!int.TryParse(notification.Id, out id))
continue;
if (!keyValuePairs.TryGetValue(id, out collection))
{
keyValuePairs.Add(notification.Id, new());
if (!keyValuePairs.TryGetValue(notification.Id, out collection))
keyValuePairs.Add(id, new());
if (!keyValuePairs.TryGetValue(id, out collection))
throw new Exception();
}
collection.Add(notification);
@ -185,7 +191,7 @@ public class Aggregation
internal static ReadOnlyDictionary<int, Aggregation> GetKeyValuePairs(Settings settings, Notification notification)
{
ReadOnlyDictionary<int, Aggregation> results;
Dictionary<int, List<Notification>> keyValuePairs = new() { { notification.Id, new Notification[] { notification }.ToList() } };
Dictionary<int, List<Notification>> keyValuePairs = new() { { int.Parse(notification.Id), new Notification[] { notification }.ToList() } };
results = GetKeyValuePairs(settings, keyValuePairs);
return results;
}

View File

@ -6,8 +6,10 @@ using Adaptation.Shared.Methods;
using log4net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Threading;
namespace Adaptation.FileHandlers.Priority;
@ -16,6 +18,7 @@ namespace Adaptation.FileHandlers.Priority;
public class FileRead : Shared.FileRead, IFileRead
{
private readonly Timer _Timer;
internal static ILog Log => _Log;
internal static Settings Settings => _Settings;
internal static Dictionary<int, WorkItem> WorkItems => _WorkItems;
@ -49,16 +52,22 @@ public class FileRead : Shared.FileRead, IFileRead
sourceFileFilter: _FileConnectorConfiguration.SourceFileFilter,
sourceFileLocation: _FileConnectorConfiguration.SourceFileLocation,
targetFileLocation: _FileConnectorConfiguration.TargetFileLocation);
string? json = WeightedShortestJobFirstHub.PopulatedWorkItemsAndGetJson(_Settings);
if (!string.IsNullOrEmpty(json))
WeightedShortestJobFirstHub.WriteJson(json);
_Timer = new Timer(Callback, null, Timeout.Infinite, Timeout.Infinite);
string cellInstanceNamed = string.Concat("CellInstance.", _EquipmentType);
string url = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.Microsoft.Owin.Hosting.WebApp.Start.URL");
if (_IsEAFHosted)
{
_ = Microsoft.Owin.Hosting.WebApp.Start(url);
_ = Microsoft.Owin.Hosting.WebApp.Start<Startup>(url);
_Log.Info($"Server running on {url}");
}
if (Debugger.IsAttached || fileConnectorConfiguration.PreProcessingMode == FileConnectorConfiguration.PreProcessingModeEnum.Process)
Callback(null);
else
{
long fileScanningIntervalInSeconds = _FileConnectorConfiguration.FileScanningIntervalInSeconds is null ? 0 : _FileConnectorConfiguration.FileScanningIntervalInSeconds.Value;
TimeSpan timeSpan = new(DateTime.Now.AddSeconds(fileScanningIntervalInSeconds).Ticks - DateTime.Now.Ticks);
_ = _Timer.Change((long)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
}
void IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> extractResults, Exception exception) => Move(extractResults);
@ -138,4 +147,28 @@ public class FileRead : Shared.FileRead, IFileRead
return results;
}
private void Callback(object? state)
{
try
{
_Log.Info($"Enter-{nameof(WeightedShortestJobFirstModule.PopulatedWorkItemsAndGetJson)}");
string? json = WeightedShortestJobFirstModule.PopulatedWorkItemsAndGetJson(_Settings);
if (!string.IsNullOrEmpty(json))
WeightedShortestJobFirstModule.WriteJson(json);
_Log.Info($"End-{nameof(WeightedShortestJobFirstModule.PopulatedWorkItemsAndGetJson)}");
}
catch (Exception exception)
{
string subject = string.Concat("Exception:", _CellInstanceConnectionName);
string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace);
_Log.Fatal($"Exception-{nameof(WeightedShortestJobFirstModule.PopulatedWorkItemsAndGetJson)}{Environment.NewLine}{body}");
try
{
_SMTP.SendHighPriorityEmailMessage(subject, body);
File.WriteAllText(".email", body);
}
catch (Exception) { }
}
}
}

View File

@ -9,15 +9,14 @@ public class Notification
[JsonConstructor]
public Notification(int? fibonacci,
int id,
string id,
int? inverse,
string? machineId,
string page,
string? remoteIpAddress,
string? site,
long time,
string time,
string? username,
int value)
string? value)
{
int? i = inverse is not null ? inverse : GetInverse(value);
Fibonacci = fibonacci is not null ? fibonacci : i is null ? null : GetFibonacci(i.Value);
@ -25,32 +24,30 @@ public class Notification
Inverse = i;
MachineId = machineId;
Page = page;
RemoteIpAddress = remoteIpAddress is not null ? remoteIpAddress : null;
Site = site is not null ? site : "MES";
Time = time;
Username = username;
Value = value;
}
[JsonPropertyName("id")] public int Id { get; }
[JsonPropertyName("id")] public string Id { get; }
[JsonPropertyName("fibonacci")] public int? Fibonacci { get; }
[JsonPropertyName("inverse")] public int? Inverse { get; }
[JsonPropertyName("machineId")] public string? MachineId { get; }
[JsonPropertyName("page")] public string Page { get; }
[JsonPropertyName("RemoteIpAddress")] public string? RemoteIpAddress { get; }
[JsonPropertyName("site")] public string? Site { get; }
[JsonPropertyName("time")] public long Time { get; }
[JsonPropertyName("time")] public string Time { get; }
[JsonPropertyName("username")] public string? Username { get; }
[JsonPropertyName("value")] public int Value { get; }
[JsonPropertyName("value")] public string? Value { get; }
internal static int? GetInverse(int value) =>
internal static int? GetInverse(string? value) =>
value switch
{
1 => 5,
2 => 4,
3 => 3,
4 => 2,
5 => 1,
"1" => 5,
"2" => 4,
"3" => 3,
"4" => 2,
"5" => 1,
_ => null
};
@ -69,18 +66,6 @@ public class Notification
_ => null
};
internal static Notification GetNotification(Notification notification, string? remoteIpAddress, string? connectionId) =>
new(notification.Fibonacci,
notification.Id,
notification.Inverse,
notification.MachineId,
notification.Page,
remoteIpAddress ?? connectionId,
notification.Site,
notification.Time,
notification.Username,
notification.Value);
}
[JsonSourceGenerationOptions(WriteIndented = true)]

View File

@ -1,4 +1,5 @@
using Microsoft.Owin.Cors;
using Nancy.Owin;
using Owin;
public class Startup
@ -7,7 +8,10 @@ public class Startup
public void Configuration(IAppBuilder app)
{
_ = app.UseCors(CorsOptions.AllowAll);
_ = app.UseNancy();
#if SignalR
_ = app.MapSignalR();
#endif
}
}

View File

@ -1,12 +1,11 @@
#if SignalR
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text.Json;
#nullable enable
#pragma warning disable CA1822
namespace Adaptation.FileHandlers.Priority;
public class WeightedShortestJobFirstHub : Microsoft.AspNet.SignalR.Hub
@ -17,105 +16,22 @@ public class WeightedShortestJobFirstHub : Microsoft.AspNet.SignalR.Hub
// await Clients.All.send(n);
// }
private string? GetRemoteIpAddress() =>
Context?.Headers?.Get("X-Real-IP");
public void Send(string name, string message)
{
Console.WriteLine($"{name}:{message};");
// FileRead.Logger.LogWarning($"{name}:{message};");
// FileRead.Log?.Info($"{name}:{message};");
Console.WriteLine(Context?.ConnectionId);
// FileRead.Logger.LogWarning(Context?.ConnectionId);
// FileRead.Log?.Info(Context?.ConnectionId);
string? remoteIpAddress = GetRemoteIpAddress();
Console.WriteLine(remoteIpAddress);
// FileRead.Logger.LogWarning(remoteIpAddress);
// FileRead.Log?.Info(remoteIpAddress);
Clients.All.addMessage(name, message);
}
private static void FileWriteAllText(Settings settings, Notification n)
public void NotifyAll(Notification notification)
{
string json = JsonSerializer.Serialize(n, NotificationSourceGenerationContext.Default.Notification);
string directory = Path.Combine(settings.SourceFileLocation, n.Page, n.Id.ToString());
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
string checkFile = Path.Combine(directory, $"{n.Time}.json");
File.WriteAllText(checkFile, json);
}
internal static void WriteJson(string json)
{
string jsonFile = Path.Combine(FileRead.Settings.ParentDirectory, "{}.json");
string jsonFileWith = Path.Combine(FileRead.Settings.ParentDirectory, "{[]}.json");
string jsonOld = File.Exists(jsonFileWith) ? File.ReadAllText(jsonFileWith) : string.Empty;
if (json != jsonOld)
try
{
File.WriteAllText(jsonFileWith, json);
Dictionary<int, WorkItem> w = JsonSerializer.Deserialize(json.Replace($"\"{nameof(Aggregation.Notifications)}\":", "\"ignore\":"), WorkItemDictionarySourceGenerationContext.Default.DictionaryInt32WorkItem) ?? throw new Exception();
json = JsonSerializer.Serialize(w, WorkItemDictionarySourceGenerationContext.Default.DictionaryInt32WorkItem);
File.WriteAllText(jsonFile, json);
WorkItem workItem = GetWorkItem(notification);
Clients.All.updateWorkItem(notification.Page, workItem);
}
}
internal static string? PopulatedWorkItemsAndGetJson(Settings settings)
{
string? result = null;
ReadOnlyDictionary<int, WorkItem?> workItems = WorkItem.GetKeyValuePairs(settings);
int useCount = (from l in workItems where l.Value.CostOfDelay is not null select true).Count();
double prioritySize = useCount / settings.Priorities;
double priorityGroupSize = useCount / settings.PriorityGroups;
WorkItem[] sorted = (from l in workItems
where l.Value is not null
orderby l.Value.Site is not null,
l.Value.Site descending,
l.Value.CostOfDelay is not null,
l.Value.CostOfDelay descending,
l.Value.BusinessValue?.FibonacciAverage is not null,
l.Value.BusinessValue?.FibonacciAverage descending,
l.Key
select l.Value).ToArray();
lock (FileRead.WorkItems)
{
int j = 0;
WorkItem w;
double value;
int lastId = -1;
int? sortBeforeId;
WorkItem workItem;
int? sortPriority;
int? sortPriorityGroup;
FileRead.WorkItems.Clear();
for (int i = 0; i < sorted.Length; i++)
{
w = sorted[i];
if (w.CostOfDelay is null)
{
sortBeforeId = null;
sortPriority = null;
sortPriorityGroup = null;
}
else
{
j += 1;
sortBeforeId = lastId;
value = (j / prioritySize) + 1;
sortPriority = (int)Math.Floor(value);
if (sortPriority > settings.Priorities)
sortPriority = settings.Priorities;
value = (j / priorityGroupSize) + 1;
sortPriorityGroup = (int)Math.Floor(value);
if (sortPriorityGroup > settings.PriorityGroups)
sortPriorityGroup = settings.PriorityGroups;
}
workItem = WorkItem.GetWorkItem(w, i, sortBeforeId, sortPriority, sortPriorityGroup);
FileRead.WorkItems.Add(workItem.Id, workItem);
lastId = w.Id;
}
result = JsonSerializer.Serialize(FileRead.WorkItems, WorkItemDictionarySourceGenerationContext.Default.DictionaryInt32WorkItem);
}
return result;
catch (Exception ex)
{ Console.WriteLine($"{ex.Message}{Environment.NewLine}{ex.StackTrace}"); }
}
private static WorkItem GetWorkItem(Notification notification)
@ -129,30 +45,6 @@ public class WeightedShortestJobFirstHub : Microsoft.AspNet.SignalR.Hub
return result;
}
public void NotifyAll(Notification notification)
{
try
{
string? json = null;
string? remoteIpAddress = GetRemoteIpAddress();
Notification n = Notification.GetNotification(notification, remoteIpAddress, Context?.ConnectionId);
Console.WriteLine(n.ToString());
// FileRead.Logger.LogWarning(n.ToString());
// FileRead.Log?.Info(n.ToString());
FileWriteAllText(FileRead.Settings, n);
json = PopulatedWorkItemsAndGetJson(FileRead.Settings);
if (!string.IsNullOrEmpty(json))
WriteJson(json);
if (!string.IsNullOrEmpty(n.RemoteIpAddress))
{
WorkItem workItem = GetWorkItem(n);
Clients.All.updateWorkItem(n.Page, workItem);
}
}
catch (Exception ex)
{ Console.WriteLine($"{ex.Message}{Environment.NewLine}{ex.StackTrace}"); }
// { FileRead.Logger.LogError(ex, "Error!"); }
// { FileRead.Log?.Error("Error!", ex); }
}
}
}
#endif

View File

@ -0,0 +1,155 @@
using log4net;
using Nancy;
using Nancy.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text.Json;
#nullable enable
#pragma warning disable CA1822
namespace Adaptation.FileHandlers.Priority;
public class WeightedShortestJobFirstModule : NancyModule
{
public WeightedShortestJobFirstModule()
{
Get("/api/v1/ado/", _ =>
{
ILog log = LogManager.GetLogger(typeof(WeightedShortestJobFirstModule));
log.Info($"{nameof(Get)}");
return "Hello";
});
base.Post("/api/v1/ado/", _ =>
{
Notification notification;
ILog log = LogManager.GetLogger(typeof(WeightedShortestJobFirstModule));
log.Info($"Enter-{nameof(Post)}");
try
{
string body = Request.Body.AsString();
DynamicDictionary form = Request.Form;
Dictionary<string, object> keyValuePairs = form.ToDictionary();
notification = GetNotification(body, keyValuePairs);
}
catch (Exception ex)
{
log.Fatal($"Exception-{nameof(Post)}{Environment.NewLine}{ex.Message}{Environment.NewLine}{Environment.NewLine}{ex.StackTrace}");
throw;
}
log.Info($"Return-{nameof(Post)}");
return notification.Time.ToString();
});
}
internal static Notification GetNotification(string body, Dictionary<string, object> keyValuePairs)
{
string? json;
Notification notification;
if (!string.IsNullOrEmpty(body) && body[0] == '{')
{
File.WriteAllText(".json", body);
notification = JsonSerializer.Deserialize(body, NotificationSourceGenerationContext.Default.Notification) ?? throw new NullReferenceException();
}
else
{
json = JsonSerializer.Serialize(keyValuePairs);
File.WriteAllText(".json", json);
notification = JsonSerializer.Deserialize(json, NotificationSourceGenerationContext.Default.Notification) ?? throw new NullReferenceException();
}
FileWriteAllText(FileRead.Settings, notification);
json = PopulatedWorkItemsAndGetJson(FileRead.Settings);
if (!string.IsNullOrEmpty(json))
WriteJson(json);
return notification;
}
private static void FileWriteAllText(Settings settings, Notification notification)
{
string json = JsonSerializer.Serialize(notification, NotificationSourceGenerationContext.Default.Notification);
string directory = Path.Combine(settings.SourceFileLocation, notification.Page, notification.Id.ToString());
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
string checkFile = Path.Combine(directory, $"{notification.Time}.json");
File.WriteAllText(checkFile, json);
}
internal static string? PopulatedWorkItemsAndGetJson(Settings settings)
{
string? result = null;
ReadOnlyDictionary<int, WorkItem?> workItems = WorkItem.GetKeyValuePairs(settings);
int useCount = (from l in workItems where l.Value.CostOfDelay is not null select true).Count();
double prioritySize = useCount / settings.Priorities;
double priorityGroupSize = useCount / settings.PriorityGroups;
WorkItem[] sorted = (from l in workItems
where l.Value is not null
orderby l.Value.Site is not null,
l.Value.Site descending,
l.Value.CostOfDelay is not null,
l.Value.CostOfDelay descending,
l.Value.BusinessValue?.FibonacciAverage is not null,
l.Value.BusinessValue?.FibonacciAverage descending,
l.Key
select l.Value).ToArray();
lock (FileRead.WorkItems)
{
int j = 0;
WorkItem w;
double value;
int lastId = -1;
int? sortBeforeId;
WorkItem workItem;
int? sortPriority;
int? sortPriorityGroup;
FileRead.WorkItems.Clear();
for (int i = 0; i < sorted.Length; i++)
{
w = sorted[i];
if (w.CostOfDelay is null)
{
sortBeforeId = null;
sortPriority = null;
sortPriorityGroup = null;
}
else
{
j += 1;
sortBeforeId = lastId;
value = (j / prioritySize) + 1;
sortPriority = (int)Math.Floor(value);
if (sortPriority > settings.Priorities)
sortPriority = settings.Priorities;
value = (j / priorityGroupSize) + 1;
sortPriorityGroup = (int)Math.Floor(value);
if (sortPriorityGroup > settings.PriorityGroups)
sortPriorityGroup = settings.PriorityGroups;
}
workItem = WorkItem.GetWorkItem(w, i, sortBeforeId, sortPriority, sortPriorityGroup);
FileRead.WorkItems.Add(workItem.Id, workItem);
lastId = w.Id;
}
result = JsonSerializer.Serialize(FileRead.WorkItems, WorkItemDictionarySourceGenerationContext.Default.DictionaryInt32WorkItem);
}
return result;
}
internal static void WriteJson(string json)
{
string jsonFile = Path.Combine(FileRead.Settings.ParentDirectory, "{}.json");
string jsonFileWith = Path.Combine(FileRead.Settings.ParentDirectory, "{[]}.json");
string jsonOld = File.Exists(jsonFileWith) ? File.ReadAllText(jsonFileWith) : string.Empty;
if (json != jsonOld)
{
File.WriteAllText(jsonFileWith, json);
Dictionary<int, WorkItem> w = JsonSerializer.Deserialize(json.Replace($"\"{nameof(Aggregation.Notifications)}\":", "\"ignore\":"), WorkItemDictionarySourceGenerationContext.Default.DictionaryInt32WorkItem) ?? throw new Exception();
json = JsonSerializer.Serialize(w, WorkItemDictionarySourceGenerationContext.Default.DictionaryInt32WorkItem);
File.WriteAllText(jsonFile, json);
}
}
}