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

noplanman's avatar
noplanman committed
12
require_once __DIR__ . '/boot.php';
noplanman's avatar
noplanman committed
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 < ?
dmorley's avatar
dmorley committed
23
        AND score > 0
24 25
        GROUP BY softwarename
        ORDER BY softwarename
dmorley's avatar
dmorley committed
26
    ', [PodStatus::PAUSED]);
noplanman's avatar
noplanman committed
27
} catch (\RedBeanPHP\RedException $e) {
28
    die('Error in SQL query: ' . $e->getMessage());
noplanman's avatar
noplanman committed
29
}
David Morley's avatar
David Morley committed
30

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

dmorley's avatar
dmorley committed
44 45 46 47 48 49 50 51 52 53 54 55 56
try {
    $check_total_pods = R::getAll("
        SELECT
            to_char(date_checked, 'yyyy-mm') AS yymm,
            total_pods AS pods
        FROM monthlystats
        GROUP BY yymm, pods
        ORDER BY yymm
    ");
} catch (\RedBeanPHP\RedException $e) {
    die('Error in SQL query: ' . $e->getMessage());
}

David Morley's avatar
David Morley committed
57 58
?>
<script>
dmorley's avatar
dmorley committed
59
    
60
    /**
dmorley's avatar
dmorley committed
61
     * Add a new pie chart for the passed data.
62 63 64 65 66
     *
     * @param id   HTML element ID to place the chart.
     * @param data Data to display on the chart.
     */
    function addPieChart(id, data) {
dmorley's avatar
dmorley committed
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
        var intToRGB = function(value, alpha, max) {
            var valueAsPercentageOfMax = value / max;
            // actual max is 16777215 but represnts white so we will take a max that is
            // below this to avoid white
            var MAX_RGB_INT = 16600000;
            var valueFromMaxRgbInt = Math.floor(MAX_RGB_INT * valueAsPercentageOfMax);

            //credit to https://stackoverflow.com/a/2262117/2737978 for the idea of how to implement
            var blue = Math.floor(valueFromMaxRgbInt % 256);
            var green = Math.floor(valueFromMaxRgbInt / 256 % 256);
            var red = Math.floor(valueFromMaxRgbInt / 256 / 256 % 256);

            return "rgba(" + red + "," + green + "," + blue + "," + alpha + ")";
        }

        var MAX = data.length;

        var backgroundColors = data.map(function(item) {
            return intToRGB(item, 0.8, MAX);
        });

        var borderColors = data.map(function(item) {
            return intToRGB(item, 1, MAX);
        });

92 93 94 95 96 97
        new Chart(document.getElementById(id), {
            type: "pie",
            data: {
                labels: <?php echo json_encode(array_column($totals, 'softwarename')); ?>,
                datasets: [{
                    data: data,
dmorley's avatar
dmorley committed
98 99 100
                    backgroundColor: backgroundColors,
                    borderColor: borderColors,
                    borderWidth: 1
101 102 103
                }]
            },
            options: {
dmorley's avatar
dmorley committed
104 105 106 107 108 109 110 111 112
                responsive: true,
                maintainAspectRatio: true,
                legend: {
                    display: false,
                    labels: {
                        fontSize: 9,
                        boxWidth: 4
                    }
                }
113 114 115 116
            }
        });
    }

dmorley's avatar
dmorley committed
117 118 119 120 121 122
    /**
     * 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.
     */
dmorley's avatar
dmorley committed
123
    function addLineChartusers(id, data) {
124 125 126
        new Chart(document.getElementById(id), {
            type: "line",
            data: {
dmorley's avatar
dmorley committed
127
                labels: <?php echo json_encode(array_column($check_total_users, 'yymm')); ?>,
128
                datasets: [{
dmorley's avatar
dmorley committed
129
                    data: data,
130 131 132 133
                    label: 'Users',
                    fill: false,
                    borderColor: "#2ecc71",
                    backgroundColor: "#2ecc71",
dmorley's avatar
dmorley committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                    borderWidth: 3,
                    pointHoverRadius: 6
                }]
            },
            options: {
                responsive: false,
                maintainAspectRatio: false
            }
        });
    }
    function addLineChartpods(id, data) {
        new Chart(document.getElementById(id), {
            type: "line",
            data: {
                labels: <?php echo json_encode(array_column($check_total_pods, 'yymm')); ?>,
                datasets: [{
                    data: data,
                    label: 'Pods',
                    fill: true,
                    borderColor: "#7D542A",
                    backgroundColor: "#2B547E",
                    borderWidth: 3,
dmorley's avatar
dmorley committed
156
                    pointHoverRadius: 6
157 158 159 160 161 162 163 164 165 166 167
                }]
            },
            options: {
                responsive: false,
                maintainAspectRatio: false
            }
        });
    }
    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')); ?>);
dmorley's avatar
dmorley committed
168 169
    addLineChartusers('user_growth', <?php echo json_encode(array_column($check_total_users, 'users')); ?>);
    addLineChartpods('pod_growth', <?php echo json_encode(array_column($check_total_pods, 'pods')); ?>);
David Morley's avatar
David Morley committed
170
</script>