Device Neighbor Map can have max depth and VLAN filter

This commit is contained in:
Oliver Gorwits
2013-12-29 16:23:46 +00:00
parent aae7880311
commit 6079c7dfed
9 changed files with 99 additions and 44 deletions

View File

@@ -10,11 +10,11 @@ __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
__PACKAGE__->table('device_links');
__PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition(<<ENDSQL
SELECT dp.ip AS left_ip, di.ip AS right_ip
FROM ( SELECT device_port.ip, device_port.remote_ip
SELECT dp.ip AS left_ip, dp.port AS left_port, di.ip AS right_ip, dp.remote_port AS right_port
FROM ( SELECT device_port.ip, device_port.port, device_port.remote_ip, device_port.remote_port
FROM device_port
WHERE device_port.remote_port IS NOT NULL
GROUP BY device_port.ip, device_port.remote_ip
GROUP BY device_port.ip, device_port.port, device_port.remote_ip, device_port.remote_port
ORDER BY device_port.ip) dp
LEFT JOIN device_ip di ON dp.remote_ip = di.alias
WHERE di.ip IS NOT NULL
@@ -26,9 +26,23 @@ __PACKAGE__->add_columns(
'left_ip' => {
data_type => 'inet',
},
'left_port' => {
data_type => 'text',
},
'right_ip' => {
data_type => 'inet',
},
'right_port' => {
data_type => 'text',
},
);
__PACKAGE__->has_many('left_vlans', 'App::Netdisco::DB::Result::DevicePortVlan',
{ 'foreign.ip' => 'self.left_ip', 'foreign.port' => 'self.left_port' },
{ join_type => 'INNER' } );
__PACKAGE__->has_many('right_vlans', 'App::Netdisco::DB::Result::DevicePortVlan',
{ 'foreign.ip' => 'self.right_ip', 'foreign.port' => 'self.right_port' },
{ join_type => 'INNER' } );
1;

View File

@@ -23,8 +23,11 @@ sub _get_name {
}
sub _add_children {
my ($ptr, $childs) = @_;
my ($ptr, $childs, $step, $limit) = @_;
return $step if $limit and $step > $limit;
my @legit = ();
my $max = $step;
foreach my $c (@$childs) {
next if exists var('seen')->{$c};
@@ -39,14 +42,24 @@ sub _add_children {
for (my $i = 0; $i < @legit; $i++) {
$ptr->[$i]->{children} = [];
_add_children($ptr->[$i]->{children}, var('links')->{$legit[$i]});
my $nm = _add_children($ptr->[$i]->{children}, var('links')->{$legit[$i]},
($step + 1), $limit);
$max = $nm if $nm > $max;
}
return $max;
}
# d3 seems not to use proper ajax semantics, so get instead of ajax
get '/ajax/data/device/netmap' => require_login sub {
my $q = param('q');
my $vlan = param('vlan');
undef $vlan if (defined $vlan and $vlan !~ m/^\d+$/);
my $depth = param('depth');
undef $depth if (defined $depth and $depth !~ m/^\d+$/);
my $device = schema('netdisco')->resultset('Device')
->search_for_device($q) or send_error('Bad device', 400);
my $start = $device->ip;
@@ -59,9 +72,19 @@ get '/ajax/data/device/netmap' => require_login sub {
var(links => {});
my $rs = schema('netdisco')->resultset('Virtual::DeviceLinks')->search({}, {
columns => [qw/left_ip right_ip/],
result_class => 'DBIx::Class::ResultClass::HashRefInflator',
});
if ($vlan) {
$rs = $rs->search({
'left_vlans.vlan' => $vlan,
'right_vlans.vlan' => $vlan,
}, {
join => [qw/left_vlans right_vlans/],
});
}
while (my $l = $rs->next) {
var('links')->{ $l->{left_ip} } ||= [];
push @{ var('links')->{ $l->{left_ip} } }, $l->{right_ip};
@@ -75,7 +98,8 @@ get '/ajax/data/device/netmap' => require_login sub {
);
var(seen => {$start => 1});
_add_children($tree{children}, var('links')->{$start});
my $max = _add_children($tree{children}, var('links')->{$start}, 1, $depth);
$tree{scale} = $max;
content_type('application/json');
to_json(\%tree);