diff --git a/app/Http/Controllers/Table/LocationController.php b/app/Http/Controllers/Table/LocationController.php index 244a84b6bf..e9ae6fdf32 100644 --- a/app/Http/Controllers/Table/LocationController.php +++ b/app/Http/Controllers/Table/LocationController.php @@ -44,7 +44,7 @@ class LocationController extends TableController protected function sortFields($request) { - return ['location', 'devices', 'network', 'servers', 'firewalls', 'down']; + return ['location', 'devices', 'down']; } /** @@ -83,9 +83,6 @@ class LocationController extends TableController 'lng' => $location->lng, 'down' => $location->devices()->isDown()->count(), 'devices' => $location->devices()->count(), - 'network' => $location->devices()->where('type', 'network')->count(), - 'servers' => $location->devices()->where('type', 'server')->count(), - 'firewalls' => $location->devices()->where('type', 'firewall')->count(), ]; } @@ -101,21 +98,6 @@ class LocationController extends TableController $query->on('devices.location_id', 'locations.id'); (new Device)->scopeIsDown($query); }; - case 'network': - return function ($query) { - $query->on('devices.location_id', 'locations.id') - ->where('devices.type', 'network'); - }; - case 'servers': - return function ($query) { - $query->on('devices.location_id', 'locations.id') - ->where('devices.type', 'server'); - }; - case 'firewalls': - return function ($query) { - $query->on('devices.location_id', 'locations.id') - ->where('devices.type', 'firewall'); - }; default: return null; } diff --git a/app/Http/Controllers/Widgets/DeviceTypeController.php b/app/Http/Controllers/Widgets/DeviceTypeController.php new file mode 100644 index 0000000000..20e1bac468 --- /dev/null +++ b/app/Http/Controllers/Widgets/DeviceTypeController.php @@ -0,0 +1,97 @@ +. + * + * @link https://www.librenms.org + * + * @copyright 2018 Tony Murray + * @author Tony Murray + */ + +namespace App\Http\Controllers\Widgets; + +use App\Models\Device; +use Illuminate\Http\Request; +use LibreNMS\Config; +use LibreNMS\DB\Eloquent; + +class DeviceTypeController extends WidgetController +{ + protected $title = 'Device Types'; + + public function __construct() + { + // init defaults we need to check config, so do it in construct + $this->defaults = [ + 'top_device_group_count' => 5, + 'sort_order' => 'name', + ]; + } + + public function getSettingsView(Request $request) + { + return view('widgets.settings.device-types', $this->getSettings(true)); + } + + protected function getData(Request $request): array + { + $data = $this->getSettings(); + + $counts = Device::groupBy(['type'])->select('type', Eloquent::DB()->raw('COUNT(*) as total'))->orderByDesc('total')->pluck('total', 'type'); + + if ($data['top_device_group_count']) { + $top = $counts->take($data['top_device_group_count']); + } else { + $top = $counts; + } + + $count = 0; + $device_types = []; + foreach (\LibreNMS\Config::get('device_types') as $device_type) { + $count++; + $device_types[] = [ + 'type' => $device_type['type'], + 'count' => $counts->get($device_type['type'], 0), + 'visible' => $top->has($device_type['type']) || (! $data['top_device_group_count'] || $count < $data['top_device_group_count']), + ]; + } + + if ($data['sort_order'] == 'name') { + usort($device_types, function ($item1, $item2) { + return $item1['type'] <=> $item2['type']; + }); + } else { + usort($device_types, function ($item1, $item2) { + return $item2['count'] <=> $item1['count']; + }); + } + + $data['device_types'] = $device_types; + + return $data; + } + + /** + * @param Request $request + * @return \Illuminate\View\View + */ + public function getView(Request $request) + { + return view('widgets.device-types', $this->getData($request)); + } +} diff --git a/database/seeders/DefaultWidgetSeeder.php b/database/seeders/DefaultWidgetSeeder.php index 4362eb967e..388705efac 100644 --- a/database/seeders/DefaultWidgetSeeder.php +++ b/database/seeders/DefaultWidgetSeeder.php @@ -110,6 +110,11 @@ class DefaultWidgetSeeder extends Seeder 'widget' => 'top-errors', 'base_dimensions' => '6,3', ], + [ + 'widget_title' => 'Device Types', + 'widget' => 'device-types', + 'base_dimensions' => '6,3', + ], ]; $existing = DB::table('widgets')->pluck('widget'); diff --git a/resources/views/locations.blade.php b/resources/views/locations.blade.php index 60418bc38e..56f0f9e114 100644 --- a/resources/views/locations.blade.php +++ b/resources/views/locations.blade.php @@ -24,9 +24,6 @@ {{ __('Location') }} {{ __('Coordinates') }} {{ __('Devices') }} - {{ __('Network') }} - {{ __('Servers') }} - {{ __('Firewalls') }} {{ __('Down') }} {{ __('Actions') }} diff --git a/resources/views/widgets/device-types.blade.php b/resources/views/widgets/device-types.blade.php new file mode 100644 index 0000000000..a1e47ae4d4 --- /dev/null +++ b/resources/views/widgets/device-types.blade.php @@ -0,0 +1,26 @@ + + + + + + + @foreach ($device_types as $device_type) + @if ($device_type['visible']) + + @endif + @endforeach + + + + + + @foreach ($device_types as $device_type) + @if ($device_type['visible']) + + @endif + @endforeach + + +
 {{ ucfirst($device_type['type']) }}
{{ __('Summary') }}{{ $device_type['count'] }}
+
+
diff --git a/resources/views/widgets/settings/device-types.blade.php b/resources/views/widgets/settings/device-types.blade.php new file mode 100644 index 0000000000..77293e5e1f --- /dev/null +++ b/resources/views/widgets/settings/device-types.blade.php @@ -0,0 +1,15 @@ +@extends('widgets.settings.base') + +@section('form') +
+ + +
+
+ + +
+@endsection diff --git a/routes/web.php b/routes/web.php index 43fb77d7e5..3b46b45807 100644 --- a/routes/web.php +++ b/routes/web.php @@ -189,6 +189,7 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () { Route::post('component-status', 'ComponentStatusController'); Route::post('device-summary-horiz', 'DeviceSummaryHorizController'); Route::post('device-summary-vert', 'DeviceSummaryVertController'); + Route::post('device-types', 'DeviceTypeController'); Route::post('eventlog', 'EventlogController'); Route::post('generic-graph', 'GraphController'); Route::post('generic-image', 'ImageController');