podstat.php 4.95 KB
Newer Older
David Morley's avatar
David Morley committed
1 2
<?php

noplanman's avatar
noplanman committed
3 4 5 6 7 8
use RedBeanPHP\R;

// Required parameters.
($_domain = $_GET['domain'] ?? null) || die('no domain given');

require_once __DIR__ . '/vendor/autoload.php';
David Morley's avatar
David Morley committed
9 10
require_once __DIR__ . '/config.php';

noplanman's avatar
noplanman committed
11
define('PODUPTIME', microtime(true));
David Morley's avatar
David Morley committed
12

noplanman's avatar
noplanman committed
13 14 15
// Set up global DB connection.
R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true);
R::testConnection() || die('Error in DB connection');
David Morley's avatar
David Morley committed
16

noplanman's avatar
noplanman committed
17
$sql = "
David Morley's avatar
David Morley committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  SELECT
    to_char(date_checked, 'yyyy MM') AS yymm,
    count(*) AS total_checks,
    round(avg(total_users)) AS users,
    round(avg(online::INT),2)*100 AS uptime,
    round(avg(latency),2) * 1000 AS latency,
    round(avg(local_posts)) AS local_posts,
    round(avg(comment_counts)) AS comment_counts,
    fpp.manualclicks,
    fpp.autoclicks
  FROM checks
  LEFT JOIN LATERAL (
    SELECT
      count(manualclick) AS manualclicks,
      count(autoclick) AS autoclicks
    FROM clicks
    WHERE checks.domain = clicks.domain
      AND EXTRACT(MONTH FROM checks.date_checked) = EXTRACT(MONTH FROM clicks.date_clicked)
      AND EXTRACT(YEAR FROM checks.date_checked) = EXTRACT(YEAR FROM clicks.date_clicked)
  ) AS fpp ON TRUE
noplanman's avatar
noplanman committed
38
  WHERE domain = ?
David Morley's avatar
David Morley committed
39 40 41 42
  GROUP BY yymm, fpp.manualclicks, fpp.autoclicks
  ORDER BY yymm
  LIMIT 24
";
noplanman's avatar
noplanman committed
43 44 45 46 47 48

try {
  $totals = R::getAll($sql, [$_domain]);
} catch (\RedBeanPHP\RedException $e) {
  die('Error in SQL query: ' . $e->getMessage());
}
David Morley's avatar
David Morley committed
49 50 51 52 53 54
?>
<canvas id="pod_chart_responses"></canvas>
<canvas id="pod_chart_counts"></canvas>
<script>
  /**
   * Add a new chart for the passed data.
noplanman's avatar
noplanman committed
55
   *
David Morley's avatar
David Morley committed
56 57 58 59 60 61 62 63 64 65 66 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
   * @param id   HTML element ID to place the chart.
   * @param data Data to display on the chart.
   */
    new Chart(document.getElementById('pod_chart_responses'), {
      type: "line",
      data: {
        labels: <?php echo json_encode(array_column($totals, 'yymm')); ?>,
        datasets: [{
          data: <?php echo json_encode(array_column($totals, 'uptime')); ?>,
          label: 'Uptime %',
          fill: false,
          yAxisID: "l1",
          borderColor: "#2ecc71",
          backgroundColor: "#2ecc71",
          borderWidth: 4,
          pointHoverRadius: 6
        },
        {
          data: <?php echo json_encode(array_column($totals, 'latency')); ?>,
          label: 'Latency ms',
          fill: true,
          yAxisID: "r1",
          borderColor: "#a93226",
          backgroundColor: "#a93226",
          borderWidth: 4,
          pointHoverRadius: 6,
          pointStyle: 'rect',
        }
        ]
      },
      options: {
        responsive: true,
        maintainAspectRatio: true,
        scales: {
          yAxes: [{
            position: "left",
            "id": "l1",
            ticks: {
              min: 0,
              max: 100,
              stepSize: 20
            }
          }, {
            position: "right",
            "id": "r1",
            ticks: {
              min: 0,
              max: 500,
              stepSize: 50
            }
          }]
        }
      }
    });
        new Chart(document.getElementById('pod_chart_counts'), {
      type: "line",
      data: {
        labels: <?php echo json_encode(array_column($totals, 'yymm')); ?>,
        datasets: [{
          data: <?php echo json_encode(array_column($totals, 'users')); ?>,
          label: 'Users',
          fill: false,
          yAxisID: "l2",
          borderColor: "#e67e22",
          backgroundColor: "#e67e22",
          borderWidth: 4,
          pointHoverRadius: 6,
        },
        {
          data: <?php echo json_encode(array_column($totals, 'local_posts')); ?>,
          label: 'Local Posts',
          fill: false,
          yAxisID: "l2",
          borderColor: "#2980b9",
          backgroundColor: "#2980b9",
          borderWidth: 4,
          pointHoverRadius: 6,
        },
        {
          data: <?php echo json_encode(array_column($totals, 'comment_counts')); ?>,
          label: 'Comments',
          fill: false,
          yAxisID: "l2",
          borderColor: "#FFD700",
          backgroundColor: "#FFD700",
          borderWidth: 4,
          pointHoverRadius: 6,
        },
        {
          data: <?php echo json_encode(array_column($totals, 'manualclicks')); ?>,
          label: 'Manual Clicks',
          fill: false,
          yAxisID: "r2",
          borderColor: "#8A2BE2",
          backgroundColor: "#8A2BE2",
          borderWidth: 4,
          pointHoverRadius: 6,
          pointStyle: 'rect',
        },
        {
          data: <?php echo json_encode(array_column($totals, 'autoclicks')); ?>,
          label: 'Auto Clicks',
          fill: false,
          yAxisID: "r2",
          borderColor: "#FFA07A",
          backgroundColor: "#FFA07A",
          borderWidth: 4,
          pointHoverRadius: 6,
          pointStyle: 'rect',
        }
        ]
      },
      options: {
        responsive: true,
        maintainAspectRatio: true,
        scales: {
          yAxes: [{
            position: "left",
            "id": "l2"
          }, {
            position: "right",
            "id": "r2"
          }]
        }
      }
    });
</script>