<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AWB Server Test</title>
<meta http-equiv="refresh" content="2"> <!-- Refresh halaman setiap 2 detik -->
<style>
    body {
        font-family: Arial, sans-serif;
        margin: 20px;
        padding: 0;
        background-color: #f2f2f2;
    }
    h1 {
        text-align: center;
    }
    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 20px;
    }
    table, th, td {
        border: 1px solid #ddd;
        padding: 10px;
        text-align: center;
    }
    th {
        background-color: #f2f2f2;
    }
    tr:nth-child(even) {
        background-color: #f9f9f9;
    }
    tr:hover {
        background-color: #f2f2f2;
    }
    h2, h3 {
        text-align: center;
        margin-top: 20px;
    }
    .metrics-container {
        display: flex;
        justify-content: space-around;
        margin-top: 20px;
    }
    .metric-box {
        padding: 10px;
        border: 1px solid #ddd;
        background-color: #fff;
        text-align: center;
        width: 200px;
    }
</style>
</head>
<body>
    <div class="metrics-container">
        <div class="metric-box">
            <h3>RAM Usage</h3>
            <p>70% Used</p>
            <div style="background-color: #4CAF50; height: 20px;"></div>
        </div>
        <div class="metric-box">
            <h3>Bandwidth</h3>
            <p>50 Mbps</p>
            <div style="background-color: #2196F3; height: 20px;"></div>
        </div>
        <div class="metric-box">
            <h3>CPU Load</h3>
            <p>30%</p>
            <div style="background-color: #f44336; height: 20px;"></div>
        </div>
    </div>

    <?php
    // Koneksi ke database
    $servername = "localhost";
    $username = "wiselie_nanda"; // Ganti dengan username database Anda
    $password = "Clemira123"; // Ganti dengan password database Anda
    $dbname = "wiselie_inventory"; // Ganti dengan nama database Anda

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Query untuk mengambil data AWB dengan beberapa kolom
    $sql = "SELECT id, resi, part_number, product_name, date_scan, quantity FROM awb";
    $result = $conn->query($sql);

    $totalDuration = 0;
    $totalCount = 0;
    $movingAverageWindow = 5; // Jumlah pengujian untuk moving average
    $durationHistory = []; // Array untuk menyimpan durasi pengujian terakhir

    // Hitung rata-rata durasi sebelum menampilkan tabel
    if ($result->num_rows > 0) {
        // Hitung moving average
        $movingAverage = 0;

        // Hitung rata-rata durasi
        while($row = $result->fetch_assoc()) {
            // Hitung waktu awal
            $startTime = microtime(true);

            // Hitung waktu akhir dan durasi
            $endTime = microtime(true);
            $duration = ($endTime - $startTime) * 1000; // Dalam milidetik

            // Tambahkan ke total durasi untuk menghitung rata-rata
            $totalDuration += $duration;
            $totalCount++;

            // Tambahkan durasi ke history
            array_push($durationHistory, $duration);

            // Jika history melebihi window, hapus yang paling awal
            if (count($durationHistory) > $movingAverageWindow) {
                array_shift($durationHistory);
            }

            // Hitung moving average
            $movingAverage = array_sum($durationHistory) / count($durationHistory);
        }

        // Tampilkan moving average di atas tabel data AWB
        echo "<h1>AWB Server Test</h1>";
        echo "<h2>Moving Average (last {$movingAverageWindow} tests): " . round($movingAverage, 2) . " milliseconds</h2>";

        // Tampilkan rata-rata durasi di bawah tabel data AWB
        if ($totalCount > 0) {
            $averageDuration = $totalDuration / $totalCount;
            echo "<h3>Average Duration: {$averageDuration} milliseconds</h3>";
        }

        // Mulai menampilkan tabel
        echo "<table>";
        echo "<tr><th>ID</th><th>Resi</th><th>Part Number</th><th>Product Name</th><th>Date Scan</th><th>Quantity</th><th>Duration</th></tr>";
        // Reset ulang query result untuk menampilkan data tabel
        $result->data_seek(0);
        
        while($row = $result->fetch_assoc()) {
            // Hitung waktu awal
            $startTime = microtime(true);

            // Menampilkan informasi AWB
            echo "<tr>";
            echo "<td>{$row['id']}</td>";
            echo "<td>{$row['resi']}</td>";
            echo "<td>{$row['part_number']}</td>";
            echo "<td>{$row['product_name']}</td>";
            echo "<td>{$row['date_scan']}</td>";
            echo "<td>{$row['quantity']}</td>";

            // Hitung waktu akhir dan durasi
            $endTime = microtime(true);
            $duration = ($endTime - $startTime) * 1000; // Dalam milidetik
            echo "<td>{$duration} milliseconds</td>";

            // Tambahkan durasi ke history
            array_push($durationHistory, $duration);

            // Jika history melebihi window, hapus yang paling awal
            if (count($durationHistory) > $movingAverageWindow) {
                array_shift($durationHistory);
            }

            // Hitung moving average
            $movingAverage = array_sum($durationHistory) / count($durationHistory);
            
            echo "</tr>";

            // Tambahkan ke total durasi untuk menghitung rata-rata
            $totalDuration += $duration;
            $totalCount++;

            // Jeda sebentar untuk menampilkan hasil per baris
            usleep(100000); // Jeda 100ms (opsional, untuk simulasi tampilan)
        }

        echo "</table>";

        // Tampilkan rata-rata durasi di bawah tabel data AWB
        if ($totalCount > 0) {
            $averageDuration = $totalDuration / $totalCount;
            echo "<h3>Average Duration: {$averageDuration} milliseconds</h3>";
        }
    } else {
        echo "<p>0 results</p>";
    }

    $conn->close();
    ?>
</body>
</html>
