From b72b1b38e060d0f733af660bd78595435dfd9747 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 19 Aug 2019 14:40:12 +0100 Subject: [PATCH] add relation to get latest node on port --- lib/App/Netdisco/DB/Result/DevicePort.pm | 28 +++++++++++++++++++++ lib/App/Netdisco/DB/ResultSet/DevicePort.pm | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/App/Netdisco/DB/Result/DevicePort.pm b/lib/App/Netdisco/DB/Result/DevicePort.pm index 0b1266e0..ed2bbe27 100644 --- a/lib/App/Netdisco/DB/Result/DevicePort.pm +++ b/lib/App/Netdisco/DB/Result/DevicePort.pm @@ -242,6 +242,34 @@ __PACKAGE__->belongs_to( neighbor_alias => 'App::Netdisco::DB::Result::DeviceIp' { join_type => 'LEFT' }, ); +=head2 last_node + +This relationship will return the last node that was seen on the port. + +The JOIN is of type "LEFT" in case there isn't any such node. + +=cut + +__PACKAGE__->belongs_to( last_node => 'App::Netdisco::DB::Result::Node', + sub { + my $args = shift; + return { + "$args->{foreign_alias}.mac" => { '=' => + $args->{self_resultsource}->schema->resultset('Node')->search({ + switch => { -ident => "$args->{self_alias}.ip"}, + port => { -ident => "$args->{self_alias}.port"} + },{ + rows => 1, + order_by => { -desc => ['time_last'] }, + columns => 'mac', + alias => 'lastnodesub' + })->as_query + } + }; + }, + { join_type => 'LEFT' }, +); + =head2 vlans As compared to C, this relationship returns a set of Device VLAN diff --git a/lib/App/Netdisco/DB/ResultSet/DevicePort.pm b/lib/App/Netdisco/DB/ResultSet/DevicePort.pm index 3ae4f80f..59bf21b6 100644 --- a/lib/App/Netdisco/DB/ResultSet/DevicePort.pm +++ b/lib/App/Netdisco/DB/ResultSet/DevicePort.pm @@ -40,7 +40,7 @@ sub with_times { }); } -=head2 with_free_ports +=head2 with_is_free This is a modifier for any C (including the helpers below) which will add the following additional synthesized columns to the result set: