Delete Work Material

Added InfinityQSV3-GetEpiProTempVerification
Added nigth shift
This commit is contained in:
2023-11-13 09:44:37 -07:00
parent 03bd20fc8c
commit cff2e06f68
27 changed files with 419 additions and 769 deletions

View File

@ -1,6 +1,9 @@
using OI.Metrology.Server.Models;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models;
using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Shared.Repositories;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Common;
using System.Text;
@ -13,13 +16,17 @@ public class InfinityQSV3Repository : IInfinityQSV3Repository
private readonly string _MockRoot;
private readonly string _RepositoryName;
private readonly IHttpClientFactory _HttpClientFactory;
private readonly IDbConnectionFactory _DBConnectionFactory;
private readonly string _OpenInsightApplicationProgrammingInterface;
public InfinityQSV3Repository(string mockRoot, IDbConnectionFactory dbConnectionFactory)
public InfinityQSV3Repository(AppSettings appSettings, IDbConnectionFactory dbConnectionFactory, IHttpClientFactory httpClientFactory)
{
_MockRoot = mockRoot;
_MockRoot = appSettings.MockRoot;
_HttpClientFactory = httpClientFactory;
_DBConnectionFactory = dbConnectionFactory;
_RepositoryName = nameof(InfinityQSV3Repository)[..^10];
_OpenInsightApplicationProgrammingInterface = appSettings.OpenInsightApplicationProgrammingInterface;
}
string IInfinityQSV3Repository.GetCommandText(string subGroupId)
@ -318,4 +325,218 @@ public class InfinityQSV3Repository : IInfinityQSV3Repository
return result.ToString();
}
private JsonElement[] GetAllReactorsAsJsonElementElement()
{
JsonElement[]? results;
HttpClient httpClient = _HttpClientFactory.CreateClient();
Task<string> task = httpClient.GetStringAsync($"{_OpenInsightApplicationProgrammingInterface}/reactors");
task.Wait();
JsonElement? jsonElement = JsonSerializer.Deserialize<JsonElement>(task.Result);
if (jsonElement is null)
throw new NullReferenceException(nameof(jsonElement));
string json = jsonElement.Value.EnumerateObject().First().Value.ToString();
results = JsonSerializer.Deserialize<JsonElement[]>(json);
if (results is null)
throw new NullReferenceException(nameof(results));
return results;
}
private ReadOnlyDictionary<int, Reactor> GetReactorsMatchingType(string? reactTypeFilter)
{
Dictionary<int, Reactor> results = new();
string json;
Reactor? reactor;
JsonElement[]? jsonElements = GetAllReactorsAsJsonElementElement();
foreach (JsonElement jsonElement in jsonElements)
{
json = jsonElement.EnumerateObject().First().Value.ToString();
if (reactTypeFilter is not null && !json.Contains(reactTypeFilter))
continue;
try
{ reactor = JsonSerializer.Deserialize(json, ReactorSourceGenerationContext.Default.Reactor); }
catch (Exception) { reactor = null; }
if (reactor is null || reactor.ReactType != reactTypeFilter)
continue;
results.Add(reactor.ReactorNo, reactor);
}
return new(results);
}
string IInfinityQSV3Repository.GetCommandText(List<string> eppReactorNumbers)
{
StringBuilder result = new();
_ = result
.AppendLine(" select se.f_sgrp, ")
.AppendLine(" dateadd(HH, -7, (dateadd(SS, convert(bigint, se.f_sgrp), '19700101'))) date_time, ")
.AppendLine(" iq.pr_name, ")
.AppendLine(" iq.pd_name, ")
.AppendLine(" max(case ")
.AppendLine(" when td.f_test = 1104769646 ")
.AppendLine(" then se.f_val ")
.AppendLine(" else null ")
.AppendLine(" end) as iq_value, ")
.AppendLine(" max(case ")
.AppendLine(" when td.f_test = 1312288843 ")
.AppendLine(" then se.f_val else null ")
.AppendLine(" end) as iq_temp_offset_percent ")
.AppendLine(" from ( ")
.AppendLine(" select ")
.AppendLine(" max(se.f_sgrp) se_max_sgrp, ")
.AppendLine(" se.f_test se_test, ")
.AppendLine(" pr.f_name pr_name, ")
.AppendLine(" pd.f_name pd_name ")
.AppendLine(" from [spcepiworld].[dbo].[sgrp_ext] se ")
.AppendLine(" join [spcepiworld].[dbo].[prcs_dat] pr ")
.AppendLine(" on se.f_prcs = pr.f_prcs ")
.AppendLine(" join [spcepiworld].[dbo].[part_dat] pd ")
.AppendLine(" on se.f_part = pd.f_part ")
.AppendLine(" where se.f_flag = 0 ")
.Append(" and pr.f_name in (").Append(string.Join(',', eppReactorNumbers)).AppendLine(") ")
.AppendLine(" and pd.f_name = '1090 - Full Load' ")
.AppendLine(" and se.f_test in (1104769646, 1312288843) ")
.AppendLine(" group by se.f_test, ")
.AppendLine(" pr.f_name, ")
.AppendLine(" pd.f_name ")
.AppendLine(" ) as iq ")
.AppendLine(" join [spcepiworld].[dbo].[sgrp_ext] se ")
.AppendLine(" on iq.se_max_sgrp = se.f_sgrp ")
.AppendLine(" join [spcepiworld].[dbo].[test_dat] td ")
.AppendLine(" on iq.se_test = td.f_test ")
.AppendLine(" and se.f_test = td.f_test ")
.AppendLine(" where se.f_flag = 0 ")
.AppendLine(" and td.f_test in (1104769646, 1312288843) ")
.AppendLine(" group by se.f_sgrp, ")
.AppendLine(" iq.pr_name, ")
.AppendLine(" iq.pd_name ")
.AppendLine(" order by iq.pr_name ")
.AppendLine(" for json path; ");
return result.ToString();
}
private List<string> Convert(int[] night)
{
List<string> results = new();
foreach (int reactor in night)
results.Add(reactor.ToString());
return results;
}
private static void Append(StringBuilder result, int loadedCount, Reactor reactor, string loadedRDS, TimeSpan timeSpan, InfinityQS1090FullLoad infinityQS1090FullLoad)
{
_ = result.
Append("<td>").Append(reactor.ReactorNo).Append("</td>").
Append("<td class='").Append(reactor.E10State).Append("'>").Append(reactor.E10State).Append("</td>").
Append("<td class='LoadedRDSCount").Append(loadedCount).Append("'>").Append(loadedRDS).Append("</td>").
Append("<td>").Append(infinityQS1090FullLoad.Value).Append("</td>").
Append("<td>").Append(infinityQS1090FullLoad.TemperatureOffsetPercentage).Append("</td>").
Append("<td>").Append(infinityQS1090FullLoad.SubGroupIdFormated).Append("</td>").
Append("<td>").Append(Math.Floor(timeSpan.TotalHours)).Append("</td>");
}
private static void Append(StringBuilder result)
{
_ = result.
Append("<td>&nbsp;</td>").
Append("<td>&nbsp;</td>").
Append("<td>&nbsp;</td>").
Append("<td>&nbsp;</td>").
Append("<td>&nbsp;</td>").
Append("<td>&nbsp;</td>").
Append("<td>&nbsp;</td>").
Append("<td>&nbsp;</td>");
}
string IInfinityQSV3Repository.GetEpiProTempVerification(int[] night)
{
StringBuilder result;
List<string> eppReactorNumbers = new();
List<string> nightSiftReactors = Convert(night);
ReadOnlyDictionary<int, Reactor> eppReactors = GetReactorsMatchingType("EPP");
foreach (KeyValuePair<int, Reactor> keyValuePair in eppReactors)
eppReactorNumbers.Add($"'{keyValuePair.Key}'");
if (!string.IsNullOrEmpty(_MockRoot))
{
string html = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IInfinityQSV3Repository.GetEpiProTempVerification)}.html"));
result = new(html);
}
else
{
bool found;
result = new();
int loadedCount;
Reactor? reactor;
string loadedRDS;
int reactorNumber;
TimeSpan timeSpan;
List<int> used = new();
List<int> dayShiftOrder = new();
long ticks = DateTime.Now.Ticks;
List<int> nightShiftOrder = new();
InfinityQS1090FullLoad infinityQS1090FullLoad;
IInfinityQSV3Repository infinityQSV3Repository = this;
string commandText = infinityQSV3Repository.GetCommandText(eppReactorNumbers);
StringBuilder stringBuilder = GetForJsonPath(_DBConnectionFactory, commandText);
InfinityQS1090FullLoad[]? results = stringBuilder.Length == 0 ? Array.Empty<InfinityQS1090FullLoad>() : JsonSerializer.Deserialize(stringBuilder.ToString(), InfinityQS1090FullLoadArraySourceGenerationContext.Default.InfinityQS1090FullLoadArray);
if (results is null)
throw new NullReferenceException(nameof(results));
for (int i = 0; i < results.Length; i++)
{
infinityQS1090FullLoad = results[i];
if (infinityQS1090FullLoad.Reactor is null)
continue;
if (!int.TryParse(infinityQS1090FullLoad.Reactor, out reactorNumber))
continue;
if (!eppReactors.TryGetValue(reactorNumber, out reactor))
continue;
if (!nightSiftReactors.Contains(infinityQS1090FullLoad.Reactor))
dayShiftOrder.Add(i);
else
nightShiftOrder.Add(i);
}
for (int i = 0; i < results.Length; i++)
{
if (used.Contains(i) || !dayShiftOrder.Contains(i))
continue;
infinityQS1090FullLoad = results[i];
if (infinityQS1090FullLoad.Reactor is null)
continue;
if (!int.TryParse(infinityQS1090FullLoad.Reactor, out reactorNumber))
continue;
if (!eppReactors.TryGetValue(reactorNumber, out reactor))
continue;
used.Add(i);
found = false;
timeSpan = new(ticks - infinityQS1090FullLoad.SubGroupIdFormated.Ticks);
loadedCount = reactor.LoadedRDS is null ? 0 : reactor.LoadedRDS.Count;
loadedRDS = reactor.LoadedRDS is null ? "" : reactor.LoadedRDS[0].ToString();
_ = result.Append("<tr>");
Append(result, loadedCount, reactor, loadedRDS, timeSpan, infinityQS1090FullLoad);
for (int j = i + 1; j < results.Length; j++)
{
if (used.Contains(j) || !nightShiftOrder.Contains(j))
continue;
infinityQS1090FullLoad = results[j];
if (infinityQS1090FullLoad.Reactor is null)
continue;
if (!int.TryParse(infinityQS1090FullLoad.Reactor, out reactorNumber))
continue;
if (!eppReactors.TryGetValue(reactorNumber, out reactor))
continue;
used.Add(j);
found = true;
_ = result.Append("<td>&nbsp;</td>");
timeSpan = new(ticks - infinityQS1090FullLoad.SubGroupIdFormated.Ticks);
loadedCount = reactor.LoadedRDS is null ? 0 : reactor.LoadedRDS.Count;
loadedRDS = reactor.LoadedRDS is null ? "" : reactor.LoadedRDS[0].ToString();
Append(result, loadedCount, reactor, loadedRDS, timeSpan, infinityQS1090FullLoad);
break;
}
if (!found)
Append(result);
_ = result.Append("</tr>");
}
}
return result.ToString();
}
}