Files
lux/EgwCoreLib.Lux.Data/Services/Stats/StatsDetailService.cs
T
2026-03-21 11:33:49 +01:00

97 lines
3.3 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Stats;
using EgwCoreLib.Lux.Data.Repository.Stats;
using EgwCoreLib.Utils;
using Microsoft.Extensions.Configuration;
using StackExchange.Redis;
namespace EgwCoreLib.Lux.Data.Services.Stats
{
public class StatsDetailService : BaseServ, IStatsDetailService
{
#region Public Constructors
public StatsDetailService(
IConfiguration config,
IConnectionMultiplexer redis,
IStatsDetailRepository repo) : base(config, redis)
{
_className = "StatsDetail";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Recupera dati stats di dettaglio dato filtro envir/tipo (opzionali) e periodo
/// </summary>
/// <param name="dtStart"></param>
/// <param name="dtEnd"></param>
/// <param name="sEnvir"></param>
/// <param name="sType"></param>
/// <returns></returns>
public async Task<List<StatsDetailModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd, string sEnvir = "", string sType = "")
{
return await TraceAsync($"{_className}.GetFilt", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:DT:{dtStart:yyyyMMdd}:{dtEnd:yyyyMMdd}",
async () => await _repo.GetFiltAsync(dtStart, dtEnd),
UltraLongCache
);
});
}
/// <summary>
/// Range periodo x chiamate detail eventualmente filtrate
/// </summary>
/// <param name="sEnvir"></param>
/// <param name="sType"></param>
/// <returns></returns>
public async Task<DtUtils.Periodo> GetRangeAsync(string sEnvir, string sType)
{
return await TraceAsync($"{_className}.GetRange", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:Range:{sEnvir}:{sType}",
async () => await _repo.GetRangeAsync(sEnvir, sType),
UltraFastCache
);
});
}
/// <summary>
/// Esegue insert statistiche di dettaglio sul DB
/// </summary>
/// <param name="listRecords">Elenco dei record da inserire</param>
/// <param name="removeOld">Se true preventivamente elimina record nel periodo richiesto</param>
/// <returns></returns>
public async Task<int> UpsertManyAsync(List<StatsDetailModel> listRecords, bool removeOld)
{
return await TraceAsync($"{_className}.UpsertMany", async (activity) =>
{
string operation = "UpsertMany";
var success = await _repo.UpsertManyAsync(listRecords, removeOld);
activity?.SetTag("db.operation", operation);
if (success > 0)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly IStatsDetailRepository _repo;
#endregion Private Fields
}
}