diff --git a/Netdisco/lib/App/Netdisco/Util/Web.pm b/Netdisco/lib/App/Netdisco/Util/Web.pm index 32638100..7e966fab 100644 --- a/Netdisco/lib/App/Netdisco/Util/Web.pm +++ b/Netdisco/lib/App/Netdisco/Util/Web.pm @@ -1,9 +1,11 @@ package App::Netdisco::Util::Web; use base 'Exporter'; +use Time::Piece; +use Time::Seconds; our @EXPORT = (); our @EXPORT_OK = qw/ - sort_port + sort_port sort_modules interval_to_daterange /; our %EXPORT_TAGS = (all => \@EXPORT_OK); @@ -150,4 +152,34 @@ sub sort_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; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/SubnetUtilization.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/SubnetUtilization.pm index 9fcecc52..f876da7a 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/SubnetUtilization.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/SubnetUtilization.pm @@ -5,6 +5,7 @@ use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; use App::Netdisco::Web::Plugin; +use App::Netdisco::Util::Web (); # for interval_to_daterange register_report({ category => 'IP', @@ -18,13 +19,16 @@ get '/ajax/content/report/subnets' => require_login sub { my $age = param('age') || '7 days'; $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') ->search(undef,{ bind => [ $subnet, $age, $age, $subnet, $age, $age ], }); if ( request->is_ajax ) { - template 'ajax/report/subnets.tt', { results => $set, }, + template 'ajax/report/subnets.tt', + { results => $set, daterange => $daterange }, { layout => undef }; } else { diff --git a/Netdisco/share/views/ajax/report/subnets.tt b/Netdisco/share/views/ajax/report/subnets.tt index 1737208e..7b3b9c3c 100644 --- a/Netdisco/share/views/ajax/report/subnets.tt +++ b/Netdisco/share/views/ajax/report/subnets.tt @@ -13,7 +13,8 @@ [% FOREACH row IN results.all %] - [% row.subnet | html_entity %] + + [% row.subnet | html_entity %] [% row.subnet_size | html_entity %] [% row.active | html_entity %] [% row.percent | html_entity %]