« blib/lib/App/Netdisco/Web/Plugin/Report/PortUtilization.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/Report/SsidInventory.pm »
| 1 | package App::Netdisco::Web::Plugin::Report::PortVLANMismatch; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | |||
| 7 | use App::Netdisco::Web::Plugin; | ||
| 8 | use List::MoreUtils qw/listcmp sort_by/; | ||
| 9 | |||
| 10 | register_report( | ||
| 11 | { category => 'Port', | ||
| 12 | tag => 'portvlanmismatch', | ||
| 13 | label => 'Mismatched VLANs', | ||
| 14 | provides_csv => 1, | ||
| 15 | api_endpoint => 1, | ||
| 16 | } | ||
| 17 | ); | ||
| 18 | |||
| 19 | get '/ajax/content/report/portvlanmismatch' => require_login sub { | ||
| 20 | return unless schema(vars->{'tenant'})->resultset('Device')->count; | ||
| 21 | my @results = schema(vars->{'tenant'}) | ||
| 22 | ->resultset('Virtual::PortVLANMismatch')->search({},{ | ||
| 23 | bind => [ setting('sidebar_defaults')->{'device_ports'}->{'p_hide1002'}->{'default'} | ||
| 24 | ? (1002, 1003, 1004, 1005) : (0, 0, 0, 0) ], | ||
| 25 | }) | ||
| 26 | ->hri->all; | ||
| 27 | |||
| 28 | # # note that the generated list is rendered without HTML escape, | ||
| 29 | # # so we MUST sanitise here with the grep | ||
| 30 | # foreach my $res (@results) { | ||
| 31 | # my @left = grep {m/^(?:n:)?\d+$/} map {s/\s//g; $_} split ',', $res->{left_vlans}; | ||
| 32 | # my @right = grep {m/^(?:n:)?\d+$/} map {s/\s//g; $_} split ',', $res->{right_vlans}; | ||
| 33 | # | ||
| 34 | # my %new = (0 => [], 1 => []); | ||
| 35 | # my %cmp = listcmp @left, @right; | ||
| 36 | # foreach my $vlan (keys %cmp) { | ||
| 37 | # map { push @{ $new{$_} }, ( (2 == scalar @{ $cmp{$vlan} }) ? $vlan : "<strong>$vlan</strong>" ) } @{ $cmp{$vlan} }; | ||
| 38 | # } | ||
| 39 | # | ||
| 40 | # $res->{left_vlans} = join ', ', sort_by { (my $a = $_) =~ s/\D//g; sprintf "%05d", $a } @{ $new{0} }; | ||
| 41 | # $res->{right_vlans} = join ', ', sort_by { (my $a = $_) =~ s/\D//g; sprintf "%05d", $a } @{ $new{1} }; | ||
| 42 | # } | ||
| 43 | |||
| 44 | foreach my $res (@results) { | ||
| 45 | $res->{only_left_vlans} = join ', ', @{ $res->{only_left_vlans} || [] }; | ||
| 46 | $res->{only_right_vlans} = join ', ', @{ $res->{only_right_vlans} || [] }; | ||
| 47 | } | ||
| 48 | |||
| 49 | if (request->is_ajax) { | ||
| 50 | my $json = to_json (\@results); | ||
| 51 | template 'ajax/report/portvlanmismatch.tt', { results => $json }, { layout => 'noop' }; | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 55 | template 'ajax/report/portvlanmismatch_csv.tt', { results => \@results, }, { layout => 'noop' }; | ||
| 56 | } | ||
| 57 | }; | ||
| 58 | |||
| 59 | 1; |