diff --git a/Netdisco/lib/Netdisco/DB/Result/DevicePort.pm b/Netdisco/lib/Netdisco/DB/Result/DevicePort.pm index 86e395d4..27cdcd4b 100644 --- a/Netdisco/lib/Netdisco/DB/Result/DevicePort.pm +++ b/Netdisco/lib/Netdisco/DB/Result/DevicePort.pm @@ -66,11 +66,23 @@ __PACKAGE__->set_primary_key("port", "ip"); =head1 RELATIONSHIPS +=head2 device + +Returns the Device table entry to which the given Port is related. + +=cut + +__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip'); + =head2 nodes Returns the set of Nodes whose MAC addresses are associated with this Device Port. +Remember you can pass a filter to this method to find only active or inactive +nodes, but do take into account that both the C and C tables +include independent C fields. + =over 4 =item * @@ -84,37 +96,29 @@ will also be retrieved. =item * -The additional column C is a preformatted value for the -C field, which reads as "X days/weeks/months/years" ago. +The additional column C is a preformatted value for the Node's +C field, which reads as "X days/weeks/months/years". =back =cut __PACKAGE__->has_many( nodes => 'Netdisco::DB::Result::Node', - { - 'foreign.switch' => 'self.ip', - 'foreign.port' => 'self.port', - }, - { - prefetch => 'ips', - order_by => 'me.mac', - '+select' => [ - \"replace(age(date_trunc('minute', - me.time_last + interval '30 second'))::text, 'mon', 'month')", - ], - '+as' => [ 'me.time_last_age' ], - }, + { + 'foreign.switch' => 'self.ip', + 'foreign.port' => 'self.port', + }, + { + prefetch => 'ips', + order_by => 'me.mac', + '+select' => [ + \"replace(age(date_trunc('minute', + me.time_last + interval '30 second'))::text, 'mon', 'month')", + ], + '+as' => [ 'me.time_last_age' ], + }, ); -sub get_nodes { - my ($row, $archive) = @_; - return $row->nodes({ - ($archive ? (-or => [{'me.active' => 1}, {'me.active' => 0}]) - : ('me.active' => 1)) - }); -} - =head2 neighbor_alias When a device port has an attached neighbor device, this relationship will @@ -128,49 +132,8 @@ database. =cut __PACKAGE__->belongs_to( neighbor_alias => 'Netdisco::DB::Result::DeviceIp', - { - 'foreign.alias' => 'self.remote_ip', - }, - { join_type => 'LEFT' }, -); - -=head2 device - -Returns the Device table entry to which the given Port is related. - -=over 4 - -=item * - -Additional columns C, C, -C provide preformatted timestamps of the C, -C and C fields. - -=item * - -The additional column C provides a performatted value for the -device uptime which reads as "X days/weeks/months/years" ago. - -=back - -=cut - -__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip', - { - '+select' => [ - \"replace(age(timestamp 'epoch' + uptime / 100 * interval '1 second', - timestamp '1970-01-01 00:00:00-00')::text, 'mon', 'month')", - \"to_char(last_discover, 'YYYY-MM-DD HH24:MI')", - \"to_char(last_macsuck, 'YYYY-MM-DD HH24:MI')", - \"to_char(last_arpnip, 'YYYY-MM-DD HH24:MI')", - ], - '+as' => [qw/ - uptime_age - last_discover_stamp - last_macsuck_stamp - last_arpnip_stamp - /], - }, + { 'foreign.alias' => 'self.remote_ip' }, + { join_type => 'LEFT' }, ); =head2 port_vlans_tagged @@ -182,13 +145,13 @@ relationship. =cut __PACKAGE__->has_many( port_vlans_tagged => 'Netdisco::DB::Result::DevicePortVlan', - { - 'foreign.ip' => 'self.ip', - 'foreign.port' => 'self.port', - }, - { - where => { -not_bool => 'me.native' }, - } + { + 'foreign.ip' => 'self.ip', + 'foreign.port' => 'self.port', + }, + { + where => { -not_bool => 'me.native' }, + } ); =head2 tagged_vlans @@ -214,13 +177,13 @@ See also the C helper method. =cut __PACKAGE__->might_have( native_port_vlan => 'Netdisco::DB::Result::DevicePortVlan', - { - 'foreign.ip' => 'self.ip', - 'foreign.port' => 'self.port', - }, - { - where => { -bool => 'me.native' }, - } + { + 'foreign.ip' => 'self.ip', + 'foreign.port' => 'self.port', + }, + { + where => { -bool => 'me.native' }, + } ); =head2 oui @@ -233,14 +196,14 @@ The JOIN is of type LEFT, in case the OUI table has not been populated. =cut __PACKAGE__->belongs_to( oui => 'Netdisco::DB::Result::Oui', - sub { - my $args = shift; - return { - "$args->{foreign_alias}.oui" => - { '=' => \"substring(cast($args->{self_alias}.mac as varchar) for 8)" } - }; - }, - { join_type => 'LEFT' } + sub { + my $args = shift; + return { + "$args->{foreign_alias}.oui" => + { '=' => \"substring(cast($args->{self_alias}.mac as varchar) for 8)" } + }; + }, + { join_type => 'LEFT' } ); =head1 ADDITIONAL METHODS @@ -255,8 +218,11 @@ the database. =cut -# FIXME make this more efficient by specifying the full join to DBIC -sub neighbor { return eval { (shift)->neighbor_alias->device } } +# make this more efficient by specifying the full join to DBIC? +sub neighbor { + my $row = shift; + return eval { $row->neighbor_alias->device || undef }; +} =head2 native_vlan @@ -281,7 +247,7 @@ Returns the number of tagged VLANs active on this device port. =cut sub tagged_vlans_count { - return (shift)->tagged_vlans->count; + return (shift)->tagged_vlans->count; } =head2 is_free( $quantity, $unit ) diff --git a/Netdisco/lib/Netdisco/DB/Result/Node.pm b/Netdisco/lib/Netdisco/DB/Result/Node.pm index 4c7bae03..8a096ef4 100644 --- a/Netdisco/lib/Netdisco/DB/Result/Node.pm +++ b/Netdisco/lib/Netdisco/DB/Result/Node.pm @@ -132,4 +132,16 @@ between the date stamp and time stamp. That is: sub time_last_stamp { return (shift)->get_column('time_last_stamp') } +=head2 time_last_age + +Formatted version of the C field, accurate to the minute. + +The format is in "X days/months/years" style, similar to: + + 1 year 4 months 05:46:00 + +=cut + +sub time_last_age { return (shift)->get_column('time_last_age') } + 1; diff --git a/Netdisco/lib/Netdisco/Web/Device.pm b/Netdisco/lib/Netdisco/Web/Device.pm index 86b2c210..fd22a1a0 100644 --- a/Netdisco/lib/Netdisco/Web/Device.pm +++ b/Netdisco/lib/Netdisco/Web/Device.pm @@ -99,10 +99,14 @@ ajax '/ajax/content/device/ports' => sub { or $_->is_free(param('age_num'), param('age_unit')) } $set->all ]; return unless scalar @$results; + my @active = (param('n_archived') + ? (-or => [{'me.active' => 1}, {'me.active' => 0}]) + : ('me.active' => 1)); content_type('text/html'); template 'ajax/device/ports.tt', { results => $results, + archive_filter => {@active}, }, { layout => undef }; }; diff --git a/Netdisco/views/ajax/device/details.tt b/Netdisco/views/ajax/device/details.tt index bcef5b67..3ecfc697 100644 --- a/Netdisco/views/ajax/device/details.tt +++ b/Netdisco/views/ajax/device/details.tt @@ -43,19 +43,19 @@ Uptime - [% d.uptime %] + [% d.uptime_age %] Last Discover - [% d.last_discover %] + [% d.last_discover_stamp %] Last Arpnip - [% d.last_arpnip %] + [% d.last_arpnip_stamp %] Last Macsuck - [% d.last_macsuck %] + [% d.last_macsuck_stamp %] Hardware Status diff --git a/Netdisco/views/ajax/device/ports.tt b/Netdisco/views/ajax/device/ports.tt index 6db3b375..30c9e1ef 100644 --- a/Netdisco/views/ajax/device/ports.tt +++ b/Netdisco/views/ajax/device/ports.tt @@ -94,11 +94,11 @@
  ([% row.remote_type %]) / ([% row.remote_id %]) [% END %] [% END %] - [% FOREACH node IN row.get_nodes(params.n_archived) %] + [% FOREACH node IN row.nodes(archive_filter) %] [% '
' IF row.remote_ip OR NOT loop.first %] [% 'a  ' IF NOT node.active %] [% node.mac %] - [% ' (' _ node.time_last _ ')' IF params.n_age %] + [% ' (' _ node.time_last_age _ ')' IF params.n_age %] [% IF params.n_ip %] [% FOREACH ip IN node.ips %]
  [% 'a  ' IF NOT ip.active %] diff --git a/Netdisco/views/ajax/search/node_by_mac.tt b/Netdisco/views/ajax/search/node_by_mac.tt index b0ac3f5a..17014e1f 100644 --- a/Netdisco/views/ajax/search/node_by_mac.tt +++ b/Netdisco/views/ajax/search/node_by_mac.tt @@ -41,7 +41,7 @@ [% IF params.stamps %] [% row.time_first_stamp %] - [% rw.time_last_stamp %] + [% row.time_last_stamp %] [% END %] [% SET first_row = 0 %]