From 1258049483bf0b44c30a03078e873eb27d951086 Mon Sep 17 00:00:00 2001 From: "Eric A. Miller" Date: Mon, 2 Jun 2014 23:27:40 -0400 Subject: [PATCH] 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 --- .../lib/App/Netdisco/DB/ResultSet/Device.pm | 31 +++++----- .../App/Netdisco/Web/Plugin/Search/VLAN.pm | 17 +++--- Netdisco/share/views/ajax/search/vlan.tt | 60 +++++++++++++------ Netdisco/share/views/ajax/search/vlan_csv.tt | 4 +- 4 files changed, 70 insertions(+), 42 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm b/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm index 66eaa0cf..f64ca2b1 100644 --- a/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm +++ b/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm @@ -343,8 +343,7 @@ sub search_fuzzy { Like C, this returns a ResultSet of matching rows from the Device table. -The returned devices each are aware of the given Vlan and have at least one -Port configured in the Vlan (either tagged, or not). +The returned devices each are aware of the given Vlan. =over 4 @@ -371,17 +370,17 @@ sub carrying_vlan { die "vlan number required for carrying_vlan\n" 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 - ->search_rs($cond, + ->search_rs({ 'vlans.vlan' => $cond->{vlan} }, { order_by => [qw/ me.dns me.ip /], - columns => [qw/ me.ip me.dns me.model me.os me.vendor /], - join => 'port_vlans', - prefetch => 'vlans', + columns => [ + 'me.ip', 'me.dns', + 'me.model', 'me.os', + 'me.vendor', 'vlans.vlan', + 'vlans.description' + ], + join => 'vlans' }) ->search({}, $attrs); } @@ -393,8 +392,7 @@ sub carrying_vlan { Like C, this returns a ResultSet of matching rows from the Device table. -The returned devices each are aware of the named Vlan and have at least one -Port configured in the Vlan (either tagged, or not). +The returned devices each are aware of the named Vlan. =over 4 @@ -427,8 +425,13 @@ sub carrying_vlan_name { return $rs ->search_rs({}, { order_by => [qw/ me.dns me.ip /], - columns => [qw/ me.ip me.dns me.model me.os me.vendor /], - prefetch => 'vlans', + columns => [ + 'me.ip', 'me.dns', + 'me.model', 'me.os', + 'me.vendor', 'vlans.vlan', + 'vlans.description' + ], + join => 'vlans' }) ->search($cond, $attrs); } diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm index 8081dd8b..315b4228 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm @@ -12,27 +12,30 @@ register_search_tab( { tag => 'vlan', label => 'VLAN', provides_csv => 1 } ); get '/ajax/content/search/vlan' => require_login sub { my $q = param('q'); send_error( 'Missing query', 400 ) unless $q; - my $set; + my $rs; if ( $q =~ m/^\d+$/ ) { - $set = schema('netdisco')->resultset('Device') + $rs = schema('netdisco')->resultset('Device') ->carrying_vlan( { vlan => $q } ); } else { - $set = schema('netdisco')->resultset('Device') + $rs = schema('netdisco')->resultset('Device') ->carrying_vlan_name( { name => $q } ); } - return unless $set->count; + + my @results = $rs->hri->all; + return unless scalar @results; 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 }; } else { 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 }; } }; -true; +1; diff --git a/Netdisco/share/views/ajax/search/vlan.tt b/Netdisco/share/views/ajax/search/vlan.tt index 25a1ab6f..f95cfe3f 100644 --- a/Netdisco/share/views/ajax/search/vlan.tt +++ b/Netdisco/share/views/ajax/search/vlan.tt @@ -1,4 +1,4 @@ - +
@@ -9,22 +9,44 @@ - - [% WHILE (row = results.next) %] - - - - - - - - - [% END %] -
VlanVendor
[% row.vlan.vlan | html_entity %][% row.dns || row.ip | html_entity %][% row.vlan.description | html_entity %][% row.model | html_entity %][% row.os | html_entity %][% row.vendor | html_entity %]
+ + diff --git a/Netdisco/share/views/ajax/search/vlan_csv.tt b/Netdisco/share/views/ajax/search/vlan_csv.tt index 67dd9916..1d095856 100644 --- a/Netdisco/share/views/ajax/search/vlan_csv.tt +++ b/Netdisco/share/views/ajax/search/vlan_csv.tt @@ -1,10 +1,10 @@ [% USE CSV -%] [% CSV.dump([ 'Vlan' 'Device' 'Description' 'Model' 'OS' 'Vendor' ]) %] -[% WHILE (row = results.next) %] +[% FOREACH row IN results %] [% mylist = [] %] [% 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) %] [% END %] [% CSV.dump(mylist) %]