Cost of Delay 1.122
This commit is contained in:
@ -191,8 +191,13 @@ public class Aggregation
|
||||
internal static ReadOnlyDictionary<int, Aggregation> GetKeyValuePairs(Settings settings, Notification notification)
|
||||
{
|
||||
ReadOnlyDictionary<int, Aggregation> results;
|
||||
Dictionary<int, List<Notification>> keyValuePairs = new() { { int.Parse(notification.Id), new Notification[] { notification }.ToList() } };
|
||||
results = GetKeyValuePairs(settings, keyValuePairs);
|
||||
if (string.IsNullOrEmpty(notification.Id) || !int.TryParse(notification.Id, out int id))
|
||||
results = new(new Dictionary<int, Aggregation>());
|
||||
else
|
||||
{
|
||||
Dictionary<int, List<Notification>> keyValuePairs = new() { { id, new Notification[] { notification }.ToList() } };
|
||||
results = GetKeyValuePairs(settings, keyValuePairs);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
@ -19,23 +19,20 @@ 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;
|
||||
#pragma warning disable IDE0032, CS8618
|
||||
private static new ILog _Log;
|
||||
private static Settings _Settings;
|
||||
private static Dictionary<int, WorkItem> _WorkItems;
|
||||
#pragma warning restore IDE0032, CS8618
|
||||
internal static ILog? Log { get; private set; }
|
||||
internal static Settings? Settings { get; private set; }
|
||||
internal static Dictionary<int, WorkItem>? WorkItems { get; private set; }
|
||||
internal static Dictionary<string, Queue<KeyValuePair<string, WorkItem>>>? Queue { get; private set; }
|
||||
|
||||
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)
|
||||
{
|
||||
_WorkItems = new();
|
||||
Queue = new();
|
||||
WorkItems = new();
|
||||
_MinFileLength = 10;
|
||||
_Logistics = new(this);
|
||||
_NullData = string.Empty;
|
||||
_Log = LogManager.GetLogger(typeof(FileRead));
|
||||
Log = LogManager.GetLogger(typeof(FileRead));
|
||||
if (_FileParameter is null)
|
||||
throw new Exception(cellInstanceConnectionName);
|
||||
if (_ModelObjectParameterDefinitions is null)
|
||||
@ -45,7 +42,7 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
if (_IsEAFHosted)
|
||||
NestExistingFiles(_FileConnectorConfiguration);
|
||||
string parentDirectory = Path.GetDirectoryName(_FileConnectorConfiguration.TargetFileLocation) ?? throw new Exception();
|
||||
_Settings = new(digits: 5,
|
||||
Settings = new(digits: 5,
|
||||
parentDirectory: parentDirectory,
|
||||
priorities: 3,
|
||||
priorityGroups: 9,
|
||||
@ -151,10 +148,14 @@ public class FileRead : Shared.FileRead, IFileRead
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Settings is null)
|
||||
throw new NullReferenceException(nameof(Settings));
|
||||
if (WorkItems is null)
|
||||
throw new NullReferenceException(nameof(WorkItems));
|
||||
_Log.Info($"Enter-{nameof(WeightedShortestJobFirstModule.PopulatedWorkItemsAndGetJson)}");
|
||||
string? json = WeightedShortestJobFirstModule.PopulatedWorkItemsAndGetJson(_Settings);
|
||||
string? json = WeightedShortestJobFirstModule.PopulatedWorkItemsAndGetJson(Settings, WorkItems);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
WeightedShortestJobFirstModule.WriteJson(json);
|
||||
WeightedShortestJobFirstModule.WriteJson(Settings, json);
|
||||
_Log.Info($"End-{nameof(WeightedShortestJobFirstModule.PopulatedWorkItemsAndGetJson)}");
|
||||
}
|
||||
catch (Exception exception)
|
||||
|
@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Adaptation.FileHandlers.Priority;
|
||||
@ -9,11 +11,12 @@ public class Notification
|
||||
|
||||
[JsonConstructor]
|
||||
public Notification(int? fibonacci,
|
||||
string id,
|
||||
string? id,
|
||||
int? inverse,
|
||||
string? machineId,
|
||||
string page,
|
||||
string? site,
|
||||
string? sessionId,
|
||||
string time,
|
||||
string? username,
|
||||
string? value)
|
||||
@ -25,21 +28,69 @@ public class Notification
|
||||
MachineId = machineId;
|
||||
Page = page;
|
||||
Site = site is not null ? site : "MES";
|
||||
SessionId = sessionId;
|
||||
Time = time;
|
||||
Username = username;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
[JsonPropertyName("id")] public string 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("site")] public string? Site { get; }
|
||||
[JsonPropertyName("sessionId")] public string? SessionId { get; }
|
||||
[JsonPropertyName("time")] public string Time { get; }
|
||||
[JsonPropertyName("username")] public string? Username { get; }
|
||||
[JsonPropertyName("value")] public string? Value { get; }
|
||||
|
||||
internal static Notification Get(Dictionary<string, string?> keyValuePairs)
|
||||
{
|
||||
Notification results;
|
||||
string? id;
|
||||
string? fibonacci;
|
||||
string? inverse;
|
||||
string? machineId;
|
||||
string? page;
|
||||
string? site;
|
||||
string? sessionId;
|
||||
string? username;
|
||||
string? time;
|
||||
string? value;
|
||||
if (!keyValuePairs.TryGetValue(nameof(id), out id))
|
||||
id = null;
|
||||
if (!keyValuePairs.TryGetValue(nameof(fibonacci), out fibonacci))
|
||||
fibonacci = null;
|
||||
if (!keyValuePairs.TryGetValue(nameof(inverse), out inverse))
|
||||
inverse = null;
|
||||
if (!keyValuePairs.TryGetValue(nameof(machineId), out machineId))
|
||||
machineId = null;
|
||||
if (!keyValuePairs.TryGetValue(nameof(page), out page))
|
||||
throw new Exception();
|
||||
if (!keyValuePairs.TryGetValue(nameof(site), out site))
|
||||
site = null;
|
||||
if (!keyValuePairs.TryGetValue(nameof(sessionId), out sessionId))
|
||||
sessionId = null;
|
||||
if (!keyValuePairs.TryGetValue(nameof(username), out username))
|
||||
username = null;
|
||||
if (!keyValuePairs.TryGetValue(nameof(time), out time))
|
||||
throw new Exception();
|
||||
if (!keyValuePairs.TryGetValue(nameof(value), out value))
|
||||
value = null;
|
||||
results = new(fibonacci: fibonacci is null ? null : int.Parse(fibonacci),
|
||||
id: id,
|
||||
inverse: inverse is null ? null : int.Parse(inverse),
|
||||
machineId: machineId,
|
||||
page: page ?? throw new Exception(),
|
||||
site: site,
|
||||
sessionId: sessionId,
|
||||
time: time ?? throw new Exception(),
|
||||
username: username,
|
||||
value: value);
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static int? GetInverse(string? value) =>
|
||||
value switch
|
||||
{
|
||||
|
@ -21,9 +21,23 @@ public class WeightedShortestJobFirstModule : NancyModule
|
||||
{
|
||||
Get("/api/v1/ado/", _ =>
|
||||
{
|
||||
string json;
|
||||
ILog log = LogManager.GetLogger(typeof(WeightedShortestJobFirstModule));
|
||||
log.Info($"{nameof(Get)}");
|
||||
return "Hello";
|
||||
log.Info($"Enter-{nameof(GetKeyValuePairs)}");
|
||||
try
|
||||
{
|
||||
string query = Request.Url.Query;
|
||||
IDictionary<string, IEnumerable<string>> collection = Nancy.Helpers.HttpUtility.ParseQueryString(query).ToDictionary();
|
||||
KeyValuePair<string, WorkItem>? workItem = GetWorkItem(collection);
|
||||
json = workItem is null ? string.Empty : JsonSerializer.Serialize(workItem, KeyValuePairStringWorkItemSourceGenerationContext.Default.KeyValuePairStringWorkItem);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.Fatal($"Exception-{nameof(GetKeyValuePairs)}{Environment.NewLine}{ex.Message}{Environment.NewLine}{Environment.NewLine}{ex.StackTrace}");
|
||||
throw;
|
||||
}
|
||||
log.Info($"Return-{nameof(GetKeyValuePairs)}");
|
||||
return json;
|
||||
});
|
||||
base.Post("/api/v1/ado/", _ =>
|
||||
{
|
||||
@ -47,30 +61,107 @@ public class WeightedShortestJobFirstModule : NancyModule
|
||||
});
|
||||
}
|
||||
|
||||
private static Dictionary<string, string?> GetKeyValuePairs(IDictionary<string, IEnumerable<string>> collection)
|
||||
{
|
||||
Dictionary<string, string?> results = new();
|
||||
string[] array;
|
||||
foreach (KeyValuePair<string, IEnumerable<string>> keyValuePair in collection)
|
||||
{
|
||||
array = keyValuePair.Value.ToArray();
|
||||
if (array.Length != 1)
|
||||
continue;
|
||||
if (array.Length == 1 && array[0] == "null")
|
||||
results.Add(keyValuePair.Key, null);
|
||||
else
|
||||
results.Add(keyValuePair.Key, array[0]);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
internal static Notification GetNotification(string body, Dictionary<string, object> keyValuePairs)
|
||||
{
|
||||
Notification result;
|
||||
if (FileRead.Queue is null)
|
||||
throw new NullReferenceException(nameof(FileRead.Queue));
|
||||
if (FileRead.Settings is null)
|
||||
throw new NullReferenceException(nameof(FileRead.Settings));
|
||||
if (FileRead.WorkItems is null)
|
||||
throw new NullReferenceException(nameof(FileRead.WorkItems));
|
||||
string? json;
|
||||
Notification notification;
|
||||
if (!string.IsNullOrEmpty(body) && body[0] == '{')
|
||||
{
|
||||
File.WriteAllText(".json", body);
|
||||
notification = JsonSerializer.Deserialize(body, NotificationSourceGenerationContext.Default.Notification) ?? throw new NullReferenceException();
|
||||
result = 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();
|
||||
result = JsonSerializer.Deserialize(json, NotificationSourceGenerationContext.Default.Notification) ?? throw new NullReferenceException();
|
||||
}
|
||||
FileWriteAllText(FileRead.Settings, notification);
|
||||
json = PopulatedWorkItemsAndGetJson(FileRead.Settings);
|
||||
if (!string.IsNullOrEmpty(result.Id))
|
||||
FileWriteAllText(FileRead.Settings, result);
|
||||
json = PopulatedWorkItemsAndGetJson(FileRead.Settings, FileRead.WorkItems);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
WriteJson(json);
|
||||
return notification;
|
||||
WriteJson(FileRead.Settings, json);
|
||||
if (!string.IsNullOrEmpty(result.SessionId))
|
||||
{
|
||||
string key = GetKey(result);
|
||||
Queue<KeyValuePair<string, WorkItem>>? queue;
|
||||
lock (FileRead.Queue)
|
||||
{
|
||||
if (!FileRead.Queue.TryGetValue(key, out queue))
|
||||
{
|
||||
FileRead.Queue.Add(key, new());
|
||||
if (!FileRead.Queue.TryGetValue(key, out queue))
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
WorkItem? workItem = GetWorkItem(FileRead.WorkItems, result);
|
||||
if (workItem is not null)
|
||||
{
|
||||
lock (FileRead.Queue)
|
||||
{
|
||||
foreach (KeyValuePair<string, Queue<KeyValuePair<string, WorkItem>>> keyValuePair in FileRead.Queue)
|
||||
keyValuePair.Value.Enqueue(new(result.Page, workItem));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static KeyValuePair<string, WorkItem>? GetWorkItem(IDictionary<string, IEnumerable<string>> collection)
|
||||
{
|
||||
KeyValuePair<string, WorkItem>? result;
|
||||
Dictionary<string, string?> keyValuePairs = GetKeyValuePairs(collection);
|
||||
Notification notification = Notification.Get(keyValuePairs);
|
||||
if (FileRead.Queue is null)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
Queue<KeyValuePair<string, WorkItem>>? queue;
|
||||
string key = GetKey(notification);
|
||||
lock (FileRead.Queue)
|
||||
{
|
||||
if (!FileRead.Queue.TryGetValue(key, out queue))
|
||||
{
|
||||
FileRead.Queue.Add(key, new());
|
||||
if (!FileRead.Queue.TryGetValue(key, out queue))
|
||||
throw new Exception();
|
||||
}
|
||||
result = queue.Count == 0 ? null : queue.Dequeue();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string GetKey(Notification notification) =>
|
||||
$"{notification.SessionId}-{notification.MachineId}-{notification.Username}";
|
||||
|
||||
private static void FileWriteAllText(Settings settings, Notification notification)
|
||||
{
|
||||
if (string.IsNullOrEmpty(notification.Id))
|
||||
throw new NullReferenceException(nameof(notification.Id));
|
||||
string json = JsonSerializer.Serialize(notification, NotificationSourceGenerationContext.Default.Notification);
|
||||
string directory = Path.Combine(settings.SourceFileLocation, notification.Page, notification.Id.ToString());
|
||||
if (!Directory.Exists(directory))
|
||||
@ -79,14 +170,14 @@ public class WeightedShortestJobFirstModule : NancyModule
|
||||
File.WriteAllText(checkFile, json);
|
||||
}
|
||||
|
||||
internal static string? PopulatedWorkItemsAndGetJson(Settings settings)
|
||||
internal static string? PopulatedWorkItemsAndGetJson(Settings settings, Dictionary<int, WorkItem> workItems)
|
||||
{
|
||||
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();
|
||||
ReadOnlyDictionary<int, WorkItem?> keyValuePairs = WorkItem.GetKeyValuePairs(settings);
|
||||
int useCount = (from l in keyValuePairs 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
|
||||
WorkItem[] sorted = (from l in keyValuePairs
|
||||
where l.Value is not null
|
||||
orderby l.Value.Site is not null,
|
||||
l.Value.Site descending,
|
||||
@ -96,7 +187,7 @@ public class WeightedShortestJobFirstModule : NancyModule
|
||||
l.Value.BusinessValue?.FibonacciAverage descending,
|
||||
l.Key
|
||||
select l.Value).ToArray();
|
||||
lock (FileRead.WorkItems)
|
||||
lock (workItems)
|
||||
{
|
||||
int j = 0;
|
||||
WorkItem w;
|
||||
@ -105,8 +196,8 @@ public class WeightedShortestJobFirstModule : NancyModule
|
||||
int? sortBeforeId;
|
||||
WorkItem workItem;
|
||||
int? sortPriority;
|
||||
workItems.Clear();
|
||||
int? sortPriorityGroup;
|
||||
FileRead.WorkItems.Clear();
|
||||
for (int i = 0; i < sorted.Length; i++)
|
||||
{
|
||||
w = sorted[i];
|
||||
@ -130,18 +221,34 @@ public class WeightedShortestJobFirstModule : NancyModule
|
||||
sortPriorityGroup = settings.PriorityGroups;
|
||||
}
|
||||
workItem = WorkItem.GetWorkItem(w, i, sortBeforeId, sortPriority, sortPriorityGroup);
|
||||
FileRead.WorkItems.Add(workItem.Id, workItem);
|
||||
workItems.Add(workItem.Id, workItem);
|
||||
lastId = w.Id;
|
||||
}
|
||||
result = JsonSerializer.Serialize(FileRead.WorkItems, WorkItemDictionarySourceGenerationContext.Default.DictionaryInt32WorkItem);
|
||||
result = JsonSerializer.Serialize(workItems, WorkItemDictionarySourceGenerationContext.Default.DictionaryInt32WorkItem);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static void WriteJson(string json)
|
||||
private static WorkItem? GetWorkItem(Dictionary<int, WorkItem> workItems, Notification notification)
|
||||
{
|
||||
string jsonFile = Path.Combine(FileRead.Settings.ParentDirectory, "{}.json");
|
||||
string jsonFileWith = Path.Combine(FileRead.Settings.ParentDirectory, "{[]}.json");
|
||||
WorkItem? result;
|
||||
if (string.IsNullOrEmpty(notification.Id) || !int.TryParse(notification.Id, out int id))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
lock (workItems)
|
||||
{
|
||||
if (!workItems.TryGetValue(id, out result))
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static void WriteJson(Settings settings, string json)
|
||||
{
|
||||
string jsonFile = Path.Combine(settings.ParentDirectory, "{}.json");
|
||||
string jsonFileWith = Path.Combine(settings.ParentDirectory, "{[]}.json");
|
||||
string jsonOld = File.Exists(jsonFileWith) ? File.ReadAllText(jsonFileWith) : string.Empty;
|
||||
if (json != jsonOld)
|
||||
{
|
||||
|
@ -202,4 +202,10 @@ public class WorkItem
|
||||
[JsonSerializable(typeof(Dictionary<int, WorkItem>))]
|
||||
internal partial class WorkItemDictionarySourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(KeyValuePair<string, WorkItem>))]
|
||||
internal partial class KeyValuePairStringWorkItemSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
Reference in New Issue
Block a user