Factor out inventory queries to the Device ResultSet (closes #20)

This commit is contained in:
Oliver Gorwits
2012-08-27 21:34:08 +01:00
parent 3fefb5b7f0
commit 74d82d86b0
3 changed files with 67 additions and 12 deletions

View File

@@ -16,6 +16,7 @@
This came as a side-effect of removing JS-only links (closes #21)
* Replace jquery-collapser with Bootstrap's collapser
* Upgraded to Twitter Bootstrap 2.1.0 - customized for 13px font 18px line
* Factor out inventory queries to the Device ResultSet (closes #20)
[BUG FIXES]

View File

@@ -328,6 +328,66 @@ sub carrying_vlan_name {
->search($cond, $attrs);
}
=head2 get_models
Returns a sorted list of Device models with the following columns only:
=over 4
=item vendor
=item model
=item count
=back
Where C<count> is the number of instances of that Vendor's Model in the
Netdisco database.
=cut
sub get_models {
my $rs = shift;
return $rs->search({}, {
select => [ 'vendor', 'model', { count => 'ip' } ],
as => [qw/vendor model count/],
group_by => [qw/vendor model/],
order_by => [{-asc => 'vendor'}, {-desc => 'count'}, {-asc => 'model'}],
})
}
=head2 get_releases
Returns a sorted list of Device OS releases with the following columns only:
=over 4
=item os
=item os_ver
=item count
=back
Where C<count> is the number of devices running that OS release in the
Netdisco database.
=cut
sub get_releases {
my $rs = shift;
return $rs->search({}, {
select => [ 'os', 'os_ver', { count => 'ip' } ],
as => [qw/os os_ver count/],
group_by => [qw/os os_ver/],
order_by => [{-asc => 'os'}, {-desc => 'count'}, {-asc => 'os_ver'}],
})
}
=head2 get_distinct( $column )
Returns an asciibetical sorted list of the distinct values in the given column

View File

@@ -4,20 +4,14 @@ use Dancer ':syntax';
use Dancer::Plugin::DBIC;
get '/inventory' => sub {
my $models = schema('netdisco')->resultset('Device')->get_models();
my $releases = schema('netdisco')->resultset('Device')->get_releases();
var(nav => 'inventory');
template 'inventory', {
models => scalar schema('netdisco')->resultset('Device')->search({},{
select => [ 'vendor', 'model', { count => 'ip' } ],
as => [qw/vendor model count/],
group_by => [qw/vendor model/],
order_by => [{-asc => 'vendor'}, {-desc => 'count'}, {-asc => 'model'}],
}),
releases => scalar schema('netdisco')->resultset('Device')->search({},{
select => [ 'os', 'os_ver', { count => 'ip' } ],
as => [qw/os os_ver count/],
group_by => [qw/os os_ver/],
order_by => [{-asc => 'os'}, {-desc => 'count'}, {-asc => 'os_ver'}],
}),
models => $models,
releases => $releases,
};
};