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

noplanman's avatar
noplanman committed
3 4 5 6 7 8 9 10 11 12 13 14
use RedBeanPHP\R;

defined('PODUPTIME') || die();

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

?>
<script>
  /**
   * Add a new chart for the passed data.
noplanman's avatar
noplanman committed
26
   *
David Morley's avatar
David Morley committed
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
   * @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: ["#FF6384", "#36A2EB", "#FFCE56", "#419641", "#A569BD", "#EB984E"],
          hoverBackgroundColor: ["#FF6360", "#36A2AD", "#FFCE10", "#419615", "#A569AA", "#EB980A"]
        }]
      },
      options: {
        responsive: true,
        maintainAspectRatio: true
      }
    });
  }

  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')); ?>);
</script>