add csv download option to device, port, and vlan search pages

This commit is contained in:
Eric A. Miller
2013-09-18 23:32:57 -04:00
parent d2b39a3f0e
commit 99ada1132c
10 changed files with 173 additions and 18 deletions

View File

@@ -11,21 +11,37 @@ use App::Netdisco::Web::Plugin;
register_search_tab({ tag => 'device', label => 'Device' });
my $headers = ['Device','Contact','Location','System Name','Model','OS Version','Management IP','Serial'];
# device with various properties or a default match-all
ajax '/ajax/content/search/device' => require_login sub {
my $has_opt = List::MoreUtils::any {param($_)}
qw/name location dns ip description model os_ver vendor/;
sub get_rs_device {
my $q = shift;
my $has_opt = shift;
my $set;
if ($has_opt) {
$set = schema('netdisco')->resultset('Device')->search_by_field(scalar params);
}
else {
my $q = param('q');
send_error('Missing query', 400) unless $q;
$set = schema('netdisco')->resultset('Device')->search_fuzzy($q);
}
return $set;
}
ajax '/ajax/content/search/device' => require_login sub {
my $q = param('q');
my $has_opt = List::MoreUtils::any {param($_)}
qw/name location dns ip description model os_ver vendor/;
unless ($has_opt || $q) {
send_error('Missing query', 400)
}
my $set = get_rs_device($q, $has_opt);
return unless $set->count;
content_type('text/html');
@@ -34,4 +50,33 @@ ajax '/ajax/content/search/device' => require_login sub {
}, { layout => undef };
};
get '/search/device' => require_login sub {
my $q = param('q');
my $format = param('format');
my $has_opt = List::MoreUtils::any {param($_)}
qw/name location dns ip description model os_ver vendor/;
unless ($has_opt || $q) {
send_error('Missing query', 400)
}
my $set = get_rs_device($q, $has_opt);
return unless $set->count;
if ( $format eq 'csv' ) {
header( 'Content-Type' => 'text/comma-separated-values' );
header( 'Content-Disposition' =>
"attachment; filename=\"nd-devicesearch.csv\"" );
template 'ajax/search/device_csv.tt', {
results => $set,
headers => $headers,
}, { layout => undef };
}
else {
return;
}
};
true;

View File

@@ -9,10 +9,9 @@ use App::Netdisco::Web::Plugin;
register_search_tab({ tag => 'port', label => 'Port' });
# device ports with a description (er, name) matching
ajax '/ajax/content/search/port' => require_login sub {
my $q = param('q');
send_error('Missing query', 400) unless $q;
sub get_rs_port {
my $q = shift;
my $set;
if ($q =~ m/^\d+$/) {
@@ -28,6 +27,16 @@ ajax '/ajax/content/search/port' => require_login sub {
$set = schema('netdisco')->resultset('DevicePort')
->search({name => $query});
}
return $set;
}
# device ports with a description (er, name) matching
ajax '/ajax/content/search/port' => require_login sub {
my $q = param('q');
send_error('Missing query', 400) unless $q;
my $set = get_rs_port($q);
return unless $set->count;
content_type('text/html');
@@ -36,4 +45,27 @@ ajax '/ajax/content/search/port' => require_login sub {
}, { layout => undef };
};
get '/search/port' => require_login sub {
my $q = param('q');
my $format = param('format');
send_error('Missing query', 400) unless $q;
my $set = get_rs_port($q);
return unless $set->count;
if ( $format eq 'csv' ) {
header( 'Content-Type' => 'text/comma-separated-values' );
header( 'Content-Disposition' =>
"attachment; filename=\"nd-portsearch.csv\"" );
template 'ajax/search/port_csv.tt', {
results => $set,
}, { layout => undef };
}
else {
return;
}
};
true;

View File

@@ -10,23 +10,54 @@ use App::Netdisco::Web::Plugin;
register_search_tab( { tag => 'vlan', label => 'VLAN' } );
# devices carrying vlan xxx
ajax '/ajax/content/search/vlan' => require_login sub {
my $q = param('q');
send_error('Missing query', 400) unless $q;
sub get_rs_vlan {
my $q = shift;
my $set;
if ( $q =~ m/^\d+$/ ) {
$set = schema('netdisco')->resultset('Device')->carrying_vlan({vlan => $q});
$set = schema('netdisco')->resultset('Device')
->carrying_vlan( { vlan => $q } );
}
else {
$set = schema('netdisco')->resultset('Device')->carrying_vlan_name({name => $q});
$set = schema('netdisco')->resultset('Device')
->carrying_vlan_name( { name => $q } );
}
return $set;
}
ajax '/ajax/content/search/vlan' => require_login sub {
my $q = param('q');
send_error( 'Missing query', 400 ) unless $q;
my $set = get_rs_vlan($q);
return unless $set->count;
content_type('text/html');
template 'ajax/search/vlan.tt', {
results => $set,
}, { layout => undef };
template 'ajax/search/vlan.tt', { results => $set, }, { layout => undef };
};
get '/search/vlan' => require_login sub {
my $q = param('q');
my $format = param('format');
send_error( 'Missing query', 400 ) unless $q;
my $set = get_rs_vlan($q);
return unless $set->count;
if ( $format eq 'csv' ) {
header( 'Content-Type' => 'text/comma-separated-values' );
header( 'Content-Disposition' =>
"attachment; filename=\"nd-vlansearch.csv\"" );
template 'ajax/search/vlan_csv.tt', { results => $set, },
{ layout => undef };
}
else {
return;
}
};
true;

View File

@@ -1,3 +1,4 @@
[% INCLUDE "download_as.tt" %]
<table class="table table-bordered table-condensed table-striped nd_floatinghead">
<thead>
<tr>

View File

@@ -0,0 +1,12 @@
[% USE CSV -%]
[% CSV.dump(headers) %]
[% WHILE (row = results.next) %]
[% mylist = [] %]
[% device = row.dns || row.ip %]
[% FOREACH col IN [ device row.contact row.location row.name row.model row.os_ver row.ip row.serial] %]
[% mylist.push(col) %]
[% END %]
[% CSV.dump(mylist) %]
[% END %]

View File

@@ -1,3 +1,4 @@
[% INCLUDE "download_as.tt" %]
<table class="table table-bordered table-condensed table-striped nd_floatinghead">
<thead>
<tr>

View File

@@ -0,0 +1,12 @@
[% USE CSV -%]
[% CSV.dump([ 'Description' 'Port' 'Name' 'Vlan' ]) %]
[% WHILE (row = results.next) %]
[% mylist = [] %]
[% myport = "$row.ip [ $row.port ] (" _ row.device.dns _ ")" IF row.device.dns %]
[% FOREACH col IN [ row.name myport row.descr row.vlan ] %]
[% mylist.push(col) %]
[% END %]
[% CSV.dump(mylist) %]
[% END %]

View File

@@ -1,3 +1,4 @@
[% INCLUDE "download_as.tt" %]
<table class="table table-bordered table-condensed table-striped nd_floatinghead">
<thead>
<tr>

View File

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

View File

@@ -0,0 +1,8 @@
[% myuri = request.uri %]
[% UNLESS myuri.match('/report') %]
[% myuri = myuri.remove('/ajax/content/') %]
[% END %]
<div>
<p class="text-right">Download as: <a href="[% myuri %]&format=csv">CSV</a></p>
</div>