Files
limanapp/LiMan.DB/Services/CommonDataServices.cs
2025-12-03 17:44:05 +01:00

2607 lines
99 KiB
C#

using Core;
using Core.DTO;
using LiMan.DB.DBModels;
using LiMan.DB.DTO;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NLog;
using Org.BouncyCastle.Asn1.Pkcs;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
using static Core.Enum;
namespace LiMan.DB.Services
{
/// <summary>
/// Classe common x gestione servizi sui dati (DB + REDIS) x UI ed API
/// </summary>
public class CommonDataServices : IDisposable
{
#region Public Constructors
/// <summary>
/// Costruttore principale
/// </summary>
/// <param name="configuration"></param>
/// <param name="memoryCache"></param>
/// <param name="distributedCache"></param>
/// <param name="redisConnMult"></param>
/// <param name="emailSender"></param>
public CommonDataServices(IConfiguration configuration, IConnectionMultiplexer redisConnMult, IEmailSender emailSender)
{
_configuration = configuration;
// Conf cache
redisConn = redisConnMult;
redisDb = this.redisConn.GetDatabase();
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
JSSettings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
// conf messagepipe: setup canali pub/sub
EnrollMessPipe = new MessagePipe(redisConn, Const.ENRL_MSG_PIPE);
UpdActMessPipe = new MessagePipe(redisConn, Const.TASK_MSG_PIPE);
TaskMessPipe = new MessagePipe(redisConn, Const.UPDT_MSG_PIPE);
_emailSender = emailSender;
// conf DB
string connStrDB = _configuration.GetConnectionString("LiMan.DB");
if (string.IsNullOrEmpty(connStrDB))
{
Log.Error("Empty ConnString for LiMan.DB!");
}
else
{
dbController = new LiMan.DB.Controllers.DbController(configuration);
Log.Info("DbController for LiMan.DB OK");
}
}
#endregion Public Constructors
#region Public Properties
/// <summary>
/// Wrapper x invio/ricezione messaggi sul canale dedicato agli eventi enroll
/// </summary>
public MessagePipe EnrollMessPipe { get; set; } = null!;
/// <summary>
/// Wrapper x invio/ricezione messaggi sul canale dedicato a generico update info dai vari EgwACC
/// </summary>
public MessagePipe UpdActMessPipe { get; set; } = null!;
/// <summary>
/// Wrapper x invio/ricezione messaggi sul canale dedicato agli eventi Task
/// </summary>
public MessagePipe TaskMessPipe { get; set; } = null!;
#endregion Public Properties
#region Public Methods
/// <summary>
/// Elenco licenze dato cliente
/// </summary>
/// <param name="CodInst">Codice Installaizone /Cliente</param>
/// <param name="CodApp">Codice Applicazione</param>
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
/// <returns></returns>
public async Task<List<ApplicativoDTO>> AppDtoSearch(string CodInst, string CodApp, bool HideData)
{
List<ApplicativoDTO> dbResult = new List<ApplicativoDTO>();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.GetApplicativiFilt(true, CodApp, CodInst, HideData);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per ApplicativiByCliente: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
public async Task<bool> ApplicHasChild(string CodApp)
{
string source = "DB";
bool dbResult = false;
try
{
string currKey = $"{Const.rKeyConfig}:Next:Applicazioni:HasChild";
Stopwatch sw = new Stopwatch();
sw.Start();
string rawData = redisHashKeyGet(currKey, CodApp);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
dbResult = JsonConvert.DeserializeObject<bool>(rawData);
}
else
{
dbResult = dbController.ApplicazioniHasChild(CodApp);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
redisHashKeySet(currKey, CodApp, rawData, UltraLongCache.Minutes);
}
sw.Stop();
Log.Debug($"ApplicHasChild | {source} in: {sw.Elapsed.TotalMilliseconds:N2} ms");
}
catch (Exception exc)
{
Log.Error($"Error during ApplicHasChild:{Environment.NewLine}{exc}");
}
await Task.Delay(0);
return dbResult;
}
/// <summary>
/// Elimina il record indicato (se non ci sono record correlati...)
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
public async Task<bool> ApplicNextDelete(ApplicativoModel currItem)
{
bool done = false;
try
{
// controllo NON ci siano record figli...
bool hasCHild = dbController.ApplicazioniHasChild(currItem.CodApp);
if (!hasCHild)
{
done = dbController.ApplicazioniNextDelete(currItem);
await FlushRedisCache();
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in ApplicNextDelete:{Environment.NewLine}{exc}");
}
return done;
}
public async Task<List<ApplicativoModel>> ApplicNextGetAll(bool forceDb)
{
string source = "DB";
List<ApplicativoModel> dbResult = new List<ApplicativoModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Next:Applicazioni:List";
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData) && !forceDb)
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<ApplicativoModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<ApplicativoModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.GetApplicazioni();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new List<ApplicativoModel>();
}
sw.Stop();
Log.Debug($"ApplicNextGetAll | {source} in: {sw.Elapsed.TotalMilliseconds:N2} ms");
}
catch (Exception exc)
{
Log.Error($"Error during ApplicNextGetAll:{Environment.NewLine}{exc}");
}
return dbResult;
}
public async Task<bool> ApplicNextUpdate(ApplicativoModel currItem)
{
bool done = false;
try
{
done = dbController.ApplicazioniNextUpdate(currItem);
await FlushRedisCache();
}
catch (Exception exc)
{
Log.Error($"Eccezione in ApplicNextUpdate:{Environment.NewLine}{exc}");
}
return done;
}
/// <summary>
/// Elimina record attivazione
/// </summary>
/// <param name="idxSubLic"></param>
/// <returns></returns>
public async Task<bool> AttivazioneDelete(int idxSubLic)
{
bool fatto = dbController.AttivazioniDelete(idxSubLic);
await Task.Delay(1);
return fatto;
}
/// <summary>
/// Elenco licenze dato cliente
/// </summary>
/// <param name="Chiave">Licenza MASTER</param>
/// <param name="CodImpiego">Codice Impiego licenza</param>
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
/// <returns></returns>
public async Task<AttivazioneDTO> AttivazioneSearch(string Chiave, string CodImpiego, bool HideData)
{
AttivazioneDTO dbResult = new AttivazioneDTO();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.GetAttivazione(Chiave, CodImpiego, HideData);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per AttivazioniSearch: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Effettua sblocco di una licenza impostando data veto a oggi
/// </summary>
/// <param name="idxSubLic"></param>
/// <returns></returns>
public async Task<bool> AttivazioneUnlock(int idxSubLic)
{
bool fatto = dbController.AttivazioniUnlock(idxSubLic);
await Task.Delay(1);
return fatto;
}
/// <summary>
/// Elenco Attivaizoni da ID Licenza master
/// </summary>
/// <param name="idxLic">Idx Licenza Master</param>
/// <param name="hideData">Indica se nascondere i dati sensibili</param>
/// <returns></returns>
public async Task<List<AttivazioneDTO>> AttivazioniByLic(int idxLic, bool hideData)
{
List<AttivazioneDTO> dbResult = new List<AttivazioneDTO>();
string cacheKey = $"{rKeyAttivByLic}:{hideData}:{idxLic}";
trackCache(cacheKey);
string rawData = await getRSV(cacheKey);
if (!string.IsNullOrEmpty(rawData))
{
dbResult = JsonConvert.DeserializeObject<List<AttivazioneDTO>>(rawData);
}
else
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.GetAttivazioniByLic(idxLic, hideData);
rawData = JsonConvert.SerializeObject(dbResult);
await setRSV(cacheKey, rawData, redCacheTtlStd);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per AttivazioniByLic: {ts.TotalMilliseconds} ms");
}
return await Task.FromResult(dbResult);
}
/// <summary>
/// Elenco Attivaizoni da valore Licenza master
/// </summary>
/// <param name="MasterKey">Licenza Master</param>
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
/// <returns></returns>
public async Task<List<AttivazioneDTO>> AttivazioniByMasterKey(string MasterKey, bool HideData)
{
List<AttivazioneDTO> dbResult = new List<AttivazioneDTO>();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
LicenzaModel licenza = await LicenzaByMasterKey(MasterKey);
if (licenza != null)
{
dbResult = await AttivazioniByLic(licenza.IdxLic, HideData);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per AttivazioniByMasterKey: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Elimina un Attivaizone
/// </summary>
/// <param name="MasterKey">Licenza Master</param>
/// <param name="ParamDict">Elenco delle attivazioni da eliminare</param>
/// <returns></returns>
public async Task<bool> AttivazioniDelete(string MasterKey, Dictionary<string, string> ParamDict)
{
bool answ = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
LicenzaModel licenza = await LicenzaByMasterKey(MasterKey);
if (licenza != null)
{
answ = dbController.AttivazioniDelete(ParamDict, MasterKey);
await FlushRedisCache();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per AttivazioniDelete: {ts.TotalMilliseconds} ms");
return await Task.FromResult(answ);
}
/// <summary>
/// Lista attivazioni dati cod identificativi CodImp e AppKey
/// </summary>
/// <param name="AppKey"></param>
/// <param name="CodImp"></param>
/// <returns></returns>
public async Task<List<SubLicenzaModel>> AttivazioniGetAppImp(string AppKey, string CodImp)
{
Stopwatch stopWatch = new Stopwatch();
List<SubLicenzaModel> dbResult = new List<SubLicenzaModel>();
stopWatch.Start();
dbResult = dbController.AttivazioniGetAppImp(AppKey, CodImp);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per AttivazioniGetAppImp: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Recupera attivazioni data licenza
/// </summary>
/// <param name="CurrFilter"></param>
/// <returns></returns>
public async Task<List<SubLicenzaModel>> AttivazioniGetByLic(int IdxLic)
{
Stopwatch sw = new Stopwatch();
List<SubLicenzaModel> dbResult = new List<SubLicenzaModel>();
sw.Start();
dbResult = dbController.AttivazioniGetByLic(IdxLic);
sw.Stop();
Log.Trace($"Effettuata lettura da DB per AttivazioniGetByLic: {sw.Elapsed.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Elimina attivaizoni con veto scaduto
/// </summary>
/// <param name="MasterKey">Licenza Master</param>
/// <returns></returns>
public async Task<bool> AttivazioniResetAvail(string MasterKey)
{
bool answ = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
LicenzaModel licenza = await LicenzaByMasterKey(MasterKey);
if (licenza != null)
{
answ = dbController.AttivazioniResetAvail(MasterKey);
await FlushRedisCache();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per AttivazioniResetAvail: {ts.TotalMilliseconds} ms");
return await Task.FromResult(answ);
}
/// <summary>
/// Effettua registrazione (se possibile) delle licenze indicate dall'elenco codici di
/// impiego indicati
/// </summary>
/// <param name="MasterKey">Codice Licenza Master</param>
/// <param name="ParamDict">Elenco codici impiego (key) + valori in formato dizionari</param>
/// <param name="DayVeto">Numero giorni x scadenza veto modifica</param>
/// <param name="TipoLic">Tipo di licenza da registrare</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<bool> AttivazioniTryAdd(string MasterKey, Dictionary<string, string> ParamDict, int DayVeto, TipoLicenza TipoLic)
{
bool taskDone = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
taskDone = dbController.AttivazioniTryAdd(MasterKey, ParamDict, DayVeto, TipoLic);
await FlushRedisCache();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata scrittura + rilettura da DB per AttivazioniTryAdd: {ts.TotalMilliseconds} ms");
return await Task.FromResult(taskDone);
}
/// <summary>
/// Effettua update (se possibile) delle licenze indicate dall'elenco codici di impiego indicati
/// </summary>
/// <param name="MasterKey">Codice Licenza Master</param>
/// <param name="ParamDict">Elenco codici impiego (key) + valori in formato dizionari</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<bool> AttivazioniTryRefresh(string MasterKey, Dictionary<string, string> ParamDict)
{
bool taskDone = false;
Stopwatch sw = new Stopwatch();
sw.Start();
taskDone = dbController.AttivazioniTryRefresh(MasterKey, ParamDict);
await FlushRedisCache();
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata scrittura + rilettura da DB per AttivazioniTryRefresh: {ts.TotalMilliseconds} ms");
return await Task.FromResult(taskDone);
}
/// <summary>
/// Recupera elenco Claim dato UserID
/// </summary>
/// <param name="userID"></param>
/// <returns></returns>
public async Task<List<AuthClaimModel>> AuthClaimByUserID(int userID)
{
string source = "DB";
List<AuthClaimModel>? dbResult = new List<AuthClaimModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Auth:Claims:UID:{userID}";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<AuthClaimModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<AuthClaimModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.AuthClaimByUserID(userID);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, LongCache);
// per evitare loopback uso deserialize...
var tempResult = JsonConvert.DeserializeObject<List<AuthClaimModel>>(rawData);
if (tempResult != null)
{
dbResult = tempResult;
}
}
if (dbResult == null)
{
dbResult = new List<AuthClaimModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"AuthClaimByUserID | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during AuthClaimByUserID:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Recupera elenco Claim dato UserName
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public async Task<List<AuthClaimModel>> AuthClaimByUserName(string userName)
{
string source = "DB";
List<AuthClaimModel>? dbResult = new List<AuthClaimModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Auth:Claims:UName:{userName}";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<AuthClaimModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<AuthClaimModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.AuthClaimByUserName(userName);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
// per evitare loopback uso deserialize...
var tempResult = JsonConvert.DeserializeObject<List<AuthClaimModel>>(rawData);
if (tempResult != null)
{
dbResult = tempResult;
}
}
if (dbResult == null)
{
dbResult = new List<AuthClaimModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"AuthClaimByUserName | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during AuthClaimByUserName:{Environment.NewLine}{exc}");
}
//return await Task.FromResult(dbResult);
return dbResult;
}
/// <summary>
/// Rimozione del Claim (Role Utente)
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public async Task<bool> AuthClaimRemove(AuthClaimModel newRec)
{
bool fatto = dbController.AuthClaimRemove(newRec);
await FlushRedisCache();
return fatto;
}
/// <summary>
/// Upsesrt del Claim di auth utente
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public async Task<bool> AuthClaimUpsert(AuthClaimModel newRec)
{
bool fatto = dbController.AuthClaimUpsert(newRec);
await FlushRedisCache();
return fatto;
}
/// <summary>
/// Rimozione dei roles x utente
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public async Task<bool> AuthRoleResetUser(int UserId)
{
bool fatto = dbController.AuthRoleResetUser(UserId);
await FlushRedisCache();
return fatto;
}
/// <summary>
/// Recupera elenco Roles (completo)
/// </summary>
/// <returns></returns>
public async Task<List<AuthRoleModel>> AuthRolesGetAll()
{
string source = "DB";
List<AuthRoleModel>? dbResult = new List<AuthRoleModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Auth:Roles";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<AuthRoleModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<AuthRoleModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.AuthRolesGetAll();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
// per evitare loopback uso deserialize...
var tempResult = JsonConvert.DeserializeObject<List<AuthRoleModel>>(rawData);
if (tempResult != null)
{
dbResult = tempResult;
}
}
if (dbResult == null)
{
dbResult = new List<AuthRoleModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"AuthRolesGetAll | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during AuthRolesGetAll:{Environment.NewLine}{exc}");
}
//return await Task.FromResult(dbResult);
return dbResult;
}
/// <summary>
/// Upsert del ROLE
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public async Task<bool> AuthRoleUpsert(AuthRoleModel newRec)
{
bool fatto = dbController.AuthRoleUpsert(newRec);
await FlushRedisCache();
return fatto;
}
/// <summary>
/// Recupera elenco Utenti (tutti)
/// </summary>
/// <returns></returns>
public async Task<List<AuthUserModel>> AuthUserAll()
{
string source = "DB";
List<AuthUserModel>? dbResult = new List<AuthUserModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Auth:UsersList";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<AuthUserModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<AuthUserModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.AuthUserAll();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
// per evitare loopback uso deserialize...
var tempResult = JsonConvert.DeserializeObject<List<AuthUserModel>>(rawData);
if (tempResult != null)
{
dbResult = tempResult;
}
}
if (dbResult == null)
{
dbResult = new List<AuthUserModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"AuthUserAll | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during AuthUserAll:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Recupera elenco Utenti UserName (idealmente 1...)
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public async Task<List<AuthUserModel>> AuthUserGetFilt(string userName)
{
string source = "DB";
List<AuthUserModel>? dbResult = new List<AuthUserModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Auth:User:{userName}";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<AuthUserModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<AuthUserModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.AuthUserGetFilt(userName);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
// per evitare loopback uso deserialize...
var tempResult = JsonConvert.DeserializeObject<List<AuthUserModel>>(rawData);
if (tempResult != null)
{
dbResult = tempResult;
}
}
if (dbResult == null)
{
dbResult = new List<AuthUserModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"AuthUserGetFilt | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during AuthUserGetFilt:{Environment.NewLine}{exc}");
}
//return await Task.FromResult(dbResult);
return dbResult;
}
/// <summary>
/// Upsert AuthUser
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public async Task<bool> AuthUserUpsert(AuthUserModel newRec)
{
bool fatto = dbController.AuthUserUpsert(newRec);
await FlushRedisCache();
return fatto;
}
public virtual void Dispose()
{
// Clear database controller
dbController.Dispose();
}
/// <summary>
/// Elimino una richiesta enroll (anche se già approvata...)
/// </summary>
/// <param name="idReq">ID record</param>
public async Task<bool> EnrollReqDelete(int idReq)
{
bool fatto = false;
// inserimento!
Stopwatch sw = new Stopwatch();
sw.Start();
fatto = dbController.EnrollReqDelete(idReq);
// svuota eventuale cache redis...
await FlushRedisCachePattern("Enroll");
await FlushRedisCachePattern("InstVerSta");
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata EnrollReqDelete: {ts.TotalMilliseconds} ms");
// invio string in messagepipe x forzare refresh...
EnrollMessPipe.sendMessage("NewEnrollReq");
return fatto;
}
/// <summary>
/// Elenco richeiste enroll attive al momento
/// </summary>
/// <returns></returns>
public async Task<List<EnrollRequestModel>> EnrollReqGetActive()
{
string source = "DB";
List<EnrollRequestModel> dbResult = new List<EnrollRequestModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Enroll:ActiveList";
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<EnrollRequestModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<EnrollRequestModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.EnrollReqGetActive();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, LongCache);
}
if (dbResult == null)
{
dbResult = new List<EnrollRequestModel>();
}
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Debug($"EnrollReqGetActive | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during EnrollReqGetActive:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Recupera una richiesta dato suo ID x verificare approvazione e dati associati...
/// </summary>
/// <param name="idReq">ID record</param>
public async Task<EnrollRequestModel> EnrollReqGetById(int idReq)
{
string source = "DB";
EnrollRequestModel dbResult = new EnrollRequestModel() { IdReq = idReq };
try
{
string currKey = $"{Const.rKeyConfig}:Enroll:ById:{idReq}";
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<EnrollRequestModel>(rawData);
if (tempResult == null)
{
dbResult = new EnrollRequestModel() { IdReq = idReq };
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.EnrollReqGetById(idReq);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, FastCache);
}
if (dbResult == null)
{
dbResult = new EnrollRequestModel() { IdReq = idReq };
}
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Debug($"EnrollReqGetById | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during EnrollReqGetById:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Elenco richeiste enroll attive al momento
/// </summary>
/// <returns></returns>
public async Task<List<EnrollRequestModel>> EnrollReqGetFilt(bool onlyActive, DateTime dtFrom, DateTime dtTo)
{
string source = "DB";
List<EnrollRequestModel> dbResult = new List<EnrollRequestModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Enroll:ActiveList";
if (!onlyActive)
{
currKey = $"{Const.rKeyConfig}:Enroll:Sel:{dtFrom:yyMMdd}_{dtTo:yyMMdd}";
}
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<EnrollRequestModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<EnrollRequestModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
if (onlyActive)
{
dbResult = dbController.EnrollReqGetActive();
}
else
{
dbResult = dbController.EnrollReqGetFilt(dtFrom, dtTo);
}
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new List<EnrollRequestModel>();
}
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Debug($"EnrollReqGetActive | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during EnrollReqGetActive:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Elimino eventuali richieste non approvate e scadute
/// </summary>
public async Task<bool> EnrollReqPurgeInvalid()
{
var reqPurged = dbController.EnrollReqPurgeInvalid();
// svuota eventuale cache redis...
await FlushRedisCachePattern("Enroll");
await FlushRedisCachePattern("InstVerSta");
// invio string in messagepipe x forzare refresh...
EnrollMessPipe.sendMessage("NewEnrollReq");
return reqPurged;
}
/// <summary>
/// Upsert record richiesta enroll
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public async Task<EnrollRequestModel> EnrollReqUpsert(EnrollRequestModel newRec)
{
int recId = 0;
// inserimento!
Stopwatch sw = new Stopwatch();
sw.Start();
recId = dbController.EnrollReqUpsert(newRec);
await FlushRedisCachePattern("Enroll");
await FlushRedisCachePattern("InstVerSta");
var dbResult = await EnrollReqGetById(recId);
// svuota eventuale cache redis...
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata EnrollReqUpsert: {ts.TotalMilliseconds} ms");
// invio string in messagepipe x forzare refresh...
EnrollMessPipe.sendMessage("NewEnrollReq");
// restituisce risultato
return dbResult;
}
/// <summary>
/// Elenco file registrati dato ticket id
/// </summary>
/// <param name="idxTicket">Identificativo del ticket</param>
/// <returns></returns>
public async Task<List<FileAttachModel>> FileGetFilt(int idxTicket)
{
List<FileAttachModel> dbResult = new List<FileAttachModel>();
Stopwatch sw = new Stopwatch();
sw.Start();
dbResult = dbController.FileGetFilt(idxTicket);
sw.Stop();
Log.Trace($"Effettuata lettura da DB per FileGetFilt: {sw.Elapsed.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Esegue flush cache dati enroll x successiva rilettura
/// </summary>
/// <param name="newRec"></param>
/// <returns></returns>
public async Task<bool> FlushEnrollCache()
{
bool fatto = await FlushRedisCachePattern("Enroll");
return fatto;
}
/// <summary>
/// Refresh globale cache redis
/// </summary>
/// <returns></returns>
public async Task<bool> FlushRedisCache()
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
await Task.Delay(1);
RedisValue pattern = new RedisValue($"{Const.rKeyConfig}:*");
bool answ = await ExecFlushRedisPattern(pattern);
UserClaimsLUT = new Dictionary<string, bool>();
stopWatch.Stop();
Log.Debug($"FlushRedisCache in {stopWatch.Elapsed.TotalMilliseconds} ms");
return answ;
}
/// <summary>
/// Refresh cache Redis dato redPattern
/// </summary>
/// <param name="pattern">Pattern da eliminare</param>
/// <returns></returns>
public async Task<bool> FlushRedisCachePattern(string pattern)
{
Stopwatch sw = new Stopwatch();
sw.Start();
await Task.Delay(1);
RedisValue redPattern = new RedisValue($"{Const.rKeyConfig}:{pattern}*");
bool answ = await ExecFlushRedisPattern(redPattern);
sw.Stop();
Log.Debug($"FlushRedisCachePattern in {sw.Elapsed.TotalMilliseconds} ms");
return answ;
}
public async Task<List<InstallazioneModel>> InstallazioniNextGetAll()
{
string source = "DB";
List<InstallazioneModel> dbResult = new List<InstallazioneModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Next:Installazioni:List";
Stopwatch sw = new Stopwatch();
sw.Start();
string rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<InstallazioneModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<InstallazioneModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.GetInstallazioni();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new List<InstallazioneModel>();
}
sw.Stop();
Log.Debug($"InstallazioniNextGetAll | {source} in: {sw.Elapsed.TotalMilliseconds:N2} ms");
}
catch (Exception exc)
{
Log.Error($"Error during InstallazioniNextGetAll:{Environment.NewLine}{exc}");
}
return dbResult;
#if false
List<InstallazioneModel> dbResult = new List<InstallazioneModel>();
string cacheKey = mHash("Next:Installazioni");
string rawData;
var redisDataList = await distributedCache.GetAsync(cacheKey);
if (redisDataList != null)
{
rawData = Encoding.UTF8.GetString(redisDataList);
dbResult = JsonConvert.DeserializeObject<List<InstallazioneModel>>(rawData);
}
else
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbControllerNext.GetInstallazioni();
rawData = JsonConvert.SerializeObject(dbResult);
redisDataList = Encoding.UTF8.GetBytes(rawData);
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB + caching per InstallazioniNextGetAll: {ts.TotalMilliseconds} ms");
}
return await Task.FromResult(dbResult);
#endif
}
public async Task<bool> InstallazioniNextUpdate(InstallazioneModel currItem)
{
bool done = false;
try
{
done = dbController.UpsertInstallazione(currItem);
await FlushRedisCache();
}
catch (Exception exc)
{
Log.Error($"Eccezione in InstallazioniNextUpdate:{Environment.NewLine}{exc}");
}
return await Task.FromResult(done);
}
/// <summary>
/// Effettua pulizia record registrazione InstalledRelease eliminando quelli non presenti in elenco
/// </summary>
/// <param name="CodImp">CodImpiego</param>
/// <param name="AppKey">Chiave App</param>
/// <param name="ListCodApp">Elenco app gestite (=da tenere)</param>
/// <returns>Num rec eliminati</returns>
public int InstallRelClean(string CodImp, string AppKey, List<string> ListCodApp)
{
return dbController.InstallRelClean(CodImp, AppKey, ListCodApp);
}
/// <summary>
/// Effettua eliminazione record registrazione InstalledRelease dato idx SubLic
/// </summary>
/// <param name="idxSubLic">Idx sub licenza</param>
/// <returns>Num rec eliminati</returns>
public int InstallRelDelBySubLic(int idxSubLic)
{
return dbController.InstallRelDelBySubLic(idxSubLic);
}
/// <summary>
/// Effettua salvataggio della situazione delle installazioni attive (se non c'è veto per registrazione appena effettuata...)
/// </summary>
/// <param name="doForce">Forza salvataggio comunque</param>
/// <returns></returns>
public async Task<bool> InstallRelHistSnapshot(bool doForce)
{
bool fatto = false;
DateTime adesso = DateTime.Now;
if (adesso > VetoInstRelHistSnap || doForce)
{
// veto x 4h (+/- rand 30 min)
VetoInstRelHistSnap = adesso.AddHours(4).AddMinutes(rnd.Next(-30, 30));
// calcolo periodo come x interfaccia...
DateTime DtFine = DateTime.Today.AddHours(DateTime.Now.Hour + 1);
DateTime DtInizio = DateTime.Today.AddMonths(-1);
// svuoto cache info...
await ResetInstallStatus();
// recupero info DTO
var rawData = await InstallStatusGetInfo(DtInizio, DtFine, "", "");
fatto = dbController.InstallRelHistSnapshot(rawData.InstallStatus);
}
return fatto;
}
/// <summary>
/// Registro su DB il record della licenza attuale relativo alla richiesta di verifica licenza ricevuta
/// </summary>
/// <param name="upRec">record da inserire/aggiornare</param>
public bool InstallRelUpsert(InstalledReleasesModel upRec)
{
bool fatto = dbController.InstallRelUpsert(upRec);
// se ok fa verifica sublicenze...
if (fatto)
{
dbController.CheckCleanOldSubLic(upRec);
}
return fatto;
}
/// <summary>
/// Reset cache info installazioni x forzare reload
/// </summary>
/// <returns></returns>
public async Task<bool> ResetInstallStatus()
{
bool fatto = false;
Stopwatch sw = new Stopwatch();
sw.Start();
string source = "REDIS";
fatto = await FlushRedisCachePattern($"InstVerSta");
sw.Stop();
Log.Debug($"ResetInstallStatus | {source} cleanup in: {sw.Elapsed.TotalMilliseconds} ms");
return fatto;
}
/// <summary>
/// Recupera info statistiche installazione dato periodo riferimento, da cache o da db
/// </summary>
/// <param name="dtStart"></param>
/// <param name="dtEnd"></param>
/// <param name="CodInst">Filtro Cliente (CodInstall)</param>
/// <param name="TipoApp">Filtro TipoApp</param>
public async Task<InstallStatusDTO> InstallStatusGetInfo(DateTime dtStart, DateTime dtEnd, string CodInst, string TipoApp)
{
string source = "DB";
InstallStatusDTO dbResult = new InstallStatusDTO();
try
{
string filtCli = string.IsNullOrEmpty(CodInst) ? "ALL-INSTALL" : $"{CodInst}";
string filtTipo = string.IsNullOrEmpty(TipoApp) ? "ALL-TIPO" : $"{TipoApp}";
string currKey = $"{Const.rKeyConfig}:InstVerSta:{filtTipo}:{filtCli}:{dtStart:yyyyMMdd-HHmm}:{dtEnd:yyyyMMdd-HHmm}";
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<InstallStatusDTO>(rawData);
if (tempResult == null)
{
dbResult = new InstallStatusDTO();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.InstallStatusGetInfo(dtStart, dtEnd, CodInst, TipoApp);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new InstallStatusDTO();
}
sw.Stop();
Log.Debug($"InstallStatusGetInfo | {source} in: {sw.Elapsed.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during InstallStatusGetInfo:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Elenco licenze dato ID
/// </summary>
/// <param name="licId">ID licenza (DB)</param>
/// <returns></returns>
public async Task<LicenzaModel> LicenzaById(int licId)
{
LicenzaModel dbResult = new LicenzaModel();
string cacheKey = $"{rKeyLicById}:{licId}";
trackCache(cacheKey);
string rawData = await getRSV(cacheKey);
if (!string.IsNullOrEmpty(rawData))
{
dbResult = JsonConvert.DeserializeObject<LicenzaModel>(rawData);
}
else
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.LicenzaById(licId);
if (dbResult != null)
{
rawData = JsonConvert.SerializeObject(dbResult);
await setRSV(cacheKey, rawData, hourTTL);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per LicenzaById: {ts.TotalMilliseconds} ms");
}
return dbResult;
}
/// <summary>
/// Record licenza data masterKey
/// </summary>
/// <param name="chiave">Chiave Licenza x ricerca</param>
/// <returns></returns>
public async Task<LicenzaModel> LicenzaByMasterKey(string chiave)
{
LicenzaModel dbResult = new LicenzaModel();
string cacheKey = $"{rKeyLicByMKey}:{chiave}";
trackCache(cacheKey);
string rawData = await getRSV(cacheKey);
if (!string.IsNullOrEmpty(rawData))
{
dbResult = JsonConvert.DeserializeObject<LicenzaModel>(rawData);
}
else
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.LicenzaByKey(chiave);
if (dbResult != null)
{
rawData = JsonConvert.SerializeObject(dbResult);
await setRSV(cacheKey, rawData, hourTTL);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per LicenzaByMasterKey: {ts.TotalMilliseconds} ms");
}
return await Task.FromResult(dbResult);
}
/// <summary>
/// Recupera licenza dato IDX
/// </summary>
/// <param name="IdxLic"></param>
/// <returns></returns>
public LicenzaModel LicenzaNextGetByIdx(int IdxLic)
{
Stopwatch sw = new Stopwatch();
LicenzaModel dbResult = new LicenzaModel();
sw.Start();
dbResult = dbController.GetLicenza(IdxLic);
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata lettura da DB per LicenzeNextGetByIdx: {ts.TotalMilliseconds} ms");
return dbResult;
}
/// <summary>
/// Effettua refresh del Payload della licenza dato info + enigma generato dal client
/// </summary>
/// <param name="appInfo"></param>
/// <returns></returns>
public async Task<ApplicativoDTO> LicenzaRefreshPayload(LicenseCoord appInfo)
{
// chiamo metodo x ricalcolare Payload dato enigma
bool done = await dbController.LicenseUpdatePayload(appInfo.CodInst, appInfo.CodApp, appInfo.MasterKey, appInfo.Enigma);
await FlushRedisCache();
// ora recupero i dati
var licList = await LicenzeSearch(appInfo.CodInst, appInfo.CodApp, appInfo.MasterKey, false);
return licList.FirstOrDefault();
}
/// <summary>
/// Elenco licenze dato cliente
/// </summary>
/// <param name="cliente"></param>
/// <returns></returns>
public async Task<List<LicenzaModel>> LicenzeByCliente(string cliente)
{
List<LicenzaModel> dbResult = new List<LicenzaModel>();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.GetLicenzeFilt(true, "", cliente);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per LicenzeByCliente: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Elenco licenze x data scadenza
/// </summary>
/// <param name="minDate">Data minima di scadenza</param>
/// <param name="maxDate">Data massima di scadenza</param>
/// <returns></returns>
public async Task<List<ApplicativoDTO>> LicenzeExpiring(DateTime minDate, DateTime maxDate)
{
List<ApplicativoDTO> dbResult = new List<ApplicativoDTO>();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.GetApplicativiExpiring(minDate, maxDate).ToList();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per LicenzeExpiring: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
public async Task<List<LicenzaModel>> LicenzeNextGetAll()
{
string source = "DB";
List<LicenzaModel> dbResult = new List<LicenzaModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Next:Licenze:List";
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<LicenzaModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<LicenzaModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.GetLicenze();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new List<LicenzaModel>();
}
sw.Stop();
Log.Debug($"LicenzeNextGetAll | {source} in: {sw.Elapsed.TotalMilliseconds:N2} ms");
}
catch (Exception exc)
{
Log.Error($"Error during LicenzeNextGetAll:{Environment.NewLine}{exc}");
}
return dbResult;
#if false
List<LicenzaModel> dbResult = new List<LicenzaModel>();
string cacheKey = mHash("Next:Licenze");
string rawData;
var redisDataList = await distributedCache.GetAsync(cacheKey);
if (redisDataList != null)
{
rawData = Encoding.UTF8.GetString(redisDataList);
dbResult = JsonConvert.DeserializeObject<List<LicenzaModel>>(rawData);
}
else
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbControllerNext.GetLicenze();
rawData = JsonConvert.SerializeObject(dbResult);
redisDataList = Encoding.UTF8.GetBytes(rawData);
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB + caching per LicenzeNextGetAll: {ts.TotalMilliseconds} ms");
}
return await Task.FromResult(dbResult);
#endif
}
/// <summary>
/// Recupera licenze SENZA cache
/// </summary>
/// <param name="CurrFilter"></param>
/// <returns></returns>
public async Task<List<LicenzaModel>> LicenzeNextGetFilt(SelectNext CurrFilter)
{
string source = "DB";
List<LicenzaModel> dbResult = new List<LicenzaModel>();
try
{
string currKey = $"{Const.rKeyConfig}:Next:LicenzeFilt:APP_{CurrFilter.ApplicazioneSel}:INS_{CurrFilter.InstallazioneSel}:OA_{CurrFilter.OnlyActive}";
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<LicenzaModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<LicenzaModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.GetLicenzeFilt(CurrFilter.OnlyActive, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new List<LicenzaModel>();
}
sw.Stop();
Log.Debug($"LicenzeNextGetFilt | {source} in: {sw.Elapsed.TotalMilliseconds:N2} ms");
}
catch (Exception exc)
{
Log.Error($"Error during LicenzeNextGetAll:{Environment.NewLine}{exc}");
}
return dbResult;
}
public async Task<bool> LicenzeNextUpdate(LicenzaModel currItem)
{
bool done = false;
// chiamo Log licenza + update insieme
try
{
done = dbController.UpsertLicenza(currItem);
await FlushRedisCache();
}
catch (Exception exc)
{
Log.Error($"Eccezione in ApplicazioniUpdate:{Environment.NewLine}{exc}");
}
return await Task.FromResult(done);
}
/// <summary>
/// Elenco licenze dato cliente
/// </summary>
/// <param name="CodInst">Codice Installaizone /Cliente</param>
/// <param name="CodApp">Codice Applicazione</param>
/// <param name="Chiave">Chiave Licenza da validare</param>
/// <param name="HideData">Indica se nascondere i dati sensibili</param>
/// <returns></returns>
public async Task<List<ApplicativoDTO>> LicenzeSearch(string CodInst, string CodApp, string Chiave, bool HideData)
{
List<ApplicativoDTO> dbResult = new List<ApplicativoDTO>();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.GetApplicativiFilt(true, CodApp, CodInst, HideData).Where(x => x.Chiave == Chiave).ToList();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per ApplicativiByCliente: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
public async Task<bool> ReleaseDelete(ReleaseModel rec2del)
{
bool fatto = dbController.ReleaseDelete(rec2del);
await FlushRedisCache();
return fatto;
}
/// <summary>
/// Elenco Release dato Applicativo
/// </summary>
/// <param name="CodApp">Codice Applicazione</param>
/// <returns></returns>
public async Task<List<ReleaseModel>> ReleaseGetByApp(string CodApp)
{
string source = "DB";
List<ReleaseModel>? dbResult = new List<ReleaseModel>();
try
{
string currKey = $"{Const.rKeyConfig}:App:AllRel:{CodApp}";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<ReleaseModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<ReleaseModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.ReleaseGetByApp(CodApp);
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, LongCache);
// per evitare loopback uso deserialize...
var tempResult = JsonConvert.DeserializeObject<List<ReleaseModel>>(rawData);
if (tempResult != null)
{
dbResult = tempResult;
}
}
if (dbResult == null)
{
dbResult = new List<ReleaseModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"ReleaseGetByApp | {source} in: {ts.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Error during ReleaseGetByApp:{Environment.NewLine}{exc}");
}
return dbResult;
}
/// <summary>
/// Elenco Release dato Applicativo + versione minima
/// </summary>
/// <param name="CodApp">Codice Applicazione</param>
/// <param name="VersMin">Versione minima richiesta</param>
/// <returns></returns>
public async Task<List<ReleaseModel>> ReleaseGetByAppVers(string CodApp, string VersMin)
{
await Task.Delay(1);
List<ReleaseModel> dbResult = new List<ReleaseModel>();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
dbResult = dbController.ReleaseGetByAppVers(CodApp, VersMin);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB per ReleaseGetByAppVers | {CodApp} | vers >= {VersMin} | {ts.TotalMilliseconds} ms");
return dbResult;
}
/// <summary>
/// Ultima Release dato Applicativo VALIDA (= rilasciata)
/// </summary>
/// <param name="CodApp">Codice Applicazione</param>
/// <returns></returns>
public async Task<string> ReleaseLastGetByApp(string CodApp)
{
string answ = "";
RedisKey currKey = $"{Const.rKeyConfig}:App:CurrRel";
var rawVal = await redisDb.HashGetAsync(currKey, CodApp);
if (rawVal.HasValue)
{
answ = $"{rawVal}";
}
else
{
var rawList = await ReleaseGetByApp(CodApp);
if (rawList != null)
{
var lastRel = rawList
.Where(x => x.IsReleased)
.OrderByDescending(x => x.VersVal)
.ThenByDescending(x => x.ReleaseDate)
.FirstOrDefault() ?? new ReleaseModel() { CodApp = CodApp };
answ = lastRel.VersNum;
// salvo su redis tab...
await redisDb.HashSetAsync(currKey, CodApp, answ);
}
}
return answ;
}
/// <summary>
/// Upsert record Release applicazione
/// </summary>
/// <param name="currItem"></param>
/// <returns></returns>
public async Task<bool> ReleaseUpsert(ReleaseModel currItem)
{
bool done = false;
Stopwatch sw = new Stopwatch();
sw.Start();
try
{
done = await dbController.ReleaseUpsert(currItem);
await FlushRedisCache();
Log.Trace($"Effettuato upsert su DB per ReleaseUpsert | {currItem.CodApp} | {currItem.VersNum} | {sw.Elapsed.TotalMilliseconds} ms");
}
catch (Exception exc)
{
Log.Error($"Eccezione in ReleaseUpsert:{Environment.NewLine}{exc}");
}
return done;
}
public async Task<bool> SendEmail(string destEmail, string oggetto, string corpo)
{
bool answ = false;
try
{
await _emailSender.SendEmailAsync(destEmail, oggetto, corpo);
answ = true;
}
catch
{ }
return answ;
}
/// <summary>
/// Statistiche del LOG chiamate all'API dato filtro
/// </summary>
/// <param name="DateFrom">Data minima</param>
/// <param name="DateTo">DataMax</param>
/// <param name="SearchVal">Valore cercato, se "" è tutti</param>
/// <returns></returns>
public async Task<List<StatsCallModel>> StatsLogCallGetFilt(DateTime DateFrom, DateTime DateTo, string SearchVal = "")
{
string source = "DB";
List<StatsCallModel> dbResult = new List<StatsCallModel>();
try
{
string currKey = $"{Const.rKeyConfig}:StatslogCall:{DateFrom:yyyyMMdd}:{DateTo:yyyyMMdd}";
if (!string.IsNullOrEmpty(SearchVal))
{
currKey += $":{SearchVal}";
}
Stopwatch sw = new Stopwatch();
sw.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<StatsCallModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<StatsCallModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
var rawResult = dbController.StatsLogCallGetFilt(DateFrom, DateTo, SearchVal);
dbResult = rawResult
.OrderByDescending(x => x.YearRef)
.ThenByDescending(x => x.TotCall)
.ToList();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new List<StatsCallModel>();
}
sw.Stop();
Log.Debug($"StatsLogCallGetFilt | {source} in: {sw.Elapsed.TotalMilliseconds:N2} ms");
}
catch (Exception exc)
{
Log.Error($"Error during StatsLogCallGetFilt:{Environment.NewLine}{exc}");
}
return dbResult;
#if false
List<StatsCallModel> dbResult = new List<StatsCallModel>();
string cacheKey = mHash($"StatslogCall:{DateFrom:yyyyMMdd}:{DateTo:yyyyMMdd}");
if (!string.IsNullOrEmpty(SearchVal))
{
cacheKey += $":{SearchVal}";
}
string rawData;
var redisDataList = await distributedCache.GetAsync(cacheKey);
if (redisDataList != null)
{
rawData = Encoding.UTF8.GetString(redisDataList);
dbResult = JsonConvert.DeserializeObject<List<StatsCallModel>>(rawData);
}
else
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var rawResult = dbControllerNext.StatsLogCallGetFilt(DateFrom, DateTo, SearchVal);
dbResult = rawResult
.OrderByDescending(x => x.YearRef)
.ThenByDescending(x => x.TotCall)
.ToList();
rawData = JsonConvert.SerializeObject(dbResult);
redisDataList = Encoding.UTF8.GetBytes(rawData);
await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"Effettuata lettura da DB + caching per StatsLogCallGetFilt: {ts.TotalMilliseconds} ms");
}
return await Task.FromResult(dbResult);
#endif
}
/// <summary>
/// Restituisce i task associati ad un dato EgwACC in stato DONE
/// </summary>
/// <param name="CodImp"></param>
/// <returns></returns>
public Dictionary<string, string> TaskDoneGet(string CodImp)
{
RedisKey currKey = (RedisKey)$"{rKeyTaskDone}:{CodImp}";
Dictionary<string, string> answ = redisHashDictGet(currKey);
return answ;
}
/// <summary>
/// Restituisce elenco dei task richiesti da hashtable
/// </summary>
/// <returns></returns>
public Dictionary<string, string> TaskListDoneGet()
{
RedisKey currKey = (RedisKey)$"{rKeyTaskDone}List";
Dictionary<string, string> answ = redisHashDictGet(currKey);
return answ;
}
/// <summary>
/// Restituisce elenco dei task richiesti da hashtable
/// </summary>
/// <returns></returns>
public Dictionary<string, string> TaskListReqGet()
{
RedisKey currKey = (RedisKey)$"{rKeyTaskReq}List";
Dictionary<string, string> answ = redisHashDictGet(currKey);
return answ;
}
/// <summary>
/// Restituisce elenco dei task richiesti da hashtable
/// </summary>
/// <returns></returns>
public Dictionary<string, string> TaskListRunGet()
{
RedisKey currKey = (RedisKey)$"{rKeyTaskRun}List";
Dictionary<string, string> answ = redisHashDictGet(currKey);
return answ;
}
/// <summary>
/// Aggiunta di un task x un dato EgwACC (senza scadenza)
/// </summary>
/// <param name="CodImp">UID impianto</param>
/// <param name="TaskType">Task richiesto</param>
/// <param name="TaskVal">Valore/opzione task</param>
/// <returns></returns>
public bool TaskReqAdd(string CodImp, EgwAccTask TaskType, string TaskVal)
{
bool answ = redisHashKeySet((RedisKey)$"{rKeyTaskReq}:{CodImp}", $"{TaskType}", TaskVal);
// aggiungo al dizionario x ricerca...
redisHashKeySet((RedisKey)$"{rKeyTaskReq}List", CodImp, $"{DateTime.Now}");
return answ;
}
/// <summary>
/// Aggiunta di un task x una lista di dato EgwACC (senza scadenza)
/// </summary>
/// <param name="ListCodImp">Lista di UID impianto</param>
/// <param name="TaskType">Task richiesto</param>
/// <param name="TaskVal">Valore/opzione task</param>
/// <returns></returns>
public bool TaskReqAdd(List<string> ListCodImp, EgwAccTask TaskType, string TaskVal)
{
int done = 0;
foreach (var CodImp in ListCodImp)
{
bool added = TaskReqAdd(CodImp, TaskType, TaskVal);
done += added ? 1 : 0;
}
return done > 0;
}
/// <summary>
/// Restituisce i task associati ad un dato EgwACC in stato RICHIESTI
/// </summary>
/// <param name="CodImp"></param>
/// <returns></returns>
public Dictionary<string, string> TaskReqGet(string CodImp)
{
RedisKey currKey = (RedisKey)$"{rKeyTaskReq}:{CodImp}";
Dictionary<string, string> answ = redisHashDictGet(currKey);
return answ;
}
/// <summary>
/// Resetta i task di un dato EgwACC
/// </summary>
/// <param name="CodImp"></param>
/// <returns></returns>
public bool TaskReqReset(string CodImp)
{
bool okReq = redisHashDictDelete((RedisKey)$"{rKeyTaskReq}:{CodImp}");
bool okRun = redisHashDictDelete((RedisKey)$"{rKeyTaskRun}:{CodImp}");
redisHashKeyDelete((RedisKey)$"{rKeyTaskReq}List", CodImp);
redisHashKeyDelete((RedisKey)$"{rKeyTaskRun}List", CodImp);
redisHashKeyDelete((RedisKey)$"{rKeyTaskDone}List", CodImp);
return okReq && okRun;
}
/// <summary>
/// Restituisce i task associati ad un dato EgwACC in stato RUNNING
/// </summary>
/// <param name="CodImp"></param>
/// <returns></returns>
public Dictionary<string, string> TaskRunGet(string CodImp)
{
RedisKey currKey = (RedisKey)$"{rKeyTaskRun}:{CodImp}";
Dictionary<string, string> answ = redisHashDictGet(currKey);
return answ;
}
/// <summary>
/// Recupera elenco Tickets filtrato
/// </summary>
/// <param name="CurrFilter"></param>
/// <returns></returns>
public async Task<List<TicketDTO>> TicketsGetAll()
{
Stopwatch sw = new Stopwatch();
List<TicketDTO> dbResult = new List<TicketDTO>();
sw.Start();
dbResult = dbController.TicketGetAll(false, 1000);
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata lettura da DB per TicketsGetAll: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Ricerca ticket filtrati
/// </summary>
/// <param name="onlyOpen"></param>
/// <param name="CodApp"></param>
/// <param name="CodInst"></param>
/// <returns></returns>
public async Task<List<TicketDTO>> TicketsGetFilt(bool onlyOpen, string CodApp, string CodInst)
{
Stopwatch sw = new Stopwatch();
List<TicketDTO> dbResult = new List<TicketDTO>();
sw.Start();
dbResult = dbController.TicketGetFilt(onlyOpen, CodApp, CodInst);
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata lettura da DB per TicketsGetFilt: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Recupera elenco Tickets filtrato
/// </summary>
/// <param name="CurrFilter"></param>
/// <returns></returns>
public async Task<List<TicketDTO>> TicketsGetFilt(SelectNext CurrFilter)
{
Stopwatch sw = new Stopwatch();
List<TicketDTO> dbResult = new List<TicketDTO>();
sw.Start();
dbResult = dbController.TicketGetFiltAllLic(CurrFilter.OnlyActive, Core.Enum.TipologiaTicket.ND, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel, 1000);
sw.Stop();
TimeSpan ts = sw.Elapsed;
Log.Trace($"Effettuata lettura da DB per TicketsGetFilt: {ts.TotalMilliseconds} ms");
return await Task.FromResult(dbResult);
}
/// <summary>
/// Aggiornamentos tato ticket
/// </summary>
/// <param name="IdxTicket"></param>
/// <param name="NewStatus"></param>
/// <returns></returns>
public async Task<bool> TicketUpdateState(int IdxTicket, StatoRichiesta NewStatus)
{
bool fatto = false;
// inserimento!
Stopwatch sw = new Stopwatch();
sw.Start();
fatto = dbController.TicketUpdateState(IdxTicket, NewStatus);
sw.Stop();
Log.Trace($"Effettuata update con TicketUpdateState: {sw.Elapsed.TotalMilliseconds} ms");
// restituisce elenco
return await Task.FromResult(fatto);
}
/// <summary>
/// Verifica di un role utente dall'elenco claims...
/// </summary>
/// <param name="userName"></param>
/// <param name="role"></param>
/// <returns></returns>
public bool UserHasClaim(string userName, string role)
{
bool answ = false;
string keyLUT = $"{userName}_{role}";
if (UserClaimsLUT.ContainsKey(keyLUT))
{
answ = UserClaimsLUT[keyLUT];
}
else
{
var pUpd = Task.Run(async () =>
{
var userClaims = await AuthClaimByUserName(userName);
if (userClaims != null && userClaims.Count > 0)
{
answ = userClaims.Where(x => x.RoleNav.Ruolo == role).Any();
}
});
pUpd.Wait();
UserClaimsLUT.Add(keyLUT, answ);
}
return answ;
}
#endregion Public Methods
#region Protected Fields
/// <summary>
/// TTL da 1 h x cache Redis
/// </summary>
protected const int hourTTL = 60 * 60;
/// <summary>
/// TTL da 1 min x cache Redis
/// </summary>
protected const int redCacheTtlFast = 60 * 1;
/// <summary>
/// TTL da 5 min x cache Redis
/// </summary>
protected const int redCacheTtlStd = 60 * 5;
/// <summary>
/// Chiave redis x attivazioni da IdxLic
/// </summary>
protected const string rKeyAttivByLic = "LiMan.UI:Licenze:AttByIdxLic";
/// <summary>
/// Chiave base dati Redis gestiti API
/// </summary>
protected const string rKeyBaseApi = "LiMan:API";
/// <summary>
/// Chiave base dati Redis gestiti UI/API
/// </summary>
protected const string rKeyBaseComm = "LiMan:ALL";
/// <summary>
/// Chiave base dati Redis gestiti UI
/// </summary>
protected const string rKeyBaseUi = "LiMan:UI";
/// <summary>
/// Chiave degli elenchi dei devices con EgwACC in esecuzione
/// </summary>
protected const string rKeyEACCDetail = $"{rKeyBaseComm}:EAccDetail";
/// <summary>
/// Chiave degli elenchi dei devices con EgwACC in esecuzione
/// </summary>
protected const string rKeyEACCList = $"{rKeyBaseComm}:EAccList";
/// <summary>
/// Chiave redis x licenze da ID
/// </summary>
protected const string rKeyLicById = "LiMan.UI:Licenze:ById";
/// <summary>
/// Chiave redis x licenze da MasterKey
/// </summary>
protected const string rKeyLicByMKey = "LiMan.UI:Licenze:ListByKey";
/// <summary>
/// Chiave redis x statistiche in acquisizione
/// </summary>
protected const string rKeySampleStats = "LiMan.UI:SampleStats:Curr";
/// <summary>
/// Chiave redis x statistiche in acquisizione
/// </summary>
protected const string rKeySampleVars = "LiMan.UI:SampleStats:Vars";
/// <summary>
/// Chiave redis x statistiche chiamate
/// </summary>
protected const string rKeyStatslogCall = "LiMan.UI:StatsLogCall";
/// <summary>
/// Chiave degli elenchi dei Task eseguiti
/// </summary>
protected const string rKeyTaskDone = $"{rKeyBaseComm}:TaskDone";
/// <summary>
/// Chiave degli elenchi dei Task req/pending
/// </summary>
protected const string rKeyTaskReq = $"{rKeyBaseComm}:TaskReq";
/// <summary>
/// Chiave degli elenchi dei Task in esecuzione
/// </summary>
protected const string rKeyTaskRun = $"{rKeyBaseComm}:TaskRun";
/// <summary>
/// Chiave degli elenchi delle ultime DataOra di comunicazione degli updater
/// </summary>
protected const string rKeyUpdLastAct = $"{rKeyBaseComm}:LastActions";
protected static IConfiguration _configuration;
protected static Controllers.DbController dbController;
protected static JsonSerializerSettings? JSSettings;
protected static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Oggetto per invio email
/// </summary>
protected readonly IEmailSender _emailSender;
/// <summary>
/// Durata cache lunga IN SECONDI
/// </summary>
protected int cacheTtlLong = 60 * 5;
/// <summary>
/// Durata cache breve IN SECONDI
/// </summary>
protected int cacheTtlShort = 60 * 1;
/// <summary>
/// Oggetto per connessione a REDIS
/// </summary>
protected IConnectionMultiplexer redisConn = null!;
/// <summary>
/// Oggetto DB redis da impiegare x chiamate R/W
/// </summary>
protected IDatabase redisDb = null!;
protected Random rnd = new Random();
#endregion Protected Fields
#region Protected Properties
/// <summary>
/// Durata cache breve (1 min circa + perturbazione percentuale +/-10%)
/// </summary>
protected TimeSpan FastCache
{
get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000);
}
/// <summary>
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
/// </summary>
protected TimeSpan LongCache
{
get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000);
}
/// <summary>
/// Durata cache MOLTO breve (10 sec circa + perturbazione percentuale +/-10%)
/// </summary>
protected TimeSpan UltraFastCache
{
get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000);
}
/// <summary>
/// Durata cache MOLTO lunga (+ perturbazione percentuale +/-10%)
/// </summary>
protected TimeSpan UltraLongCache
{
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
}
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Esegue flush memoria redis dato pat2Flush
/// </summary>
/// <param name="pat2Flush"></param>
/// <returns></returns>
protected async Task<bool> ExecFlushRedisPattern(RedisValue pat2Flush)
{
bool answ = false;
var masterEndpoint = redisConn.GetEndPoints()
.Where(ep => redisConn.GetServer(ep).IsConnected && !redisConn.GetServer(ep).IsReplica)
.FirstOrDefault();
// sepattern è "*" elimino intero DB...
if (masterEndpoint != null && (pat2Flush.Equals(new RedisValue("*")) || pat2Flush == RedisValue.Null))
{
redisConn.GetServer(masterEndpoint).FlushDatabase(database: redisDb.Database);
}
else
{
var server = redisConn.GetServer(masterEndpoint);
var keys = server.Keys(database: redisDb.Database, pattern: pat2Flush, pageSize: 1000);
var deleteTasks = new List<Task>();
foreach (var key in keys)
{
deleteTasks.Add(redisDb.KeyDeleteAsync(key));
if (deleteTasks.Count >= 1000)
{
await Task.WhenAll(deleteTasks);
deleteTasks.Clear();
}
}
if (deleteTasks.Count > 0)
{
await Task.WhenAll(deleteTasks);
}
}
answ = true;
#if false
var listEndpoints = redisConn.GetEndPoints();
foreach (var endPoint in listEndpoints)
{
//var server = redisConnAdmin.GetServer(listEndpoints[0]);
var server = redisConn.GetServer(endPoint);
if (server != null)
{
var keyList = server.Keys(redisDb.Database, pattern);
foreach (var item in keyList)
{
await redisDb.KeyDeleteAsync(item);
}
answ = true;
}
}
#endif
return answ;
}
/// <summary>
/// Recupero chiave da redis
/// </summary>
/// <param name="rKey"></param>
/// <returns></returns>
protected async Task<string> getRSV(string rKey)
{
string answ = await redisDb.StringGetAsync(rKey);
return answ;
}
/// <summary>
/// Recupera contatore x la chiave redis indicata...
/// </summary>
/// <returns></returns>
protected async Task<int> redCount(string rKey)
{
int currCount = 0;
string rawVal = await getRSV(rKey);
if (!string.IsNullOrEmpty(rawVal))
{
int.TryParse(rawVal, out currCount);
}
return currCount;
}
/// <summary>
/// Resetta contatore x la chiave redis indicata...
/// </summary>
/// <returns></returns>
protected async Task<bool> redCountClear(string rKey)
{
bool answ = false;
int currCount = 0;
answ = await setRSV(rKey, currCount, 2 * hourTTL);
return answ;
}
/// <summary>
/// Incrementa contatore x la chiave redis indicata...
/// </summary>
/// <returns></returns>
protected async Task<bool> redCountIncr(string rKey)
{
bool answ = false;
int currCount = 0;
string rawVal = await getRSV(rKey);
if (!string.IsNullOrEmpty(rawVal))
{
int.TryParse(rawVal, out currCount);
}
currCount++;
answ = await setRSV(rKey, currCount, 2 * hourTTL);
return answ;
}
/// <summary>
/// Eliminazione di un HashSet Redis
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
protected bool redisHashDictDelete(RedisKey dictKey)
{
bool fatto = false;
try
{
// rimuovo scrivendo un set vuoto con expiry a 1 sec
HashEntry[] data2ins = new HashEntry[1];
data2ins[0] = new HashEntry("removed", $"{DateTime.Now}");
// salvo!
redisDb.HashSet(dictKey, data2ins);
redisDb.KeyExpire(dictKey, DateTime.Now.AddMilliseconds(1));
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in redisHashDictDelete | dictKey: {dictKey}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Recupero HashSet redis come Dictionary
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <returns>Dizionario valori salvato</returns>
protected Dictionary<string, string> redisHashDictGet(RedisKey dictKey)
{
Dictionary<string, string> answ = new Dictionary<string, string>();
if (redisDb.KeyExists(dictKey))
{
try
{
answ = redisDb
.HashGetAll(dictKey)
.ToDictionary(x => $"{x.Name}", x => $"{x.Value}");
}
catch (Exception exc)
{
Log.Info($"Errore redisHashDictGet | dictKey: {dictKey}{Environment.NewLine}{exc}");
}
}
return answ;
}
/// <summary>
/// Salvataggio Dictionary come HashSet Redis, con expiry NON gestito (0 = mai)
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <param name="dict">Valore Dizionario da salvare</param>
protected bool redisHashDictSet(RedisKey dictKey, Dictionary<string, string> dict)
{
// ove non indicato expiry è 0 = MAI
return redisHashDictSet(dictKey, dict, 0);
}
/// <summary>
/// Salvataggio Dictionary come HashSet Redis
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <param name="dict">Valore Dizionario da salvare</param>
/// <param name="expireMin">Expiry in minuti del valore, se 0 = mai</param>
protected bool redisHashDictSet(RedisKey dictKey, Dictionary<string, string> dict, double expireMin)
{
bool fatto = false;
try
{
HashEntry[] data2ins = new HashEntry[dict.Count];
int i = 0;
foreach (KeyValuePair<string, string> kvp in dict)
{
data2ins[i] = new HashEntry(kvp.Key, kvp.Value);
i++;
}
// salvo!
redisDb.HashSet(dictKey, data2ins);
// se richiesto imposto Expiry
if (expireMin > 0)
{
redisDb.KeyExpire(dictKey, DateTime.Now.AddMinutes(expireMin));
}
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in redisHashDictSet | dictKey: {dictKey}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Eliminazione di un singolo valore da un HashSet Redis
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <param name="recKey">Chiave del valore da eliminare (singolo record)</param>
protected bool redisHashKeyDelete(RedisKey dictKey, string recKey)
{
bool fatto = false;
try
{
redisDb.HashDelete(dictKey, (RedisValue)recKey);
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in redisHashKeyDelete | dictKey: {dictKey}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// recupero un singolo valore in HashSet Redis
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <param name="recKey">Chiave valore da recuperare</param>
protected string redisHashKeyGet(RedisKey dictKey, string recKey)
{
string answ = redisDb.HashGet(dictKey, (RedisValue)recKey);
return answ;
}
/// <summary>
/// Salvataggio variazione in (incremento/decremento) di un singolo valore in HashSet Redis, con expiry NON gestito (0 = mai)
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <param name="recKey">Chiave valore da salvare</param>
/// <param name="incrVal">Valore di incremento/decremento da salvare</param>
protected bool redisHashKeyIncDecBy(RedisKey dictKey, string recKey, long incrVal)
{
// ove non indicato expiry è 0 = MAI
return redisHashKeyIncDecBy(dictKey, recKey, incrVal, 0);
}
/// <summary>
/// Salvataggio INCREMENTO di un singolo valore in HashSet Redis
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <param name="recKey">Chiave valore da salvare</param>
/// <param name="incrVal">Incremento/decremento da salvare</param>
/// <param name="expireMin">Expiry in minuti del valore, se 0 = mai</param>
protected bool redisHashKeyIncDecBy(RedisKey dictKey, string recKey, long incrVal, double expireMin)
{
bool fatto = false;
try
{
// salvo!
if (incrVal > 0)
{
redisDb.HashIncrement(dictKey, (RedisValue)recKey, incrVal);
}
else
{
redisDb.HashDecrement(dictKey, (RedisValue)recKey, Math.Abs(incrVal));
}
// se richiesto imposto Expiry
if (expireMin > 0)
{
redisDb.KeyExpire(dictKey, DateTime.Now.AddMinutes(expireMin));
}
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in redisHashKeyIncDecBy | dictKey: {dictKey}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Salvataggio di un singolo valore in HashSet Redis, con expiry NON gestito (0 = mai)
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <param name="recKey">Chiave valore da salvare</param>
/// <param name="recVal">Valore da salvare</param>
protected bool redisHashKeySet(RedisKey dictKey, string recKey, string recVal)
{
// ove non indicato expiry è 0 = MAI
return redisHashKeySet(dictKey, recKey, recVal, 0);
}
/// <summary>
/// Salvataggio di un singolo valore in HashSet Redis
/// </summary>
/// <param name="dictKey">Chiave del dizionario</param>
/// <param name="recKey">Chiave valore da salvare</param>
/// <param name="recVal">Valore da salvare</param>
/// <param name="expireMin">Expiry in minuti del valore, se 0 = mai</param>
protected bool redisHashKeySet(RedisKey dictKey, string recKey, string recVal, double expireMin)
{
bool fatto = false;
try
{
HashEntry[] data2ins = new HashEntry[1] { new HashEntry(recKey, recVal) };
// salvo!
redisDb.HashSet(dictKey, data2ins);
// se richiesto imposto Expiry
if (expireMin > 0)
{
redisDb.KeyExpire(dictKey, DateTime.Now.AddMinutes(expireMin));
}
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in redisHashKeySet | dictKey: {dictKey}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Salvataggio chiave in redis
/// </summary>
/// <param name="rKey"></param>
/// <param name="rVal"></param>
/// <param name="ttlSec"></param>
/// <returns></returns>
protected async Task<bool> setRSV(string rKey, string rVal, int ttlSec)
{
bool fatto = false;
await redisDb.StringSetAsync(rKey, rVal, TimeSpan.FromSeconds(ttlSec));
fatto = true;
return fatto;
}
/// <summary>
/// Salvataggio chiave in redis
/// </summary>
/// <param name="rKey"></param>
/// <param name="rValInt"></param>
/// <param name="ttlSec"></param>
/// <returns></returns>
protected async Task<bool> setRSV(string rKey, int rValInt, int ttlSec)
{
bool fatto = false;
await redisDb.StringSetAsync(rKey, $"{rValInt}", TimeSpan.FromSeconds(ttlSec));
fatto = true;
return fatto;
}
/// <summary>
/// Registra in cache chiave se non fosse già in elenco
/// </summary>
/// <param name="newKey"></param>
protected void trackCache(string newKey)
{
if (!cachedDataList.Contains(newKey))
{
cachedDataList.Add(newKey);
}
}
#endregion Protected Methods
#region Private Fields
/// <summary>
/// Elenco obj in cache
/// </summary>
private List<string> cachedDataList = new List<string>();
private Dictionary<string, bool> UserClaimsLUT = new Dictionary<string, bool>();
/// <summary>
/// data-ora veto registrazione di una nuova registrazione di snapshot installazioni attive al giorno corrente
/// </summary>
private DateTime VetoInstRelHistSnap = DateTime.Today;
#endregion Private Fields
}
}