110 lines
3.8 KiB
PHP
110 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Paymenter\Extensions\Servers\Proxmox\Livewire;
|
|
|
|
use Paymenter\Extensions\Servers\Proxmox\Models\Server as ServerModel;
|
|
use Paymenter\Extensions\Servers\Proxmox\Models\ServerStatistics;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
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 getBandwidthThreshold95Property()
|
|
{
|
|
// Cache for 15 minutes to avoid recalculating for every server
|
|
return Cache::remember('bandwidth_threshold_95_' . Carbon::now()->format('Y-m'), 15, function () {
|
|
$startOfMonth = Carbon::now()->startOfMonth();
|
|
|
|
// Get all servers
|
|
$allServers = ServerModel::all();
|
|
|
|
if ($allServers->isEmpty()) {
|
|
return 10; // Minimum threshold of 10 GiB
|
|
}
|
|
|
|
$totalTraffic = 0;
|
|
$serverCount = 0;
|
|
|
|
foreach ($allServers as $server) {
|
|
// Get the earliest and latest statistics for this month for each server
|
|
$firstStat = ServerStatistics::where('server_id', $server->id)
|
|
->where('DateTime', '>=', $startOfMonth)
|
|
->orderBy('DateTime', 'asc')
|
|
->first();
|
|
|
|
$latestStat = ServerStatistics::where('server_id', $server->id)
|
|
->where('DateTime', '>=', $startOfMonth)
|
|
->orderBy('DateTime', 'desc')
|
|
->first();
|
|
|
|
if ($firstStat && $latestStat) {
|
|
// 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 and add to total
|
|
$totalTraffic += $totalBytes / (1024 * 1024 * 1024);
|
|
$serverCount++;
|
|
}
|
|
}
|
|
|
|
if ($serverCount === 0) {
|
|
return 10; // Minimum threshold of 10 GiB
|
|
}
|
|
|
|
// Calculate average bandwidth across all servers
|
|
$averageBandwidth = $totalTraffic / $serverCount;
|
|
|
|
// Calculate 95% of average
|
|
$threshold95Percent = $averageBandwidth * 0.95;
|
|
|
|
// Return the maximum of 95% average or 10 GiB minimum
|
|
return round(max($threshold95Percent, 10), 2);
|
|
});
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('proxmox::server');
|
|
}
|
|
|
|
public function placeholder(array $params = [])
|
|
{
|
|
return view('proxmox::serverplaceholder', $params);
|
|
}
|
|
}
|