Stichproben regeln

This commit is contained in:
Stoyan Zlatev
2025-01-31 11:21:23 +01:00
parent 6a310dc141
commit c3d5ea172d
20 changed files with 957 additions and 196 deletions
+17
View File
@@ -0,0 +1,17 @@
namespace Stichproben.Cordonel
{
internal class Stichprobe
{
public int Id { get; set; }
public int PcbId { get; set; }
public int Nummer { get; set; }
public int Prozentsatz { get; set; }
public bool IstStichprobe { get; set; }
public bool IstDicht { get; set; }
}
}
@@ -0,0 +1,21 @@
namespace Stichproben.Cordonel
{
public class StichprobeResult
{
internal StichprobeResult(bool shouldBeTested, int number, int rule, int count)
{
this.ShouldBeTested = shouldBeTested;
this.Rule = rule;
this.Number = number;
this.Count = count;
}
public int Rule { get; }
public int Count { get; }
public int Number { get; }
public bool ShouldBeTested { get; }
}
}
@@ -0,0 +1,68 @@
namespace Stichproben.Cordonel
{
public class Stichproben
{
private readonly StichprobenRules stichprobenRules;
private readonly StichprobenDbContext stichprobenDb;
public Stichproben(string assemblyLocation)
{
this.stichprobenRules = new StichprobenRules(assemblyLocation);
this.stichprobenDb = new StichprobenDbContext(assemblyLocation);
}
public StichprobeResult ShouldBePressureTested(int pcbId)
{
var previousStichprobe = this.stichprobenDb.GetLastOrDefault();
bool? pressureTestPassed = true;
if (previousStichprobe != null)
{
pressureTestPassed = this.stichprobenDb.PressureTestPassed(pcbId);
this.stichprobenDb.SetPressureTestPassed(previousStichprobe.Id, pressureTestPassed ?? false);
}
else
{
previousStichprobe = new Stichprobe();
}
var stichprobenRule = this.stichprobenRules.Min();
var currentStichprobe = new Stichprobe
{
PcbId = pcbId
};
if (pressureTestPassed != false)
{
stichprobenRule = this.stichprobenRules.FindRuleOrMin(previousStichprobe.Prozentsatz);
if (previousStichprobe.Nummer >= stichprobenRule.Count)
{
stichprobenRule = this.stichprobenRules.GetNext(previousStichprobe.Prozentsatz);
}
else if (previousStichprobe.Nummer != 0)
{
currentStichprobe.Nummer = previousStichprobe.Nummer + 1;
}
}
else
{
stichprobenRule = this.stichprobenRules.Max();
}
var percent = stichprobenRule.Percent;
var moduloDevider = 100 / percent;
currentStichprobe.Prozentsatz = percent;
currentStichprobe.IstStichprobe = currentStichprobe.Nummer % moduloDevider == 0;
if (currentStichprobe.Nummer == 0)
{
currentStichprobe.Nummer = 1;
}
this.stichprobenDb.AddNew(currentStichprobe);
return new StichprobeResult(currentStichprobe.IstStichprobe, currentStichprobe.Nummer, stichprobenRule.Percent, stichprobenRule.Count);
}
}
}
@@ -0,0 +1,84 @@
namespace Stichproben.Cordonel
{
using LaaPackages.SqlClient;
using System;
using System.Configuration;
internal class StichprobenDbContext
{
private readonly SqlConnection sqlConnection;
public StichprobenDbContext(string assemblyLocation)
{
var connectionString = ConfigurationManager
.OpenExeConfiguration(assemblyLocation)
.ConnectionStrings
.ConnectionStrings["auftrag"]
.ConnectionString;
this.sqlConnection = SqlConnection.CreateSqlConnection(connectionString, error =>
{
Console.WriteLine(error);
});
}
public void AddNew(Stichprobe stichprobe)
=> this.sqlConnection
.CreateCommand($@"
INSERT INTO Stichproben
( PcbId
, Nummer
, Prozentsatz
, IstStichprobe)
VALUES (@{nameof(stichprobe.PcbId)}
, @{nameof(stichprobe.Nummer)}
, @{nameof(stichprobe.Prozentsatz)}
, @{nameof(stichprobe.IstStichprobe)})")
.SetParameter(nameof(stichprobe.PcbId), stichprobe.PcbId)
.SetParameter(nameof(stichprobe.Nummer), stichprobe.Nummer)
.SetParameter(nameof(stichprobe.Prozentsatz), stichprobe.Prozentsatz)
.SetParameter(nameof(stichprobe.IstStichprobe), stichprobe.IstStichprobe)
.ExecuteNonQuery();
public Stichprobe GetLastOrDefault()
=> this.sqlConnection
.CreateCommand(@"
SELECT Id
, PcbId
, Nummer
, Prozentsatz
, IstStichprobe
, IstDicht
FROM Stichproben
ORDER BY Id DESC")
.FirstOrDefault(x => new Stichprobe
{
Id = x.GetValue<int>(0),
PcbId = x.GetValue<int>(1),
Nummer = x.GetValue<int>(2),
Prozentsatz = x.GetValue<int>(3),
IstStichprobe = x.GetValue<bool>(4),
IstDicht = x.GetValue<bool>(5),
});
public void SetPressureTestPassed(int id, bool pressureTestPassed)
=> this.sqlConnection
.CreateCommand($@"
UPDATE Stichproben
SET IstDicht = @{nameof(pressureTestPassed)}
WHERE Id = @{nameof(id)}")
.SetParameter(nameof(pressureTestPassed), pressureTestPassed)
.SetParameter(nameof(id), id)
.ExecuteNonQuery();
public bool? PressureTestPassed(int pcbId)
=> this.sqlConnection
.CreateCommand($@"
SELECT TOP 1 Dicht
FROM Druckpruefung
WHERE FabNr = @{nameof(pcbId)}
ORDER BY Pruefgang DESC")
.SetParameter(nameof(pcbId), pcbId)
.FirstOrDefault(x => x.GetValue<bool?>(0));
}
}
@@ -0,0 +1,9 @@
namespace Stichproben.Cordonel
{
internal class StichprobenRule
{
public int Percent { get; set; }
public int Count { get; set; }
}
}
@@ -0,0 +1,112 @@
namespace Stichproben.Cordonel
{
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
internal class StichprobenRules
{
private readonly List<StichprobenRule> rules;
public StichprobenRules(string assemblyLocation)
{
this.rules = new List<StichprobenRule>();
this.LoadOrCreateRules(assemblyLocation);
}
public void Add(int percent, int count)
{
var rule = new StichprobenRule
{
Percent = percent,
Count = count
};
var rulesCount = this.rules.Count;
var insertIndex = default(int);
for (; insertIndex < rulesCount; insertIndex++)
{
var current = this.rules[insertIndex];
if (percent >= current.Percent)
{
break;
}
}
this.rules.Insert(insertIndex, rule);
}
public StichprobenRule FindRuleOrMin(int percent)
=> this.rules.FirstOrDefault(x => x.Percent == percent)
?? this.rules.LastOrDefault();
public StichprobenRule GetNext(int percent)
{
var stichprobenRule = this.Min();
foreach (var rule in this.rules)
{
if (rule.Percent < percent)
{
stichprobenRule = rule;
break;
}
}
return stichprobenRule;
}
public StichprobenRule Min()
=> this.rules.LastOrDefault();
public StichprobenRule Max()
=> this.rules.FirstOrDefault();
private void LoadOrCreateRules(string assemblyLocation)
{
var configuration = ConfigurationManager.OpenExeConfiguration(assemblyLocation);
var settings = configuration.AppSettings.Settings;
var stichprobenRulesKeys = settings
.AllKeys
.Where(x => Regex.IsMatch($"{x}", $@"{nameof(Stichprobe)}\.(?<percent>\d+)"));
if (stichprobenRulesKeys.Any())
{
foreach (var key in stichprobenRulesKeys)
{
var tokens = key.Split(new [] { '.' }, StringSplitOptions.RemoveEmptyEntries);
if (!int.TryParse($"{tokens[1]}", out var percent))
{
percent = 5;
}
if (!int.TryParse($"{settings[key].Value}", out var count))
{
count = 100;
}
this.Add(percent, count);
}
}
else
{
settings.Add($"{nameof(Stichprobe)}.100", "25");
settings.Add($"{nameof(Stichprobe)}.50", "50");
settings.Add($"{nameof(Stichprobe)}.10", "100");
settings.Add($"{nameof(Stichprobe)}.5", "100");
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
this.LoadOrCreateRules(assemblyLocation);
}
}
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Stichproben")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Stichproben")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f69a79fa-548d-4e21-887e-01fabdad9e73")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+62
View File
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F69A79FA-548D-4E21-887E-01FABDAD9E73}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Stichproben</RootNamespace>
<AssemblyName>Stichproben</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>IDE0003;IDE0049;</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Cordonel\Stichprobe.cs" />
<Compile Include="Cordonel\Stichproben.cs" />
<Compile Include="Cordonel\StichprobenDbContext.cs" />
<Compile Include="Cordonel\StichprobenRule.cs" />
<Compile Include="Cordonel\StichprobenRules.cs" />
<Compile Include="Cordonel\StichprobeResult.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LaaPackages\LaaPackages.SqlClient\LaaPackages.SqlClient.csproj">
<Project>{F90B940C-9A83-496D-AAE0-11CD02CE0A22}</Project>
<Name>LaaPackages.SqlClient</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>