feat(Production): Complete production deployment infrastructure

- Add comprehensive health check system with multiple endpoints
- Add Prometheus metrics endpoint
- Add production logging configurations (5 strategies)
- Add complete deployment documentation suite:
  * QUICKSTART.md - 30-minute deployment guide
  * DEPLOYMENT_CHECKLIST.md - Printable verification checklist
  * DEPLOYMENT_WORKFLOW.md - Complete deployment lifecycle
  * PRODUCTION_DEPLOYMENT.md - Comprehensive technical reference
  * production-logging.md - Logging configuration guide
  * ANSIBLE_DEPLOYMENT.md - Infrastructure as Code automation
  * README.md - Navigation hub
  * DEPLOYMENT_SUMMARY.md - Executive summary
- Add deployment scripts and automation
- Add DEPLOYMENT_PLAN.md - Concrete plan for immediate deployment
- Update README with production-ready features

All production infrastructure is now complete and ready for deployment.
This commit is contained in:
2025-10-25 19:18:37 +02:00
parent caa85db796
commit fc3d7e6357
83016 changed files with 378904 additions and 20919 deletions

View File

@@ -0,0 +1,269 @@
<?php
declare(strict_types=1);
/**
* N+1 Detection Model Management Integration Example
*
* Demonstrates how the N+1 Detection ML system integrates with
* the ML Model Management System for versioning, monitoring, and auto-tuning.
*/
require_once __DIR__ . '/../vendor/autoload.php';
use App\Framework\Database\NPlusOneDetection\MachineLearning\NPlusOneModelAdapter;
use App\Framework\Database\NPlusOneDetection\QueryExecutionContext;
use App\Framework\MachineLearning\ModelManagement\ModelRegistry;
use App\Framework\MachineLearning\ModelManagement\ModelPerformanceMonitor;
use App\Framework\MachineLearning\ModelManagement\AutoTuningEngine;
use App\Framework\Core\ValueObjects\Version;
use App\Framework\Core\ValueObjects\Duration;
// ============================================================================
// Setup: Get services from DI container
// ============================================================================
$container = require __DIR__ . '/../bootstrap/container.php';
$adapter = $container->get(NPlusOneModelAdapter::class);
$registry = $container->get(ModelRegistry::class);
$performanceMonitor = $container->get(ModelPerformanceMonitor::class);
$autoTuning = $container->get(AutoTuningEngine::class);
echo "=== N+1 Detection Model Management Integration ===\n\n";
// ============================================================================
// Example 1: Model Registration
// ============================================================================
echo "1. Model Registration\n";
echo str_repeat('-', 60) . "\n";
// Register current N+1 detector model
$metadata = $adapter->registerCurrentModel();
echo "✓ Registered N+1 Detector Model\n";
echo " Name: {$metadata->modelName}\n";
echo " Version: {$metadata->version->toString()}\n";
echo " Type: {$metadata->modelType->value}\n";
echo " Configuration:\n";
foreach ($metadata->configuration as $key => $value) {
echo " - {$key}: " . json_encode($value) . "\n";
}
echo "\n";
// ============================================================================
// Example 2: Analysis with Automatic Tracking
// ============================================================================
echo "2. Analysis with Automatic Tracking\n";
echo str_repeat('-', 60) . "\n";
// Simulate query execution contexts
$contexts = [
// N+1 pattern detected
new QueryExecutionContext(
query: 'SELECT * FROM users WHERE id = ?',
bindParams: [1],
executionTime: Duration::fromMilliseconds(5),
stackTrace: ['UserRepository::find', 'UserController::show', 'index.php'],
contextData: [
'parent_query' => 'SELECT * FROM posts',
'query_count_in_request' => 15,
'similar_query_count' => 12
]
),
// Normal query (no N+1)
new QueryExecutionContext(
query: 'SELECT * FROM users',
bindParams: [],
executionTime: Duration::fromMilliseconds(25),
stackTrace: ['UserRepository::findAll', 'UserController::index', 'index.php'],
contextData: [
'query_count_in_request' => 3,
'similar_query_count' => 0
]
),
];
foreach ($contexts as $i => $context) {
echo "\nAnalyzing Query " . ($i + 1) . "...\n";
// Analyze with automatic tracking (ground truth provided for training)
$groundTruth = $i === 0; // First query is N+1, second is not
$result = $adapter->analyzeWithTracking($context, $groundTruth);
echo " Query: {$context->query}\n";
echo " Execution Time: {$context->executionTime->toMilliseconds()}ms\n";
echo " Detection Result:\n";
echo " - Success: " . ($result['success'] ? 'Yes' : 'No') . "\n";
echo " - Anomalies Found: " . count($result['anomalies']) . "\n";
echo " - Confidence: " . sprintf('%.2f', $result['overall_confidence']) . "\n";
echo " Tracking Info:\n";
echo " - Prediction: " . ($result['tracking']['prediction'] ? 'N+1 Detected' : 'Normal') . "\n";
echo " - Ground Truth: " . ($groundTruth ? 'N+1 Pattern' : 'Normal Query') . "\n";
echo " - Tracked: " . ($result['tracking']['tracked'] ? 'Yes' : 'No') . "\n";
}
echo "\n";
// ============================================================================
// Example 3: Real-Time Performance Monitoring
// ============================================================================
echo "3. Real-Time Performance Monitoring\n";
echo str_repeat('-', 60) . "\n";
// Get current performance metrics
$metrics = $adapter->getCurrentPerformanceMetrics();
echo "Performance Metrics (last 1 hour):\n";
echo " Total Predictions: {$metrics['total_predictions']}\n";
echo " Correct Predictions: {$metrics['correct_predictions']}\n";
echo " Accuracy: " . sprintf('%.4f', $metrics['accuracy']) . "\n";
echo " Precision: " . sprintf('%.4f', $metrics['precision']) . "\n";
echo " Recall: " . sprintf('%.4f', $metrics['recall']) . "\n";
echo " F1-Score: " . sprintf('%.4f', $metrics['f1_score']) . "\n";
echo " Average Confidence: " . sprintf('%.4f', $metrics['average_confidence']) . "\n";
echo "\n";
// Check for performance degradation
$degradationInfo = $adapter->checkPerformanceDegradation(thresholdPercent: 0.05);
echo "Performance Degradation Check:\n";
echo " Has Degraded: " . ($degradationInfo['has_degraded'] ? 'Yes' : 'No') . "\n";
echo " Baseline Accuracy: " . sprintf('%.4f', $degradationInfo['baseline_accuracy']) . "\n";
echo " Current Accuracy: " . sprintf('%.4f', $degradationInfo['current_accuracy']) . "\n";
echo " Degradation: " . sprintf('%.2f%%', $degradationInfo['degradation_percent']) . "\n";
echo "\nRecommendation: {$degradationInfo['recommendation']}\n\n";
// ============================================================================
// Example 4: Automatic Threshold Optimization
// ============================================================================
echo "4. Automatic Threshold Optimization\n";
echo str_repeat('-', 60) . "\n";
// Optimize confidence threshold for maximum F1-score
$optimizationResult = $autoTuning->optimizeThreshold(
modelName: 'n1-detector',
version: Version::fromString('1.0.0'),
metricToOptimize: 'f1_score',
thresholdRange: [0.5, 0.9],
step: 0.05,
timeWindow: Duration::fromHours(1)
);
echo "Threshold Optimization Results:\n";
echo " Current Threshold: " . sprintf('%.2f', $optimizationResult['current_threshold']) . "\n";
echo " Optimal Threshold: " . sprintf('%.2f', $optimizationResult['optimal_threshold']) . "\n";
echo " Current F1-Score: " . sprintf('%.4f', $optimizationResult['current_metric_value']) . "\n";
echo " Optimal F1-Score: " . sprintf('%.4f', $optimizationResult['optimal_metric_value']) . "\n";
echo " Improvement: " . sprintf('%+.2f%%', $optimizationResult['improvement_percent']) . "\n";
echo "\nRecommendation: {$optimizationResult['recommendation']}\n\n";
// ============================================================================
// Example 5: Adaptive Threshold Adjustment
// ============================================================================
echo "5. Adaptive Threshold Adjustment\n";
echo str_repeat('-', 60) . "\n";
// Automatically adjust threshold based on FP/FN rates
$adaptiveResult = $autoTuning->adaptiveThresholdAdjustment(
'n1-detector',
Version::fromString('1.0.0')
);
echo "Adaptive Adjustment Results:\n";
echo " Current Threshold: " . sprintf('%.2f', $adaptiveResult['current_threshold']) . "\n";
echo " Recommended Threshold: " . sprintf('%.2f', $adaptiveResult['recommended_threshold']) . "\n";
echo " Adjustment Reason: {$adaptiveResult['adjustment_reason']}\n";
echo "\nExpected Improvement:\n";
echo " Accuracy: " . sprintf('%+.4f', $adaptiveResult['expected_improvement']['accuracy']) . "\n";
echo " Precision: " . sprintf('%+.4f', $adaptiveResult['expected_improvement']['precision']) . "\n";
echo " Recall: " . sprintf('%+.4f', $adaptiveResult['expected_improvement']['recall']) . "\n";
echo "\n";
// ============================================================================
// Example 6: Model Configuration Update
// ============================================================================
echo "6. Model Configuration Update\n";
echo str_repeat('-', 60) . "\n";
// Update model configuration based on optimization results
if ($optimizationResult['improvement_percent'] > 5.0) {
echo "Applying optimized threshold...\n";
$newConfiguration = $metadata->configuration;
$newConfiguration['confidence_threshold'] = $optimizationResult['optimal_threshold'];
$adapter->updateConfiguration($newConfiguration);
echo "✓ Configuration Updated\n";
echo " New Threshold: " . sprintf('%.2f', $optimizationResult['optimal_threshold']) . "\n";
echo " Expected F1-Score Improvement: " . sprintf('%+.2f%%', $optimizationResult['improvement_percent']) . "\n";
} else {
echo "Current configuration is optimal. No update needed.\n";
}
echo "\n";
// ============================================================================
// Example 7: Production Deployment
// ============================================================================
echo "7. Production Deployment\n";
echo str_repeat('-', 60) . "\n";
// Deploy to production when performance is validated
if ($metrics['accuracy'] >= 0.90 && !$degradationInfo['has_degraded']) {
echo "Deploying N+1 Detector to production...\n";
$adapter->deployToProduction();
// Verify deployment
$deployedMetadata = $adapter->getModelMetadata();
echo "✓ Successfully deployed to production\n";
echo " Environment: {$deployedMetadata->environment}\n";
echo " Deployed At: {$deployedMetadata->deployedAt->format('Y-m-d H:i:s')}\n";
echo " Accuracy: " . sprintf('%.4f', $deployedMetadata->getAccuracy()) . "\n";
} else {
echo "⚠ Deployment criteria not met:\n";
if ($metrics['accuracy'] < 0.90) {
echo " - Accuracy too low: " . sprintf('%.4f', $metrics['accuracy']) . " (required: 0.90)\n";
}
if ($degradationInfo['has_degraded']) {
echo " - Performance degradation detected\n";
}
}
echo "\n";
// ============================================================================
// Example 8: Model Versioning
// ============================================================================
echo "8. Model Versioning\n";
echo str_repeat('-', 60) . "\n";
// List all versions of N+1 detector
$allVersions = $registry->getAll('n1-detector');
echo "N+1 Detector Versions:\n";
foreach ($allVersions as $versionMetadata) {
echo " - v{$versionMetadata->version->toString()}\n";
echo " Created: {$versionMetadata->createdAt->format('Y-m-d H:i:s')}\n";
if ($versionMetadata->deployedAt) {
echo " Deployed: {$versionMetadata->deployedAt->format('Y-m-d H:i:s')} ({$versionMetadata->environment})\n";
}
if ($versionMetadata->getAccuracy()) {
echo " Accuracy: " . sprintf('%.4f', $versionMetadata->getAccuracy()) . "\n";
}
}
echo "\n=== Integration Complete ===\n";