DataTables for VLAN search results tab

Modify carrying_vlan and carrying_vlan_name searches to use device_vlan table to capture devices carrying a VLAN but no port members
This commit is contained in:
Eric A. Miller
2014-06-02 23:27:40 -04:00
parent fb65ae30f0
commit 1258049483
4 changed files with 70 additions and 42 deletions

View File

@@ -343,8 +343,7 @@ sub search_fuzzy {
Like C<search()>, this returns a ResultSet of matching rows from the Device Like C<search()>, this returns a ResultSet of matching rows from the Device
table. table.
The returned devices each are aware of the given Vlan and have at least one The returned devices each are aware of the given Vlan.
Port configured in the Vlan (either tagged, or not).
=over 4 =over 4
@@ -371,17 +370,17 @@ sub carrying_vlan {
die "vlan number required for carrying_vlan\n" die "vlan number required for carrying_vlan\n"
if ref {} ne ref $cond or !exists $cond->{vlan}; if ref {} ne ref $cond or !exists $cond->{vlan};
$cond->{'-and'} ||= [];
push @{$cond->{'-and'}}, { 'vlans.vlan' => $cond->{vlan} };
push @{$cond->{'-and'}}, { 'port_vlans.vlan' => delete $cond->{vlan} };
return $rs return $rs
->search_rs($cond, ->search_rs({ 'vlans.vlan' => $cond->{vlan} },
{ {
order_by => [qw/ me.dns me.ip /], order_by => [qw/ me.dns me.ip /],
columns => [qw/ me.ip me.dns me.model me.os me.vendor /], columns => [
join => 'port_vlans', 'me.ip', 'me.dns',
prefetch => 'vlans', 'me.model', 'me.os',
'me.vendor', 'vlans.vlan',
'vlans.description'
],
join => 'vlans'
}) })
->search({}, $attrs); ->search({}, $attrs);
} }
@@ -393,8 +392,7 @@ sub carrying_vlan {
Like C<search()>, this returns a ResultSet of matching rows from the Device Like C<search()>, this returns a ResultSet of matching rows from the Device
table. table.
The returned devices each are aware of the named Vlan and have at least one The returned devices each are aware of the named Vlan.
Port configured in the Vlan (either tagged, or not).
=over 4 =over 4
@@ -427,8 +425,13 @@ sub carrying_vlan_name {
return $rs return $rs
->search_rs({}, { ->search_rs({}, {
order_by => [qw/ me.dns me.ip /], order_by => [qw/ me.dns me.ip /],
columns => [qw/ me.ip me.dns me.model me.os me.vendor /], columns => [
prefetch => 'vlans', 'me.ip', 'me.dns',
'me.model', 'me.os',
'me.vendor', 'vlans.vlan',
'vlans.description'
],
join => 'vlans'
}) })
->search($cond, $attrs); ->search($cond, $attrs);
} }

View File

@@ -12,27 +12,30 @@ register_search_tab( { tag => 'vlan', label => 'VLAN', provides_csv => 1 } );
get '/ajax/content/search/vlan' => require_login sub { get '/ajax/content/search/vlan' => require_login sub {
my $q = param('q'); my $q = param('q');
send_error( 'Missing query', 400 ) unless $q; send_error( 'Missing query', 400 ) unless $q;
my $set; my $rs;
if ( $q =~ m/^\d+$/ ) { if ( $q =~ m/^\d+$/ ) {
$set = schema('netdisco')->resultset('Device') $rs = schema('netdisco')->resultset('Device')
->carrying_vlan( { vlan => $q } ); ->carrying_vlan( { vlan => $q } );
} }
else { else {
$set = schema('netdisco')->resultset('Device') $rs = schema('netdisco')->resultset('Device')
->carrying_vlan_name( { name => $q } ); ->carrying_vlan_name( { name => $q } );
} }
return unless $set->count;
my @results = $rs->hri->all;
return unless scalar @results;
if (request->is_ajax) { if (request->is_ajax) {
template 'ajax/search/vlan.tt', { results => $set }, my $json = to_json( \@results );
template 'ajax/search/vlan.tt', { results => $json },
{ layout => undef }; { layout => undef };
} }
else { else {
header( 'Content-Type' => 'text/comma-separated-values' ); header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/search/vlan_csv.tt', { results => $set }, template 'ajax/search/vlan_csv.tt', { results => \@results },
{ layout => undef }; { layout => undef };
} }
}; };
true; 1;

View File

@@ -1,4 +1,4 @@
<table class="table table-bordered table-condensed table-striped nd_floatinghead"> <table id="data-table" class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead> <thead>
<tr> <tr>
<th>Vlan</th> <th>Vlan</th>
@@ -9,22 +9,44 @@
<th>Vendor</th> <th>Vendor</th>
</tr> </tr>
</thead> </thead>
</tbody>
[% WHILE (row = results.next) %]
<tr>
<td><a class="nd_linkcell nd_stealth-link"
href="[% device_ports %]&q=[% row.ip | uri %]&f=[% row.vlan.vlan | uri %]">[% row.vlan.vlan | html_entity %]</a></td>
<td><a class="nd_linkcell"
href="[% device_ports %]&q=[% row.ip | uri %]&f=[% row.vlan.vlan | uri %]">[% row.dns || row.ip | html_entity %]</a></td>
<td><a class="nd_linkcell nd_stealth-link"
href="[% device_ports %]&q=[% row.ip | uri %]&f=[% row.vlan.vlan | uri %]">[% row.vlan.description | html_entity %]</a></td>
<td><a class="nd_linkcell nd_stealth-link"
href="[% device_ports %]&q=[% row.ip | uri %]&f=[% row.vlan.vlan | uri %]">[% row.model | html_entity %]</a></td>
<td><a class="nd_linkcell nd_stealth-link"
href="[% device_ports %]&q=[% row.ip | uri %]&f=[% row.vlan.vlan | uri %]">[% row.os | html_entity %]</a></td>
<td><a class="nd_linkcell nd_stealth-link"
href="[% device_ports %]&q=[% row.ip | uri %]&f=[% row.vlan.vlan | uri %]">[% row.vendor | html_entity %]</a></td>
</tr>
[% END %]
</tbody>
</table> </table>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#data-table').dataTable({
"deferRender": true,
"data": [% results %],
"columns": [{
"data": 'vlans.vlan',
"render": function(data, type, row, meta) {
return '<a class="nd_linkcell nd_stealth-link" href="[% device_ports %]&q=' + encodeURIComponent(row.ip) + '&f=' + encodeURIComponent(data) + '">' + data + '</a>';
}
}, {
"data": 'ip',
"render": function(data, type, row, meta) {
return '<a href="[% device_ports %]&q=' + encodeURIComponent(row.ip) + '&f=' + encodeURIComponent(row.vlans.vlan) + '">' + he.encode(row.dns || row.ip) + '</a>';
}
}, {
"data": 'vlans.description',
"render": function(data, type, row, meta) {
return '<a class="nd_linkcell nd_stealth-link" href="[% device_ports %]&q=' + encodeURIComponent(row.ip) + '&f=' + encodeURIComponent(row.vlans.vlan) + '">' + he.encode(data || '') + '</a>';
}
}, {
"data": 'model',
"render": function(data, type, row, meta) {
return '<a class="nd_linkcell nd_stealth-link" href="[% device_ports %]&q=' + encodeURIComponent(row.ip) + '&f=' + encodeURIComponent(row.vlans.vlan) + '">' + he.encode(data || '') + '</a>';
}
}, {
"data": 'os',
"render": function(data, type, row, meta) {
return '<a class="nd_linkcell nd_stealth-link" href="[% device_ports %]&q=' + encodeURIComponent(row.ip) + '&f=' + encodeURIComponent(row.vlans.vlan) + '">' + he.encode(data || '') + '</a>';
}
}, {
"data": 'vendor',
"render": function(data, type, row, meta) {
return '<a class="nd_linkcell nd_stealth-link" href="[% device_ports %]&q=' + encodeURIComponent(row.ip) + '&f=' + encodeURIComponent(row.vlans.vlan) + '">' + he.encode(data || '') + '</a>';
}
}
]
});
});</script>

View File

@@ -1,10 +1,10 @@
[% USE CSV -%] [% USE CSV -%]
[% CSV.dump([ 'Vlan' 'Device' 'Description' 'Model' 'OS' 'Vendor' ]) %] [% CSV.dump([ 'Vlan' 'Device' 'Description' 'Model' 'OS' 'Vendor' ]) %]
[% WHILE (row = results.next) %] [% FOREACH row IN results %]
[% mylist = [] %] [% mylist = [] %]
[% device = row.dns || row.ip %] [% device = row.dns || row.ip %]
[% FOREACH col IN [ row.vlan.vlan device row.vlan.description row.model row.os row.vendor ] %] [% FOREACH col IN [ row.vlans.vlan device row.vlans.description row.model row.os row.vendor ] %]
[% mylist.push(col) %] [% mylist.push(col) %]
[% END %] [% END %]
[% CSV.dump(mylist) %] [% CSV.dump(mylist) %]