DataTables for Access Point Radios Channel and Power report
Optimize SQL for ap_radio_channel_power virtual table Convert mW to dBm via the database query
This commit is contained in:
		@@ -10,30 +10,38 @@ __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
 | 
			
		||||
__PACKAGE__->table('ap_radio_channel_power');
 | 
			
		||||
__PACKAGE__->result_source_instance->is_virtual(1);
 | 
			
		||||
__PACKAGE__->result_source_instance->view_definition(<<ENDSQL
 | 
			
		||||
  SELECT distinct d.name as device_name, d.ip, d.dns, d.model, d.location,
 | 
			
		||||
      dp.port, dp.name as port_name, dp.descr, w.channel, w.power
 | 
			
		||||
    FROM device AS d, device_port_wireless AS w, device_port AS dp
 | 
			
		||||
    WHERE dp.port = w.port AND d.ip = w.ip
 | 
			
		||||
    ORDER BY d.name
 | 
			
		||||
SELECT w.channel,
 | 
			
		||||
       w.power,
 | 
			
		||||
       w.ip,
 | 
			
		||||
       w.port,
 | 
			
		||||
       dp.name AS port_name,
 | 
			
		||||
       dp.descr,
 | 
			
		||||
       d.name AS device_name,
 | 
			
		||||
       d.dns,
 | 
			
		||||
       d.model,
 | 
			
		||||
       d.location,
 | 
			
		||||
       CASE
 | 
			
		||||
           WHEN w.power > 0 THEN round((10.0 * log(w.power) / log(10))::numeric, 1)
 | 
			
		||||
           ELSE NULL
 | 
			
		||||
       END AS power2
 | 
			
		||||
FROM device_port_wireless AS w
 | 
			
		||||
JOIN device_port AS dp ON dp.port = w.port
 | 
			
		||||
AND dp.ip = w.ip
 | 
			
		||||
JOIN device AS d ON d.ip = w.ip
 | 
			
		||||
WHERE w.channel != '0'
 | 
			
		||||
ENDSQL
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
__PACKAGE__->add_columns(
 | 
			
		||||
  'device_name' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  'channel' => {
 | 
			
		||||
    data_type => 'integer',
 | 
			
		||||
  },
 | 
			
		||||
  'power' => {
 | 
			
		||||
    data_type => 'integer',
 | 
			
		||||
  },
 | 
			
		||||
  'ip' => {
 | 
			
		||||
    data_type => 'inet',
 | 
			
		||||
  },
 | 
			
		||||
  'dns' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
  'model' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
  'location' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
  'port' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
@@ -43,11 +51,20 @@ __PACKAGE__->add_columns(
 | 
			
		||||
  'descr' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
  'channel' => {
 | 
			
		||||
    data_type => 'integer',
 | 
			
		||||
  'device_name' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
  'power' => {
 | 
			
		||||
    data_type => 'integer',
 | 
			
		||||
  'dns' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
  'model' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
  'location' => {
 | 
			
		||||
    data_type => 'text',
 | 
			
		||||
  },
 | 
			
		||||
  'power2' => {
 | 
			
		||||
    data_type => 'numeric',
 | 
			
		||||
  },
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,13 +3,14 @@ package App::Netdisco::Web::Plugin::Report::ApRadioChannelPower;
 | 
			
		||||
use Dancer ':syntax';
 | 
			
		||||
use Dancer::Plugin::DBIC;
 | 
			
		||||
use Dancer::Plugin::Auth::Extensible;
 | 
			
		||||
use App::Netdisco::Util::ExpandParams 'expand_hash';
 | 
			
		||||
 | 
			
		||||
use App::Netdisco::Web::Plugin;
 | 
			
		||||
 | 
			
		||||
register_report(
 | 
			
		||||
    {   category => 'Wireless',
 | 
			
		||||
        tag      => 'apradiochannelpower',
 | 
			
		||||
        label    => 'Access Point Radios Channel and Power',
 | 
			
		||||
    {   category     => 'Wireless',
 | 
			
		||||
        tag          => 'apradiochannelpower',
 | 
			
		||||
        label        => 'Access Point Radios Channel and Power',
 | 
			
		||||
        provides_csv => 1,
 | 
			
		||||
    }
 | 
			
		||||
);
 | 
			
		||||
@@ -47,22 +48,50 @@ sub port_tree {
 | 
			
		||||
    return \%ports;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
get '/ajax/content/report/apradiochannelpower/data' => require_role admin =>
 | 
			
		||||
    sub {
 | 
			
		||||
    send_error( 'Missing parameter', 400 )
 | 
			
		||||
        unless ( param('draw') && param('draw') =~ /\d+/ );
 | 
			
		||||
 | 
			
		||||
    my $rs = schema('netdisco')->resultset('Virtual::ApRadioChannelPower');
 | 
			
		||||
 | 
			
		||||
    my $exp_params = expand_hash( scalar params );
 | 
			
		||||
 | 
			
		||||
    my $recordsTotal = $rs->count;
 | 
			
		||||
 | 
			
		||||
    my @data = $rs->get_datatables_data($exp_params)->hri->all;
 | 
			
		||||
 | 
			
		||||
    my $recordsFiltered = $rs->get_datatables_filtered_count($exp_params);
 | 
			
		||||
 | 
			
		||||
    content_type 'application/json';
 | 
			
		||||
    return to_json(
 | 
			
		||||
        {   draw            => int( param('draw') ),
 | 
			
		||||
            recordsTotal    => int($recordsTotal),
 | 
			
		||||
            recordsFiltered => int($recordsFiltered),
 | 
			
		||||
            data            => \@data,
 | 
			
		||||
        }
 | 
			
		||||
    );
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
get '/ajax/content/report/apradiochannelpower' => require_login sub {
 | 
			
		||||
    my @set
 | 
			
		||||
        = schema('netdisco')->resultset('Virtual::ApRadioChannelPower')->all;
 | 
			
		||||
 | 
			
		||||
    my $results = port_tree( \@set );
 | 
			
		||||
    return unless scalar %$results;
 | 
			
		||||
 | 
			
		||||
    if (request->is_ajax) {
 | 
			
		||||
    template 'ajax/report/apradiochannelpower.tt', { results => $results, },
 | 
			
		||||
        { layout => undef };
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        header( 'Content-Type' => 'text/comma-separated-values' );
 | 
			
		||||
        template 'ajax/report/apradiochannelpower_csv.tt', { results => $results, },
 | 
			
		||||
    if ( request->is_ajax ) {
 | 
			
		||||
        template 'ajax/report/apradiochannelpower.tt', {},
 | 
			
		||||
            { layout => undef };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    else {
 | 
			
		||||
        my @results
 | 
			
		||||
            = schema('netdisco')->resultset('Virtual::ApRadioChannelPower')
 | 
			
		||||
            ->hri->all;
 | 
			
		||||
 | 
			
		||||
        return unless scalar @results;
 | 
			
		||||
 | 
			
		||||
        header( 'Content-Type' => 'text/comma-separated-values' );
 | 
			
		||||
        template 'ajax/report/apradiochannelpower_csv.tt',
 | 
			
		||||
            { results => \@results, },
 | 
			
		||||
            { layout  => undef };
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
true;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user