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 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 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 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 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); } } }