810 lines
30 KiB
C#
810 lines
30 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.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis.Extensions.Core.Abstractions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LiMan.APi.Data
|
|
{
|
|
/// <summary>
|
|
/// Classe astrazione accesso dati
|
|
/// </summary>
|
|
public class ApiDataService : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
|
|
private static ILogger<ApiDataService> _logger;
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly IEmailSender _emailSender;
|
|
|
|
//private readonly IDistributedCache distributedCache;
|
|
private readonly IRedisCacheClient _redisCacheClient;
|
|
|
|
/// <summary>
|
|
/// Elenco obj in cache
|
|
/// </summary>
|
|
private List<string> cachedDataList = new List<string>();
|
|
|
|
/// <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 * 2;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Protected Fields
|
|
|
|
/// <summary>
|
|
/// TTL da 1 h x cache Redis
|
|
/// </summary>
|
|
protected const int hourTTL = 60 * 60;
|
|
|
|
/// <summary>
|
|
/// Chiave redis x attivazioni da IdxLic
|
|
/// </summary>
|
|
protected const string rKeyAttivByLic = "LiMan.UI:Licenze:AttByIdxLic";
|
|
|
|
/// <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>
|
|
/// TTL da 1 min x cache Redis
|
|
/// </summary>
|
|
protected const int shortTTL = 60 * 5;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Fields
|
|
|
|
/// <summary>
|
|
/// Classe Accesso metodi DB
|
|
/// </summary>
|
|
public static LiMan.DB.Controllers.DbController dbController;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="emailSender"></param>
|
|
/// <param name="redisCacheClient"></param>
|
|
public ApiDataService(IConfiguration configuration, ILogger<ApiDataService> logger, IEmailSender emailSender, IRedisCacheClient redisCacheClient)
|
|
//public ApiDataService(IConfiguration configuration, ILogger<ApiDataService> logger, IDistributedCache distributedCache, IEmailSender emailSender, IRedisCacheClient redisCacheClient)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_emailSender = emailSender;
|
|
_redisCacheClient = redisCacheClient;
|
|
//this.distributedCache = distributedCache;
|
|
|
|
// conf DB
|
|
string connStrDB = _configuration.GetConnectionString("LiMan.DB");
|
|
if (string.IsNullOrEmpty(connStrDB))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new LiMan.DB.Controllers.DbController(configuration);
|
|
_logger.LogInformation("DbController OK");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Parametri per generare opzioni cache
|
|
/// </summary>
|
|
/// <param name="multFact">Fattore di moltiplica cache (se 1 --> 2 e 5 min)</param>
|
|
/// <returns></returns>
|
|
private DistributedCacheEntryOptions cacheOpt(int multFact)
|
|
{
|
|
var numSecAbsExp = multFact <= 0 ? chAbsExp : chAbsExp * multFact;
|
|
var numSecSliExp = multFact <= 0 ? chSliExp : chSliExp * multFact;
|
|
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp));
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Recupera statistiche correnti
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected async Task<SampleStats> getCurrStats()
|
|
{
|
|
DateTime adesso = DateTime.Now;
|
|
SampleStats answ = new SampleStats()
|
|
{
|
|
Name = "ApiStats"
|
|
};
|
|
// in primis check data/ora prima/ultima scrittura del set... (2 date, lista chiavi gestite)
|
|
string rawData = await getRSV(rKeySampleStats);
|
|
if (rawData != null)
|
|
{
|
|
answ = JsonConvert.DeserializeObject<SampleStats>(rawData);
|
|
// aggiorno ultimo controllo e salvo...
|
|
answ.DtLast = adesso;
|
|
// salvo!
|
|
await setCurrStats(answ);
|
|
}
|
|
// controllo se scadute...
|
|
if (adesso.Subtract(answ.DtFirst).TotalMinutes > 60)
|
|
{
|
|
// se scaduto --> registrazione set sul DB (async), resettando i vari contatori...
|
|
bool salvato = await saveStatsToDb(answ.VList);
|
|
// inizio NUOVO set vuoto con record corrente
|
|
answ = new SampleStats()
|
|
{
|
|
Name = "ApiStats"
|
|
};
|
|
// salvo!
|
|
await setCurrStats(answ);
|
|
}
|
|
// restituisco record!
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero chiave da redis
|
|
/// </summary>
|
|
/// <param name="rKey"></param>
|
|
/// <returns></returns>
|
|
protected async Task<string> getRSV(string rKey)
|
|
{
|
|
string answ = await _redisCacheClient.GetDbFromConfiguration().GetAsync<string>(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>
|
|
/// Salva statistiche correnti
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected async Task<bool> setCurrStats(SampleStats newVal)
|
|
{
|
|
bool answ = false;
|
|
string rawData = JsonConvert.SerializeObject(newVal);
|
|
answ = await setRSV(rKeySampleStats, rawData, 24 * hourTTL);
|
|
return answ;
|
|
}
|
|
|
|
/// <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 _redisCacheClient.GetDbFromConfiguration().AddAsync(rKey, rVal, DateTimeOffset.Now.AddSeconds(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 _redisCacheClient.GetDbFromConfiguration().AddAsync<int>(rKey, rValInt, DateTimeOffset.Now.AddSeconds(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 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>> ApplicativiSearch(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);
|
|
}
|
|
|
|
/// <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>
|
|
/// 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, shortTTL);
|
|
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 InvalidateAllCache();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AttivazioniDelete: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
/// <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 InvalidateAllCache();
|
|
}
|
|
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>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<bool> AttivazioniTryAdd(string MasterKey, Dictionary<string, string> ParamDict, int DayVeto)
|
|
{
|
|
bool taskDone = false;
|
|
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
taskDone = dbController.AttivazioniTryAdd(MasterKey, ParamDict, DayVeto);
|
|
await InvalidateAllCache();
|
|
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 stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
taskDone = dbController.AttivazioniTryRefresh(MasterKey, ParamDict);
|
|
await InvalidateAllCache();
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata scrittura + rilettura da DB per AttivazioniTryRefresh: {ts.TotalMilliseconds} ms");
|
|
|
|
return await Task.FromResult(taskDone);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dispose classe
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue aggiunta file dato ticket e list uploadResult
|
|
/// </summary>
|
|
/// <param name="idxTicket">Identificativo del ticket</param>
|
|
/// <param name="baseDir">Directory di salvataggio dei file</param>
|
|
/// <param name="fileUploaded">lista risultati della funzione di upload</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> FileAdd(int idxTicket, string baseDir, List<UploadResult> fileUploaded)
|
|
{
|
|
bool fatto = false;
|
|
// inserimento!
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
fatto = dbController.FileAdd(idxTicket, baseDir, fileUploaded);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata inserimento con FileAdd: {ts.TotalMilliseconds} ms");
|
|
|
|
// restituisce elenco
|
|
return await Task.FromResult(fatto);
|
|
}
|
|
|
|
/// <summary>
|
|
/// invalida tutta la cache in caso di update
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task InvalidateAllCache()
|
|
{
|
|
foreach (var item in cachedDataList)
|
|
{
|
|
await _redisCacheClient.GetDbFromConfiguration().RemoveAsync(item);
|
|
}
|
|
cachedDataList = new List<string>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco licenze dato cliente
|
|
/// </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);
|
|
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>
|
|
/// 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 = dbController.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>
|
|
/// 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 InvalidateAllCache();
|
|
// 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 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua registrazione chiamata verificando se vada messa sul DB o in redis...
|
|
/// </summary>
|
|
/// <param name="codInst"></param>
|
|
/// <param name="codApp"></param>
|
|
/// <param name="targetUrl"></param>
|
|
public async Task<bool> recordCall(string codInst, string codApp, string targetUrl)
|
|
{
|
|
bool fatto = false;
|
|
|
|
// in primis recupero statistiche (e nel mentre eventualmente salvo su DB)
|
|
SampleStats currStats = await getCurrStats();
|
|
|
|
// preparo chiave x dato da loggare
|
|
string currKey = $"{rKeySampleVars}:{codInst}:{codApp}:{targetUrl}";
|
|
|
|
// verifico presenza contatore corrente altrimenti aggiungo e salvo...
|
|
if (!currStats.VList.Contains(currKey))
|
|
{
|
|
currStats.VList.Add(currKey);
|
|
// salvo!
|
|
await setCurrStats(currStats);
|
|
}
|
|
// incremento valore contatore corrente
|
|
await redCountIncr(currKey);
|
|
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua registrazione da codice chiave...
|
|
/// </summary>
|
|
/// <param name="chiave"></param>
|
|
/// <param name="targetUrl"></param>
|
|
public async Task<bool> recordCall(string chiave, string targetUrl)
|
|
{
|
|
// valutare se cache key --> lic...
|
|
var currLic = await LicenzaByMasterKey(chiave);
|
|
var fatto = await recordCall(currLic.CodInst, currLic.CodApp, targetUrl);
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registro su DB le statistiche delle chiavi in elenco, resettando i vari contatori quando fatto
|
|
/// </summary>
|
|
/// <param name="keyList">Elenco key nel formato {rKeySampleVars}:{codInst}:{codApp}:{targetUrl}</param>
|
|
public async Task<bool> saveStatsToDb(List<string> keyList)
|
|
{
|
|
bool fatto = false;
|
|
// ciclo x eseguire 1:1
|
|
foreach (var item in keyList)
|
|
{
|
|
// recupero counter...
|
|
var currCount = await redCount(item);
|
|
// scompongo key... senza url di base
|
|
string[] valStr = item.Replace($"{rKeySampleVars}:", "").Split(":");
|
|
if (valStr.Length > 2)
|
|
{
|
|
LogCallModel newRec = new LogCallModel()
|
|
{
|
|
CodInst = valStr[0],
|
|
CodApp = valStr[1],
|
|
TargetUrl = item.Replace($"{rKeySampleVars}:", "").Replace($"{valStr[0]}:{valStr[1]}:", ""),
|
|
DataRif = DateTime.Now,
|
|
NumCall = currCount
|
|
};
|
|
fatto = await dbController.LogCallUpsert(newRec);
|
|
if (fatto)
|
|
{
|
|
await redCountClear(item);
|
|
}
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invio email richiesta
|
|
/// </summary>
|
|
/// <param name="destEmail"></param>
|
|
/// <param name="oggetto"></param>
|
|
/// <param name="corpo"></param>
|
|
/// <returns></returns>
|
|
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>
|
|
/// Esegue aggiunta Ticket richeisto + restitusice aperti x cliente
|
|
/// </summary>
|
|
/// <param name="currRequest"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<bool> TicketAdd(SupportRequest currRequest)
|
|
{
|
|
bool fatto = false;
|
|
// inserimento!
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
fatto = dbController.TicketAddNew(currRequest);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata inserimento con TicketAdd: {ts.TotalMilliseconds} ms");
|
|
|
|
// restituisce elenco
|
|
return await Task.FromResult(fatto);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco ticket dato cliente + App + MasterKey
|
|
/// </summary>
|
|
/// <param name="CodInst"></param>
|
|
/// <param name="CodApp"></param>
|
|
/// <param name="MasterKey"></param>
|
|
/// <param name="numRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<TicketDTO>> TicketByCliente(string CodInst, string CodApp, string MasterKey, int numRec = 10)
|
|
{
|
|
List<TicketDTO> dbResult = new List<TicketDTO>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
|
|
dbResult = dbController.TicketGetFilt(true, TipologiaTicket.ND, CodApp, CodInst, MasterKey, numRec);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per TicketByCliente: {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 = dbController.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);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
}
|
|
} |