diff --git a/Changes b/Changes index e0b87c4e..b985b6ba 100644 --- a/Changes +++ b/Changes @@ -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] diff --git a/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm b/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm index 993102c0..15541d79 100644 --- a/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm +++ b/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm @@ -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 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 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 diff --git a/Netdisco/lib/Netdisco/Web/Inventory.pm b/Netdisco/lib/Netdisco/Web/Inventory.pm index dc32e459..74eef1cf 100644 --- a/Netdisco/lib/Netdisco/Web/Inventory.pm +++ b/Netdisco/lib/Netdisco/Web/Inventory.pm @@ -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, }; };