« blib/lib/App/Netdisco/Web/Plugin/Report/ModuleInventory.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/Report/NodeMultiIPs.pm »
| 1 | package App::Netdisco::Web::Plugin::Report::Netbios; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | use App::Netdisco::Util::ExpandParams 'expand_hash'; | ||
| 7 | |||
| 8 | use App::Netdisco::Web::Plugin; | ||
| 9 | |||
| 10 | register_report( | ||
| 11 | { category => 'Node', | ||
| 12 | tag => 'netbios', | ||
| 13 | label => 'NetBIOS Inventory', | ||
| 14 | provides_csv => 1, | ||
| 15 | } | ||
| 16 | ); | ||
| 17 | |||
| 18 | hook 'before_template' => sub { | ||
| 19 | my $tokens = shift; | ||
| 20 | |||
| 21 | return | ||
| 22 | unless ( request->path eq uri_for('/report/netbios')->path | ||
| 23 | or | ||
| 24 | index( request->path, uri_for('/ajax/content/report/netbios')->path ) | ||
| 25 | == 0 ); | ||
| 26 | |||
| 27 | # used in the search sidebar template to set selected items | ||
| 28 | foreach my $opt (qw/domain/) { | ||
| 29 | my $p = ( | ||
| 30 | ref [] eq ref param($opt) | ||
| 31 | ? param($opt) | ||
| 32 | : ( param($opt) ? [ param($opt) ] : [] ) | ||
| 33 | ); | ||
| 34 | $tokens->{"${opt}_lkp"} = { map { $_ => 1 } @$p }; | ||
| 35 | } | ||
| 36 | }; | ||
| 37 | |||
| 38 | get '/ajax/content/report/netbios/data' => require_login sub { | ||
| 39 | send_error( 'Missing parameter', 400 ) | ||
| 40 | unless ( param('draw') && param('draw') =~ /\d+/ ); | ||
| 41 | |||
| 42 | my $domain = param('domain'); | ||
| 43 | |||
| 44 | my $rs = schema(vars->{'tenant'})->resultset('NodeNbt'); | ||
| 45 | |||
| 46 | my $search = $domain eq 'blank' ? '' : $domain; | ||
| 47 | $rs = $rs->search( { domain => $search } ) | ||
| 48 | ->order_by( [ { -asc => 'domain' }, { -desc => 'time_last' } ] ); | ||
| 49 | |||
| 50 | my $exp_params = expand_hash( scalar params ); | ||
| 51 | |||
| 52 | my $recordsTotal = $rs->count; | ||
| 53 | |||
| 54 | my @data = $rs->get_datatables_data($exp_params)->hri->all; | ||
| 55 | |||
| 56 | my $recordsFiltered = $rs->get_datatables_filtered_count($exp_params); | ||
| 57 | |||
| 58 | content_type 'application/json'; | ||
| 59 | return to_json( | ||
| 60 | { draw => int( param('draw') ), | ||
| 61 | recordsTotal => int($recordsTotal), | ||
| 62 | recordsFiltered => int($recordsFiltered), | ||
| 63 | data => \@data, | ||
| 64 | } | ||
| 65 | ); | ||
| 66 | }; | ||
| 67 | |||
| 68 | get '/ajax/content/report/netbios' => require_login sub { | ||
| 69 | |||
| 70 | my $domain = param('domain'); | ||
| 71 | |||
| 72 | my $rs = schema(vars->{'tenant'})->resultset('NodeNbt'); | ||
| 73 | my @results; | ||
| 74 | |||
| 75 | if ( defined $domain && !request->is_ajax ) { | ||
| 76 | my $search = $domain eq 'blank' ? '' : $domain; | ||
| 77 | @results | ||
| 78 | = $rs->search( { domain => $search } ) | ||
| 79 | ->order_by( [ { -asc => 'domain' }, { -desc => 'time_last' } ] ) | ||
| 80 | ->hri->all; | ||
| 81 | |||
| 82 | return unless scalar @results; | ||
| 83 | } | ||
| 84 | elsif ( !defined $domain ) { | ||
| 85 | @results = $rs->search( | ||
| 86 | {}, | ||
| 87 | { select => [ 'domain', { count => 'domain' } ], | ||
| 88 | as => [qw/ domain count /], | ||
| 89 | group_by => [qw/ domain /] | ||
| 90 | } | ||
| 91 | )->order_by( { -desc => 'count' } )->hri->all; | ||
| 92 | |||
| 93 | return unless scalar @results; | ||
| 94 | } | ||
| 95 | |||
| 96 | if ( request->is_ajax ) { | ||
| 97 | my $json = to_json( \@results ); | ||
| 98 | template 'ajax/report/netbios.tt', | ||
| 99 | { results => $json, opt => $domain }, | ||
| 100 | { layout => 'noop' }; | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 104 | template 'ajax/report/netbios_csv.tt', | ||
| 105 | { results => \@results, opt => $domain }, | ||
| 106 | { layout => 'noop' }; | ||
| 107 | } | ||
| 108 | }; | ||
| 109 | |||
| 110 | 1; |