Files
mapo-core/MP.Data/Services/Utils/StatsAggrService.cs
T
2026-05-04 17:19:28 +02:00

232 lines
8.3 KiB
C#

using EgwCoreLib.Utils;
using Microsoft.Extensions.Configuration;
using MP.Core.DTO;
using MP.Data.DbModels.Utils;
using MP.Data.DTO;
using MP.Data.Repository.Utils;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Data.Services.Utils
{
public class StatsAggrService : BaseServ, IStatsAggrService
{
#region Public Constructors
public StatsAggrService(
IConfiguration config,
IConnectionMultiplexer redis,
IStatsAggrRepository repo) : base(config, redis)
{
_className = "StatsAggr";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<List<StatsAggregatedModel>> GetFiltAsync(DateTime dtStart, DateTime dtEnd)
{
return await TraceAsync($"{_className}.GetFilt", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:DT:{dtStart:yyyyMMdd}:{dtEnd:yyyyMMdd}",
async () => await _repo.GetFiltAsync(dtStart, dtEnd),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<Dictionary<string, List<StatDataDTO>>> GetParetoStatsDayAsync(int numDay)
{
return await TraceAsync($"{_className}.GetParetoStatsDayAsync", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ParetoDay",
async () => await GetParetoMaccAsync(numDay),
LongCache
);
});
}
/// <inheritdoc />
public async Task<Dictionary<string, List<StatDataDTO>>> GetParetoStatsWeekAsync()
{
return await TraceAsync($"{_className}.GetParetoStatsWeekAsync", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ParetoWeek",
async () => await GetParetoDataAsync(),
LongCache
);
});
}
/// <inheritdoc />
public async Task<DtUtils.Periodo> GetRangeAsync()
{
return await TraceAsync($"{_className}.GetRange", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:Range",
async () => await _repo.GetRangeAsync(),
UltraFastCache
);
});
}
/// <inheritdoc />
public List<ChartSeriesDto> GetTimeSeriesData(List<StatsAggregatedModel> rawData, bool groupMach, bool getCount)
{
DateTime adesso = DateTime.Now;
List<ChartSeriesDto> series = new();
// a seconda della richiesta cambio il group by...
if (groupMach)
{
series = rawData
.GroupBy(s => new { s.Destination })
.Select(group => new ChartSeriesDto
{
SeriesName = group.Key.Destination,
DataPoints = group
.OrderBy(p => p.Hour)
.Select(p => new chartJsData.chartJsTSerie
{
x = p.Hour,
y = (getCount ? 1 : p.AvgDuration) * p.RequestCount / (p.Hour.Date.Equals(adesso.Date) ? adesso.TimeOfDay.TotalHours : 24)
})
.ToList()
})
//.OrderByDescending(c => c.DataPoints.Sum(x => x.y))
//.OrderBy(s => s.SeriesName)
.ToList();
}
else
{
series = rawData
.GroupBy(s => new { s.MachineId })
.Select(group => new ChartSeriesDto
{
SeriesName = group.Key.MachineId,
DataPoints = group
.OrderBy(p => p.Hour)
.Select(p => new chartJsData.chartJsTSerie
{
x = p.Hour,
y = (getCount ? 1 : p.AvgDuration) * p.RequestCount / (p.Hour.Date.Equals(adesso.Date) ? adesso.TimeOfDay.TotalHours : 24)
})
.ToList()
})
.OrderBy(s => s.SeriesName)
.ToList();
}
return series;
}
/// <inheritdoc />
public async Task<int> UpsertManyAsync(List<StatsAggregatedModel> 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 Protected Methods
/// <summary>
/// metodo locale per recupero e trasformazione dati da includere con processo generare di tracking & cache
/// </summary>
/// <returns></returns>
protected async Task<Dictionary<string, List<StatDataDTO>>> GetParetoDataAsync()
{
Dictionary<string, List<StatDataDTO>> result = new();
DateTime oggi = DateTime.Today;
int numDays = 7;
var rawData = await GetFiltAsync(oggi.AddDays(-numDays), oggi);
// calcolo le varie statistiche...
var pDestRequest = rawData.GroupBy(x => x.Destination)
.Select(g => new StatDataDTO
{
Label = g.Key,
Value = g.Sum(x => x.RequestCount) / numDays
})
.OrderByDescending(x => x.Value)
.ToList();
result.Add("Dest.Request (#)", pDestRequest);
var pDestDuration = rawData.GroupBy(x => x.Destination)
.Select(g => new StatDataDTO
{
Label = g.Key,
Value = g.Sum(x => x.RequestCount * x.AvgDuration) / numDays
})
.OrderByDescending(x => x.Value)
.ToList();
result.Add("Dest.Duration (ms)", pDestDuration);
return result;
}
protected async Task<Dictionary<string, List<StatDataDTO>>> GetParetoMaccAsync(int numDay)
{
Dictionary<string, List<StatDataDTO>> result = new();
DateTime adesso = DateTime.Now;
DateTime start = DateTime.Today.AddDays(-numDay);
var rawData = await GetFiltAsync(start, adesso);
var numHour = adesso.Subtract(start).TotalHours;
// calcolo le varie statistiche...
var pDestRequest = rawData.GroupBy(x => x.MachineId)
.Select(g => new StatDataDTO
{
Label = g.Key,
Value = g.Sum(x => x.RequestCount) / numHour
})
.OrderByDescending(x => x.Value)
.ToList();
result.Add("Mach.Request (#/h)", pDestRequest);
var pDestDuration = rawData.GroupBy(x => x.MachineId)
.Select(g => new StatDataDTO
{
Label = g.Key,
Value = g.Sum(x => x.RequestCount * x.AvgDuration) / numHour
})
.OrderByDescending(x => x.Value)
.ToList();
result.Add("Mach.Duration (avg ms/h)", pDestDuration);
return result;
}
#endregion Protected Methods
#region Private Fields
private readonly string _className;
private readonly IStatsAggrRepository _repo;
#endregion Private Fields
}
}