Files
mapo-core/MP.Stats/Pages/TrendAnalysis.razor.cs
T
2025-07-04 08:55:14 +02:00

253 lines
7.0 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Data.DbModels;
using MP.Data.Services;
using MP.Stats.Data;
using Org.BouncyCastle.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Stats.Pages
{
public partial class TrendAnalysis
{
#region Public Methods
public void Dispose()
{
MServ.EA_SearchUpdated -= OnSeachUpdated;
}
public async void OnSeachUpdated()
{
await InvokeAsync(() =>
{
Task task = UpdateData();
StateHasChanged();
});
}
#endregion Public Methods
#region Protected Fields
protected string CodFluxSel = "*";
protected string fileName = "TrendAnalysis.csv";
protected int totalCount = 0;
#endregion Protected Fields
#region Protected Properties
protected List<string> CodFluxList { get; set; } = new List<string>() { "Energy", "Parameter" };
[Inject]
protected IJSRuntime JSRuntime { get; set; }
/// <summary>
/// Numero max di punti da mostrare
/// </summary>
protected int MaxPoints
{
get => _maxPoints;
set
{
if ((_maxPoints != value))
{
_maxPoints = value;
FiltData();
}
}
}
[Inject]
protected Data.MessageService MServ { get; set; }
[Inject]
protected NavigationManager NavManager { get; set; }
[Inject]
protected MpStatsService StatService { get; set; }
[Inject]
protected TranslateSrv TradService { get; set; }
#endregion Protected Properties
#region Protected Methods
protected async Task DoFilter(SelectData newFilter)
{
SearchRecords = null;
currFilter = newFilter;
await ReloadData();
}
protected override async Task OnInitializedAsync()
{
MServ.ShowSearch = false;
MServ.PageName = "Trend Analisys";
MServ.PageIcon = "fa-solid fa-arrow-trend-up";
CalcFilt();
MServ.EA_SearchUpdated += OnSeachUpdated;
await LoadConfData();
await ReloadData();
}
protected async Task ResetFilter(SelectData newFilter)
{
SearchRecords = null;
CalcFilt();
await ReloadData();
}
/// <summary>
/// Salvataggio codflux
/// </summary>
/// <param name="newCodFlux"></param>
/// <returns></returns>
protected async Task SetCodFlux(string newCodFlux)
{
CodFluxSel = newCodFlux;
await ReloadData();
}
/// <summary>
/// Traduzione lemma richeisto (lingua default="IT")
/// </summary>
/// <param name="Lemma"></param>
/// <returns></returns>
protected string Traduci(string Lemma)
{
return TradService.Traduci(Lemma);
}
protected async Task UpdateData()
{
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private int _maxPoints = 256;
private List<ConfigModel> ConfigList;
private List<FLModel> PlotRecords;
private List<FLModel> SearchRecords;
protected ResolutionLevel ReqRes
{
get => _reqRes;
set
{
if (_reqRes != value)
{
_reqRes = value;
if (value < ResolutionLevel.Custom)
{
int scale = 256;
switch (value)
{
case ResolutionLevel.Low:
MaxPoints = (int)Math.Round(totalCount / 10 / (double)scale) * scale;
break;
case ResolutionLevel.Med:
MaxPoints = (int)Math.Round(totalCount / 3 / (double)scale) * scale;
break;
case ResolutionLevel.High:
MaxPoints = (int)Math.Round(totalCount / (double)scale) * scale;
break;
default:
break;
}
}
}
}
}
private ResolutionLevel _reqRes = ResolutionLevel.Low;
protected enum ResolutionLevel
{
Low,
Med,
High,
Custom
}
#endregion Private Fields
#region Private Properties
private SelectData currFilter
{
get
{
return MServ.ODL_Filter;
}
set
{
MServ.ODL_Filter = value;
}
}
private bool isLoading { get; set; } = false;
#endregion Private Properties
#region Private Methods
/// <summary>
/// Calcola il filtro alla data necessaria x arrivare al confronto x mese...
/// </summary>
private void CalcFilt()
{
DateTime oggi = DateTime.Now.Date;
DateTime inizio = new DateTime(oggi.Year, oggi.Month, 1).AddMonths(-1);
int numPrev = (int)oggi.Subtract(inizio).TotalDays;
currFilter = SelectData.Init(5, numPrev);
}
/// <summary>
/// Effettua filtro/downsample dei dati
/// </summary>
private void FiltData()
{
// in primis suddivido in un dizionario x ogni macchina...
PlotRecords = new List<FLModel>();
Dictionary<string, List<FLModel>> grpDict = SearchRecords
.GroupBy(o => o.IdxMacchina)
.ToDictionary(g => g.Key, g => g.ToList());
// per ogni valore recupero timeserie downsampled
foreach (var item in grpDict)
{
// ora effettuo deduplica valori per tenere un subset minore ed evitare problemi visualizzazione...
PlotRecords.AddRange(TimeSeriesUtils.DownsampleFluxModels(item.Value, MaxPoints));
}
}
private async Task LoadConfData()
{
ConfigList = await StatService.ConfigGetAll();
CodFluxList = await StatService.FluxTypeList();
}
private async Task ReloadData()
{
isLoading = true;
SearchRecords = await StatService.FluxLogRawData(currFilter, CodFluxSel, MServ.SearchVal);
totalCount = SearchRecords.Count;
FiltData();
isLoading = false;
}
#endregion Private Methods
}
}