1package App::Netdisco::Web::Plugin::Report::ApClients;
2
3use Dancer ':syntax';
4use Dancer::Plugin::DBIC;
5use Dancer::Plugin::Auth::Extensible;
6
7use App::Netdisco::Web::Plugin;
8
9register_report(
10    {   category     => 'Wireless',
11        tag          => 'apclients',
12        label        => 'Access Point Client Count',
13        provides_csv => 1,
14        api_endpoint => 1,
15    }
16);
17
18get '/ajax/content/report/apclients' => require_login sub {
19    my @results = schema('netdisco')->resultset('Device')->search(
20        { 'nodes.time_last' => { '>=', \'me.last_macsuck' } },
21        {   select => [ 'ip', 'dns', 'name', 'model', 'location' ],
22            join       => { 'ports' => { 'ssid' => 'nodes' } },
23            '+columns' => [
24                { 'port'        => 'ports.port' },
25                { 'description' => 'ports.name' },
26                { 'ssid'        => 'ssid.ssid' },
27                { 'mac_count'   => { count => 'nodes.mac' } },
28            ],
29            group_by => [
30                'me.ip',       'me.dns',     'me.name',     'me.model',
31                'me.location', 'ports.port', 'ports.descr', 'ports.name', 'ssid.ssid',
32            ],
33            order_by => { -desc => [qw/count/] },
34        }
35    )->hri->all;
36
37    return unless scalar @results;
38
39    if ( request->is_ajax ) {
40        my $json = to_json( \@results );
41        template 'ajax/report/apclients.tt', { results => $json };
42    }
43    else {
44        header( 'Content-Type' => 'text/comma-separated-values' );
45        template 'ajax/report/apclients_csv.tt',
46            { results => \@results };
47    }
48};
49
501;
51