podstat-uptime.php 2.78 KB
Newer Older
dmorley's avatar
dmorley committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
<?php

/**
 * Show detailed pod stats.
 */

declare(strict_types=1);

use RedBeanPHP\R;

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

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/config.php';

define('PODUPTIME', microtime(true));

// Set up global DB connection.
R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true);
R::testConnection() || die('Error in DB connection');
R::usePartialBeans(true);

$sql = "
    SELECT
        to_char(date_checked, 'yyyy MM') AS yymm,
        count(*) AS total_checks,
        round(avg(online::INT),2)*100 AS uptime,
        round(avg(latency),2) * 1000 AS latency
    FROM checks
    WHERE domain = ?
    GROUP BY yymm
    ORDER BY yymm
    LIMIT 24
";

try {
    $totals = R::getAll($sql, [$_domain]);
} catch (\RedBeanPHP\RedException $e) {
    die('Error in SQL query: ' . $e->getMessage());
}
?>
<div class="chart-container mb-6 p-1" style="height:400px; width:700px">
    <canvas id="pod_chart_responses"></canvas>
</div>
<script>
    /**
     * 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.
     */
    new Chart(document.getElementById('pod_chart_responses'), {
        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",
                borderColor: "#2ecc71",
                backgroundColor: "#2ecc71",
                borderWidth: 4,
                pointHoverRadius: 6
            }, {
                data: <?php echo json_encode(array_column($totals, 'latency')); ?>,
                label: 'Latency ms',
                fill: false,
                yAxisID: "r1",
                borderColor: "#a93226",
                backgroundColor: "#a93226",
                borderWidth: 4,
                pointHoverRadius: 6,
                pointStyle: 'rect'
            }]
        },
        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
                    }
                }]
            }
        }
    });
</script>