statsviewjs.php 3.33 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;

dmorley's avatar
dmorley committed
12 13 14 15 16 17 18 19 20
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);
noplanman's avatar
noplanman committed
21 22

try {
23 24 25 26 27 28 29 30 31 32 33
    $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
34
} catch (\RedBeanPHP\RedException $e) {
35
    die('Error in SQL query: ' . $e->getMessage());
noplanman's avatar
noplanman committed
36
}
David Morley's avatar
David Morley committed
37

dmorley's avatar
dmorley committed
38
try {
39 40 41 42 43 44 45 46
    $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
47
} catch (\RedBeanPHP\RedException $e) {
48
    die('Error in SQL query: ' . $e->getMessage());
dmorley's avatar
dmorley committed
49 50
}

David Morley's avatar
David Morley committed
51 52
?>
<script>
53
    /**
dmorley's avatar
dmorley committed
54
     * Add a new pie chart for the passed data.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
     *
     * @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
77 78 79 80 81 82
    /**
     * 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.
     */
83 84 85 86 87 88
    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
89
                    data: data,
90 91 92 93 94
                    label: 'Users',
                    fill: false,
                    borderColor: "#2ecc71",
                    backgroundColor: "#2ecc71",
                    borderWidth: 2,
dmorley's avatar
dmorley committed
95
                    pointHoverRadius: 6
96 97 98 99 100 101 102 103
                }]
            },
            options: {
                responsive: false,
                maintainAspectRatio: false
            }
        });
    }
David Morley's avatar
David Morley committed
104

105 106 107 108
    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
109
</script>