Link from the subnet utilization report to ip inventory report for each subnet with the same date range.

This commit is contained in:
Eric A. Miller
2014-01-01 23:31:57 -05:00
parent 992af240fe
commit ed1158a481
3 changed files with 40 additions and 3 deletions

View File

@@ -1,9 +1,11 @@
package App::Netdisco::Util::Web; package App::Netdisco::Util::Web;
use base 'Exporter'; use base 'Exporter';
use Time::Piece;
use Time::Seconds;
our @EXPORT = (); our @EXPORT = ();
our @EXPORT_OK = qw/ our @EXPORT_OK = qw/
sort_port sort_port sort_modules interval_to_daterange
/; /;
our %EXPORT_TAGS = (all => \@EXPORT_OK); our %EXPORT_TAGS = (all => \@EXPORT_OK);
@@ -150,4 +152,34 @@ sub sort_modules {
return \%modules; return \%modules;
} }
=head2 interval_to_daterange( $interval )
Takes an interval in days, weeks, months, or years in a format like '7 days'
and returns a date range in the format 'YYYY-MM-DD to YYYY-MM-DD' by
subtracting the interval from the current date.
=cut
sub interval_to_daterange {
my $interval = shift;
return unless $interval =~ m/^(?:\d+)\s+(?:day|week|month|year)s?$/;
my %const = (
day => ONE_DAY,
week => ONE_WEEK,
month => ONE_MONTH,
year => ONE_YEAR
);
my ( $amt, $factor )
= $interval =~ /^(\d+)\s+(day|week|month|year)s?$/gmx;
$amt-- if $factor eq 'day';
my $start = Time::Piece->new - $const{$factor} * $amt;
return $start->ymd . " to " . Time::Piece->new->ymd;
}
1; 1;

View File

@@ -5,6 +5,7 @@ use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::Web (); # for interval_to_daterange
register_report({ register_report({
category => 'IP', category => 'IP',
@@ -18,13 +19,16 @@ get '/ajax/content/report/subnets' => require_login sub {
my $age = param('age') || '7 days'; my $age = param('age') || '7 days';
$age = '7 days' unless $age =~ m/^(?:\d+)\s+(?:day|week|month|year)s?$/; $age = '7 days' unless $age =~ m/^(?:\d+)\s+(?:day|week|month|year)s?$/;
my $daterange = App::Netdisco::Util::Web::interval_to_daterange($age);
my $set = schema('netdisco')->resultset('Virtual::SubnetUtilization') my $set = schema('netdisco')->resultset('Virtual::SubnetUtilization')
->search(undef,{ ->search(undef,{
bind => [ $subnet, $age, $age, $subnet, $age, $age ], bind => [ $subnet, $age, $age, $subnet, $age, $age ],
}); });
if ( request->is_ajax ) { if ( request->is_ajax ) {
template 'ajax/report/subnets.tt', { results => $set, }, template 'ajax/report/subnets.tt',
{ results => $set, daterange => $daterange },
{ layout => undef }; { layout => undef };
} }
else { else {

View File

@@ -13,7 +13,8 @@
</tbody> </tbody>
[% FOREACH row IN results.all %] [% FOREACH row IN results.all %]
<tr> <tr>
<td class="nd_center-cell">[% row.subnet | html_entity %]</td> <td class="nd_center-cell"><a href="[% uri_for('/report/ipinventory') %]?subnet=[% row.subnet | uri %]&daterange=[% daterange | uri %]&limit=[% row.subnet_size | uri %]&age_on=on">
[% row.subnet | html_entity %]</a></td>
<td class="nd_center-cell">[% row.subnet_size | html_entity %]</td> <td class="nd_center-cell">[% row.subnet_size | html_entity %]</td>
<td class="nd_center-cell">[% row.active | html_entity %]</td> <td class="nd_center-cell">[% row.active | html_entity %]</td>
<td class="nd_center-cell">[% row.percent | html_entity %]</td> <td class="nd_center-cell">[% row.percent | html_entity %]</td>