statsviewjs.php 3.06 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;
noplanman's avatar
noplanman committed
10 11
use RedBeanPHP\R;

12
require_once __DIR__ . '/boot.php';
noplanman's avatar
noplanman committed
13 14

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

dmorley's avatar
dmorley committed
30
try {
31 32 33 34 35 36 37 38
    $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
    ");
dmorley's avatar
dmorley committed
39
} catch (\RedBeanPHP\RedException $e) {
40
    die('Error in SQL query: ' . $e->getMessage());
dmorley's avatar
dmorley committed
41 42
}

David Morley's avatar
David Morley committed
43 44
?>
<script>
45
    /**
dmorley's avatar
dmorley committed
46
     * Add a new pie chart for the passed data.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
     *
     * @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
            }
        });
    }

dmorley's avatar
dmorley committed
69 70 71 72 73 74
    /**
     * 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.
     */
75 76 77 78 79 80
    function addLineChart(id, data) {
        new Chart(document.getElementById(id), {
            type: "line",
            data: {
                labels: <?php echo json_encode(array_column($check_totals, 'yymm')); ?>,
                datasets: [{
dmorley's avatar
dmorley committed
81
                    data: data,
82 83 84 85 86
                    label: 'Users',
                    fill: false,
                    borderColor: "#2ecc71",
                    backgroundColor: "#2ecc71",
                    borderWidth: 2,
dmorley's avatar
dmorley committed
87
                    pointHoverRadius: 6
88 89 90 91 92 93 94 95
                }]
            },
            options: {
                responsive: false,
                maintainAspectRatio: false
            }
        });
    }
David Morley's avatar
David Morley committed
96

97 98 99 100
    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
101
</script>