Fixed customers page (#9521)

move customers table backend to Laravel

DO NOT DELETE THIS TEXT

#### Please note

> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.

- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)

#### Testers

If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`.  If there are schema changes, you can ask on discord how to revert.
This commit is contained in:
Tony Murray
2018-12-07 16:24:53 -06:00
committed by Neil Lathwood
parent 6f74628b14
commit 93f44d67f8
6 changed files with 150 additions and 127 deletions

View File

@@ -83,7 +83,7 @@ class Url
$contents .= ' (' . htmlentities($device->features) . ')';
}
if ($device->location) {
if ($device->location_id) {
$contents .= ' - ' . htmlentities($device->location);
}
@@ -303,7 +303,13 @@ class Url
return $device->status ? 'list-device' : 'list-device-down';
}
private static function portLinkDisplayClass($port)
/**
* Get html class for a port using ifAdminStatus and ifOperStatus
*
* @param Port $port
* @return string
*/
public static function portLinkDisplayClass($port)
{
if ($port->ifAdminStatus == "down") {
return "interface-admindown";

View File

@@ -0,0 +1,129 @@
<?php
/**
* CustomersController.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 <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Http\Controllers\Table;
use App\Models\Port;
use LibreNMS\Config;
use LibreNMS\Util\Html;
use LibreNMS\Util\Url;
class CustomersController extends TableController
{
public function searchFields($request)
{
return ['port_descr_descr', 'ifName', 'ifDescr', 'ifAlias', 'hostname', 'sysDescr', 'port_descr_speed', 'port_descr_notes'];
}
/**
* Defines the base query for this resource
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
public function baseQuery($request)
{
$cust_descrs = (array)Config::get('customers_descr', ['cust']);
// selecting just the customer name, will fetch port data later
return Port::hasAccess($request->user())
->with('device')
->leftJoin('devices', 'ports.device_id', 'devices.device_id')
->select('port_descr_descr')
->whereIn('port_descr_type', $cust_descrs)
->groupBy('port_descr_descr');
}
/**
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
* @return \Illuminate\Http\JsonResponse
*/
protected function formatResponse($paginator)
{
$customers = collect($paginator->items())->pluck('port_descr_descr');
// fetch all ports
$ports = Port::whereIn('port_descr_descr', $customers)
->with('device')
->get()
->groupBy('port_descr_descr');
$rows = $customers->reduce(function ($rows, $customer) use ($ports) {
$graph_row = $this->getGraphRow($customer);
foreach ($ports->get($customer) as $port) {
$port->port_descr_descr = $customer;
$rows->push($this->formatItem($port));
$customer = ''; // only display customer in the first row
}
// add graphs row
$rows->push($graph_row);
return $rows;
}, collect());
return response()->json([
'current' => $paginator->currentPage(),
'rowCount' => $paginator->count(),
'rows' => $rows,
'total' => $paginator->total(),
]);
}
/**
* @param Port $port
* @return array|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection
*/
public function formatItem($port)
{
return [
'port_descr_descr' => $port->port_descr_descr,
'hostname' => Url::deviceLink($port->device),
'ifDescr' => Url::portLink($port),
'port_descr_speed' => $port->port_descr_speed,
'port_descr_circuit' => $port->port_descr_circuit,
'port_descr_notes' => $port->port_descr_notes,
];
}
private function getGraphRow($customer)
{
$graph_array = [
'type' => 'customer_bits',
'height' => 100,
'width' => 220,
'id' => $customer,
];
$graph_data = Html::graphRow($graph_array);
return [
'port_descr_descr' => $graph_data[0],
'hostname' => $graph_data[1],
'ifDescr' => '',
'port_descr_speed' => '',
'port_descr_circuit' => $graph_data[2],
'port_descr_notes' => $graph_data[3],
];
}
}

View File

@@ -1,104 +0,0 @@
<?php
use LibreNMS\Config;
$cust_descrs = (array)Config::get('customers_descr', ['cust']);
$sql = ' FROM `ports` LEFT JOIN `devices` AS `D` ON `ports`.`device_id` = `D`.`device_id` WHERE `port_descr_type` IN ' . dbGenPlaceholders(count($cust_descrs));
$param = $cust_descrs;
if (isset($searchPhrase) && !empty($searchPhrase)) {
$sql .= " AND (`port_descr_descr` LIKE ? OR `ifName` LIKE ? OR `ifDescr` LIKE ? OR `ifAlias` LIKE ? OR `D`.`hostname` LIKE ? OR `port_descr_speed` LIKE ? OR `port_descr_notes` LIKE ?)";
array_push($param, "%$searchPhrase%", "%$searchPhrase%", "%$searchPhrase%", "%$searchPhrase%", "%$searchPhrase%", "%$searchPhrase%", "%$searchPhrase%");
}
$count_sql = "SELECT COUNT(DISTINCT(`port_descr_descr`)) $sql";
$sql .= ' GROUP BY `port_descr_descr`';
$total = dbFetchCell($count_sql, $param);
if (empty($total)) {
$total = 0;
}
if (!isset($sort) || empty($sort)) {
$sort = '`port_descr_descr`';
}
$sql .= " ORDER BY $sort";
if (isset($current)) {
$limit_low = ($current * $rowCount) - ($rowCount);
$limit_high = $rowCount;
}
if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high";
}
$sql = "SELECT `port_descr_descr` $sql";
foreach (dbFetchRows($sql, $param) as $customer) {
$customer_name = $customer['port_descr_descr'];
$port_query = 'SELECT * FROM `ports` WHERE `port_descr_type` IN ' . dbGenPlaceholders(count($cust_descrs)) . ' AND `port_descr_descr` = ?';
$port_params = $cust_descrs;
$port_params[] = $customer_name;
foreach (dbFetchRows($port_query, $port_params) as $port) {
$device = device_by_id_cache($port['device_id']);
$ifname = fixifname($device['ifDescr']);
$ifclass = ifclass($port['ifOperStatus'], $port['ifAdminStatus']);
$port = cleanPort($port);
if ($device['os'] == 'ios') {
if ($port['ifTrunk']) {
$vlan = '<span class=box-desc><span class=red>'.$port['ifTrunk'].'</span></span>';
} elseif ($port['ifVlan']) {
$vlan = '<span class=box-desc><span class=blue>VLAN '.$port['ifVlan'].'</span></span>';
} else {
$vlan = '';
}
}
$response[] = array(
'port_descr_descr' => $customer_name,
'device_id' => generate_device_link($device),
'ifDescr' => generate_port_link($port, makeshortif($port['ifDescr'])),
'port_descr_speed' => $port['port_descr_speed'],
'port_descr_circuit' => $port['port_descr_circuit'],
'port_descr_notes' => $port['port_descr_notes'],
);
unset($customer_name);
}
$graph_array['type'] = 'customer_bits';
$graph_array['height'] = '100';
$graph_array['width'] = '220';
$graph_array['to'] = $config['time']['now'];
$graph_array['id'] = $customer['port_descr_descr'];
$return_data = true;
include 'includes/print-graphrow.inc.php';
$response[] = array(
'port_descr_descr' => $graph_data[0],
'device_id' => $graph_data[1],
'ifDescr' => '',
'port_descr_speed' => '',
'port_descr_circuit' => $graph_data[2],
'port_descr_notes' => $graph_data[3],
);
}
$output = array(
'current' => $current,
'rowCount' => $rowCount,
'rows' => $response,
'total' => $total,
);
echo _json_encode($output);

View File

@@ -1,15 +1,16 @@
<?php
$pagetitle[] = 'Customers';
$no_refresh = true;
?>
<div class="table-responsive">
<table id="customers" class="table table-hover table-condensed table-striped">
<table id="customers" class="table table-hover table-condensed">
<thead>
<tr>
<th data-column-id="port_descr_descr" data-order="asc">Customer</th>
<th data-column-id="device_id">Device</th>
<th data-column-id="port_descr_descr" data-order="asc" data-formatter="customer">Customer</th>
<th data-column-id="hostname">Device</th>
<th data-column-id="ifDescr">Interface</th>
<th data-column-id="port_descr_speed">Speed</th>
<th data-column-id="port_descr_circuit">Circuit</th>
@@ -24,12 +25,11 @@ $pagetitle[] = 'Customers';
var grid = $("#customers").bootgrid({
ajax: true,
rowCount: [50, 100, 250, -1],
post: function ()
{
return {
id: "customers",
};
},
url: "ajax_table.php"
url: "ajax/table/customers",
formatters: {
customer: function (column, row) {
return '<strong>' + row[column.id] + '</strong>';
}
}
});
</script>

View File

@@ -330,17 +330,8 @@ function get_device_id_by_app_id($app_id)
function ifclass($ifOperStatus, $ifAdminStatus)
{
$ifclass = "interface-upup";
if ($ifAdminStatus == "down") {
$ifclass = "interface-admindown";
}
if ($ifAdminStatus == "up" && $ifOperStatus== "down") {
$ifclass = "interface-updown";
}
if ($ifAdminStatus == "up" && $ifOperStatus== "up") {
$ifclass = "interface-upup";
}
return $ifclass;
// fake a port model
return \LibreNMS\Util\Url::portLinkDisplayClass((object) ['ifOperStatus' => $ifOperStatus, 'ifAdminStatus' => $ifAdminStatus]);
}
function device_by_name($name, $refresh = 0)

View File

@@ -47,6 +47,7 @@ Route::group(['middleware' => ['auth', '2fa'], 'guard' => 'auth'], function () {
});
Route::group(['prefix' => 'table', 'namespace' => 'Table'], function () {
Route::post('customers', 'CustomersController');
Route::post('eventlog', 'EventlogController');
Route::post('location', 'LocationController');
Route::post('syslog', 'SyslogController');