From b031dd0bec3b6937cbfaf80b68b937ea0a7c3d55 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 19 Apr 2023 08:26:17 +0200 Subject: [PATCH] Estensione metodi di lettura QUEUE --- .../Controllers/QueueController.cs | 66 +++++++++++++++---- .../Services/QueueDataService.cs | 52 ++++++++++++++- 2 files changed, 102 insertions(+), 16 deletions(-) diff --git a/WebDoorCreator.API/Controllers/QueueController.cs b/WebDoorCreator.API/Controllers/QueueController.cs index d61abc7..66901a9 100644 --- a/WebDoorCreator.API/Controllers/QueueController.cs +++ b/WebDoorCreator.API/Controllers/QueueController.cs @@ -14,7 +14,7 @@ namespace WebDoorCreator.API.Controllers { Log.Info("Starting QueueController"); _configuration = configuration; - DService = DataService; + QDService = DataService; Log.Info("Avviato QueueController"); } @@ -22,26 +22,64 @@ namespace WebDoorCreator.API.Controllers #region Public Methods - [HttpGet("GetQueueLenght")] - public int GetQueueLenght() + /// + /// Lunghezza coda in attesa + /// + /// + [HttpGet("ActPendingLenght")] + public async Task ActPendingLenght() { - return rndGen.Next(0, 20); + long numQueue = await QDService.NumRequestPending(); + return numQueue; + } + /// + /// Lunghezza coda in fase di processing + /// + /// + [HttpGet("ActProcessingLenght")] + public async Task ActProcessingLenght() + { + long numQueue = await QDService.NumRequestProcessing(); + return numQueue; } - [HttpGet("GetQueueList")] - public IEnumerable GetQueueList() + /// + /// Elenco richieste in stato pending + /// + /// + [HttpGet("ShowPending")] + public async Task?> ShowPending() { - return Enumerable.Range(1, rndGen.Next(5, 20)).Select(index => $"WDC{rndGen.Next(0, 2000):000000}").ToArray(); + var actQueue = await QDService.RequestPending(); + return actQueue;// != null && actQueue.Count > 0 ? actQueue : null; + } + + /// + /// Elenco richeiste in stato processing + /// + /// + [HttpGet("ShowProcessing")] + public async Task> ShowProcessing() + { + var actQueue = await QDService.RequestProcessing(); + return actQueue; + } + + /// + /// Chiede un numero massimo di items dalla coda NB: + /// - verranno tolti dalla coda FIFO richieste + /// - verranno messi nella coda FIFO processing + /// + /// + [HttpGet("TakeProcessingItems")] + public async Task> TakeProcessingItems(int numItems) + { + var actQueue = await QDService.RequestPending(); + return actQueue; } #endregion Public Methods - #region Protected Fields - - protected Random rndGen = new Random(); - - #endregion Protected Fields - #region Private Fields private static IConfiguration _configuration = null!; @@ -52,7 +90,7 @@ namespace WebDoorCreator.API.Controllers #region Private Properties - private QueueDataService DService { get; set; } = null!; + private QueueDataService QDService { get; set; } = null!; #endregion Private Properties } diff --git a/WebDoorCreator.Data/Services/QueueDataService.cs b/WebDoorCreator.Data/Services/QueueDataService.cs index 3ce28ea..530973d 100644 --- a/WebDoorCreator.Data/Services/QueueDataService.cs +++ b/WebDoorCreator.Data/Services/QueueDataService.cs @@ -128,8 +128,29 @@ namespace WebDoorCreator.API.Data return numReq; } - /// Get calculation request pending as Dictionary --> (doorId, - /// revnumb) + /// + /// Get # of calculation request processing + /// + public async Task NumRequestProcessing() + { + long numReq = 0; + string source = "REDIS"; + Dictionary dictResult = new Dictionary(); + // cerco da cache + RedisKey currKey = new RedisKey(Constants.CALC_REQ_PROC); + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + numReq = await redisDb.HashLengthAsync(currKey); + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Debug($"NumRequestProcessing | {source} in: {ts.TotalMilliseconds} ms"); + return numReq; + } + + /// + /// Get Queue request pending + /// + /// Dictionary of DoorId, saveVersNumb public async Task> RequestPending() { string source = "REDIS"; @@ -153,6 +174,33 @@ namespace WebDoorCreator.API.Data Log.Debug($"RequestPending | {source} in: {ts.TotalMilliseconds} ms"); return dictResult; } + /// + /// Get Queue request processing + /// + /// Dictionary of DoorId, saveVersNumb + public async Task> RequestProcessing() + { + string source = "REDIS"; + long numReq = 0; + Dictionary dictResult = new Dictionary(); + // cerco da cache + RedisKey currKey = new RedisKey(Constants.CALC_REQ_PROC); + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + numReq = redisDb.HashLength(currKey); + if (numReq > 0) + { + var rawData = await redisDb.HashGetAllAsync(currKey); + foreach (var item in rawData) + { + dictResult.Add($"{item.Name}", $"{item.Value}"); + } + } + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Debug($"RequestProcessing | {source} in: {ts.TotalMilliseconds} ms"); + return dictResult; + } #endregion Public Methods