Files
ladill-hosting/app/Services/Infrastructure/InfrastructureUsageReportService.php
T
isaaccladandCursor e251a4cf60
Deploy Ladill Hosting / deploy (push) Failing after 17s
Initial Ladill Hosting app with Gitea deploy pipeline.
Shared web hosting extracted from the platform monolith, with CI deploy
to /var/www/ladill-hosting matching Bird/Domains/Email.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-06 16:24:20 +00:00

75 lines
2.5 KiB
PHP

<?php
namespace App\Services\Infrastructure;
use App\Models\EmailServer;
use App\Models\HostingAccount;
use App\Models\HostingNode;
use App\Models\Mailbox;
use App\Models\User;
use Illuminate\Support\Collection;
class InfrastructureUsageReportService
{
/**
* @return Collection<int, HostingAccount>
*/
public function sharedHostingAccountsForNode(HostingNode $node): Collection
{
return HostingAccount::query()
->where('hosting_node_id', $node->id)
->where('type', HostingAccount::TYPE_SHARED)
->with(['user:id,name,email', 'product:id,name'])
->orderByDesc('disk_used_bytes')
->get();
}
/**
* @return Collection<int, object{
* user_id: int,
* name: string|null,
* email: string|null,
* mailbox_count: int,
* allocated_storage_mb: int,
* used_storage_mb: int
* }>
*/
public function mailStorageByUserForServer(EmailServer $server): Collection
{
$memberIds = app(\App\Services\Mail\EmailServerPoolService::class)
->poolMembers($server->poolRoot())
->pluck('id');
$rows = Mailbox::query()
->selectRaw('mailboxes.user_id')
->selectRaw('COUNT(mailboxes.id) as mailbox_count')
->selectRaw('COALESCE(SUM(mailboxes.quota_mb), 0) as allocated_storage_mb')
->selectRaw('COALESCE(SUM(mailboxes.disk_used_bytes), 0) as used_storage_bytes')
->where(function ($query) use ($memberIds) {
$query->whereHas('emailDomain', fn ($q) => $q->whereIn('email_server_id', $memberIds))
->orWhereHas('domain', fn ($q) => $q->whereIn('email_server_id', $memberIds));
})
->groupBy('mailboxes.user_id')
->orderByDesc('used_storage_bytes')
->get();
$users = User::query()
->whereIn('id', $rows->pluck('user_id')->filter())
->get()
->keyBy('id');
return $rows->map(function ($row) use ($users) {
$user = $users->get($row->user_id);
return (object) [
'user_id' => (int) $row->user_id,
'name' => $user?->name,
'email' => $user?->email,
'mailbox_count' => (int) $row->mailbox_count,
'allocated_storage_mb' => (int) $row->allocated_storage_mb,
'used_storage_mb' => (int) ceil(((int) $row->used_storage_bytes) / 1048576),
];
});
}
}