From 70f5da8bb65d5fdf5f2ad5a56da70467269a10b9 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 9 May 2013 23:30:32 +0100 Subject: [PATCH] implement "no devices" prompt for admin users to do first discover --- Netdisco/lib/App/Netdisco/DB/Result/Admin.pm | 42 +++++++++++++++++ .../lib/App/Netdisco/DB/ResultSet/Admin.pm | 41 ++++++++++++++++ Netdisco/lib/App/Netdisco/Web.pm | 7 +++ Netdisco/lib/App/Netdisco/Web/JobControl.pm | 25 ++++++++++ .../Netdisco/Web/Plugin/AdminTask/JobQueue.pm | 27 +++++++++++ .../Web/Plugin/AdminTask/PseudoDevice.pm | 4 +- .../Netdisco/Web/Plugin/AdminTask/Topology.pm | 4 +- Netdisco/share/views/admintask.tt | 3 ++ .../share/views/ajax/admintask/jobqueue.tt | 47 +++++++++++++++++++ Netdisco/share/views/index.tt | 13 +++++ Netdisco/share/views/js/admintask.js | 17 +++++++ TODO | 7 ++- 12 files changed, 231 insertions(+), 6 deletions(-) create mode 100644 Netdisco/lib/App/Netdisco/DB/ResultSet/Admin.pm create mode 100644 Netdisco/lib/App/Netdisco/Web/JobControl.pm create mode 100644 Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm create mode 100644 Netdisco/share/views/ajax/admintask/jobqueue.tt diff --git a/Netdisco/lib/App/Netdisco/DB/Result/Admin.pm b/Netdisco/lib/App/Netdisco/DB/Result/Admin.pm index fb98b3fa..50dd0fa5 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/Admin.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/Admin.pm @@ -55,4 +55,46 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("job"); # You can replace this text with custom code or comments, and it will be preserved on regeneration + +=head1 ADDITIONAL COLUMNS + +=head2 entererd_stamp + +Formatted version of the C field, accurate to the minute. + +The format is somewhat like ISO 8601 or RFC3339 but without the middle C +between the date stamp and time stamp. That is: + + 2012-02-06 12:49 + +=cut + +sub entered_stamp { return (shift)->get_column('entered_stamp') } + +=head2 started_stamp + +Formatted version of the C field, accurate to the minute. + +The format is somewhat like ISO 8601 or RFC3339 but without the middle C +between the date stamp and time stamp. That is: + + 2012-02-06 12:49 + +=cut + +sub started_stamp { return (shift)->get_column('started_stamp') } + +=head2 finished_stamp + +Formatted version of the C field, accurate to the minute. + +The format is somewhat like ISO 8601 or RFC3339 but without the middle C +between the date stamp and time stamp. That is: + + 2012-02-06 12:49 + +=cut + +sub finished_stamp { return (shift)->get_column('finished_stamp') } + 1; diff --git a/Netdisco/lib/App/Netdisco/DB/ResultSet/Admin.pm b/Netdisco/lib/App/Netdisco/DB/ResultSet/Admin.pm new file mode 100644 index 00000000..21c033ab --- /dev/null +++ b/Netdisco/lib/App/Netdisco/DB/ResultSet/Admin.pm @@ -0,0 +1,41 @@ +package App::Netdisco::DB::ResultSet::Admin; +use base 'DBIx::Class::ResultSet'; + +use strict; +use warnings FATAL => 'all'; + +=head1 ADDITIONAL METHODS + +=head2 with_times + +This is a modifier for any C (including the helpers below) which +will add the following additional synthesized columns to the result set: + +=over 4 + +=item entered_stamp + +=item started_stamp + +=item finished_stamp + +=back + +=cut + +sub with_times { + my ($rs, $cond, $attrs) = @_; + + return $rs + ->search_rs($cond, $attrs) + ->search({}, + { + '+columns' => { + entered_stamp => \"to_char(entered, 'YYYY-MM-DD HH24:MI')", + started_stamp => \"to_char(started, 'YYYY-MM-DD HH24:MI')", + finished_stamp => \"to_char(finished, 'YYYY-MM-DD HH24:MI')", + }, + }); +} + +1; diff --git a/Netdisco/lib/App/Netdisco/Web.pm b/Netdisco/lib/App/Netdisco/Web.pm index d51d4bf4..67fb07ff 100644 --- a/Netdisco/lib/App/Netdisco/Web.pm +++ b/Netdisco/lib/App/Netdisco/Web.pm @@ -16,6 +16,7 @@ use App::Netdisco::Web::Report; use App::Netdisco::Web::AdminTask; use App::Netdisco::Web::TypeAhead; use App::Netdisco::Web::PortControl; +use App::Netdisco::Web::JobControl; sub _load_web_plugins { my $plugin_list = shift; @@ -54,6 +55,12 @@ hook 'before_template' => sub { }; get '/' => sub { + if (var('user') and var('user')->admin) { + if (schema('netdisco')->resultset('Device')->count == 0) { + var('nodevices' => true); + } + } + template 'index'; }; diff --git a/Netdisco/lib/App/Netdisco/Web/JobControl.pm b/Netdisco/lib/App/Netdisco/Web/JobControl.pm new file mode 100644 index 00000000..cd7eb989 --- /dev/null +++ b/Netdisco/lib/App/Netdisco/Web/JobControl.pm @@ -0,0 +1,25 @@ +package App::Netdisco::Web::JobControl; + +use Dancer ':syntax'; +use Dancer::Plugin::DBIC; + +post '/admin/discover' => sub { + return unless var('user') and var('user')->admin; + + my $ip = NetAddr::IP::Lite->new(param('device')); + return unless $ip + and $ip->addr ne '0.0.0.0'; + + schema('netdisco')->resultset('Admin')->create({ + device => $ip->addr, + action => 'discover', + status => 'queued', + username => session('user'), + userip => request->remote_address, + }); + + status(302); + header(Location => uri_for('/admin/jobqueue')->path_query()); +}; + +true; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm new file mode 100644 index 00000000..49efcdc2 --- /dev/null +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm @@ -0,0 +1,27 @@ +package App::Netdisco::Web::Plugin::AdminTask::JobQueue; + +use Dancer ':syntax'; +use Dancer::Plugin::Ajax; +use Dancer::Plugin::DBIC; + +use App::Netdisco::Web::Plugin; + +register_admin_task({ + tag => 'jobqueue', + label => 'Job Queue', +}); + +ajax '/ajax/content/admin/jobqueue' => sub { + return unless var('user') and var('user')->admin; + + my $set = schema('netdisco')->resultset('Admin') + ->with_times + ->search({}, {order_by => { -desc => [qw/entered device action/] }}); + + content_type('text/html'); + template 'ajax/admintask/jobqueue.tt', { + results => $set, + }, { layout => undef }; +}; + +true; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm index 3fd0b924..d6ea76df 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm @@ -13,7 +13,7 @@ register_admin_task({ }); sub _sanity_ok { - return 0 unless var('user')->admin; + return 0 unless var('user') and var('user')->admin; return 0 unless length param('dns') and param('dns') =~ m/^[[:print:]]+$/ @@ -86,7 +86,7 @@ ajax '/ajax/content/admin/pseudodevice/update' => sub { }; ajax '/ajax/content/admin/pseudodevice' => sub { - return unless var('user')->admin; + return unless var('user') and var('user')->admin; my $set = schema('netdisco')->resultset('Device') ->search( diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/Topology.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/Topology.pm index d735d8c4..269213bf 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/Topology.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/Topology.pm @@ -13,7 +13,7 @@ register_admin_task({ }); sub _sanity_ok { - return 0 unless var('user')->admin; + return 0 unless var('user') and var('user')->admin; my $dev1 = NetAddr::IP::Lite->new(param('dev1')); return 0 unless ($dev1 and $dev1->addr ne '0.0.0.0'); @@ -54,7 +54,7 @@ ajax '/ajax/content/admin/topology/del' => sub { }; ajax '/ajax/content/admin/topology' => sub { - return unless var('user')->admin; + return unless var('user') and var('user')->admin; my $set = schema('netdisco')->resultset('Topology') ->search({},{order_by => [qw/dev1 dev2 port1/]}); diff --git a/Netdisco/share/views/admintask.tt b/Netdisco/share/views/admintask.tt index c64188b0..92346f7a 100644 --- a/Netdisco/share/views/admintask.tt +++ b/Netdisco/share/views/admintask.tt @@ -28,6 +28,9 @@
diff --git a/Netdisco/share/views/ajax/admintask/jobqueue.tt b/Netdisco/share/views/ajax/admintask/jobqueue.tt new file mode 100644 index 00000000..e888f96f --- /dev/null +++ b/Netdisco/share/views/ajax/admintask/jobqueue.tt @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + [% WHILE (row = results.next) %] + + + + [% IF row.status.search('^queued-') %] + + [% ELSE %] + + [% END %] + + + + + + + + + [% END %] + +
EnteredStatusActionDevicePortParamUserStartedFinishedAction
[% row.entered_stamp | html_entity %][% row.action.ucfirst | html_entity %]Running on "[% row.status.remove('^queued-') | html_entity %]"[% row.status.ucfirst | html_entity %][% row.device | html_entity %][% row.port | html_entity %][% row.subaction | html_entity %][% row.username | html_entity %][% row.started_stamp | html_entity %][% row.finished_stamp | html_entity %] +
+ + +
+
+ diff --git a/Netdisco/share/views/index.tt b/Netdisco/share/views/index.tt index cbf034c2..f747d978 100644 --- a/Netdisco/share/views/index.tt +++ b/Netdisco/share/views/index.tt @@ -44,6 +44,19 @@ [% END %]
+ [% IF vars.nodevices %] +
+

Initial Discovery

+

You haven't discovered any devices yet.

+

Enter a network device name or IP to queue the first discovery:

+
+
+ + +
+
+
+ [% END %] diff --git a/Netdisco/share/views/js/admintask.js b/Netdisco/share/views/js/admintask.js index 4bd2d25f..deaa6d1d 100644 --- a/Netdisco/share/views/js/admintask.js +++ b/Netdisco/share/views/js/admintask.js @@ -7,6 +7,23 @@ // but which cannot use jQuery delegation via .on() function inner_view_processing(tab) { + // reload this table every 10 seconds + if (tab == 'jobqueue') { + $('#nd_device_name').text('10'); + setTimeout(function() { $('#nd_device_name').text('9') }, 1000 ); + setTimeout(function() { $('#nd_device_name').text('8') }, 2000 ); + setTimeout(function() { $('#nd_device_name').text('7') }, 3000 ); + setTimeout(function() { $('#nd_device_name').text('6') }, 4000 ); + setTimeout(function() { $('#nd_device_name').text('5') }, 5000 ); + setTimeout(function() { $('#nd_device_name').text('4') }, 6000 ); + setTimeout(function() { $('#nd_device_name').text('3') }, 7000 ); + setTimeout(function() { $('#nd_device_name').text('2') }, 8000 ); + setTimeout(function() { $('#nd_device_name').text('1') }, 9000 ); + setTimeout(function() { + $('#' + tab + '_form').trigger('submit'); + }, 10000); + } + // activate typeahead on the topo boxes $('.nd_topo_dev').autocomplete({ source: '/ajax/data/deviceip/typeahead' diff --git a/TODO b/TODO index f5417a4b..111830ea 100644 --- a/TODO +++ b/TODO @@ -1,14 +1,17 @@ FRONTEND ======== -* UI for topo DB table editing -* No devices - trigger first discover splash page * view job queue * view logs with colour * moar reports * (jeneric) device module tab +BACKEND +======= + +* Job Delete handler + DAEMON ======