feat: Dashboard Insights

This commit is contained in:
slawkens
2026-03-15 13:04:07 +01:00
parent ccd70d2ee3
commit 853520cfc4
3 changed files with 237 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
<?php
use MyAAC\Admin\Insights;
defined('MYAAC') or die('Direct access not allowed!');
$getYear = (int)($_GET['year'] ?? date('Y'));
$getMonth = $_GET['month'] ?? (int)date('M') + 1;
$insights = new Insights($db);
$twig->display('insights.html.twig', [
'lastLoginPlayers' => $insights->getLastLoggedPlayers($getYear, $getMonth),
'lastCreatedAccounts' => $insights->getLastCreatedAccounts($getYear, $getMonth),
'firstYear' => $insights->getFirstYear(),
'getYear' => $getYear,
'getMonth' => $getMonth,
'months' => $insights->getMonths(),
]);

View File

@@ -0,0 +1,99 @@
{% set currentYear = 'now' | date('Y') %}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<div class="col-sm-3 col-md-6 col-lg-12">
<div class="card card-info card-outline">
<div class="card-header">
<h5 class="m-0">Insights</h5>
</div>
<div class="card-body p-3 row">
<div class="col-md-6 col-sm-3">
<label for="month">Month:</label>
<select class="form-control" id="month" name="month">
{% for id, name in months %}
<option value="{{ id }}" {{ getMonth == id ? 'selected="selected"' : '' }}>{{ name }}</option>
{% endfor %}
</select>
</div>
<div class="col-md-6 col-sm-3">
<label for="year">Year:</label>
<select class="form-control" id="year" name="year">
{% for year in range(firstYear, currentYear) %}
<option value="{{ year }}" {{ getYear == year ? 'selected' : '' }}>{{ year }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="card-body p-3 row">
<div class="col-6 col-md-6">
<div class="chart">
<canvas id="lastLoginsChart" style="min-height: 250px; height: 250px; max-height: 250px; max-width: 100%;"></canvas>
</div>
</div>
<div class="col-6 col-md-6">
<div class="chart">
<canvas id="lastCreatedChart" style="min-height: 250px; height: 250px; max-height: 250px; max-width: 100%;"></canvas>
</div>
</div>
</div>
</div>
</div>
<script>
$(function () {
$('#year').on('change', function() {
window.location.href = "?p=dashboard&year=" + $(this).val() + "&month={{ getMonth }}";
});
$('#month').on('change', function() {
window.location.href = "?p=dashboard&year={{ getYear }}" + "&month=" + $(this).val();
});
});
const config = {
type: 'bar',
data: {
labels: [
{% for player in lastLoginPlayers %}
"{{ player.date }}",
{% endfor %}
],
datasets: [
{
label: "Logged players",
backgroundColor: ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850"],
data: [
{% for player in lastLoginPlayers %}
{{ player.how_much }},
{% endfor %}
]
}
]
}
}
const myChart = new Chart(document.getElementById('lastLoginsChart'), config);
const config2 = {
type: 'bar',
data: {
labels: [
{% for account in lastCreatedAccounts %}
"{{ account.date }}",
{% endfor %}
],
datasets: [
{
label: "Accounts created",
backgroundColor: ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850"],
data: [
{% for account in lastCreatedAccounts %}
{{ account.how_much }},
{% endfor %}
]
}
]
}
}
const myChart2 = new Chart(document.getElementById('lastCreatedChart'), config2);
</script>

View File

@@ -0,0 +1,116 @@
<?php
namespace MyAAC\Admin;
use MyAAC\Cache\Cache;
class Insights
{
private $db;
public function __construct($db){
$this->db = $db;
}
public function getLastLoggedPlayers(int $year, $month): array
{
return Cache::remember("admin_dashboard_insights_players_lastlogin_{$year}_{$month}", 5 * 60, function() use ($year, $month) {
$lastLoggedPlayers = [];
$getFromTo = $this->getFromTo($year, $month);
$whereLastLogin = 'AND lastlogin >= ' . $getFromTo[0] . ' AND lastlogin <= ' . $getFromTo[1];
if ($month === 'all') {
$query = $this->db->query('SELECT count(id) as how_much, DATE_FORMAT(FROM_UNIXTIME(lastlogin), "%m.%Y") as lastdate FROM players WHERE lastlogin > 0 ' . $whereLastLogin . ' GROUP BY lastdate LIMIT 14');
foreach ($query as $item) {
$date = explode('.', $item['lastdate']);
$monthName = date('F', mktime(0, 0, 0, $date[0], 10));;
$lastLoggedPlayers[] = ['date' => $monthName, 'how_much' => $item['how_much']];
}
}
else {
$query = $this->db->query('SELECT count(id) as how_much, DATE_FORMAT(FROM_UNIXTIME(lastlogin), "%d.%m.%Y") as lastdate FROM players WHERE lastlogin > 0 ' . $whereLastLogin . ' GROUP BY lastdate LIMIT 14');
foreach ($query as $item) {
$lastLoggedPlayers[] = ['date' => $item['lastdate'], 'how_much' => $item['how_much']];
}
}
return $lastLoggedPlayers;
});
}
public function getLastCreatedAccounts(int $year, $month): array
{
return Cache::remember("admin_dashboard_insights_accounts_created_{$year}_{$month}", 10 * 60, function() use ($year, $month) {
$lastCreatedAccounts = [];
$getFromTo = $this->getFromTo($year, $month);
$whereCreated = 'AND created >= ' . $getFromTo[0] . ' AND created <= ' . $getFromTo[1];
if ($month == 'all') {
$query = $this->db->query('SELECT count(id) as how_much, DATE_FORMAT(FROM_UNIXTIME(created), "%m.%Y") as createdDate FROM accounts WHERE created > 0 ' . $whereCreated . ' GROUP BY createdDate LIMIT 31');
foreach ($query as $item) {
$date = explode('.', $item['createdDate']);
$monthName = date('F', mktime(0, 0, 0, $date[0], 10));;
$lastCreatedAccounts[] = ['date' => $monthName, 'how_much' => $item['how_much']];
}
}
else {
$query = $this->db->query('SELECT count(id) as how_much, DATE_FORMAT(FROM_UNIXTIME(created), "%d.%m.%Y") as createdDate FROM accounts WHERE created > 0 ' . $whereCreated . ' GROUP BY createdDate LIMIT 31');
foreach ($query as $item) {
$lastCreatedAccounts[] = ['date' => $item['createdDate'], 'how_much' => $item['how_much']];
}
}
return $lastCreatedAccounts;
});
}
public function getFirstYear(): int
{
$query = $this->db->query('SELECT created FROM accounts WHERE created > 0 ORDER BY created LIMIT 1');
if ($query->rowCount()) {
$firstAccountCreated = $query->fetch()['created'];
}
else {
$firstAccountCreated = time();
}
return (int)date('Y', $firstAccountCreated);
}
public function getMonths(): array
{
$months = [];
$months['all'] = 'All';
for ($i = 1; $i <= 12; $i++) {
$months[$i] = date('F', mktime(0, 0, 0, $i, 10));
}
return $months;
}
private function getFromTo(int $year, $month): array
{
if ($month == 'all') {
$firstMonth = 1;
$lastMonth = 12;
}
else {
$firstMonth = $month;
$lastMonth = $month;
}
$from = date('U', mktime(0, 0, 0, $firstMonth, 1, $year));
$to = date('U', mktime(0, 0, 0, $lastMonth, 31, $year));
return [$from, $to];
}
}