From da3be714d2e46f69e5dd385197bba8bd6d1d9735 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 25 Jan 2022 17:28:08 +0100 Subject: [PATCH] Update API x email e lettura dati proj --- GPW.CORE.Api/Controllers/CheckProj.cs | 21 +++- GPW.CORE.Api/Data/ApiDataService.cs | 136 ++++++++++++++++++++++++- GPW.CORE.Api/Data/IEmailSender.cs | 6 -- GPW.CORE.Api/Data/IRedisCacheClient.cs | 6 -- GPW.CORE.Api/Program.cs | 40 +++++++- GPW.CORE.Api/appsettings.json | 14 +++ GPW.CORE.UI/Data/GpwDataService.cs | 2 +- GPW.CORE.UI/GPW.CORE.UI.csproj | 2 +- Resources/ChangeLog.html | 2 +- Resources/VersNum.txt | 2 +- Resources/manifest.xml | 2 +- 11 files changed, 208 insertions(+), 25 deletions(-) delete mode 100644 GPW.CORE.Api/Data/IEmailSender.cs delete mode 100644 GPW.CORE.Api/Data/IRedisCacheClient.cs diff --git a/GPW.CORE.Api/Controllers/CheckProj.cs b/GPW.CORE.Api/Controllers/CheckProj.cs index 745563c..6ae55cb 100644 --- a/GPW.CORE.Api/Controllers/CheckProj.cs +++ b/GPW.CORE.Api/Controllers/CheckProj.cs @@ -1,5 +1,7 @@ -using Microsoft.AspNetCore.Http; +using GPW.CORE.Api.Data; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using System.Text; namespace GPW.CORE.Api.Controllers { @@ -8,21 +10,32 @@ namespace GPW.CORE.Api.Controllers public class CheckProj : ControllerBase { + /// + /// Dataservice x accesso DB + /// + protected ApiDataService dataService { get; set; } + private readonly ILogger _logger; - public CheckProj(ILogger logger) + public CheckProj(ILogger logger, ApiDataService DataService) { _logger = logger; + dataService = DataService; + _logger.LogInformation("Avviata classe CheckProj"); } [HttpGet("TryLock")] public async Task Get() { string answ = "ND"; - // provo ad eseguire... + StringBuilder sb = new StringBuilder(); + + // recupero progetti attivi... + var currProj = await dataService.AnagProjActiv(); + sb.Append($"Found {currProj.Count} Active Projects"); - await Task.Delay(100); + answ = sb.ToString(); return answ; } } diff --git a/GPW.CORE.Api/Data/ApiDataService.cs b/GPW.CORE.Api/Data/ApiDataService.cs index 858e072..ec341d7 100644 --- a/GPW.CORE.Api/Data/ApiDataService.cs +++ b/GPW.CORE.Api/Data/ApiDataService.cs @@ -1,9 +1,16 @@ -using NLog; +using GPW.CORE.Data.DbModels; +using Newtonsoft.Json; +using NLog; +using Microsoft.AspNetCore.Identity.UI.Services; +using StackExchange.Redis.Extensions.Core.Abstractions; +using System.Diagnostics; namespace GPW.CORE.Api.Data { public class ApiDataService { + #region Private Fields + private static IConfiguration _configuration; private static ILogger _logger; @@ -14,17 +21,36 @@ namespace GPW.CORE.Api.Data //private readonly IDistributedCache distributedCache; private readonly IRedisCacheClient _redisCacheClient; + + /// + /// Elenco obj in cache + /// + private List cachedDataList = new List(); + + #endregion Private Fields + + #region Protected Fields + + protected const string rKeyProjAct = "Cache:ProjAct"; + /// /// TTL da 1 min x cache Redis /// protected const int shortTTL = 60 * 5; + #endregion Protected Fields + + #region Public Fields /// /// Classe Accesso metodi DB /// public static CORE.Data.Controllers.GPWController dbController; + #endregion Public Fields + + #region Public Constructors + /// /// Init classe /// @@ -39,7 +65,6 @@ namespace GPW.CORE.Api.Data _configuration = configuration; _emailSender = emailSender; _redisCacheClient = redisCacheClient; - //this.distributedCache = distributedCache; // conf DB string connStrDB = _configuration.GetConnectionString("GPW.DB"); @@ -53,5 +78,110 @@ namespace GPW.CORE.Api.Data _logger.LogInformation("DbController OK"); } } + + #endregion Public Constructors + + #region Protected Methods + + /// + /// Recupero chiave da redis + /// + /// + /// + protected async Task getRSV(string rKey) + { + string answ = await _redisCacheClient.GetDbFromConfiguration().GetAsync(rKey); + return answ; + } + + /// + /// Salvataggio chiave in redis + /// + /// + /// + /// + /// + protected async Task setRSV(string rKey, string rVal, int ttlSec) + { + bool fatto = false; + await _redisCacheClient.GetDbFromConfiguration().AddAsync(rKey, rVal, DateTimeOffset.Now.AddSeconds(ttlSec)); + fatto = true; + return fatto; + } + + /// + /// Salvataggio chiave in redis + /// + /// + /// + /// + /// + protected async Task setRSV(string rKey, int rValInt, int ttlSec) + { + bool fatto = false; + await _redisCacheClient.GetDbFromConfiguration().AddAsync(rKey, rValInt, DateTimeOffset.Now.AddSeconds(ttlSec)); + fatto = true; + return fatto; + } + + /// + /// Registra in cache chiave se non fosse già in elenco + /// + /// + protected void trackCache(string newKey) + { + if (!cachedDataList.Contains(newKey)) + { + cachedDataList.Add(newKey); + } + } + + #endregion Protected Methods + + #region Public Methods + + /// + /// Recupera l'elenco progetti (tutti) + /// + /// + public async Task> AnagProjActiv() + { + List dbResult = new List(); + string cacheKey = $"{rKeyProjAct}"; + trackCache(cacheKey); + string rawData = await getRSV(cacheKey); + if (!string.IsNullOrEmpty(rawData)) + { + dbResult = JsonConvert.DeserializeObject>(rawData); + } + else + { + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + dbResult = dbController.AnagProjAll(true); + rawData = JsonConvert.SerializeObject(dbResult, Formatting.None, new JsonSerializerSettings(){ ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); + await setRSV(cacheKey, rawData, shortTTL); + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Trace($"Effettuata lettura da DB per AttivazioniByLic: {ts.TotalMilliseconds} ms"); + } + + return await Task.FromResult(dbResult); + } + + /// + /// invalida tutta la cache in caso di update + /// + /// + public async Task InvalidateAllCache() + { + foreach (var item in cachedDataList) + { + await _redisCacheClient.GetDbFromConfiguration().RemoveAsync(item); + } + cachedDataList = new List(); + } + + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/GPW.CORE.Api/Data/IEmailSender.cs b/GPW.CORE.Api/Data/IEmailSender.cs deleted file mode 100644 index 079743c..0000000 --- a/GPW.CORE.Api/Data/IEmailSender.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GPW.CORE.Api.Data -{ - public interface IEmailSender - { - } -} \ No newline at end of file diff --git a/GPW.CORE.Api/Data/IRedisCacheClient.cs b/GPW.CORE.Api/Data/IRedisCacheClient.cs deleted file mode 100644 index 7a9068b..0000000 --- a/GPW.CORE.Api/Data/IRedisCacheClient.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GPW.CORE.Api.Data -{ - public interface IRedisCacheClient - { - } -} \ No newline at end of file diff --git a/GPW.CORE.Api/Program.cs b/GPW.CORE.Api/Program.cs index 48863a6..ca211ce 100644 --- a/GPW.CORE.Api/Program.cs +++ b/GPW.CORE.Api/Program.cs @@ -1,12 +1,50 @@ +using GPW.CORE.Api.Data; +using GPW.CORE.Data; +using Microsoft.AspNetCore.Identity.UI.Services; +using StackExchange.Redis.Extensions.Core.Configuration; +using StackExchange.Redis.Extensions.Newtonsoft; +using System.Text.Json.Serialization; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. +// configuration setup +ConfigurationManager configuration = builder.Configuration; +// Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +// abilitazione x email management con MailKit +builder.Services.AddTransient(); +builder.Services.Configure(options => +{ + options.Host_Address = configuration["ExternalProviders:MailKit:SMTP:Address"]; + options.Host_Port = Convert.ToInt32(configuration["ExternalProviders:MailKit:SMTP:Port"]); + options.Host_Username = configuration["ExternalProviders:MailKit:SMTP:Account"]; + options.Host_Password = configuration["ExternalProviders:MailKit:SMTP:Password"]; + options.Sender_EMail = configuration["ExternalProviders:MailKit:SMTP:SenderEmail"]; + options.Sender_Name = configuration["ExternalProviders:MailKit:SMTP:SenderName"]; +}); + +//services.AddStackExchangeRedisCache(options => +//{ +// options.Configuration = Configuration.GetConnectionString("Redis"); +//}); + +builder.Services.AddControllers() + .AddJsonOptions(c => c.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve); + +builder.Services.AddStackExchangeRedisExtensions((options) => +{ + return configuration.GetSection("Redis").Get(); +}); + +builder.Services.AddSingleton(); + + + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/GPW.CORE.Api/appsettings.json b/GPW.CORE.Api/appsettings.json index adbeb82..678dbcd 100644 --- a/GPW.CORE.Api/appsettings.json +++ b/GPW.CORE.Api/appsettings.json @@ -25,5 +25,19 @@ "MailDest": { "Admin": "samuele@steamware.net", "ProcOp": "ceo@steamware.net" + }, + "Redis": { + "Password": "", + "AllowAdmin": false, + "Ssl": false, + "ConnectTimeout": 6000, + "ConnectRetry": 2, + "Hosts": [ + { + "Host": "localhost", + "Port": "6379" + } + ], + "Database": 15 } } diff --git a/GPW.CORE.UI/Data/GpwDataService.cs b/GPW.CORE.UI/Data/GpwDataService.cs index 23ec646..4fc67c9 100644 --- a/GPW.CORE.UI/Data/GpwDataService.cs +++ b/GPW.CORE.UI/Data/GpwDataService.cs @@ -319,7 +319,7 @@ namespace GPW.CORE.UI.Data { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); - dbResult = dbController.AnagProjAll(); + dbResult = dbController.AnagProjAll(false); rawData = JsonConvert.SerializeObject(dbResult, JSSettings); redisDataList = Encoding.UTF8.GetBytes(rawData); await distributedCache.SetAsync(currKey, redisDataList, cacheOpt(true)); diff --git a/GPW.CORE.UI/GPW.CORE.UI.csproj b/GPW.CORE.UI/GPW.CORE.UI.csproj index f63ccae..851b507 100644 --- a/GPW.CORE.UI/GPW.CORE.UI.csproj +++ b/GPW.CORE.UI/GPW.CORE.UI.csproj @@ -2,7 +2,7 @@ net6.0 - 3.0.2201.2514 + 3.0.2201.2517 enable enable diff --git a/Resources/ChangeLog.html b/Resources/ChangeLog.html index 176ff34..8a42b81 100644 --- a/Resources/ChangeLog.html +++ b/Resources/ChangeLog.html @@ -1,6 +1,6 @@ GPW - Gestione Presenze Web -

Versione: 3.0.2201.2514

+

Versione: 3.0.2201.2517


Note di rilascio:
  • diff --git a/Resources/VersNum.txt b/Resources/VersNum.txt index 3650f65..704b23c 100644 --- a/Resources/VersNum.txt +++ b/Resources/VersNum.txt @@ -1 +1 @@ -3.0.2201.2514 +3.0.2201.2517 diff --git a/Resources/manifest.xml b/Resources/manifest.xml index 52db8c5..a1735dc 100644 --- a/Resources/manifest.xml +++ b/Resources/manifest.xml @@ -1,6 +1,6 @@ - 3.0.2201.2514 + 3.0.2201.2517 http://nexus.steamware.net/repository/SWS/GWMS/stable/0/GWMS.UI.zip http://nexus.steamware.net/repository/SWS/GWMS/stable/0/ChangeLog.html false