« blib/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/AdminTask/PollerPerformance.pm »
| 1 | package App::Netdisco::Web::Plugin::AdminTask::OrphanedDevices; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Dancer ':syntax'; | ||
| 6 | use Dancer::Plugin::DBIC; | ||
| 7 | use Dancer::Plugin::Auth::Extensible; | ||
| 8 | |||
| 9 | use App::Netdisco::Web::Plugin; | ||
| 10 | |||
| 11 | register_admin_task( | ||
| 12 | { tag => 'orphaned', | ||
| 13 | label => 'Orphaned Devices / Networks', | ||
| 14 | provides_csv => 1, | ||
| 15 | } | ||
| 16 | ); | ||
| 17 | |||
| 18 | get '/ajax/content/admin/orphaned' => require_role admin => sub { | ||
| 19 | |||
| 20 | my @tree = schema(vars->{'tenant'})->resultset('Virtual::UnDirEdgesAgg') | ||
| 21 | ->search( undef, { prefetch => 'device' } )->hri->all; | ||
| 22 | |||
| 23 | my @orphans | ||
| 24 | = schema(vars->{'tenant'})->resultset('Virtual::OrphanedDevices')->search() | ||
| 25 | ->order_by('ip')->hri->all; | ||
| 26 | |||
| 27 | return unless ( scalar @tree || scalar @orphans ); | ||
| 28 | |||
| 29 | my @ordered; | ||
| 30 | |||
| 31 | if ( scalar @tree ) { | ||
| 32 | my %tree = map { $_->{'left_ip'} => $_ } @tree; | ||
| 33 | |||
| 34 | my $current_graph = 0; | ||
| 35 | my %visited = (); | ||
| 36 | my @to_visit = (); | ||
| 37 | foreach my $node ( keys %tree ) { | ||
| 38 | next if exists $visited{$node}; | ||
| 39 | |||
| 40 | $current_graph++; | ||
| 41 | @to_visit = ($node); | ||
| 42 | while (@to_visit) { | ||
| 43 | my $node_to_visit = shift @to_visit; | ||
| 44 | |||
| 45 | $visited{$node_to_visit} = $current_graph; | ||
| 46 | |||
| 47 | push @to_visit, | ||
| 48 | grep { !exists $visited{$_} } | ||
| 49 | @{ $tree{$node_to_visit}->{'links'} }; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | my @graphs = (); | ||
| 54 | foreach my $key ( keys %visited ) { | ||
| 55 | push @{ $graphs[ $visited{$key} - 1 ] }, $tree{$key}->{'device'}; | ||
| 56 | } | ||
| 57 | |||
| 58 | @ordered = sort { scalar @{$b} <=> scalar @{$a} } @graphs; | ||
| 59 | } | ||
| 60 | |||
| 61 | return if ( scalar @ordered < 2 && !scalar @tree ); | ||
| 62 | |||
| 63 | if ( request->is_ajax ) { | ||
| 64 | template 'ajax/admintask/orphaned.tt', | ||
| 65 | { | ||
| 66 | orphans => \@orphans, | ||
| 67 | graphs => \@ordered, | ||
| 68 | }, | ||
| 69 | { layout => undef }; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 73 | template 'ajax/admintask/orphaned_csv.tt', | ||
| 74 | { | ||
| 75 | orphans => \@orphans, | ||
| 76 | graphs => \@ordered, | ||
| 77 | }, | ||
| 78 | { layout => undef }; | ||
| 79 | } | ||
| 80 | }; | ||
| 81 | |||
| 82 | 1; |