Device Types Widget (#13670)

* show all Device Types in Location Overview

* .

* .

* .

* get device types from config_definition

* reduce column to present device types

* .

* fixes

* .

* show/hide columns, even device types which are not present

* only show top n used device groups

* .

* .

* .

* Device Type Widget

* .

* .

* linter fix

* Update DeviceTypeController.php

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
SourceDoctor
2022-05-25 22:03:01 +02:00
committed by GitHub
parent 3b255edc37
commit 1c6fc0f130
7 changed files with 145 additions and 22 deletions

View File

@@ -44,7 +44,7 @@ class LocationController extends TableController
protected function sortFields($request) 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, 'lng' => $location->lng,
'down' => $location->devices()->isDown()->count(), 'down' => $location->devices()->isDown()->count(),
'devices' => $location->devices()->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'); $query->on('devices.location_id', 'locations.id');
(new Device)->scopeIsDown($query); (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: default:
return null; return null;
} }

View File

@@ -0,0 +1,97 @@
<?php
/**
* DeviceSummaryController.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
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));
}
}

View File

@@ -110,6 +110,11 @@ class DefaultWidgetSeeder extends Seeder
'widget' => 'top-errors', 'widget' => 'top-errors',
'base_dimensions' => '6,3', 'base_dimensions' => '6,3',
], ],
[
'widget_title' => 'Device Types',
'widget' => 'device-types',
'base_dimensions' => '6,3',
],
]; ];
$existing = DB::table('widgets')->pluck('widget'); $existing = DB::table('widgets')->pluck('widget');

View File

@@ -24,9 +24,6 @@
<th data-column-id="location" data-formatter="location" data-order="asc">{{ __('Location') }}</th> <th data-column-id="location" data-formatter="location" data-order="asc">{{ __('Location') }}</th>
<th data-column-id="coordinates" data-formatter="coordinates" data-sortable="false">{{ __('Coordinates') }}</th> <th data-column-id="coordinates" data-formatter="coordinates" data-sortable="false">{{ __('Coordinates') }}</th>
<th data-column-id="devices" data-formatter="primaryLabel">{{ __('Devices') }}</th> <th data-column-id="devices" data-formatter="primaryLabel">{{ __('Devices') }}</th>
<th data-column-id="network" data-formatter="defaultLabel">{{ __('Network') }}</th>
<th data-column-id="servers" data-formatter="defaultLabel">{{ __('Servers') }}</th>
<th data-column-id="firewalls" data-formatter="defaultLabel">{{ __('Firewalls') }}</th>
<th data-column-id="down" data-formatter="down">{{ __('Down') }}</th> <th data-column-id="down" data-formatter="down">{{ __('Down') }}</th>
<th data-column-id="actions" data-formatter="actions" data-sortable="false">{{ __('Actions') }}</th> <th data-column-id="actions" data-formatter="actions" data-sortable="false">{{ __('Actions') }}</th>
</tr> </tr>

View File

@@ -0,0 +1,26 @@
<x-panel class="table-responsive">
<x-slot name="table">
<table class="table table-hover table-condensed table-striped">
<thead>
<tr>
<th>&nbsp;</th>
@foreach ($device_types as $device_type)
@if ($device_type['visible'])
<th>{{ ucfirst($device_type['type']) }}</th>
@endif
@endforeach
</tr>
</thead>
<tbody>
<tr>
<td>{{ __('Summary') }}</td>
@foreach ($device_types as $device_type)
@if ($device_type['visible'])
<td>{{ $device_type['count'] }}</td>
@endif
@endforeach
</tr>
</tbody>
</table>
</x-slot>
</x-panel>

View File

@@ -0,0 +1,15 @@
@extends('widgets.settings.base')
@section('form')
<div class="form-group">
<label for="sort_order-{{ $id }}" class="control-label">{{ __('Sort Order') }}</label>
<select class="form-control" id="sort_order-{{ $id }}" name="sort_order">
<option value="name" @if($sort_order == 'name') selected @endif>{{ __('Device Type') }}</option>
<option value="count" @if($sort_order == 'count') selected @endif>{{ __('Count') }}</option>
</select>
</div>
<div class="form-group">
<label for="top_device_group_count-{{ $id }}" class="control-label">{{ __('Top Count of Devices Types') }}</label>
<input class="form-control" name="top_device_group_count" id="top_device_group_count-{{ $id }}" value="{{ $top_device_group_count }}">
</div>
@endsection

View File

@@ -189,6 +189,7 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () {
Route::post('component-status', 'ComponentStatusController'); Route::post('component-status', 'ComponentStatusController');
Route::post('device-summary-horiz', 'DeviceSummaryHorizController'); Route::post('device-summary-horiz', 'DeviceSummaryHorizController');
Route::post('device-summary-vert', 'DeviceSummaryVertController'); Route::post('device-summary-vert', 'DeviceSummaryVertController');
Route::post('device-types', 'DeviceTypeController');
Route::post('eventlog', 'EventlogController'); Route::post('eventlog', 'EventlogController');
Route::post('generic-graph', 'GraphController'); Route::post('generic-graph', 'GraphController');
Route::post('generic-image', 'ImageController'); Route::post('generic-image', 'ImageController');