Cost of Delay 1.122
This commit is contained in:
@ -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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user