493 lines
19 KiB
C#
493 lines
19 KiB
C#
using Egw.Core;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
using MP.Data.Conf;
|
|
using MP.Data.DatabaseModels;
|
|
using MP.INVE.Pages;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
|
|
namespace MP.INVE.Data
|
|
{
|
|
public class MiDataService : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public MiDataService(IConfiguration configuration, ILogger<MiDataService> logger)
|
|
{
|
|
_logger = logger;
|
|
_logger.LogInformation("Starting MiDataService INIT");
|
|
_configuration = configuration;
|
|
|
|
// setup compoenti REDIS
|
|
redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis"));
|
|
redisConnAdmin = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("RedisAdmin"));
|
|
redisDb = redisConn.GetDatabase();
|
|
|
|
// leggo cache lungo periodo
|
|
int.TryParse(_configuration.GetValue<string>("ServerConf:redisLongTimeCache"), out redisLongTimeCache);
|
|
|
|
_logger.LogInformation("Redis INIT");
|
|
|
|
// conf DB
|
|
string connStrData = _configuration.GetConnectionString("Mp.Data");
|
|
string connStrInve = _configuration.GetConnectionString("Mp.Inve");
|
|
if (string.IsNullOrEmpty(connStrData) || string.IsNullOrEmpty(connStrInve))
|
|
{
|
|
_logger.LogError($"DbController: ConnString empty! connStrData: {connStrData} | connStrInve: {connStrInve}");
|
|
}
|
|
else
|
|
{
|
|
dbController = new MP.Data.Controllers.MpInveController(configuration);
|
|
_logger.LogInformation("DbController OK");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public static MP.Data.Controllers.MpInveController dbController { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Dizionario dei tag configurati per IOB
|
|
/// </summary>
|
|
public Dictionary<string, List<TagData>> currTagConf { get; set; } = new Dictionary<string, List<TagData>>();
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
public string DeriptData(string encData)
|
|
{
|
|
return SteamCrypto.DecryptString(encData, passPhrase);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
public async Task<bool> InsertNewSessione(InventorySessionModel newSess)
|
|
{
|
|
bool result = false;
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
result = await dbController.InsertNewSessione(newSess);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{redisBaseAddr}{redisSessionBaseAddr}*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"InsertNewSessione | Id sessione: {newSess.InveSessID} | Id sessione: {newSess.Description} | {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
// aggiungo record al DB
|
|
}
|
|
public async Task<bool> deleteSessione(InventorySessionModel session)
|
|
{
|
|
bool result = false;
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
result = await dbController.deleteSessione(session);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{redisBaseAddr}{redisSessionBaseAddr}*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"deleteSessione | Id sessione: {session.InveSessID} | Id sessione: {session.Description} | {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
// elimino record dal DB
|
|
}
|
|
|
|
public async Task<bool> InsertNewMag(AnagMagModel newMag)
|
|
{
|
|
bool result = false;
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
result = await dbController.InsertNewMag(newMag);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{redisMagList}");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"InsertNewMag | Codice magazzino: {newMag.CodMag} | Descrizione magazzino: {newMag.DescMag} | {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
// aggiungo record al DB
|
|
}
|
|
public async Task<bool> UpdateMag(AnagMagModel Mag)
|
|
{
|
|
bool result = false;
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
result = await dbController.UpdateMag(Mag);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{redisMagList}");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"UpdateMag | Codice magazzino: {Mag.CodMag} | Descrizione magazzino: {Mag.DescMag} | {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
// aggiungo record al DB
|
|
}
|
|
public async Task<bool> DeleteMag(AnagMagModel Mag)
|
|
{
|
|
bool result = false;
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
result = await dbController.DeleteMag(Mag);
|
|
// elimino cache redis...
|
|
RedisValue pattern = new RedisValue($"{redisMagList}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"DeleteMag | Codice magazzino: {Mag.CodMag} | Descrizione magazzino: {Mag.DescMag} | {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public bool loginOperatore(int matrOpr, string authkey)
|
|
{
|
|
string source = "";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
AnagOperatoriModel? result = new AnagOperatoriModel();
|
|
bool answ = false;
|
|
// cerco in redis...
|
|
RedisValue rawData = redisDb.StringGet($"{redisElencoOperatori}:{matrOpr}:{authkey}");
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
|
|
result = JsonConvert.DeserializeObject<AnagOperatoriModel>($"{rawData}");
|
|
if (result != null)
|
|
{
|
|
answ = true;
|
|
source = "REDIS";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
answ = dbController.LoginOperatore(matrOpr, authkey);
|
|
if (answ)
|
|
{
|
|
result = ElencoOperatori().Where(x => (x.MatrOpr == matrOpr) && (x.authKey == authkey)).SingleOrDefault();
|
|
}
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSetAsync(redisOperatoreLogged, rawData, getRandTOut(redisLongTimeCache));
|
|
source = "DB";
|
|
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new AnagOperatoriModel();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"loginOperatore Read from {source}: {ts.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
public List<AnagOperatoriModel> ElencoOperatori()
|
|
{
|
|
string source = "";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<AnagOperatoriModel>? result = new List<AnagOperatoriModel>();
|
|
// cerco in redis...
|
|
RedisValue rawData = redisDb.StringGet(redisElencoOperatori);
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagOperatoriModel>>($"{rawData}");
|
|
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ElencoOperatori();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSetAsync(redisElencoOperatori, rawData, getRandTOut(redisLongTimeCache));
|
|
source = "DB";
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<AnagOperatoriModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ElencoOperatori Read from {source}: {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public List<AnagUdcModel> ElencoUDC()
|
|
{
|
|
string source = "";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<AnagUdcModel>? result = new List<AnagUdcModel>();
|
|
// cerco in redis...
|
|
RedisValue rawData = redisDb.StringGet(redisUdcBaseAddr);
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagUdcModel>>($"{rawData}");
|
|
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ElencoUdc();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSetAsync(redisUdcBaseAddr, rawData, getRandTOut(redisLongTimeCache));
|
|
source = "DB";
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<AnagUdcModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ElencoUDC Read from {source}: {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
public List<AnagLottoModel> ElencoLotti()
|
|
{
|
|
string source = "";
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<AnagLottoModel>? result = new List<AnagLottoModel>();
|
|
// cerco in redis...
|
|
RedisValue rawData = redisDb.StringGet(redisLottiBaseAddr);
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagLottoModel>>($"{rawData}");
|
|
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ElencoLotti();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSetAsync(redisLottiBaseAddr, rawData, getRandTOut(redisLongTimeCache));
|
|
source = "DB";
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<AnagLottoModel>();
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ElencoLotti Read from {source}: {ts.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public string EncriptData(string rawData)
|
|
{
|
|
return SteamCrypto.EncryptString(rawData, passPhrase);
|
|
}
|
|
|
|
public List<InventorySessionModel> InventSessCurrList()
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<InventorySessionModel> result = new List<InventorySessionModel>();
|
|
string source = "DB";
|
|
// cerco in redis...
|
|
RedisValue rawData = redisDb.StringGet(redisSessionCurrList);
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<InventorySessionModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.InventSessCurrList();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSetAsync(redisSessionCurrList, rawData, getRandTOut(redisLongTimeCache));
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"InventSessCurrList Read from {source}: {ts.TotalMilliseconds}ms");
|
|
if (result == null)
|
|
{
|
|
result = new List<InventorySessionModel>();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public List<AnagMagModel> ElencoMagazzini()
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<AnagMagModel> result = new List<AnagMagModel>();
|
|
string source = "DB";
|
|
// cerco in redis...
|
|
RedisValue rawData = redisDb.StringGet(redisMagList);
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagMagModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ElencoMagazzini();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSetAsync(redisMagList, rawData, getRandTOut(redisLongTimeCache));
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"ElencoMagazzini Read from {source}: {ts.TotalMilliseconds}ms");
|
|
if (result == null)
|
|
{
|
|
result = new List<AnagMagModel>();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public List<InventorySessionModel> InventSessHistList(DateTime dtStart, DateTime dtEnd)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<InventorySessionModel> result = new List<InventorySessionModel>();
|
|
string source = "DB";
|
|
// cerco in redis...
|
|
RedisValue rawData = redisDb.StringGet($"{redisSessionHistList}:{dtStart:yyyyMMdd_HHmmss}:{dtEnd:yyyyMMdd_HHmmss}");
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<InventorySessionModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.InventSessHistList(dtStart, dtEnd);
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSetAsync(redisSessionHistList, rawData, getRandTOut(redisLongTimeCache));
|
|
}
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Debug($"InventSessHistList Read from {source}: {ts.TotalMilliseconds}ms");
|
|
if (result == null)
|
|
{
|
|
result = new List<InventorySessionModel>();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected const string passPhrase = "7871D817-C71F-4DFC-BF12-00A95BE507B5";
|
|
|
|
protected Random rand = new Random();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Restituisce un timeout dai minuti richiesti + tempo random 1..60 sec
|
|
/// </summary>
|
|
/// <param name="stdMinutes"></param>
|
|
/// <returns></returns>
|
|
protected TimeSpan getRandTOut(int stdMinutes)
|
|
{
|
|
double rndValue = (double)stdMinutes + (double)rand.Next(1, 60) / 60;
|
|
return TimeSpan.FromMinutes(rndValue);
|
|
}
|
|
public async Task<bool> FlushRedisCache()
|
|
{
|
|
await Task.Delay(1);
|
|
RedisValue pattern = new RedisValue($"{redisBaseAddr}*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
#if false
|
|
|
|
public async Task<bool> FlushRedisCache(string keyPattern)
|
|
{
|
|
await Task.Delay(1);
|
|
RedisValue pattern = new RedisValue($"{keyPattern}*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// Esegue flush memoria redis dato pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
bool answ = false;
|
|
var listEndpoints = redisConnAdmin.GetEndPoints();
|
|
foreach (var endPoint in listEndpoints)
|
|
{
|
|
//var server = redisConnAdmin.GetServer(listEndpoints[0]);
|
|
var server = redisConnAdmin.GetServer(endPoint);
|
|
if (server != null)
|
|
{
|
|
var keyList = server.Keys(redisDb.Database, pattern);
|
|
foreach (var item in keyList)
|
|
{
|
|
await redisDb.KeyDeleteAsync(item);
|
|
}
|
|
// brutalmente rimuovo intero contenuto DB... DANGER
|
|
//await server.FlushDatabaseAsync();
|
|
answ = true;
|
|
}
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private const string redisBaseAddr = "MP:INVE";
|
|
private const string redisSessionBaseAddr = ":Session";
|
|
private const string redisOperatoriBaseAddr = ":Operatore";
|
|
private const string redisUdcBaseAddr = ":UDC";
|
|
private const string redisLottiBaseAddr = ":Lotti";
|
|
private const string redisElencoOperatori = redisBaseAddr + redisOperatoriBaseAddr + ":ListOperatori";
|
|
private const string redisOperatoreLogged = redisBaseAddr + redisOperatoriBaseAddr + ":OperatoreLogged";
|
|
private const string redisSessionCurrList = redisBaseAddr + redisSessionBaseAddr + ":ListSessHist";
|
|
private const string redisSessionHistList = redisBaseAddr + redisSessionBaseAddr + ":ListSessCurr";
|
|
private const string redisMagList = redisBaseAddr + ":ListMag";
|
|
private static IConfiguration _configuration = null!;
|
|
|
|
private static ILogger<MiDataService> _logger = null!;
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// Oggetto vocabolario x uso continuo traduzione
|
|
/// </summary>
|
|
private List<VocabolarioModel> ObjVocabolario = new List<VocabolarioModel>();
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
private ConnectionMultiplexer redisConn = null!;
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS modalità admin (ex flux dati)
|
|
/// </summary>
|
|
private ConnectionMultiplexer redisConnAdmin = null!;
|
|
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
private IDatabase redisDb = null!;
|
|
|
|
private int redisLongTimeCache = 5;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |