From 72a0f4196b5741cdbe27d9f53e3181f4377a54fa Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Sat, 1 Aug 2026 22:42:57 +0300 Subject: [PATCH] Deploy: 2026-08-01 22:42:57 --- backend/test_round_robin_cli.php | 83 ++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 backend/test_round_robin_cli.php diff --git a/backend/test_round_robin_cli.php b/backend/test_round_robin_cli.php new file mode 100644 index 0000000..9efa1f3 --- /dev/null +++ b/backend/test_round_robin_cli.php @@ -0,0 +1,83 @@ + 'data', 'timestamp' => time()], 10); +$getVal = Cache::get($testKey); +Cache::delete($testKey); + +if ($setOk && is_array($getVal) && isset($getVal['test'])) { + echo " - Redis Server Status: ✅ CONNECTED & OPERATIONAL\n\n"; +} else { + echo " - Redis Server Status: ⚠️ DISCONNECTED (Using Database Fallback)\n\n"; +} + +// 2. Fetch Companies with Sessions +echo "2️⃣ Fetching WhatsApp Sessions from Database...\n"; +$companies = Database::select("SELECT DISTINCT company_id FROM whatsapp_sessions ORDER BY company_id ASC"); + +if (empty($companies)) { + echo " - ⚠️ No WhatsApp sessions found in database.\n"; + exit; +} + +foreach ($companies as $comp) { + $companyId = (int)$comp['company_id']; + echo " ----------------------------------------------------\n"; + echo " 🏬 Testing Company ID: {$companyId}\n"; + + // Clear cache first to test cold cache read + WhatsAppSession::clearSessionsCache($companyId); + + $allSessions = WhatsAppSession::findAllByCompany($companyId); + $connectedSessions = WhatsAppSession::getConnectedSessionsForCompany($companyId); + + echo " - Total Configured Sessions: " . count($allSessions) . "\n"; + echo " - Total Connected Sessions: " . count($connectedSessions) . "\n"; + + foreach ($allSessions as $s) { + $statusIcon = ($s['status'] === 'connected') ? '🟢' : '🔴'; + echo " [ID {$s['id']}] {$statusIcon} {$s['name']} ({$s['session_key']}) | Status: {$s['status']} | Phone: " . ($s['phone'] ?: 'N/A') . "\n"; + } + + echo "\n 3️⃣ Testing Round-Robin Selection (10 Requests Simulation)...\n"; + $history = []; + for ($req = 1; $req <= 10; $req++) { + $selected = WhatsAppSession::getRoundRobinSession($companyId); + $name = $selected['name'] ?? 'N/A'; + $key = $selected['session_key'] ?? 'N/A'; + $status = $selected['status'] ?? 'N/A'; + $history[] = $key; + echo " Request #{$req} ➔ Selected Session: {$name} ({$key}) [Status: {$status}]\n"; + } + + // Verify Distribution Metrics + $counts = array_count_values($history); + echo "\n 📊 Distribution Summary:\n"; + foreach ($counts as $sessionKey => $count) { + $pct = round(($count / 10) * 100, 1); + echo " - Session {$sessionKey}: {$count} times ({$pct}%)\n"; + } + echo " ----------------------------------------------------\n\n"; +} + +echo "====================================================\n"; +echo "✅ TEST COMPLETED SUCCESSFULLY!\n"; +echo "====================================================\n";