podstat-uptime.php 2.51 KB
Newer Older
David Morley's avatar
David Morley committed
1 2
<?php

3 4 5 6 7 8
/**
 * Show detailed pod stats.
 */

declare(strict_types=1);

9 10 11 12 13
use RedBeanPHP\R;

// Required parameters.
($_domain = $_GET['domain'] ?? null) || die('no domain given');

14
require_once __DIR__ . '/boot.php';
David Morley's avatar
David Morley committed
15

16
$sql = "
17 18 19
    SELECT
        to_char(date_checked, 'yyyy MM') AS yymm,
        count(*) AS total_checks,
David Morley's avatar
David Morley committed
20
        round(avg(online::INT),4)*100 AS uptime,
21
        round(avg(latency),2) * 1000 AS latency
22 23 24 25 26
    FROM checks
    WHERE domain = ?
    GROUP BY yymm
    ORDER BY yymm
    LIMIT 24
David Morley's avatar
David Morley committed
27
";
28 29

try {
30
    $totals = R::getAll($sql, [$_domain]);
31
} catch (\RedBeanPHP\RedException $e) {
32
    die('Error in SQL query: ' . $e->getMessage());
33
}
David Morley's avatar
David Morley committed
34
?>
35 36 37
<div class="chart-container mb-6 p-1" style="height:400px; width:700px">
    <canvas id="pod_chart_responses"></canvas>
</div>
David Morley's avatar
David Morley committed
38
<script>
39 40 41 42 43 44
    /**
     * Add a new chart for the passed data.
     *
     * @param id   HTML element ID to place the chart.
     * @param data Data to display on the chart.
     */
David Morley's avatar
David Morley committed
45
    new Chart(document.getElementById('pod_chart_responses'), {
46 47 48 49 50 51 52 53
        type: "line",
        data: {
            labels: <?php echo json_encode(array_column($totals, 'yymm')); ?>,
            datasets: [{
                data: <?php echo json_encode(array_column($totals, 'uptime')); ?>,
                label: 'Uptime %',
                fill: false,
                yAxisID: "l1",
54 55
                borderColor: "#A07614",
                backgroundColor: "#A07614)",
56 57 58 59 60
                borderWidth: 4,
                pointHoverRadius: 6
            }, {
                data: <?php echo json_encode(array_column($totals, 'latency')); ?>,
                label: 'Latency ms',
61
                fill: false,
62
                yAxisID: "r1",
63 64
                borderColor: "#4b6588",
                backgroundColor: "#4b6588",
65 66 67 68
                borderWidth: 4,
                pointHoverRadius: 6,
                pointStyle: 'rect'
            }]
David Morley's avatar
David Morley committed
69
        },
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        options: {
            responsive: true,
            maintainAspectRatio: true,
            scales: {
                yAxes: [{
                    position: "left",
                    "id": "l1",
                    ticks: {
                        min: 0,
                        max: 100,
                        stepSize: 50
                    }
                }, {
                    position: "right",
                    "id": "r1",
                    ticks: {
                        min: 0,
                        max: 500,
                        stepSize: 50
                    }
                }]
David Morley's avatar
David Morley committed
91 92 93 94
            }
        }
    });
</script>