From 33a43ce370088e0609ebcf12eea6def13ab47527 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 18 Apr 2023 20:58:06 +0200 Subject: [PATCH] Iniziati metodi x recuperare da redis le richieste --- .../Controllers/QueueController.cs | 14 +++-- .../Controllers/WeatherForecastController.cs | 33 ------------ WebDoorCreator.API/Program.cs | 2 +- WebDoorCreator.API/WeatherForecast.cs | 13 ----- WebDoorCreator.Core/Constants.cs | 7 ++- .../Services/QueueDataService.cs | 52 +++++++++++++++++-- .../WebDoorCreator.Data.csproj | 1 + 7 files changed, 65 insertions(+), 57 deletions(-) delete mode 100644 WebDoorCreator.API/Controllers/WeatherForecastController.cs delete mode 100644 WebDoorCreator.API/WeatherForecast.cs rename WebDoorCreator.API/Data/ApiDataService.cs => WebDoorCreator.Data/Services/QueueDataService.cs (78%) diff --git a/WebDoorCreator.API/Controllers/QueueController.cs b/WebDoorCreator.API/Controllers/QueueController.cs index 8ced669..d61abc7 100644 --- a/WebDoorCreator.API/Controllers/QueueController.cs +++ b/WebDoorCreator.API/Controllers/QueueController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using NLog; +using WebDoorCreator.API.Data; namespace WebDoorCreator.API.Controllers { @@ -9,14 +10,11 @@ namespace WebDoorCreator.API.Controllers { #region Public Constructors - // da inizializzare con redis x recuperare elenco delle VERE richieste... - - //public QueueController(IConfiguration configuration, MpDataService DataService) - public QueueController(IConfiguration configuration) + public QueueController(IConfiguration configuration, QueueDataService DataService) { Log.Info("Starting QueueController"); _configuration = configuration; - //DService = DataService; + DService = DataService; Log.Info("Avviato QueueController"); } @@ -51,5 +49,11 @@ namespace WebDoorCreator.API.Controllers private static Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields + + #region Private Properties + + private QueueDataService DService { get; set; } = null!; + + #endregion Private Properties } } \ No newline at end of file diff --git a/WebDoorCreator.API/Controllers/WeatherForecastController.cs b/WebDoorCreator.API/Controllers/WeatherForecastController.cs deleted file mode 100644 index 5c59ac3..0000000 --- a/WebDoorCreator.API/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace WebDoorCreator.API.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} \ No newline at end of file diff --git a/WebDoorCreator.API/Program.cs b/WebDoorCreator.API/Program.cs index 56cf12e..e81b02e 100644 --- a/WebDoorCreator.API/Program.cs +++ b/WebDoorCreator.API/Program.cs @@ -56,7 +56,7 @@ builder.Services.AddStackExchangeRedisExtensions((options) //return configuration.GetSection("Redis").Get(); }); -builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(redisMultiplexer); var app = builder.Build(); diff --git a/WebDoorCreator.API/WeatherForecast.cs b/WebDoorCreator.API/WeatherForecast.cs deleted file mode 100644 index 4d36f7c..0000000 --- a/WebDoorCreator.API/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace WebDoorCreator.API -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file diff --git a/WebDoorCreator.Core/Constants.cs b/WebDoorCreator.Core/Constants.cs index b684365..1ef2c8f 100644 --- a/WebDoorCreator.Core/Constants.cs +++ b/WebDoorCreator.Core/Constants.cs @@ -16,11 +16,14 @@ namespace WebDoorCreator.Core public static readonly string BASE_PATH = Directory.GetCurrentDirectory(); - // REDIS KEY Dati correnti + // REDIS KEY Dati correnti x QueueMan public static readonly string LAST_CALC_REQ_KEY = $"{BASE_HASH}:Current:LastCalcReq"; public static readonly string LAST_CALC_DONE_KEY = $"{BASE_HASH}:Current:LastCalcDone"; + public static readonly string CALC_REQ_PEND = $"{BASE_HASH}:CalcRequests:Pending"; + public static readonly string CALC_REQ_PROC = $"{BASE_HASH}:CalcRequests:Processing"; + public static readonly string CALC_REQ_DONE = $"{BASE_HASH}:CalcRequests:Completed"; - // REDIS Channels messaggi (verso UI) + // REDIS Channels messaggi x QueueMan (verso UI/srv) public static readonly string CALC_REQ_QUEUE = $"CalcRequest"; public static readonly string CALC_DONE_QUEUE = $"CalcCompleted"; diff --git a/WebDoorCreator.API/Data/ApiDataService.cs b/WebDoorCreator.Data/Services/QueueDataService.cs similarity index 78% rename from WebDoorCreator.API/Data/ApiDataService.cs rename to WebDoorCreator.Data/Services/QueueDataService.cs index 8bcc5a9..3ce28ea 100644 --- a/WebDoorCreator.API/Data/ApiDataService.cs +++ b/WebDoorCreator.Data/Services/QueueDataService.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Identity.UI.Services; +using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using NLog; using StackExchange.Redis; @@ -8,7 +9,7 @@ using WebDoorCreator.Data.DbModels; namespace WebDoorCreator.API.Data { - public class ApiDataService : IDisposable + public class QueueDataService : IDisposable { #region Public Fields @@ -28,11 +29,11 @@ namespace WebDoorCreator.API.Data /// /// /// - public ApiDataService(IConfiguration configuration, IEmailSender emailSender, IConnectionMultiplexer redisConn) + public QueueDataService(IConfiguration configuration, IEmailSender emailSender, IConnectionMultiplexer redisConn) { _configuration = configuration; _emailSender = emailSender; - // setup compoenti REDIS + // setup componenti REDIS var redConnString = _configuration.GetConnectionString("Redis"); if (redConnString == null) { @@ -108,6 +109,51 @@ namespace WebDoorCreator.API.Data return dbResult; } + /// + /// Get # of calculation request pending + /// + public async Task NumRequestPending() + { + long numReq = 0; + string source = "REDIS"; + Dictionary dictResult = new Dictionary(); + // cerco da cache + RedisKey currKey = new RedisKey(Constants.CALC_REQ_PEND); + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + numReq = await redisDb.HashLengthAsync(currKey); + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Debug($"NumRequestPending | {source} in: {ts.TotalMilliseconds} ms"); + return numReq; + } + + /// Get calculation request pending as Dictionary --> (doorId, + /// revnumb) + public async Task> RequestPending() + { + string source = "REDIS"; + long numReq = 0; + Dictionary dictResult = new Dictionary(); + // cerco da cache + RedisKey currKey = new RedisKey(Constants.CALC_REQ_PEND); + 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($"RequestPending | {source} in: {ts.TotalMilliseconds} ms"); + return dictResult; + } + #endregion Public Methods #region Protected Fields diff --git a/WebDoorCreator.Data/WebDoorCreator.Data.csproj b/WebDoorCreator.Data/WebDoorCreator.Data.csproj index 6cc42f6..c352183 100644 --- a/WebDoorCreator.Data/WebDoorCreator.Data.csproj +++ b/WebDoorCreator.Data/WebDoorCreator.Data.csproj @@ -23,6 +23,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive +