Speedup device list (#12514)

* Speedup device list

* Update DeviceController.php
This commit is contained in:
Jellyfrog
2021-02-09 06:07:49 +01:00
committed by GitHub
parent 20b4215204
commit 4126663b3a

View File

@@ -27,7 +27,6 @@ namespace App\Http\Controllers\Table;
use App\Models\Device;
use App\Models\Location;
use Illuminate\Database\Eloquent\Builder;
use LibreNMS\Alert\AlertUtil;
use LibreNMS\Config;
use LibreNMS\Util\Rewrite;
use LibreNMS\Util\Time;
@@ -88,7 +87,7 @@ class DeviceController extends TableController
protected function baseQuery($request)
{
/** @var Builder $query */
$query = Device::hasAccess($request->user())->with('location')->select('devices.*');
$query = Device::hasAccess($request->user())->with('location')->withCount(['ports', 'sensors', 'wirelessSensors']);
// if searching or sorting the location field, join the locations table
if ($request->get('searchPhrase') || in_array('location', array_keys($request->get('sort', [])))) {
@@ -140,7 +139,7 @@ class DeviceController extends TableController
return [
'extra' => $this->getLabel($device),
'status' => $this->getStatus($device),
'maintenance' => AlertUtil::isMaintenance($device->device_id),
'maintenance' => $device->isUnderMaintenance(),
'icon' => '<img src="' . asset($device->icon) . '" title="' . pathinfo($device->icon, PATHINFO_FILENAME) . '">',
'hostname' => $this->getHostname($device),
'metrics' => $this->getMetrics($device),
@@ -229,9 +228,9 @@ class DeviceController extends TableController
*/
private function getMetrics($device)
{
$port_count = $device->ports()->count();
$sensor_count = $device->sensors()->count();
$wireless_count = $device->wirelessSensors()->count();
$port_count = $device->ports_count;
$sensor_count = $device->sensors_count;
$wireless_count = $device->wirelessSensors_count;
$metrics = [];
if ($port_count) {