statsviewjs.php 2.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
dmorley's avatar
dmorley committed
15
    WHERE status < ?
noplanman's avatar
noplanman committed
16
    GROUP BY softwarename
dmorley's avatar
dmorley committed
17
  ', [PodStatus::System_Deleted]);
noplanman's avatar
noplanman committed
18 19 20
} catch (\RedBeanPHP\RedException $e) {
  die('Error in SQL query: ' . $e->getMessage());
}
David Morley's avatar
David Morley committed
21

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

David Morley's avatar
David Morley committed
35 36 37 38
?>
<script>
  /**
   * Add a new chart for the passed data.
noplanman's avatar
noplanman committed
39
   *
David Morley's avatar
David Morley committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
   * @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"]
        }]
      },
dmorley's avatar
dmorley committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      options: {
        responsive: false,
        maintainAspectRatio: false
      }
    });
  }
  function addLineChart(id, data) {
      new Chart(document.getElementById(id), {
      type: "line",
      data: {
        labels: <?php echo json_encode(array_column($check_totals, 'yymm')); ?>,
        datasets: [{
          data: <?php echo json_encode(array_column($check_totals, 'users')); ?>,
          label: 'Users',
          fill: false,
          borderColor: "#2ecc71",
          backgroundColor: "#2ecc71",
dmorley's avatar
dmorley committed
71 72
          borderWidth: 2,
          pointHoverRadius: 2
dmorley's avatar
dmorley committed
73 74 75
        }
        ]
      },
David Morley's avatar
David Morley committed
76
      options: {
dmorley's avatar
dmorley committed
77 78
        responsive: false,
        maintainAspectRatio: false
David Morley's avatar
David Morley committed
79 80 81 82 83 84 85
      }
    });
  }

  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
86
  addLineChart('user_growth', <?php echo json_encode(array_column($check_totals, 'users')); ?>);
David Morley's avatar
David Morley committed
87
</script>