Change node vendor report by vendor to use server-side processing

This commit is contained in:
Eric A. Miller
2014-06-19 19:03:09 -04:00
parent 89de64b20d
commit 184f320299
2 changed files with 80 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ package App::Netdisco::Web::Plugin::Report::NodeVendor;
use Dancer ':syntax'; use Dancer ':syntax';
use Dancer::Plugin::DBIC; use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Util::ExpandParams 'expand_hash';
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
@@ -35,14 +36,14 @@ hook 'before_template' => sub {
} }
}; };
get '/ajax/content/report/nodevendor' => require_login sub { get '/ajax/content/report/nodevendor/data' => require_login sub {
send_error( 'Missing parameter', 400 )
unless ( param('draw') && param('draw') =~ /\d+/ );
my $vendor = param('vendor'); my $vendor = param('vendor');
my $rs = schema('netdisco')->resultset('Node'); my $rs = schema('netdisco')->resultset('Node');
if ( defined $vendor ) {
my $match = $vendor eq 'blank' ? undef : $vendor; my $match = $vendor eq 'blank' ? undef : $vendor;
$rs = $rs->search( { 'oui.abbrev' => $match }, $rs = $rs->search( { 'oui.abbrev' => $match },
@@ -54,20 +55,66 @@ get '/ajax/content/report/nodevendor' => require_login sub {
unless ( param('archived') ) { unless ( param('archived') ) {
$rs = $rs->search( { -bool => 'me.active' } ); $rs = $rs->search( { -bool => 'me.active' } );
} }
my $exp_params = expand_hash( scalar params );
my $recordsTotal = $rs->count;
my @data = $rs->get_datatables_data($exp_params)->hri->all;
my $recordsFiltered = $rs->get_datatables_filtered_count($exp_params);
content_type 'application/json';
return to_json(
{ draw => int( param('draw') ),
recordsTotal => int($recordsTotal),
recordsFiltered => int($recordsFiltered),
data => \@data,
}
);
};
get '/ajax/content/report/nodevendor' => require_login sub {
my $vendor = param('vendor');
my $rs = schema('netdisco')->resultset('Node');
my @results;
if ( defined $vendor && !request->is_ajax ) {
my $match = $vendor eq 'blank' ? undef : $vendor;
$rs = $rs->search( { 'oui.abbrev' => $match },
{ '+columns' => [qw/ device.dns device.name oui.abbrev /],
join => [qw/ oui device /],
collapse => 1,
});
unless ( param('archived') ) {
$rs = $rs->search( { -bool => 'me.active' } );
}
@results = $rs->hri->all;
return unless scalar @results;
} }
else { elsif ( !defined $vendor ) {
$rs = $rs->search( $rs = $rs->search(
{ -bool => 'me.active' }, { },
{ join => 'oui', { join => 'oui',
select => [ 'oui.abbrev', { count => 'me.mac' } ], select => [ 'oui.abbrev', { count => 'me.mac' } ],
as => [qw/ vendor count /], as => [qw/ vendor count /],
group_by => [qw/ oui.abbrev /] group_by => [qw/ oui.abbrev /]
} }
)->order_by( { -desc => 'count' } ); )->order_by( { -desc => 'count' } );
}
my @results = $rs->hri->all; unless ( param('archived') ) {
return unless scalar @results; $rs = $rs->search( { -bool => 'me.active' } );
}
@results = $rs->hri->all;
return unless scalar @results;
}
if ( request->is_ajax ) { if ( request->is_ajax ) {
my $json = to_json( \@results ); my $json = to_json( \@results );

View File

@@ -1,3 +1,4 @@
[% USE url %]
[% IF opt %] [% IF opt %]
<table id="data-table" class="table table-striped table-bordered" width="100%" cellspacing="0"> <table id="data-table" class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead> <thead>
@@ -5,6 +6,9 @@
<th>MAC</th> <th>MAC</th>
<th>Vendor</th> <th>Vendor</th>
<th>Device (Port)</th> <th>Device (Port)</th>
<th>Device DNS</th>
<th>Device Name</th>
<th>Device IP</th>
</tr> </tr>
</thead> </thead>
</table> </table>
@@ -22,11 +26,16 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
var table = $('#data-table').dataTable({ var table = $('#data-table').dataTable({
"deferRender": true, "processing": true,
"stateSave": true, "stateSave": true,
"pageLength": 25, "pageLength": 25,
"data": [% results %], "language": {
"search": 'Filter records: '
},
[% IF opt %] [% IF opt %]
"serverSide": true,
"order": [[ 0, "desc" ]],
"ajax": '/ajax/content/report/nodevendor/data?[% url(params('query').hash) %]',
"columns": [ "columns": [
{ {
"data": 'mac', "data": 'mac',
@@ -47,9 +56,23 @@ $(document).ready(function() {
"render": function(data, type, row, meta) { "render": function(data, type, row, meta) {
return '<a href="[% device_ports %]&q=' + encodeURIComponent(row.switch) + '&f=' + encodeURIComponent(data) + '&c_nodes=on&n_ssid=on">' + he.encode(row.device.dns || row.device.name || row.switch) + '(' + he.encode(data) + ')</a>'; return '<a href="[% device_ports %]&q=' + encodeURIComponent(row.switch) + '&f=' + encodeURIComponent(data) + '&c_nodes=on&n_ssid=on">' + he.encode(row.device.dns || row.device.name || row.switch) + '(' + he.encode(data) + ')</a>';
} }
}, {
// Included for filtering
"data": 'device.dns',
"visible": false
}, {
// Included for filtering
"data": 'device.name',
"visible": false
}, {
// Included for filtering
"data": 'switch',
"visible": false
} }
] ]
[% ELSE %] [% ELSE %]
"deferRender": true,
"data": [% results %],
"columns": [ "columns": [
{ {
"data": 'vendor', "data": 'vendor',