Mike Phares a27686684d Added Hyphen
Dual write PDSF for Metrology Viewer
Version Error Message
Tests passed
net8.0
v2_52_0-Tests
v2_52_0-Tests
editorconfig
editorconfig
yml ec fix
yaml explicit contents
Delete File if Exists
dotnet_diagnostic
Removed Open Insight API IFX Directory
Removed Open Insight API IFX Directory from Save
CA1862 and GetWeekOfYear for WritePDSF
gitignore
cellInstanceVersion.EdaConnection.PortNumber
Added Climatec to Test.cs
NETFRAMEWORK
GetJobIdDirectory
Remove and
2024-05-16 12:30:40 -07:00

324 lines
17 KiB
C#

using Adaptation.Shared;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
namespace Adaptation.FileHandlers.OpenInsight;
public class FromIQS
{
#nullable enable
private static string GetCommandText(Logistics logistics, json.Description description, string dateTime, long? subGroupId)
{ // cSpell:disable
StringBuilder result = new();
_ = result
.AppendLine(" select iq.ev_count, iq.cl_count, iq.sl_count, iq.se_sgrp, iq.se_sgtm, iq.se_tsno, iq.td_test, iq.pr_name, iq.jd_name, iq.pl_name, iq.pd_name, iq.td_name, iq.se_val ")
.AppendLine(" from ( ")
.AppendLine(" select ")
.AppendLine(" se.f_sgrp se_sgrp, ")
.AppendLine(" se.f_sgtm se_sgtm, ")
.AppendLine(" se.f_tsno se_tsno, ")
.AppendLine(" se.f_val se_val, ")
.AppendLine(" pr.f_name pr_name, ")
.AppendLine(" jd.f_name jd_name, ")
.AppendLine(" pl.f_name pl_name, ")
.AppendLine(" pd.f_name pd_name, ")
.AppendLine(" td.f_test td_test, ")
.AppendLine(" td.f_name td_name, ")
.AppendLine(" (select count(cl.f_part) ")
.AppendLine(" from [spcepiworld].[dbo].[ctrl_lim] cl ")
.AppendLine(" where cl.f_part = pd.f_part ")
.AppendLine(" and cl.f_test = td.f_test ")
.AppendLine(" ) cl_count, ")
.AppendLine(" (select count(sl.f_part) ")
.AppendLine(" from [spcepiworld].[dbo].[spec_lim] sl ")
.AppendLine(" where sl.f_part = pd.f_part ")
.AppendLine(" and sl.f_test = td.f_test ")
.AppendLine(" ) sl_count, ")
.AppendLine(" (select count(ev.f_evnt) ")
.AppendLine(" from [spcepiworld].[dbo].[evnt_inf] ev ")
.AppendLine(" where ev.f_prcs = pr.f_prcs ")
.AppendLine(" and ev.f_part = pd.f_part ")
.AppendLine(" and ev.f_sgtm = se.f_sgtm ")
.AppendLine(" ) ev_count ")
.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].[job_dat] jd ")
.AppendLine(" on se.f_job = jd.f_job ")
.AppendLine(" join [spcepiworld].[dbo].[part_lot] pl ")
.AppendLine(" on se.f_lot = pl.f_lot ")
.AppendLine(" join [spcepiworld].[dbo].[part_dat] pd ")
.AppendLine(" on se.f_part = pd.f_part ")
.AppendLine(" join [spcepiworld].[dbo].[test_dat] td ")
.AppendLine(" on se.f_test = td.f_test ")
.AppendLine(" where se.f_flag = 0 ");
if (subGroupId is not null)
_ = result.Append(" and se.f_sgrp = ").Append(subGroupId).AppendLine(" ");
if (!string.IsNullOrEmpty(description.RDS))
_ = result.Append(" and pl.f_name = '").Append(description.RDS).AppendLine("' ");
_ = result
.Append(" and pr.f_name = '").Append(description.Reactor).AppendLine("' ")
.Append(" and pd.f_name = '").Append(description.PSN).AppendLine("' ")
.AppendLine(" and jd.f_name in ('SRP') ")
.Append(" and jd.f_name = '").Append(logistics.MesEntity).AppendLine("' ")
.Append(" and dateadd(HH, -7, (dateadd(SS, convert(bigint, se.f_sgtm), '19700101'))) = '").Append(dateTime).AppendLine("' ")
.AppendLine(" ) as iq ")
.AppendLine(" order by iq.ev_count desc, iq.cl_count desc, iq.sl_count desc, iq.se_sgrp, iq.se_tsno, iq.td_test ")
.AppendLine(" for json path ");
return result.ToString();
} // cSpell:restore
private static StringBuilder GetForJsonPath(string connectionString, string commandText)
{
StringBuilder stringBuilder = new();
using SqlConnection sqlConnection = new(connectionString);
sqlConnection.Open();
using SqlCommand sqlCommand = new(commandText, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
while (sqlDataReader.Read())
_ = stringBuilder.Append(sqlDataReader.GetString(0));
return stringBuilder;
}
internal static (long?, int?, string) GetCommandText(string connectionString, Logistics logistics, json.Description description, long breakAfter, long preWait)
{
string dateTime;
int? count = null;
string commandText;
long? result = null;
string dateFormat = json.Description.GetDateFormat();
if (DateTime.TryParseExact(description.Date, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTimeParsed))
dateTime = dateTimeParsed.ToString("yyyy-MM-dd HH:mm:ss");
else if (DateTime.TryParse(description.Date, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeParsed))
dateTime = dateTimeParsed.ToString("yyyy-MM-dd HH:mm:ss");
else
dateTime = logistics.DateTimeFromSequence.ToString("yyyy-MM-dd HH:mm:ss");
commandText = GetCommandText(logistics, description, dateTime, subGroupId: null);
for (short i = 0; i < short.MaxValue; i++)
{
if (DateTime.Now.Ticks > preWait)
break;
Thread.Sleep(100);
}
StringBuilder stringBuilder = new();
for (short z = 0; z < short.MaxValue; z++)
{
stringBuilder = GetForJsonPath(connectionString, commandText);
if (stringBuilder.Length > 0)
{
long postBreakAfter = DateTime.Now.AddSeconds(5).Ticks;
for (short y = 0; y < short.MaxValue; y++)
{
if (DateTime.Now.Ticks > postBreakAfter)
break;
Thread.Sleep(250);
}
stringBuilder = GetForJsonPath(connectionString, commandText);
break;
}
if (DateTime.Now.Ticks > breakAfter)
// throw new Exception($"After {breakAfterSeconds} seconds, didn't find sub group id!");
break;
Thread.Sleep(250);
}
if (stringBuilder.Length == 0)
commandText = stringBuilder.ToString();
else
{
JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(stringBuilder.ToString());
if (jsonElements is null || jsonElements.Length == 0 || jsonElements[0].ValueKind != JsonValueKind.Object)
commandText = stringBuilder.ToString();
else
{
JsonProperty[] jsonProperties = jsonElements[0].EnumerateObject().ToArray();
if (jsonProperties.Length == 0 || jsonProperties[3].Name != "se_sgrp" || !long.TryParse(jsonProperties[3].Value.ToString(), out long subGroupId))
commandText = stringBuilder.ToString();
else
{
result = subGroupId;
if (jsonProperties.Length != 0 && jsonProperties[0].Name == "ev_count" && int.TryParse(jsonProperties[0].Value.ToString(), out int evCount))
count = evCount;
}
}
}
return new(result, count, commandText);
}
private static string GetJson(Logistics logistics, string logisticLines, json.Description description)
{
string result;
StringBuilder stringBuilder = new();
var @object = new
{
description.MesEntity,
description.Employee,
description.Layer,
description.PSN,
description.RDS,
description.Reactor,
description.Recipe,
description.Zone,
logistics.DateTimeFromSequence.Ticks
};
string[] pair;
string safeValue;
string[] segments;
string serializerValue;
foreach (string line in logisticLines.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
{
segments = line.Split('\t');
if (segments.Length < 2)
continue;
segments = segments[1].Split(';');
_ = stringBuilder.Append('{');
foreach (string segment in segments)
{
pair = segment.Split('=');
if (pair.Length != 2 || pair[0].Length < 3)
continue;
serializerValue = JsonSerializer.Serialize(pair[1]);
safeValue = serializerValue.Substring(1, serializerValue.Length - 2);
_ = stringBuilder.Append('"').Append(pair[0].Substring(2)).Append('"').Append(':').Append('"').Append(safeValue).Append('"').Append(',');
}
if (stringBuilder.Length > 0)
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
_ = stringBuilder.Append('}').Append(',');
}
if (stringBuilder.Length > 0)
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
_ = stringBuilder.Append(']').Append('}');
_ = stringBuilder.Insert(0, ",\"Logistics\":[");
string json = JsonSerializer.Serialize(@object);
_ = stringBuilder.Insert(0, json.Substring(0, json.Length - 1));
JsonElement? jsonElement = JsonSerializer.Deserialize<JsonElement>(stringBuilder.ToString());
result = jsonElement is null ? "{}" : JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions { WriteIndented = true });
return result;
}
internal static void Save(string openInsightApiECDirectory, Logistics logistics, string reportFullPath, string logisticLines, json.Description description, string lines, long? subGroupId, string weekOfYear)
{
string checkFile;
string fileName = Path.GetFileName(reportFullPath);
string json = GetJson(logistics, logisticLines, description);
string? ecPathRoot = Path.GetPathRoot(openInsightApiECDirectory);
bool ecExists = ecPathRoot is not null && Directory.Exists(ecPathRoot);
string weekYear = $"{logistics.DateTimeFromSequence:yyyy}_Week_{weekOfYear}";
string ecDirectory = Path.Combine(openInsightApiECDirectory, weekYear, $"-{description.PSN}", $"-{description.Reactor}", $"-{description.RDS}", $"-{subGroupId}");
if (ecExists && !Directory.Exists(ecDirectory))
_ = Directory.CreateDirectory(ecDirectory);
checkFile = Path.Combine(ecDirectory, fileName);
if (ecExists && !File.Exists(checkFile))
File.Copy(reportFullPath, checkFile);
checkFile = Path.Combine(ecDirectory, $"{logistics.DateTimeFromSequence.Ticks}.txt");
if (ecExists && !File.Exists(checkFile))
File.WriteAllText(checkFile, lines);
checkFile = Path.Combine(ecDirectory, $"{logistics.DateTimeFromSequence.Ticks}.json");
if (ecExists && !File.Exists(checkFile))
File.WriteAllText(checkFile, json);
}
private static string GetCommandText(string[] iqsCopyValues)
{ // cSpell:disable
StringBuilder result = new();
if (iqsCopyValues.Length != 4)
throw new NotSupportedException();
string find = iqsCopyValues[1];
string replace = iqsCopyValues[3];
_ = result
.AppendLine(" select pd.f_name [Part Name], ")
.AppendLine(" null [Part Revision], ")
.Append(" '").Append(replace).AppendLine("' [Test Name], ")
.AppendLine(" null [Description], ")
.AppendLine(" null [Lot Number], ")
.AppendLine(" null [Job Name], ")
.AppendLine(" null [Process Name], ")
.AppendLine(" case when sl.f_url = 0 then null else sl.f_url end [Reasonable Limit (Upper)], ")
.AppendLine(" case when sl.f_url = 0 then 0 else 1 end [Alarm Reasonable Limit (Upper)], ")
.AppendLine(" case when sl.f_usl = 0 then null else sl.f_usl end [Specification Limit (Upper)], ")
.AppendLine(" case when sl.f_usl = 0 then 0 else 1 end [Alarm Specification Limit (Upper)], ")
.AppendLine(" case when sl.f_ugb = 0 then null else sl.f_ugb end [Warning Limit (Upper)], ")
.AppendLine(" case when sl.f_ugb = 0 then 0 else 1 end [Alarm Warning Limit (Upper)], ")
.AppendLine(" case when sl.f_tar = 0 then null else sl.f_tar end [Specification Limit (Target)], ")
.AppendLine(" case when sl.f_lgb = 0 then null else sl.f_lgb end [Warning Limit (Lower)], ")
.AppendLine(" case when sl.f_lgb = 0 then 0 else 1 end [Alarm Warning Limit (Lower)], ")
.AppendLine(" case when sl.f_lsl = 0 then null else sl.f_lsl end [Specification Limit (Lower)], ")
.AppendLine(" case when sl.f_lsl = 0 then 0 else 1 end [Alarm Specification Limit (Lower)], ")
.AppendLine(" case when sl.f_lrl = 0 then null else sl.f_lrl end [Reasonable Limit (Lower)], ")
.AppendLine(" case when sl.f_lrl = 0 then 0 else 1 end [Alarm Reasonable Limit (Lower)], ")
.AppendLine(" td.f_name [Original Test Name], ")
.AppendLine(" td.f_test [Test Id], ")
.AppendLine(" ( ")
.AppendLine(" select count(sl_b.f_spec) ")
.AppendLine(" from [spcepiworld].[dbo].[spec_lim] sl_b ")
.AppendLine(" join [spcepiworld].[dbo].[part_dat] pd_b ")
.AppendLine(" on sl_b.f_part = pd_b.f_part ")
.AppendLine(" join [spcepiworld].[dbo].[test_dat] td_b ")
.AppendLine(" on sl_b.f_test = td_b.f_test ")
.AppendLine(" where sl_b.f_prcs = 0 ")
.Append(" and td_b.f_name = '").Append(replace).AppendLine("' ")
.AppendLine(" and pd_b.f_name = pd.f_name ")
.AppendLine(" and sl_b.f_url = sl.f_url ")
.AppendLine(" and sl_b.f_usl = sl.f_usl ")
.AppendLine(" and sl_b.f_ugb = sl.f_ugb ")
.AppendLine(" and sl_b.f_tar = sl.f_tar ")
.AppendLine(" and sl_b.f_lgb = sl.f_lgb ")
.AppendLine(" and sl_b.f_lsl = sl.f_lsl ")
.AppendLine(" and sl_b.f_lrl = sl.f_lrl ")
.AppendLine(" group by sl_b.f_spec ")
.AppendLine(" ) count ")
.AppendLine(" from [spcepiworld].[dbo].[spec_lim] sl ")
.AppendLine(" join [spcepiworld].[dbo].[part_dat] pd ")
.AppendLine(" on sl.f_part = pd.f_part ")
.AppendLine(" join [spcepiworld].[dbo].[test_dat] td ")
.AppendLine(" on sl.f_test = td.f_test ")
.AppendLine(" where sl.f_prcs = 0 ")
.Append(" and td.f_name = '").Append(find).AppendLine("' ")
.AppendLine(" and isnull(( ")
.AppendLine(" select count(sl_b.f_spec) ")
.AppendLine(" from [spcepiworld].[dbo].[spec_lim] sl_b ")
.AppendLine(" join [spcepiworld].[dbo].[part_dat] pd_b ")
.AppendLine(" on sl_b.f_part = pd_b.f_part ")
.AppendLine(" join [spcepiworld].[dbo].[test_dat] td_b ")
.AppendLine(" on sl_b.f_test = td_b.f_test ")
.AppendLine(" where sl_b.f_prcs = 0 ")
.Append(" and td_b.f_name = '").Append(replace).AppendLine("' ")
.AppendLine(" and pd_b.f_name = pd.f_name ")
.AppendLine(" and sl_b.f_url = sl.f_url ")
.AppendLine(" and sl_b.f_usl = sl.f_usl ")
.AppendLine(" and sl_b.f_ugb = sl.f_ugb ")
.AppendLine(" and sl_b.f_tar = sl.f_tar ")
.AppendLine(" and sl_b.f_lgb = sl.f_lgb ")
.AppendLine(" and sl_b.f_lsl = sl.f_lsl ")
.AppendLine(" and sl_b.f_lrl = sl.f_lrl ")
.AppendLine(" group by sl_b.f_spec ")
.AppendLine(" ), 0) = 0 ")
.AppendLine(" for json path ");
return result.ToString();
} // cSpell:restore
internal static void SaveCopy(string fileConnectorConfigurationSourceFileLocation, string connectionString, string name, string[] iqsCopyValues)
{
string checkFile = Path.Combine(fileConnectorConfigurationSourceFileLocation, $"{name}.json");
if (!File.Exists(checkFile))
{
string commandText = GetCommandText(iqsCopyValues);
StringBuilder stringBuilder = GetForJsonPath(connectionString, commandText);
if (stringBuilder.Length != 0)
File.WriteAllText(checkFile, stringBuilder.ToString());
else
File.WriteAllText(Path.Combine(fileConnectorConfigurationSourceFileLocation, $"{name}.sql"), commandText);
}
}
#nullable disable
}