statsviewjs.php 3.08 KB
Newer Older
David Morley's avatar
David Morley committed
1 2
<?php

3 4 5 6 7 8 9
/**
 * Include JS for stats view.
 */

declare(strict_types=1);

use Poduptime\PodStatus;
10 11
use RedBeanPHP\R;

12
require_once __DIR__ . '/boot.php';
13 14

try {
15 16 17 18 19 20 21 22
    $totals = R::getAll('
        SELECT
            softwarename,
            count(*) AS pods,
            sum(total_users) AS users,
            round(avg(uptime_alltime),2) AS uptime
        FROM pods
        WHERE status < ?
David Morley's avatar
David Morley committed
23
        AND score > 0
24 25
        GROUP BY softwarename
        ORDER BY softwarename
David Morley's avatar
David Morley committed
26
    ', [PodStatus::PAUSED]);
27
} catch (\RedBeanPHP\RedException $e) {
28
    die('Error in SQL query: ' . $e->getMessage());
29
}
David Morley's avatar
David Morley committed
30

David Morley's avatar
David Morley committed
31
try {
32 33 34 35 36 37 38 39
    $check_totals = R::getAll("
        SELECT
            to_char(date_checked, 'yyyy-mm') AS yymm,
            total_users AS users
        FROM monthlystats
        GROUP BY yymm, users
        ORDER BY yymm
    ");
David Morley's avatar
David Morley committed
40
} catch (\RedBeanPHP\RedException $e) {
41
    die('Error in SQL query: ' . $e->getMessage());
David Morley's avatar
David Morley committed
42 43
}

David Morley's avatar
David Morley committed
44 45
?>
<script>
46
    /**
47
     * Add a new pie chart for the passed data.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
     *
     * @param id   HTML element ID to place the chart.
     * @param data Data to display on the chart.
     */
    function addPieChart(id, data) {
        new Chart(document.getElementById(id), {
            type: "pie",
            data: {
                labels: <?php echo json_encode(array_column($totals, 'softwarename')); ?>,
                datasets: [{
                    data: data,
                    backgroundColor: ["#C8412E", "#36A2EB", "#FFCE56", "#419641", "#A569BD", "#EB984E", "#AC8416", "#4F8AAA", "#19FFE2", "#87FE12", "#F3BB88"],
                    hoverBackgroundColor: ["#C8887B", "#36A2AD", "#FFCE10", "#419615", "#A569AA", "#EB980A", "#AC8456", "#4F8BAB", "#19FFE9", "#87FE32", "#F3FB88"]
                }]
            },
            options: {
                responsive: false,
                maintainAspectRatio: false
            }
        });
    }

70 71 72 73 74 75
    /**
     * Add a new line chart for the passed data.
     *
     * @param id   HTML element ID to place the chart.
     * @param data Data to display on the chart.
     */
76 77 78 79 80 81
    function addLineChart(id, data) {
        new Chart(document.getElementById(id), {
            type: "line",
            data: {
                labels: <?php echo json_encode(array_column($check_totals, 'yymm')); ?>,
                datasets: [{
82
                    data: data,
83 84 85 86 87
                    label: 'Users',
                    fill: false,
                    borderColor: "#2ecc71",
                    backgroundColor: "#2ecc71",
                    borderWidth: 2,
David Morley's avatar
David Morley committed
88
                    pointHoverRadius: 6
89 90 91 92 93 94 95 96
                }]
            },
            options: {
                responsive: false,
                maintainAspectRatio: false
            }
        });
    }
David Morley's avatar
David Morley committed
97

98 99 100 101
    addPieChart('total_network_users', <?php echo json_encode(array_column($totals, 'users')); ?>);
    addPieChart('total_network_pods', <?php echo json_encode(array_column($totals, 'pods')); ?>);
    addPieChart('total_network_uptime', <?php echo json_encode(array_column($totals, 'uptime')); ?>);
    addLineChart('user_growth', <?php echo json_encode(array_column($check_totals, 'users')); ?>);
David Morley's avatar
David Morley committed
102
</script>