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

3 4 5 6 7 8
/**
 * Show detailed pod stats.
 */

declare(strict_types=1);

9 10 11 12 13
use RedBeanPHP\R;

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

14
require_once __DIR__ . '/boot.php';
David Morley's avatar
David Morley committed
15

16
$sql = "
17 18 19 20 21 22 23 24 25 26 27
    SELECT
        to_char(date_checked, 'yyyy MM') AS yymm,
        count(*) AS total_checks,
        round(avg(total_users)) AS users,
        round(avg(local_posts)) AS local_posts,
        round(avg(comment_counts)) AS comment_counts
    FROM checks
    WHERE domain = ?
    GROUP BY yymm
    ORDER BY yymm
    LIMIT 24
David Morley's avatar
David Morley committed
28
";
29 30

try {
31
    $totals = R::getAll($sql, [$_domain]);
32
} catch (\RedBeanPHP\RedException $e) {
33
    die('Error in SQL query: ' . $e->getMessage());
34
}
David Morley's avatar
David Morley committed
35
?>
36 37 38
<div class="chart-container p-1" style="height:400px; width:700px">
    <canvas id="pod_chart_counts"></canvas>
</div>
David Morley's avatar
David Morley committed
39
<script>
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    /**
     * Add a new chart for the passed data.
     *
     * @param id   HTML element ID to place the chart.
     * @param data Data to display on the chart.
     */
    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",
55 56
                borderColor: "#A07614",
                backgroundColor: "#A07614",
57 58 59 60 61 62 63
                borderWidth: 4,
                pointHoverRadius: 6
            }, {
                data: <?php echo json_encode(array_column($totals, 'local_posts')); ?>,
                label: 'Local Posts',
                fill: false,
                yAxisID: "l2",
64 65
                borderColor: "#4b6588",
                backgroundColor: "#4b6588",
66 67 68 69 70 71 72
                borderWidth: 4,
                pointHoverRadius: 6
            }, {
                data: <?php echo json_encode(array_column($totals, 'comment_counts')); ?>,
                label: 'Comments',
                fill: false,
                yAxisID: "l2",
73 74
                borderColor: "#cecaa7",
                backgroundColor: "#cecaa7",
75 76 77
                borderWidth: 4,
                pointHoverRadius: 6
            }]
David Morley's avatar
David Morley committed
78
        },
79 80 81 82 83 84 85 86 87
        options: {
            responsive: true,
            maintainAspectRatio: true,
            scales: {
                yAxes: [{
                    position: "left",
                    "id": "l2"
                }]
            }
David Morley's avatar
David Morley committed
88 89 90
        }
    });
</script>