diff --git a/LibreNMS/Util/Url.php b/LibreNMS/Util/Url.php index d3b69cdfed..b8e62cfd13 100644 --- a/LibreNMS/Util/Url.php +++ b/LibreNMS/Util/Url.php @@ -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"; diff --git a/app/Http/Controllers/Table/CustomersController.php b/app/Http/Controllers/Table/CustomersController.php new file mode 100644 index 0000000000..7a40aa2747 --- /dev/null +++ b/app/Http/Controllers/Table/CustomersController.php @@ -0,0 +1,129 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2018 Tony Murray + * @author Tony Murray + */ + +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], + ]; + } +} diff --git a/html/includes/table/customers.inc.php b/html/includes/table/customers.inc.php deleted file mode 100644 index 6782d26e15..0000000000 --- a/html/includes/table/customers.inc.php +++ /dev/null @@ -1,104 +0,0 @@ -'.$port['ifTrunk'].''; - } elseif ($port['ifVlan']) { - $vlan = 'VLAN '.$port['ifVlan'].''; - } 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); diff --git a/html/pages/customers.inc.php b/html/pages/customers.inc.php index 20289cc394..6572ae2b48 100644 --- a/html/pages/customers.inc.php +++ b/html/pages/customers.inc.php @@ -1,15 +1,16 @@
- +
- - + + @@ -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 '' + row[column.id] + ''; + } + } }); diff --git a/includes/common.php b/includes/common.php index 5d1f5023e4..aca728deee 100644 --- a/includes/common.php +++ b/includes/common.php @@ -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) diff --git a/routes/web.php b/routes/web.php index 333fbfbe12..6bfd610eef 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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');
CustomerDeviceCustomerDevice Interface Speed Circuit