Convert device module inventory web report to DataTables server-side processing
This commit is contained in:
@@ -3,6 +3,7 @@ package App::Netdisco::Web::Plugin::Report::ModuleInventory;
|
|||||||
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;
|
||||||
use List::MoreUtils ();
|
use List::MoreUtils ();
|
||||||
@@ -58,16 +59,59 @@ hook 'before_template' => sub {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
get '/ajax/content/report/moduleinventory/data' => require_login sub {
|
||||||
|
|
||||||
|
my $rs = schema('netdisco')->resultset('DeviceModule');
|
||||||
|
$rs = $rs->search( { -bool => 'fru' } ) if param('fruonly');
|
||||||
|
|
||||||
|
if ( param('device') ) {
|
||||||
|
my @ips = schema('netdisco')->resultset('Device')
|
||||||
|
->search_fuzzy( param('device') )->get_column('ip')->all;
|
||||||
|
|
||||||
|
params->{'ips'} = \@ips;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rs = $rs->search_by_field( scalar params )->columns(
|
||||||
|
[ 'ip', 'description', 'name', 'class',
|
||||||
|
'type', 'serial', 'hw_ver', 'fw_ver',
|
||||||
|
'sw_ver', 'model'
|
||||||
|
]
|
||||||
|
)->search(
|
||||||
|
{},
|
||||||
|
{ '+columns' => [qw/ device.dns device.name /],
|
||||||
|
join => 'device',
|
||||||
|
collapse => 1,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
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/moduleinventory' => require_login sub {
|
get '/ajax/content/report/moduleinventory' => require_login sub {
|
||||||
|
|
||||||
my $has_opt = List::MoreUtils::any { param($_) }
|
my $has_opt = List::MoreUtils::any { param($_) }
|
||||||
qw/device description name type model serial class/;
|
qw/device description name type model serial class/;
|
||||||
|
|
||||||
my $rs = schema('netdisco')->resultset('DeviceModule');
|
my $rs = schema('netdisco')->resultset('DeviceModule');
|
||||||
$rs = $rs->search({-bool => 'fru'}) if param('fruonly');
|
$rs = $rs->search( { -bool => 'fru' } ) if param('fruonly');
|
||||||
my @results;
|
my @results;
|
||||||
|
|
||||||
if ($has_opt) {
|
if ( $has_opt && !request->is_ajax ) {
|
||||||
|
|
||||||
if ( param('device') ) {
|
if ( param('device') ) {
|
||||||
my @ips = schema('netdisco')->resultset('Device')
|
my @ips = schema('netdisco')->resultset('Device')
|
||||||
@@ -84,23 +128,25 @@ get '/ajax/content/report/moduleinventory' => require_login sub {
|
|||||||
{ '+columns' => [qw/ device.dns device.name /],
|
{ '+columns' => [qw/ device.dns device.name /],
|
||||||
join => 'device',
|
join => 'device',
|
||||||
collapse => 1,
|
collapse => 1,
|
||||||
})->hri->all;
|
}
|
||||||
|
)->hri->all;
|
||||||
|
|
||||||
|
return unless scalar @results;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@results = $rs->search(
|
@results = $rs->search(
|
||||||
{class => { '!=', undef }},
|
{ class => { '!=', undef } },
|
||||||
{ select => [ 'class', { count => 'class' } ],
|
{ select => [ 'class', { count => 'class' } ],
|
||||||
as => [qw/ class count /],
|
as => [qw/ class count /],
|
||||||
group_by => [qw/ class /]
|
group_by => [qw/ class /]
|
||||||
}
|
}
|
||||||
)->order_by( { -desc => 'count' } )->hri->all;
|
)->order_by( { -desc => 'count' } )->hri->all;
|
||||||
|
|
||||||
|
return unless scalar @results;
|
||||||
}
|
}
|
||||||
|
|
||||||
return unless scalar @results;
|
|
||||||
|
|
||||||
if ( request->is_ajax ) {
|
if ( request->is_ajax ) {
|
||||||
my $json = to_json (\@results);
|
my $json = to_json( \@results );
|
||||||
template 'ajax/report/moduleinventory.tt',
|
template 'ajax/report/moduleinventory.tt',
|
||||||
{ results => $json, opt => $has_opt },
|
{ results => $json, opt => $has_opt },
|
||||||
{ layout => undef };
|
{ layout => undef };
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -14,21 +15,6 @@
|
|||||||
<th>FW Version</th>
|
<th>FW Version</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
|
||||||
<tfoot>
|
|
||||||
<tr>
|
|
||||||
<th>Device</th>
|
|
||||||
<th>Description</th>
|
|
||||||
<th>Name</th>
|
|
||||||
<th>Class</th>
|
|
||||||
<th>Type</th>
|
|
||||||
<th>Model</th>
|
|
||||||
<th>Serial</th>
|
|
||||||
<th>HW Version</th>
|
|
||||||
<th>SW Version</th>
|
|
||||||
<th>FW Version</th>
|
|
||||||
</tr>
|
|
||||||
</tfoot>
|
|
||||||
</table>
|
</table>
|
||||||
[% ELSE %]
|
[% ELSE %]
|
||||||
<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">
|
||||||
@@ -44,10 +30,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,
|
[% IF opt %]
|
||||||
"data": [% results %],
|
"processing": true,
|
||||||
|
"serverSide": true,
|
||||||
|
"searching": false,
|
||||||
|
"stateSave": true,
|
||||||
|
"pageLength": 25,
|
||||||
|
"order": [[ 0, "desc" ]],
|
||||||
|
"ajax": '/ajax/content/report/moduleinventory/data?[% url(params('query').hash) %]',
|
||||||
"columns": [
|
"columns": [
|
||||||
[% IF opt %] {
|
{
|
||||||
"data": 'ip',
|
"data": 'ip',
|
||||||
"render": function(data, type, row, meta) {
|
"render": function(data, type, row, meta) {
|
||||||
return '<a href="[% uri_for('/device') %]?tab=modules&q=' + encodeURIComponent(data) + '">' + he.encode(row.device.dns || row.device.name || row.ip) + '</a>';
|
return '<a href="[% uri_for('/device') %]?tab=modules&q=' + encodeURIComponent(data) + '">' + he.encode(row.device.dns || row.device.name || row.ip) + '</a>';
|
||||||
@@ -98,7 +90,12 @@ $(document).ready(function() {
|
|||||||
return he.encode(data || '');
|
return he.encode(data || '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[% ELSE %] {
|
]
|
||||||
|
[% ELSE %]
|
||||||
|
"deferRender": true,
|
||||||
|
"data": [% results %],
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
"data": 'class',
|
"data": 'class',
|
||||||
"render": function(data, type, row, meta) {
|
"render": function(data, type, row, meta) {
|
||||||
return '<a href="[% uri_for('/report/moduleinventory') %]?class=' + encodeURIComponent(data) + '">' + he.encode(capitalizeFirstLetter(data + '')) + '</a>';
|
return '<a href="[% uri_for('/report/moduleinventory') %]?class=' + encodeURIComponent(data) + '">' + he.encode(capitalizeFirstLetter(data + '')) + '</a>';
|
||||||
@@ -109,8 +106,8 @@ $(document).ready(function() {
|
|||||||
return data.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
return data.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[% END %]
|
|
||||||
]
|
]
|
||||||
|
[% END %]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user