From 63f0585ac64ee317aefd6f3e715e450010a3a101 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 12 Feb 2012 16:42:17 +0000 Subject: [PATCH] add with_times to device_port and use when param c_lastchange is set --- .../lib/Netdisco/DB/ResultSet/DevicePort.pm | 77 ++++++++++--------- Netdisco/lib/Netdisco/Web/Device.pm | 3 + 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/Netdisco/lib/Netdisco/DB/ResultSet/DevicePort.pm b/Netdisco/lib/Netdisco/DB/ResultSet/DevicePort.pm index 606eca15..524c89e7 100644 --- a/Netdisco/lib/Netdisco/DB/ResultSet/DevicePort.pm +++ b/Netdisco/lib/Netdisco/DB/ResultSet/DevicePort.pm @@ -4,7 +4,40 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings FATAL => 'all'; -=head1 search_by_mac( \%cond, \%attrs? ) +=head1 ADDITIONAL METHODS + +=head2 with_times + +This is a modifier for any C (including the helpers below) which +will add the following additional synthesized columns to the result set: + +=over 4 + +=item lastchange_stamp + +=back + +=cut + +sub with_times { + my ($rs, $cond, $attrs) = @_; + $cond ||= {}; + $attrs ||= {}; + + return $rs + ->search_rs($cond, $attrs) + ->search({}, + { + '+select' => [ + \"to_char(last_discover - (uptime - lastchange) / 100 * interval '1 second', + 'YYYY-MM-DD HH24:MI:SS')", + ], + '+as' => [qw/ lastchange_stamp /], + join => 'device', + }); +} + +=head2 search_by_mac( \%cond, \%attrs? ) my $set = $rs->search_by_mac({mac => '00:11:22:33:44:55'}); @@ -20,12 +53,7 @@ the value to search for. =item * -Results are ordered by the creation timestamp. - -=item * - -The additional column C provides a preformatted timestamp of -the C field. +Results are ordered by the port's Device IP and Port fields. =back @@ -42,16 +70,13 @@ sub search_by_mac { return $rs ->search_rs({}, { - order_by => {'-desc' => 'me.creation'}, - '+select' => [ - \"to_char(me.creation, 'YYYY-MM-DD HH24:MI')", - ], - '+as' => [qw/ creation_stamp /], + # order_by => {'-desc' => 'me.creation'}, + order_by => [qw/ me.ip me.port /], }) ->search($cond, $attrs); } -=head1 search_by_ip( \%cond, \%attrs? ) +=head2 search_by_ip( \%cond, \%attrs? ) my $set = $rs->search_by_ip({ip => '192.0.2.1'}); @@ -65,16 +90,6 @@ DevicePort table. The C parameter must be a hashref containing a key C with the IPv4 or IPv6 value to search for in plain string form. -=item * - -The additional column C provides a preformatted timestamp of -the C field in the C table. - -=item * - -A JOIN is performed on the C table in order to retrieve data required -for the C calculation. - =back =cut @@ -88,18 +103,10 @@ sub search_by_ip { $cond->{'me.ip'} = delete $cond->{ip}; $attrs ||= {}; - return $rs - ->search_rs({}, { - '+select' => [ - \"to_char(last_discover - (uptime - lastchange) / 100 * interval '1 second', 'YYYY-MM-DD HH24:MI:SS')", - ], - '+as' => [qw/ lastchange_stamp /], - join => 'device', - }) - ->search($cond, $attrs); + return $rs->search($cond, $attrs); } -=head1 search_by_name( \%cond, \%attrs? ) +=head2 search_by_name( \%cond, \%attrs? ) my $set = $rs->search_by_name({name => 'sniffer'}); @@ -137,7 +144,7 @@ sub search_by_name { ->search($cond, $attrs); } -=head1 search_by_vlan( \%cond, \%attrs? ) +=head2 search_by_vlan( \%cond, \%attrs? ) my $set = $rs->search_by_vlan({vlan => 123}); @@ -175,7 +182,7 @@ sub search_by_vlan { ->search($cond, $attrs); } -=head1 search_by_port( \%cond, \%attrs? ) +=head2 search_by_port( \%cond, \%attrs? ) my $set = $rs->search_by_port({port => 'FastEthernet0/23'}); diff --git a/Netdisco/lib/Netdisco/Web/Device.pm b/Netdisco/lib/Netdisco/Web/Device.pm index c95660de..43e6b218 100644 --- a/Netdisco/lib/Netdisco/Web/Device.pm +++ b/Netdisco/lib/Netdisco/Web/Device.pm @@ -75,6 +75,9 @@ ajax '/ajax/content/device/ports' => sub { my $set = schema('netdisco')->resultset('DevicePort') ->search_by_ip({ip => $ip}); + # make sure query asks for formatted timestamps when needed + $set = $set->with_times if param('c_lastchange'); + # refine by ports if requested my $q = param('f'); if ($q) {