Files
laatzen/LaaProductionWeb/LaaProductionWeb.Blazor/Components/HeliumTester/Statistics.razor.cs
T
2025-03-17 16:03:24 +01:00

213 lines
7.4 KiB
C#

namespace LaaProductionWeb.Blazor.Components.HeliumTester
{
using LaaProductionWeb.Blazor.Components.HeliumTester.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public partial class Statistics : ComponentBase
{
private IJSObjectReference charts;
private OverallItems OverallItems { get; set; }
[Inject]
private IJSRuntime JSRuntime { get; set; }
[Inject]
private MatchCollectionModel MatchCollectionModel { get; set; }
[Inject]
private HeliumTesterService HeliumTesterService { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
this.charts = await this.JSRuntime.InvokeAsync<IJSObjectReference>("import", "./Components/HeliumTester/Statistics.razor.js");
await this.charts.InvokeVoidAsync("setUpCahrts");
}
}
private void MatchSelectionChanged()
=> this.InvokeAsync(() => this.OverallItems.UpdateOverallIdents());
private IEnumerable<OverallItem> GetLatestResultIdents(int? ident)
=> this.HeliumTesterService.GetLatestResultIdents(ident, this.MatchCollectionModel);
private void IdentsSelected(IEnumerable<OverallItem> overallItems)
{
if (this.charts != null)
{
this.InvokeAsync(async () =>
{
if (overallItems?.Any() == true)
{
await this.LoadHeliumLeackageCurvesAsync(overallItems);
await this.LoadHeliumPressureCurvesAsync(overallItems);
await this.LoadWaterPressureCurvesAsync(overallItems);
}
else
{
await this.charts.InvokeVoidAsync("clearCharts");
}
});
}
}
private async Task LoadHeliumLeackageCurvesAsync(IEnumerable<OverallItem> overallItems)
{
var chartOptionsHe = new ChartOptions();
var historiesHe = new List<KeyValuePair<int, List<object>>>();
var countHe = 0;
foreach (var overallItem in overallItems)
{
var historyHe = this.HeliumTesterService.GetHeHistory(overallItem.OverallId);
var _countHe = historyHe?.Count() ?? 0;
if (_countHe > 0)
{
var minHe = historyHe.Min(x => x.Time);
countHe = Math.Max(countHe, _countHe);
historiesHe.Add(new KeyValuePair<int, List<object>>(overallItem.PcbId, historyHe
.Select(h => (object)new
{
x = (h.Time - minHe).TotalSeconds,
y = h.LekageValue
})
.ToList()));
}
}
for (var i = 0; i < countHe; i++)
{
chartOptionsHe.Data.Labels.Add(i * 10);
}
foreach (var (pcb, history) in historiesHe)
{
chartOptionsHe.Data.DataSets.Add(new ChartDataset
{
Data = history,
Label = $"{pcb}"
});
}
var optionsHe = JsonConvert.SerializeObject(chartOptionsHe, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include,
Formatting = Formatting.Indented
});
await this.charts.InvokeVoidAsync("showHelChart", optionsHe);
}
private async Task LoadHeliumPressureCurvesAsync(IEnumerable<OverallItem> overallItems)
{
var chartOptionsHe = new ChartOptions();
var historiesHe = new List<KeyValuePair<int, List<object>>>();
var countHe = 0;
foreach (var overallItem in overallItems)
{
var historyHe = this.HeliumTesterService.GetHeHistory(overallItem.OverallId);
var _countHe = historyHe?.Count() ?? 0;
if (_countHe > 0)
{
var minHe = historyHe.Min(x => x.Time);
countHe = Math.Max(countHe, _countHe);
historiesHe.Add(new KeyValuePair<int, List<object>>(overallItem.PcbId, historyHe
.Select(h => (object)new
{
x = (h.Time - minHe).TotalSeconds,
y = h.HeliumPressure
})
.ToList()));
}
}
for (var i = 0; i < countHe; i++)
{
chartOptionsHe.Data.Labels.Add(i * 10);
}
foreach (var (pcb, history) in historiesHe)
{
chartOptionsHe.Data.DataSets.Add(new ChartDataset
{
Data = history,
Label = $"{pcb}"
});
}
var optionsHe = JsonConvert.SerializeObject(chartOptionsHe, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include,
Formatting = Formatting.Indented
});
await this.charts.InvokeVoidAsync("showHepChart", optionsHe);
}
private async Task LoadWaterPressureCurvesAsync(IEnumerable<OverallItem> overallItems)
{
var chartOptionsWa = new ChartOptions();
var historiesWa = new List<KeyValuePair<int, List<object>>>();
var countWa = 0;
foreach (var overallItem in overallItems)
{
var historyWa = this.HeliumTesterService.GetWaHistory(overallItem.OverallId);
var _countWa = historyWa?.Count() ?? 0;
if (_countWa > 0)
{
var minWa = historyWa.Min(x => x.Time);
countWa = Math.Max(countWa, _countWa);
historiesWa.Add(new KeyValuePair<int, List<object>>(overallItem.PcbId, historyWa
.Select(h => (object)new
{
x = (h.Time - minWa).TotalSeconds,
y = h.WaterPressure
})
.ToList()));
}
}
for (var i = 0; i < countWa; i++)
{
chartOptionsWa.Data.Labels.Add(i * 10);
}
foreach (var (pcb, history) in historiesWa)
{
chartOptionsWa.Data.DataSets.Add(new ChartDataset
{
Data = history,
Label = $"{pcb}"
});
}
var optionsWa = JsonConvert.SerializeObject(chartOptionsWa, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include,
Formatting = Formatting.Indented
});
await this.charts.InvokeVoidAsync("showWapChart", optionsWa);
}
}
}