926 lines
36 KiB
C#
926 lines
36 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using MP.Data.DbModels;
|
|
using MP.Data.DbModels.Energy;
|
|
using MP.Data.Services;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.Stats.Data
|
|
{
|
|
public class MpStatsService : BaseServ
|
|
{
|
|
#region Public Fields
|
|
|
|
public static MP.Data.Controllers.MpStatsController dbController;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public MpStatsService(IConfiguration configuration, IConnectionMultiplexer redConn) : base(configuration, redConn)
|
|
{
|
|
Stopwatch sw = Stopwatch.StartNew();
|
|
sw.Start();
|
|
|
|
// 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;
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero dati FluxLog secondo filtro + richiesta raggruppamento
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <param name="fluxType"></param>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<FLModel>> FluxLogRawData(SelectData CurrFilter, string fluxType, 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:FT_{fluxType}", 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, fluxType);
|
|
// 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>
|
|
/// Elenco di tipi flusso disponibili
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<string>> FluxTypeList()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<string> result = new List<string>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = $"{redisBaseKey}:FTList";
|
|
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<string>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.FluxTypeList();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await _redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<string>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"FluxTypeList | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public List<MacchineStatModel> MacchineEnergy()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<MacchineStatModel> result = new List<MacchineStatModel>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = $"{redisBaseKey}:Cache:MacchineEnergy";
|
|
RedisValue rawData = _redisDb.StringGet(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<MacchineStatModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.MacchineEnergy();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
_redisDb.StringSet(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<MacchineStatModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"MacchineEnergy | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public List<MacchineModel> MacchineGetAll()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<MacchineModel> result = new List<MacchineModel>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = $"{redisBaseKey}:Cache:Macchine";
|
|
RedisValue rawData = _redisDb.StringGet(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<MacchineModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.MacchineGetAll();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
_redisDb.StringSet(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<MacchineModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"MacchineGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco check stato Macchine Energy
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<MaccEnergyCheckModel>> MacchineEnergyCheckGetAll()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<MaccEnergyCheckModel> result = new List<MaccEnergyCheckModel>();
|
|
// cerco in redis...
|
|
DateTime adesso = DateTime.Now;
|
|
string currKey = $"{redisBaseKey}:Cache:MacchineEnegyCheck";
|
|
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue && rawData.Length() > 2)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<MaccEnergyCheckModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = await dbController.MacchineEnergyCheckGetAllAsync();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await _redisDb.StringSetAsync(currKey, rawData, TimeSpan.FromSeconds(60));
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<MaccEnergyCheckModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"MacchineEnergyCheckGetAllAsync | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public Task<List<AutocompleteModel>> MachineList(bool onlyEnergy = false)
|
|
{
|
|
List<AutocompleteModel> answ = new List<AutocompleteModel>();
|
|
answ.Add(new AutocompleteModel { LabelField = "--- TUTTE ---", ValueField = "*" });
|
|
if (onlyEnergy)
|
|
{
|
|
answ.AddRange(dbController
|
|
.MacchineEnergy()
|
|
.Select(x => new AutocompleteModel
|
|
{
|
|
LabelField = $"{x.IdxMacchina} | {x.Nome} {x.Descrizione} ",
|
|
ValueField = x.IdxMacchina
|
|
})
|
|
.ToList());
|
|
}
|
|
else
|
|
{
|
|
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>
|
|
/// 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>();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Methods
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
// Free managed resources here
|
|
dbController.Dispose();
|
|
}
|
|
|
|
// Free unmanaged resources here
|
|
_disposed = true;
|
|
}
|
|
|
|
// Call base class implementation.
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
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 bool _disposed = false;
|
|
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 pat2Flush
|
|
/// </summary>
|
|
/// <param name="pat2Flush"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> ExecFlushRedisPattern(RedisValue pat2Flush)
|
|
{
|
|
bool answ = false;
|
|
var masterEndpoint = _redisConn.GetEndPoints()
|
|
.Where(ep => _redisConn.GetServer(ep).IsConnected && !_redisConn.GetServer(ep).IsReplica)
|
|
.FirstOrDefault();
|
|
|
|
// sepattern è "*" elimino intero DB...
|
|
if (masterEndpoint != null && (pat2Flush.Equals(new RedisValue("*")) || pat2Flush == RedisValue.Null))
|
|
{
|
|
_redisConn.GetServer(masterEndpoint).FlushDatabase(database: _redisDb.Database);
|
|
}
|
|
else
|
|
{
|
|
var server = _redisConn.GetServer(masterEndpoint);
|
|
var keys = server.Keys(database: _redisDb.Database, pattern: pat2Flush, pageSize: 1000);
|
|
|
|
var deleteTasks = new List<Task>();
|
|
foreach (var key in keys)
|
|
{
|
|
deleteTasks.Add(_redisDb.KeyDeleteAsync(key));
|
|
if (deleteTasks.Count >= 1000)
|
|
{
|
|
await Task.WhenAll(deleteTasks);
|
|
deleteTasks.Clear();
|
|
}
|
|
}
|
|
if (deleteTasks.Count > 0)
|
|
{
|
|
await Task.WhenAll(deleteTasks);
|
|
}
|
|
}
|
|
answ = true;
|
|
#if false
|
|
var listEndpoints = _redisConn.GetEndPoints();
|
|
foreach (var endPoint in listEndpoints)
|
|
{
|
|
//var server = _redisConnAdmin.GetServer(listEndpoints[0]);
|
|
var server = _redisConn.GetServer(endPoint);
|
|
if (server != null)
|
|
{
|
|
var keyList = server.Keys(_redisDb.Database, pattern);
|
|
foreach (var item in keyList)
|
|
{
|
|
await _redisDb.KeyDeleteAsync(item);
|
|
}
|
|
answ = true;
|
|
}
|
|
}
|
|
#endif
|
|
// notifico update ai client in ascolto x reset cache
|
|
NotifyReloadRequest($"FlushRedisCache | {pat2Flush}");
|
|
return answ;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |