#!/usr/bin/env php format('Y-m-d') !== $date) { echo "Error: Invalid date format. Expected Y-m-d (e.g., 2025-01-01) or 'today'/'yesterday'\n"; exit(1); } try { // Get database connection $dbHost = AppConfig::get('DB_HOST', 'localhost'); $dbName = AppConfig::get('DB_NAME', ''); $dbUser = AppConfig::get('DB_USER', ''); $dbPass = AppConfig::get('DB_PASS', ''); if (empty($dbName) || empty($dbUser)) { echo "Error: Database configuration not found in .env\n"; exit(1); } $db = Database::getConnection($dbHost, $dbName, $dbUser, $dbPass); echo "=== Test Hourly Summary API Response ===\n\n"; echo "Date: {$date}\n\n"; // Test service method (sama seperti yang dipanggil oleh API) $service = new HourlySummaryService($db); $data = $service->getHourlySummary($date); echo "1. Service Response:\n"; echo str_repeat("-", 80) . "\n"; echo "Labels count: " . count($data['labels']) . " (should be 24)\n"; echo "Total count series length: " . count($data['series']['total_count']) . " (should be 24)\n"; echo "Total amount series length: " . count($data['series']['total_amount']) . " (should be 24)\n\n"; echo "2. Data per Hour (non-zero only):\n"; echo str_repeat("-", 80) . "\n"; $hasData = false; $totalAmountSum = 0; $totalCountSum = 0; for ($hour = 0; $hour < 24; $hour++) { $count = $data['series']['total_count'][$hour]; $amount = $data['series']['total_amount'][$hour]; if ($count > 0 || $amount > 0) { $hasData = true; $totalAmountSum += $amount; $totalCountSum += $count; echo sprintf( " Hour %02d: Count = %d, Amount = %s\n", $hour, $count, number_format($amount, 0, ',', '.') ); } } if (!$hasData) { echo " ⚠️ Tidak ada data untuk tanggal tersebut\n"; } echo "\n"; echo "3. Summary:\n"; echo str_repeat("-", 80) . "\n"; echo "Total Count (sum all hours): " . number_format($totalCountSum, 0, ',', '.') . "\n"; echo "Total Amount (sum all hours): " . number_format($totalAmountSum, 0, ',', '.') . "\n\n"; // Verify dengan query langsung ke database echo "4. Database Verification:\n"; echo str_repeat("-", 80) . "\n"; $verifySql = " SELECT summary_hour, SUM(total_count) as total_count, SUM(total_amount) as total_amount FROM hourly_summary WHERE summary_date = ? GROUP BY summary_hour ORDER BY summary_hour ASC "; $verifyStmt = $db->prepare($verifySql); $verifyStmt->execute([$date]); $verifyResults = $verifyStmt->fetchAll(PDO::FETCH_ASSOC); $dbTotalAmount = 0; $dbTotalCount = 0; foreach ($verifyResults as $result) { $hour = (int) $result['summary_hour']; $count = (int) $result['total_count']; $amount = (int) $result['total_amount']; $dbTotalAmount += $amount; $dbTotalCount += $count; // Compare dengan service response $serviceCount = $data['series']['total_count'][$hour]; $serviceAmount = $data['series']['total_amount'][$hour]; $countMatch = ($count == $serviceCount) ? '✅' : '❌'; $amountMatch = ($amount == $serviceAmount) ? '✅' : '❌'; if ($count > 0 || $amount > 0) { echo sprintf( " Hour %02d: DB Count=%d %s Service Count=%d | DB Amount=%s %s Service Amount=%s\n", $hour, $count, $countMatch, $serviceCount, number_format($amount, 0, ',', '.'), $amountMatch, number_format($serviceAmount, 0, ',', '.') ); } } echo "\n"; echo "Database Total Amount: " . number_format($dbTotalAmount, 0, ',', '.') . "\n"; echo "Service Total Amount: " . number_format($totalAmountSum, 0, ',', '.') . "\n"; if ($dbTotalAmount == $totalAmountSum) { echo "✅ Total amount MATCH antara database dan service\n"; } else { echo "❌ Total amount TIDAK MATCH!\n"; echo " Difference: " . number_format(abs($dbTotalAmount - $totalAmountSum), 0, ',', '.') . "\n"; } echo "\n"; echo "5. JSON Response Format (first 3 hours):\n"; echo str_repeat("-", 80) . "\n"; $sampleData = [ 'labels' => array_slice($data['labels'], 0, 3), 'series' => [ 'total_count' => array_slice($data['series']['total_count'], 0, 3), 'total_amount' => array_slice($data['series']['total_amount'], 0, 3) ] ]; echo json_encode($sampleData, JSON_PRETTY_PRINT) . "\n"; echo "\n=== Test Complete ===\n"; } catch (Exception $e) { echo "Error: " . $e->getMessage() . "\n"; echo $e->getTraceAsString() . "\n"; exit(1); }