Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device Types Widget #13670

Merged
merged 24 commits into from May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 1 addition & 19 deletions app/Http/Controllers/Table/LocationController.php
Expand Up @@ -44,7 +44,7 @@ public function searchFields($request)

protected function sortFields($request)
{
return ['location', 'devices', 'network', 'servers', 'firewalls', 'down'];
return ['location', 'devices', 'down'];
}

/**
Expand Down Expand Up @@ -83,9 +83,6 @@ public function formatItem($location)
'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(),
];
}

Expand All @@ -101,21 +98,6 @@ private function getJoinQuery($field)
$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;
}
Expand Down
97 changes: 97 additions & 0 deletions app/Http/Controllers/Widgets/DeviceTypeController.php
@@ -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));
}
}
5 changes: 5 additions & 0 deletions database/seeders/DefaultWidgetSeeder.php
Expand Up @@ -110,6 +110,11 @@ public function run()
'widget' => 'top-errors',
'base_dimensions' => '6,3',
],
[
'widget_title' => 'Device Types',
'widget' => 'device-types',
'base_dimensions' => '6,3',
],
];

$existing = DB::table('widgets')->pluck('widget');
Expand Down
3 changes: 0 additions & 3 deletions resources/views/locations.blade.php
Expand Up @@ -24,9 +24,6 @@
<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="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="actions" data-formatter="actions" data-sortable="false">{{ __('Actions') }}</th>
</tr>
Expand Down
26 changes: 26 additions & 0 deletions resources/views/widgets/device-types.blade.php
@@ -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>
15 changes: 15 additions & 0 deletions resources/views/widgets/settings/device-types.blade.php
@@ -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
1 change: 1 addition & 0 deletions routes/web.php
Expand Up @@ -189,6 +189,7 @@
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');
Expand Down