Files
breadcrumb-the-shire/pages/admin/stats/index().php
2026-02-04 23:31:53 +01:00

167 lines
5.6 KiB
PHP

<?php
use MintyPHP\Buffer;
use MintyPHP\Support\Guard;
use MintyPHP\Service\TenantService;
use MintyPHP\Service\DepartmentService;
use MintyPHP\Service\RoleService;
use MintyPHP\Service\UserService;
use MintyPHP\Service\PermissionService;
use MintyPHP\Repository\PermissionRepository;
use MintyPHP\Repository\MailLogRepository;
use MintyPHP\DB;
Guard::requirePermissionOrForbidden(PermissionService::STATS_VIEW);
// Basic counts
$tenants = TenantService::list();
$tenantCount = count($tenants);
$departments = DepartmentService::list();
$departmentCount = count($departments);
$roles = RoleService::list();
$roleCount = count($roles);
$users = UserService::list();
$userCount = count($users);
$permissions = PermissionRepository::list();
$permissionCount = count($permissions);
// User stats (active/inactive)
$activeUserCount = 0;
$inactiveUserCount = 0;
foreach ($users as $user) {
$isActive = (int) ($user['active'] ?? 1);
if ($isActive === 1) {
$activeUserCount++;
} else {
$inactiveUserCount++;
}
}
// Organization breakdown (per tenant)
$tenantBreakdown = [];
// Load user-tenant assignments
$userTenantAssignments = [];
$userTenantRows = DB::select('select user_id, tenant_id from user_tenants');
if (is_array($userTenantRows)) {
foreach ($userTenantRows as $row) {
$data = $row['user_tenants'] ?? $row;
$userId = (int) ($data['user_id'] ?? 0);
$tenantId = (int) ($data['tenant_id'] ?? 0);
if ($userId > 0 && $tenantId > 0) {
if (!isset($userTenantAssignments[$userId])) {
$userTenantAssignments[$userId] = [];
}
$userTenantAssignments[$userId][] = $tenantId;
}
}
}
// Load tenant-department assignments
$tenantDepartmentAssignments = [];
$tenantDepartmentRows = DB::select('select tenant_id, department_id from tenant_departments');
if (is_array($tenantDepartmentRows)) {
foreach ($tenantDepartmentRows as $row) {
$data = $row['tenant_departments'] ?? $row;
$tenantId = (int) ($data['tenant_id'] ?? 0);
$departmentId = (int) ($data['department_id'] ?? 0);
if ($tenantId > 0 && $departmentId > 0) {
if (!isset($tenantDepartmentAssignments[$tenantId])) {
$tenantDepartmentAssignments[$tenantId] = [];
}
$tenantDepartmentAssignments[$tenantId][] = $departmentId;
}
}
}
foreach ($tenants as $tenant) {
$tenantId = (int) ($tenant['id'] ?? 0);
if ($tenantId === 0) {
continue;
}
// Count departments for this tenant
$deptCount = 0;
$tenantDepartmentIds = $tenantDepartmentAssignments[$tenantId] ?? [];
foreach ($departments as $dept) {
$deptId = (int) ($dept['id'] ?? 0);
if ($deptId > 0 && in_array($deptId, $tenantDepartmentIds, true)) {
$deptCount++;
}
}
// Count users for this tenant
$totalUsers = 0;
$activeUsers = 0;
$inactiveUsers = 0;
foreach ($users as $user) {
$userId = (int) ($user['id'] ?? 0);
if ($userId === 0) {
continue;
}
// Check if user is assigned to this tenant
$userTenantIds = $userTenantAssignments[$userId] ?? [];
if (in_array($tenantId, $userTenantIds, true)) {
$totalUsers++;
$isActive = (int) ($user['active'] ?? 1);
if ($isActive === 1) {
$activeUsers++;
} else {
$inactiveUsers++;
}
}
}
$tenantBreakdown[] = [
'id' => $tenantId,
'uuid' => $tenant['uuid'] ?? '',
'description' => $tenant['description'] ?? '',
'departments' => $deptCount,
'total_users' => $totalUsers,
'active_users' => $activeUsers,
'inactive_users' => $inactiveUsers,
];
}
// Sort by description
usort($tenantBreakdown, function ($a, $b) {
return strcmp((string) ($a['description'] ?? ''), (string) ($b['description'] ?? ''));
});
// Mail log stats
$mailLogTotalCount = DB::selectValue('select count(*) from mail_log');
$mailLogTotalCount = $mailLogTotalCount ? (int) $mailLogTotalCount : 0;
$mailLogSentCount = DB::selectValue("select count(*) from mail_log where status = 'sent'");
$mailLogSentCount = $mailLogSentCount ? (int) $mailLogSentCount : 0;
$mailLogFailedCount = DB::selectValue("select count(*) from mail_log where status = 'failed'");
$mailLogFailedCount = $mailLogFailedCount ? (int) $mailLogFailedCount : 0;
$mailLogQueuedCount = DB::selectValue("select count(*) from mail_log where status = 'queued'");
$mailLogQueuedCount = $mailLogQueuedCount ? (int) $mailLogQueuedCount : 0;
// Mail log stats (last 24h)
$mailLogTotal24hCount = DB::selectValue("select count(*) from mail_log where created_at >= NOW() - INTERVAL 24 HOUR");
$mailLogTotal24hCount = $mailLogTotal24hCount ? (int) $mailLogTotal24hCount : 0;
$mailLogSent24hCount = DB::selectValue("select count(*) from mail_log where status = 'sent' and created_at >= NOW() - INTERVAL 24 HOUR");
$mailLogSent24hCount = $mailLogSent24hCount ? (int) $mailLogSent24hCount : 0;
$mailLogFailed24hCount = DB::selectValue("select count(*) from mail_log where status = 'failed' and created_at >= NOW() - INTERVAL 24 HOUR");
$mailLogFailed24hCount = $mailLogFailed24hCount ? (int) $mailLogFailed24hCount : 0;
$mailLogQueued24hCount = DB::selectValue("select count(*) from mail_log where status = 'queued' and created_at >= NOW() - INTERVAL 24 HOUR");
$mailLogQueued24hCount = $mailLogQueued24hCount ? (int) $mailLogQueued24hCount : 0;
// Calculate date range for last 24 hours
$date24hAgo = gmdate('Y-m-d', strtotime('-24 hours'));
$dateToday = gmdate('Y-m-d');
Buffer::set('title', t('Statistics'));