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

dmorley's avatar
dmorley committed
23 24 25 26 27 28 29 30 31 32 33 34 35
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
36 37 38 39
?>
<script>
  /**
   * Add a new chart for the passed data.
noplanman's avatar
noplanman committed
40
   *
David Morley's avatar
David Morley committed
41 42 43 44 45 46 47 48 49 50
   * @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,
51 52
          backgroundColor: ["#C8412E", "#36A2EB", "#FFCE56", "#419641", "#A569BD", "#EB984E", "#AC8416", "#4F8AAA", "#19FFE2", "#87FE12", "#F3BB88"],
          hoverBackgroundColor: ["#C8887B", "#36A2AD", "#FFCE10", "#419615", "#A569AA", "#EB980A", "#AC8456", "#4F8BAB", "#19FFE9", "#87FE32", "#F3FB88"]
David Morley's avatar
David Morley committed
53 54
        }]
      },
dmorley's avatar
dmorley committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      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
72 73
          borderWidth: 2,
          pointHoverRadius: 2
dmorley's avatar
dmorley committed
74 75 76
        }
        ]
      },
David Morley's avatar
David Morley committed
77
      options: {
dmorley's avatar
dmorley committed
78 79
        responsive: false,
        maintainAspectRatio: false
David Morley's avatar
David Morley committed
80 81 82 83 84 85 86
      }
    });
  }

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