1388 lines
53 KiB
C#
1388 lines
53 KiB
C#
using Core;
|
|
using Core.DTO;
|
|
using Liman.CadCam.DbModel;
|
|
using LiMan.DB.Controllers;
|
|
using LiMan.DB.DBModels;
|
|
using LiMan.DB.DTO;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
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.UI.Data
|
|
{
|
|
public class LiManDataService : IDisposable
|
|
{
|
|
#region Public Fields
|
|
|
|
public static GLS.Controllers.LicManController dbControllerGLS;
|
|
public static DB.Controllers.DbController dbControllerNext;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public LiManDataService(IConfiguration configuration, ILogger<LiManDataService> logger, IMemoryCache memoryCache, IDistributedCache distributedCache, IConnectionMultiplexer redisConnMult, IEmailSender emailSender)
|
|
{
|
|
_logger = logger;
|
|
_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
|
|
};
|
|
_emailSender = emailSender;
|
|
// conf cache
|
|
this.memoryCache = memoryCache;
|
|
this.distributedCache = distributedCache;
|
|
// conf DB
|
|
string connStrGLS = _configuration.GetConnectionString("LiMan.GLS");
|
|
string connStrDB = _configuration.GetConnectionString("LiMan.DB");
|
|
if (string.IsNullOrEmpty(connStrDB) || string.IsNullOrEmpty(connStrGLS))
|
|
{
|
|
_logger.LogError("Almost one ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbControllerGLS = new LiMan.GLS.Controllers.LicManController(configuration);
|
|
dbControllerNext = new LiMan.DB.Controllers.DbController(configuration);
|
|
_logger.LogInformation("DbControllers OK");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public async Task<List<GLS.DatabaseModels.AnagApplicazioni>> ApplicazioniGLSGetAll()
|
|
{
|
|
List<GLS.DatabaseModels.AnagApplicazioni> dbResult = new List<GLS.DatabaseModels.AnagApplicazioni>();
|
|
string cacheKey = mHash("GLS:Applicazioni");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.AnagApplicazioni>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerGLS.GetApplicazioni();
|
|
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 ApplicazioniGLSGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> ApplicazioniGLSUpdate(GLS.DatabaseModels.AnagApplicazioni currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerGLS.UpdateApplicazioni(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniGLSUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<List<ApplicativoModel>> ApplicazioniNextGetAll()
|
|
{
|
|
List<ApplicativoModel> dbResult = new List<ApplicativoModel>();
|
|
string cacheKey = mHash("Next:Applicazioni");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<ApplicativoModel>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.GetApplicazioni();
|
|
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 ApplicazioniNextGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> ApplicazioniNextUpdate(ApplicativoModel currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerNext.ApplicazioniNextUpdate(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniNextUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public async Task<bool> ApplicazioniHasChild(string CodApp)
|
|
{
|
|
bool dbResult = false;
|
|
string cacheKey = mHash($"Next:Applicazioni:hasChild:{CodApp}");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<bool>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.ApplicazioniHasChild(CodApp);
|
|
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 ApplicazioniHasChild: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina il record indicato (se non ci sono record correlati...)
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ApplicazioniNextDelete(ApplicativoModel currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
// controllo NON ci siano record figli...
|
|
bool hasCHild = dbControllerNext.ApplicazioniHasChild(currItem.CodApp);
|
|
if (!hasCHild)
|
|
{
|
|
done = dbControllerNext.ApplicazioniNextDelete(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniNextDelete:{Environment.NewLine}{exc}");
|
|
}
|
|
return done;
|
|
}
|
|
|
|
|
|
|
|
/// <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 = dbControllerNext.AttivazioniUnlock(idxSubLic);
|
|
await Task.Delay(1);
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera attivazioni data licenza
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<SubLicenzaModel>> AttivazioniGetByLic(int IdxLic)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<SubLicenzaModel> dbResult = new List<SubLicenzaModel>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.AttivazioniGetByLic(IdxLic);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AttivazioniGetByLic: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <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 = dbControllerNext.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 = dbControllerNext.AuthClaimByUserName(userName);
|
|
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($"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 = dbControllerNext.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 = dbControllerNext.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 = dbControllerNext.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 = dbControllerNext.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 = dbControllerNext.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 = dbControllerNext.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 = dbControllerNext.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 = dbControllerNext.AuthUserUpsert(newRec);
|
|
await FlushRedisCache();
|
|
return fatto;
|
|
}
|
|
|
|
public async Task<bool> DbForceMigrate()
|
|
{
|
|
return await Task.FromResult(dbControllerGLS.DbForceMigrate());
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbControllerGLS.Dispose();
|
|
}
|
|
|
|
/// <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 stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.FileGetFilt(idxTicket);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per FileGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <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);
|
|
stopWatch.Stop();
|
|
Log.Debug($"FlushRedisCache in {stopWatch.Elapsed.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
public async Task<List<GLS.DatabaseModels.AnagInstallazioni>> InstallazioniGLSGetAll()
|
|
{
|
|
List<GLS.DatabaseModels.AnagInstallazioni> dbResult = new List<GLS.DatabaseModels.AnagInstallazioni>();
|
|
string cacheKey = mHash("GLS:Installazioni");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.AnagInstallazioni>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerGLS.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 InstallazioniGLSGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> InstallazioniGLSUpdate(GLS.DatabaseModels.AnagInstallazioni currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerGLS.UpdateInstallazioni(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in InstallazioniGLSUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<List<InstallazioneModel>> InstallazioniNextGetAll()
|
|
{
|
|
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);
|
|
}
|
|
|
|
public async Task<bool> InstallazioniNextUpdate(InstallazioneModel currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = dbControllerNext.UpsertInstallazione(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in InstallazioniNextUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
/// <summary>
|
|
/// invalida tutta la cache in caso di update
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task InvalidateAllCache()
|
|
{
|
|
await distributedCache.RemoveAsync(mHash("GLS:Applicazioni"));
|
|
await distributedCache.RemoveAsync(mHash("Next:Applicazioni"));
|
|
await distributedCache.RemoveAsync(mHash("GLS:Installazioni"));
|
|
await distributedCache.RemoveAsync(mHash("Next:Installazioni"));
|
|
await distributedCache.RemoveAsync(mHash("GLS:Licenze"));
|
|
await distributedCache.RemoveAsync(mHash("Next:Licenze"));
|
|
//await distributedCache.RemoveAsync(mHash("SUPPL:List"));
|
|
//await distributedCache.RemoveAsync(mHash("TRANSP:List"));
|
|
//await distributedCache.RemoveAsync(mHash("WEEKPLAN:List"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trasferisce una licenza da GLS a Next come LOG di una licenza scaduta
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> LicenzaLogGlsNext(GLS.DatabaseModels.LicenzeAttive currItem, int IdxLicNext)
|
|
{
|
|
bool done = false;
|
|
|
|
var currLicenza = dbControllerNext.GetLicenza(IdxLicNext);
|
|
// step 1: converto licenza, faccio upsert
|
|
var logLicNext = new LogLicenzaModel()
|
|
{
|
|
CodApp = currItem.ApplicativoNavigation.Applicativo,
|
|
CodInst = currItem.InstallazioneNavigation.Installazione,
|
|
Chiave = currItem.Licenza,
|
|
NumLicenze = currItem.NumLicenze,
|
|
Scadenza = currItem.Scadenza,
|
|
Descrizione = currItem.ApplicativoNavigation.Descrizione,
|
|
Tipo = TipoLicenza.GLS,
|
|
IdxLic = IdxLicNext,
|
|
Locked = true
|
|
};
|
|
bool step1 = dbControllerNext.UpsertLogLic(logLicNext);
|
|
|
|
if (currLicenza != null && step1)
|
|
{
|
|
// step 2: disattivo vecchia licenza
|
|
currItem.Locked = true;
|
|
done = dbControllerGLS.UpdateLicenze(currItem);
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trasferisce una licenza da GLS a Next
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> LicenzaTransferGlsNext(GLS.DatabaseModels.LicenzeAttive currItem)
|
|
{
|
|
bool done = false;
|
|
// step 1: controllo applicazione esistente
|
|
ApplicativoModel appNext = new ApplicativoModel()
|
|
{
|
|
CodApp = currItem.ApplicativoNavigation.Applicativo,
|
|
Descrizione = currItem.ApplicativoNavigation.Descrizione
|
|
};
|
|
bool step1 = dbControllerNext.ApplicazioniNextUpdate(appNext);
|
|
|
|
// step 2: controllo installazione esistente
|
|
var instNext = new InstallazioneModel()
|
|
{
|
|
Cliente = currItem.InstallazioneNavigation.Installazione,
|
|
CodInst = currItem.InstallazioneNavigation.Installazione,
|
|
Contatto = currItem.InstallazioneNavigation.Contatto,
|
|
Email = currItem.InstallazioneNavigation.Email,
|
|
Descrizione = currItem.ApplicativoNavigation.Descrizione
|
|
};
|
|
bool step2 = dbControllerNext.UpsertInstallazione(instNext);
|
|
|
|
// step 3: converto licenza, faccio upsert
|
|
var licNext = new LicenzaModel()
|
|
{
|
|
CodApp = currItem.ApplicativoNavigation.Applicativo,
|
|
CodInst = currItem.InstallazioneNavigation.Installazione,
|
|
Chiave = currItem.Licenza,
|
|
NumLicenze = currItem.NumLicenze,
|
|
Scadenza = currItem.Scadenza,
|
|
Descrizione = currItem.ApplicativoNavigation.Descrizione,
|
|
Tipo = TipoLicenza.GLS,
|
|
Enigma = "",
|
|
Payload = "",
|
|
DataEnigma = DateTime.Now
|
|
};
|
|
bool step3 = dbControllerNext.UpsertLicenza(licNext);
|
|
|
|
if (step1 && step2 && step3)
|
|
{
|
|
// step 4: disattivo vecchia licenza
|
|
currItem.Locked = true;
|
|
done = dbControllerGLS.UpdateLicenze(currItem);
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<List<GLS.DatabaseModels.LicenzeAttive>> LicenzeGLSGetAll()
|
|
{
|
|
List<GLS.DatabaseModels.LicenzeAttive> dbResult = new List<GLS.DatabaseModels.LicenzeAttive>();
|
|
string cacheKey = mHash("GLS:Licenze");
|
|
string rawData;
|
|
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
dbResult = JsonConvert.DeserializeObject<List<GLS.DatabaseModels.LicenzeAttive>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerGLS.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 LicenzeGLSGetAll: {ts.TotalMilliseconds} ms");
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera licenze SENZA cache
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<GLS.DatabaseModels.LicenzeAttive>> LicenzeGLSGetFilt(SelectGLS CurrFilter)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<GLS.DatabaseModels.LicenzeAttive> dbResult = new List<GLS.DatabaseModels.LicenzeAttive>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerGLS.GetLicenzeFilt(CurrFilter.OnlyActive, CurrFilter.OnlyUnlock, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per LicenzeGLSGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> LicenzeGLSUpdate(GLS.DatabaseModels.LicenzeAttive currItem)
|
|
{
|
|
bool done = false;
|
|
#if false
|
|
try
|
|
{
|
|
done = dbControllerGLS.UpdateApplicazioni(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
#endif
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<List<LicenzaModel>> LicenzeNextGetAll()
|
|
{
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera licenze SENZA cache
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<LicenzaModel>> LicenzeNextGetFilt(SelectNext CurrFilter)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<LicenzaModel> dbResult = new List<LicenzaModel>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.GetLicenzeFilt(CurrFilter.OnlyActive, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per LicenzeNextGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> LicenzeNextUpdate(LicenzaModel currItem)
|
|
{
|
|
bool done = false;
|
|
|
|
// chiamo Log licenza + update insieme
|
|
try
|
|
{
|
|
done = dbControllerNext.UpsertLicenza(currItem);
|
|
await InvalidateAllCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ApplicazioniUpdate:{Environment.NewLine}{exc}");
|
|
}
|
|
return await Task.FromResult(done);
|
|
}
|
|
|
|
public async Task<bool> ReleaseDelete(ReleaseModel rec2del)
|
|
{
|
|
bool fatto = dbControllerNext.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 = dbControllerNext.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;
|
|
|
|
#if false
|
|
await Task.Delay(1);
|
|
List<ReleaseModel> dbResult = new List<ReleaseModel>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.ReleaseGetByApp(CodApp);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per ReleaseGetByApp | {CodApp} | {ts.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
#endif
|
|
}
|
|
|
|
/// <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>
|
|
/// 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 = dbControllerNext.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>
|
|
/// Upsert record Release applicazione
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ReleaseUpsert(ReleaseModel currItem)
|
|
{
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = await dbControllerNext.ReleaseUpsert(currItem);
|
|
await InvalidateAllCache();
|
|
await FlushRedisCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in ReleaseUpsert:{Environment.NewLine}{exc}");
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public void rollBackEditGLS(object item)
|
|
{
|
|
dbControllerGLS.rollBackEntity(item);
|
|
}
|
|
|
|
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 = "")
|
|
{
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera elenco Tickets filtrato
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<TicketDTO>> TicketsGetAll()
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
List<TicketDTO> dbResult = new List<TicketDTO>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.TicketGetAll(false, 1000);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per TicketsGetAll: {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 stopWatch = new Stopwatch();
|
|
List<TicketDTO> dbResult = new List<TicketDTO>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.TicketGetFiltAllLic(CurrFilter.OnlyActive, Core.Enum.TipologiaTicket.ND, CurrFilter.ApplicazioneSel, CurrFilter.InstallazioneSel, 1000);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per TicketsGetFilt: {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 stopWatch = new Stopwatch();
|
|
List<TicketDTO> dbResult = new List<TicketDTO>();
|
|
stopWatch.Start();
|
|
dbResult = dbControllerNext.TicketGetFilt(onlyOpen, CodApp, CodInst);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.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 stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
fatto = dbControllerNext.TicketUpdateState(IdxTicket, NewStatus);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata update con TicketUpdateState: {ts.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;
|
|
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();
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Internal Methods
|
|
|
|
/// <summary>
|
|
/// Effettua sblocco di una licenza impostando data veto a oggi
|
|
/// </summary>
|
|
/// <param name="idxSubLic"></param>
|
|
/// <returns></returns>
|
|
internal async Task<bool> AttivazioneDelete(int idxSubLic)
|
|
{
|
|
bool fatto = dbControllerNext.AttivazioniDelete(idxSubLic);
|
|
await Task.Delay(1);
|
|
return fatto;
|
|
}
|
|
|
|
#endregion Internal Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected static JsonSerializerSettings? JSSettings;
|
|
|
|
/// <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 pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
protected async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
bool answ = 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;
|
|
}
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
protected string getCacheKey(string TableName, SelectData CurrFilter)
|
|
{
|
|
string answ = $"{TableName}:D_{CurrFilter.DateStart:yyyyMMddHHmm}_{CurrFilter.DateEnd:yyyyMMddHHmm}";
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
private static ILogger<LiManDataService> _logger;
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
private readonly IEmailSender _emailSender;
|
|
private readonly IDistributedCache distributedCache;
|
|
private readonly IMemoryCache memoryCache;
|
|
|
|
/// <summary>
|
|
/// Durata assoluta massima della cache IN SECONDI
|
|
/// </summary>
|
|
private int chAbsExp = 60 * 5;
|
|
|
|
/// <summary>
|
|
/// Durata della cache IN SECONDI in modalità inattiva (non acceduta) prima di venire
|
|
/// rimossa NON estende oltre il tempo massimo di validità della cache (chAbsExp)
|
|
/// </summary>
|
|
private int chSliExp = 60 * 1;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Hash Redis contenente i dati MP di una specifico TYPE (es StatusMacchina,
|
|
/// StateMachineIngressi, ...)
|
|
/// </summary>
|
|
/// <param name="dataType"></param>
|
|
/// <returns></returns>
|
|
private static string mHash(string dataType)
|
|
{
|
|
return $"DATA:{dataType}";
|
|
}
|
|
|
|
private DistributedCacheEntryOptions cacheOpt(bool fastCache)
|
|
{
|
|
var numSecAbsExp = fastCache ? chAbsExp : chAbsExp * 10;
|
|
var numSecSliExp = fastCache ? chSliExp : chSliExp * 10;
|
|
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp));
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |