Refactor Proxmox components: optimize data fetching in Server and Overview; enhance macro handling in Firewall; improve task data refresh logic in Statistics and views
This commit is contained in:
@@ -58,36 +58,26 @@ class Server extends Component
|
||||
return Cache::remember('bandwidth_threshold_95_' . Carbon::now()->format('Y-m'), 900, function () {
|
||||
$startOfMonth = Carbon::now()->startOfMonth();
|
||||
|
||||
// Get all servers
|
||||
$allServers = ServerModel::all();
|
||||
// Use a single query with subqueries to get min/max netin/netout per server,
|
||||
// avoiding N+1 queries (previously 2 queries per server)
|
||||
$trafficData = ServerStatistics::where('DateTime', '>=', $startOfMonth)
|
||||
->selectRaw('server_id, MIN(netin) as min_netin, MAX(netin) as max_netin, MIN(netout) as min_netout, MAX(netout) as max_netout')
|
||||
->groupBy('server_id')
|
||||
->get();
|
||||
|
||||
if ($allServers->isEmpty()) {
|
||||
if ($trafficData->isEmpty()) {
|
||||
return 10; // Minimum threshold of 10 GiB
|
||||
}
|
||||
|
||||
$trafficValues = [];
|
||||
|
||||
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();
|
||||
foreach ($trafficData as $row) {
|
||||
$netinDiff = max(0, $row->max_netin - $row->min_netin);
|
||||
$netoutDiff = max(0, $row->max_netout - $row->min_netout);
|
||||
$totalBytes = $netinDiff + $netoutDiff;
|
||||
|
||||
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 collect
|
||||
$trafficValues[] = $totalBytes / (1024 * 1024 * 1024);
|
||||
}
|
||||
// Convert to GiB and collect
|
||||
$trafficValues[] = $totalBytes / (1024 * 1024 * 1024);
|
||||
}
|
||||
|
||||
if (empty($trafficValues)) {
|
||||
|
||||
Reference in New Issue
Block a user