789 lines
31 KiB
C#
789 lines
31 KiB
C#
using Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using MimeKit.Cryptography;
|
|
using MP.Data.Controllers;
|
|
using MP.Data.DbModels;
|
|
using MP.Data.Services;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using NLog.Fluent;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static MP.Data.Objects.Enums;
|
|
|
|
namespace MP.Stats.Data
|
|
{
|
|
public class MpStatsService : BaseServ, IDisposable
|
|
{
|
|
#region Public Fields
|
|
|
|
public static MP.Data.Controllers.MpStatsController dbController;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public MpStatsService(IConfiguration configuration)
|
|
{
|
|
Stopwatch sw = Stopwatch.StartNew();
|
|
sw.Start();
|
|
_configuration = configuration;
|
|
|
|
// setup compoenti REDIS
|
|
redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis"));
|
|
redisDb = redisConn.GetDatabase();
|
|
|
|
// conf DB
|
|
string connStr = _configuration.GetConnectionString("MP.Stats");
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
Log.Error("MP.Stats: ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new MP.Data.Controllers.MpStatsController(configuration);
|
|
InitDict();
|
|
sw.Stop();
|
|
Log.Info($"MpStatsService | MpStatsController OK | {sw.Elapsed.TotalMilliseconds} ms");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Events
|
|
|
|
/// <summary>
|
|
/// Evento richiesta rilettura dati pagina (x refresh pagine aperte)
|
|
/// </summary>
|
|
public event EventHandler ReloadRequest = delegate { };
|
|
|
|
#endregion Public Events
|
|
|
|
#region Public Methods
|
|
|
|
public async Task<List<AzioniUL>> ActionsGetAll()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<AzioniUL> result = new List<AzioniUL>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = $"{redisBaseKey}:Cache:AZIONI_ALL";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AzioniUL>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ActionsGetAll();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<AzioniUL>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ActionsGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<AnagFLTransModel>> AnagFLTransGetAll()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<AnagFLTransModel> result = new List<AnagFLTransModel>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = $"{redisBaseKey}:Cache:FLTrans";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagFLTransModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.AnagFLTransGetAll();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<AnagFLTransModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"AnagFLTransGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public Task<List<AutocompleteModel>> ArticoliGetSearch(int numRecord, string searchVal = "")
|
|
{
|
|
List<AutocompleteModel> answ = new List<AutocompleteModel>();
|
|
answ.Add(new AutocompleteModel { LabelField = "--- TUTTE ---", ValueField = "*" });
|
|
if (numRecord > -1)
|
|
{
|
|
answ.AddRange(dbController.ArticoliGetSearch(numRecord, searchVal).Select(x => new AutocompleteModel { LabelField = $"{x.CodArticolo} {x.DescArticolo} {x.Disegno}", ValueField = x.CodArticolo }).ToList());
|
|
}
|
|
return Task.FromResult(answ);
|
|
}
|
|
|
|
public async Task<List<AutocompleteModel>> ArticoliList(string searchVal)
|
|
{
|
|
List<AutocompleteModel> answ = new List<AutocompleteModel>();
|
|
answ.Add(new AutocompleteModel { LabelField = "--- TUTTE ---", ValueField = "*" });
|
|
var listMacchine = dbController.MacchineGetAll();
|
|
answ.AddRange(listMacchine.Select(x => new AutocompleteModel { LabelField = x.IdxMacchina, ValueField = x.IdxMacchina }).ToList());
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
public Task<List<AutocompleteModel>> AzioniList()
|
|
{
|
|
List<AutocompleteModel> answ = new List<AutocompleteModel>();
|
|
answ.Add(new AutocompleteModel { LabelField = "--- TUTTE ---", ValueField = "*" });
|
|
answ.AddRange(dbController.ActionsGetAll().Select(x => new AutocompleteModel { LabelField = $"{x.Descrizione}", ValueField = x.Azione }).ToList());
|
|
return Task.FromResult(answ);
|
|
}
|
|
|
|
public Task<List<AutocompleteModel>> CommesseGetSearch(int numRecord, string searchVal = "")
|
|
{
|
|
List<AutocompleteModel> answ = new List<AutocompleteModel>();
|
|
answ.Add(new AutocompleteModel { LabelField = "--- TUTTE ---", ValueField = "*" });
|
|
if (numRecord > -1)
|
|
{
|
|
answ.AddRange(dbController.CommesseGetSearch(numRecord, searchVal).GroupBy(x => x.KeyRichiesta).Select(x => new AutocompleteModel { LabelField = $"{x.First().CodArticolo} | {x.First().KeyRichiesta}", ValueField = x.First().KeyRichiesta }).ToList());
|
|
}
|
|
return Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero elenco config
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<ConfigModel>? result = new List<ConfigModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:Conf";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<ConfigModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ConfigGetAll();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSet(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<ConfigModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ConfigGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
// redis dispose
|
|
redisConn = null;
|
|
redisDb = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia cache Redis (tutta)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushCache()
|
|
{
|
|
RedisValue pattern = new RedisValue($"{redisBaseKey}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
|
|
// rileggo conf UM!
|
|
var rawData = await AnagFLTransGetAll();
|
|
DictFluxUm = rawData.ToDictionary(kvp => $"{kvp.CodFluxOut}", kvp => kvp.UM);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia cache Redis per chiave specifica (da redisBaseKey...)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushCache(string KeyReq)
|
|
{
|
|
RedisValue pattern = new RedisValue($"{redisBaseKey}:{KeyReq}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce UM del flusso configurato
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string FluxGetUM(string codFlux)
|
|
{
|
|
string answ = "#";
|
|
if (DictFluxUm.ContainsKey(codFlux))
|
|
{
|
|
answ = DictFluxUm[codFlux];
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public async Task<List<Macchine>> MacchineGetAll()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<Macchine> result = new List<Macchine>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = $"{redisBaseKey}:Cache:Macchine";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<Macchine>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.MacchineGetAll().ToList();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<Macchine>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"MacchineGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public Task<List<AutocompleteModel>> MachineList()
|
|
{
|
|
List<AutocompleteModel> answ = new List<AutocompleteModel>();
|
|
answ.Add(new AutocompleteModel { LabelField = "--- TUTTE ---", ValueField = "*" });
|
|
answ.AddRange(dbController
|
|
.MacchineGetAll()
|
|
.Select(x => new AutocompleteModel
|
|
{
|
|
LabelField = $"{x.IdxMacchina} | {x.Nome} {x.Descrizione} ",
|
|
ValueField = x.IdxMacchina
|
|
})
|
|
.ToList());
|
|
return Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invio notifica rilettura (con parametro)
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
public void NotifyReloadRequest(string message)
|
|
{
|
|
if (ReloadRequest != null)
|
|
{
|
|
// messaggio
|
|
ReloadEventArgs rea = new ReloadEventArgs(message);
|
|
ReloadRequest.Invoke(this, rea);
|
|
}
|
|
}
|
|
|
|
public void rollBackEdit(object item)
|
|
{
|
|
dbController.RollBackEntity(item);
|
|
}
|
|
|
|
public async Task<List<ResControlli>> StatControlliGetAll(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<ResControlli> result = new List<ResControlli>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKey($"{redisBaseKey}:CONTROLLI", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<ResControlli>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.StatControlliGetAll(CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxMacchina, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<ResControlli>();
|
|
}
|
|
// se necessario filtro..
|
|
if (!string.IsNullOrEmpty(searchVal))
|
|
{
|
|
result = result
|
|
.Where(x => x.Note.Contains(searchVal, StringComparison.InvariantCultureIgnoreCase))
|
|
.ToList();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatControlliGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<DdbTurni>> StatDdbGetAll(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<DdbTurni> result = new List<DdbTurni>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKeyPaged($"{redisBaseKey}:DDBT", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<DdbTurni>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.StatDdbGetAll(CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxMacchina, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo, CurrFilter.FirstRecord, CurrFilter.NumRecord);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<DdbTurni>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatDdbGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera TUTTI x export
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<DdbTurni>> StatDdbGetAllExport(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
//return Task.FromResult(dbController.StatDdbGetAll(numRecord, searchVal).ToArray());
|
|
List<DdbTurni> dbResult = new List<DdbTurni>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbController.StatDdbGetAll(CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxMacchina, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo, 1, 0);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura COMPLETA da DB + caching per StatDdbGetAllExport: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<int> StatDdbGetCount(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
int numRec = 0;
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKey($"{redisBaseKey}:DDBT-COUNT", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
numRec = JsonConvert.DeserializeObject<int>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
numRec = dbController.StatDdbGetCount(CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxMacchina, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(numRec);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatDdbGetCount | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return numRec;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Recupero dati FluxLog secondo filtro + richiesta raggruppamento
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<FLModel>> FluxLogRawData(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<FLModel> result = new List<FLModel>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKey($"{redisBaseKey}:FL_DATA", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<FLModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.FluxLogRawData(CurrFilter.IdxMacchina, CurrFilter.DateStart, CurrFilter.DateEnd);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<FLModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"FluxLogRawData | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Statistiche ODL
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<OdlEnergyModel>> StatOdlEnergyGetAll(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<OdlEnergyModel> result = new List<OdlEnergyModel>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKey($"{redisBaseKey}:ODL_ENERGY", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<OdlEnergyModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.StatOdlEnergyGetFilt(CurrFilter.IdxMacchina, CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<OdlEnergyModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatOdlEnergyGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Statistiche Energia x ODL
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<StatsODL>> StatOdlGetAll(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<StatsODL> result = new List<StatsODL>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKey($"{redisBaseKey}:ODL", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<StatsODL>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.StatOdlGetAll(CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxMacchina, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<StatsODL>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatOdlEnergyGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<ResScarti>> StatScartiGetAll(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<ResScarti> result = new List<ResScarti>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKey($"{redisBaseKey}:SCARTI:RAW", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<ResScarti>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.StatScartiGetAll(CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxMacchina, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<ResScarti>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatScartiGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<TurniOee>> StatTurniOeeGetAllAsync(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo, string searchVal = "")
|
|
{
|
|
return await Task.FromResult(dbController.StatTurniOeeGetAll(DataStart, DataEnd, IdxMacchina, IdxODL, KeyRichiesta, CodArticolo));
|
|
}
|
|
|
|
public async Task<List<TurniOee>> StatTurniOeeGetAllCached(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<TurniOee> result = new List<TurniOee>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKey($"{redisBaseKey}:OEE", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<TurniOee>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.StatTurniOeeGetAll(CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxMacchina, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<TurniOee>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatTurniOeeGetAllCached | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<UserActionLog>> StatUserLogGetAll(SelectData CurrFilter, string searchVal = "")
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<UserActionLog> result = new List<UserActionLog>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = getCacheKey($"{redisBaseKey}:USRACTLOG", CurrFilter);
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<UserActionLog>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.StatUserLogGetAll(CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.IdxMacchina, CurrFilter.IdxOdl, CurrFilter.KeyRichiesta, CurrFilter.CodArticolo);
|
|
// filtro x tipo azione "a mano" prima di salvare...
|
|
if (CurrFilter.Azione != "*")
|
|
{
|
|
result = result.Where(x => x.Azione == CurrFilter.Azione).ToList();
|
|
}
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<UserActionLog>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatUserLogGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero vocabolario
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<VocabolarioModel>> VocabolarioGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<VocabolarioModel> result = new List<VocabolarioModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:Vocabolario";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<VocabolarioModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.VocabolarioGetAll();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSet(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<VocabolarioModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"VocabolarioGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected static string connStringBBM = "";
|
|
|
|
protected static string connStringFatt = "";
|
|
|
|
/// <summary>
|
|
/// Vocabolario x recupero rapido traduzioni
|
|
/// </summary>
|
|
protected static Dictionary<string, string> DictFluxUm = new Dictionary<string, string>();
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
protected ConnectionMultiplexer redisConn = null!;
|
|
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
protected IDatabase redisDb = null!;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Methods
|
|
|
|
protected string getCacheKey(string TableName, SelectData CurrFilter)
|
|
{
|
|
string answ = $"{TableName}:M_{CurrFilter.IdxMacchina}:AZ_{CurrFilter.Azione}:ART_{CurrFilter.CodArticolo}:KR_{CurrFilter.KeyRichiesta}:O_{CurrFilter.IdxOdl}:D_{CurrFilter.DateStart:yyyyMMddHHmm}_{CurrFilter.DateEnd:yyyyMMddHHmm}";
|
|
return answ;
|
|
}
|
|
|
|
protected string getCacheKeyPaged(string TableName, SelectData CurrFilter)
|
|
{
|
|
string answ = $"{TableName}:M_{CurrFilter.IdxMacchina}:A_{CurrFilter.CodArticolo}:K_{CurrFilter.KeyRichiesta}:O_{CurrFilter.IdxOdl}:D_{CurrFilter.DateStart:yyMMddHHmm}_{CurrFilter.DateEnd:yyMMddHHmm}:R_{CurrFilter.FirstRecord}_{CurrFilter.FirstRecord + CurrFilter.NumRecord}";
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
private string redisBaseKey = "MP:STATS";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Inizializzazione dict vari
|
|
/// </summary>
|
|
private static void InitDict()
|
|
{
|
|
// inizializzo dizionario vocabolario
|
|
var rawData = dbController.AnagFLTransGetAll();
|
|
DictFluxUm = rawData.ToDictionary(kvp => $"{kvp.CodFluxOut}", kvp => kvp.UM);
|
|
}
|
|
|
|
/// <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 = 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;
|
|
}
|
|
}
|
|
// notifico update ai client in ascolto x reset cache
|
|
NotifyReloadRequest($"FlushRedisCache | {pattern}");
|
|
return answ;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |