feat: add bandwidth threshold calculation to Proxmox server component and update view to display current usage against threshold for enhanced monitoring

This commit is contained in:
2025-12-04 08:09:01 -05:00
parent 581011731b
commit b04dd667a7
2 changed files with 58 additions and 2 deletions

View File

@@ -3,7 +3,9 @@
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
{
@@ -40,6 +42,58 @@ class Server extends Component
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 0;
}
$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 0;
}
// Calculate average bandwidth across all servers
$averageBandwidth = $totalTraffic / $serverCount;
// Return 95% of average
return round($averageBandwidth * 0.95, 2);
});
}
public function render()
{
return view('proxmox::server');

View File

@@ -119,9 +119,11 @@
<x-ri-speed-line class="size-4 text-base/50" />
<span class="text-xs text-base/50">Bandwidth</span>
</div>
<span class="text-base font-medium">{{ $this->monthlyTraffic }} GiB this month</span>
<span class="text-base font-medium">
{{ $this->monthlyTraffic }} GiB / {{ $this->bandwidthThreshold95 }} GiB (95%)
</span>
<span class="text-base text-xs leading-relaxed text-base/60 mt-1">
Connection reduced to 1 MiB/s if account's data usage exceeds network's 95% threshold.
Connection reduced to 1 MiB/s if usage exceeds the 95% threshold.
</span>
</div>
</div>