53 lines
1.4 KiB
PHP
53 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Paymenter\Extensions\Servers\Proxmox\Livewire;
|
|
|
|
use Paymenter\Extensions\Servers\Proxmox\Models\Server as ServerModel;
|
|
use Carbon\Carbon;
|
|
|
|
class Server extends Component
|
|
{
|
|
public function mount(ServerModel $server): void
|
|
{
|
|
parent::mount($server);
|
|
}
|
|
|
|
public function getMonthlyTrafficProperty()
|
|
{
|
|
$startOfMonth = Carbon::now()->startOfMonth();
|
|
|
|
// Get the earliest and latest statistics for this month
|
|
$firstStat = $this->server->statistics()
|
|
->where('DateTime', '>=', $startOfMonth)
|
|
->orderBy('DateTime', 'asc')
|
|
->first();
|
|
|
|
$latestStat = $this->server->statistics()
|
|
->where('DateTime', '>=', $startOfMonth)
|
|
->orderBy('DateTime', 'desc')
|
|
->first();
|
|
|
|
if (!$firstStat || !$latestStat) {
|
|
return 0;
|
|
}
|
|
|
|
// Calculate total traffic (in + out) for the month
|
|
$netinDiff = max(0, $latestStat->netin - $firstStat->netin);
|
|
$netoutDiff = max(0, $latestStat->netout - $firstStat->netout);
|
|
$totalBytes = $netinDiff + $netoutDiff;
|
|
|
|
// Convert to GiB
|
|
return round($totalBytes / (1024 * 1024 * 1024), 2);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('proxmox::server');
|
|
}
|
|
|
|
public function placeholder(array $params = [])
|
|
{
|
|
return view('proxmox::serverplaceholder', $params);
|
|
}
|
|
}
|