diff --git a/Netdisco/lib/App/Netdisco/Daemon/DB/Result/Admin.pm b/Netdisco/lib/App/Netdisco/Daemon/DB/Result/Admin.pm index 39c09fd5..802fb4c6 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/DB/Result/Admin.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/DB/Result/Admin.pm @@ -45,5 +45,6 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("job"); sub extra { (shift)->subaction } +sub entered_stamp { (shift)->entered } 1; diff --git a/Netdisco/lib/App/Netdisco/JobQueue.pm b/Netdisco/lib/App/Netdisco/JobQueue.pm index 46f28ab6..56aade6d 100644 --- a/Netdisco/lib/App/Netdisco/JobQueue.pm +++ b/Netdisco/lib/App/Netdisco/JobQueue.pm @@ -12,6 +12,8 @@ our @EXPORT_OK = qw/ jq_get jq_getlocal jq_queued + jq_log + jq_userlog jq_lock jq_defer jq_complete @@ -49,6 +51,17 @@ Netdisco job instance interface (see below). Returns a list of IP addresses of devices which currently have a job of the given C<$job_type> queued (e.g. C, C, etc). +=head2 jq_log() + +Returns a list of the most recent 50 jobs in the queue. Jobs are returned as +objects which implement the Netdisco job instance interface (see below). + +=head2 jq_userlog( $user ) + +Returns a list of jobs which have been entered into the queue by the passed +C<$user>. Jobs are returned as objects which implement the Netdisco job +instance interface (see below). + =head2 jq_lock( $job ) Marks a job in the queue as booked out to this processing node (denoted by the diff --git a/Netdisco/lib/App/Netdisco/JobQueue/PostgreSQL.pm b/Netdisco/lib/App/Netdisco/JobQueue/PostgreSQL.pm index 0ccb6880..33c868fd 100644 --- a/Netdisco/lib/App/Netdisco/JobQueue/PostgreSQL.pm +++ b/Netdisco/lib/App/Netdisco/JobQueue/PostgreSQL.pm @@ -12,6 +12,8 @@ our @EXPORT_OK = qw/ jq_get jq_getlocal jq_queued + jq_log + jq_userlog jq_lock jq_defer jq_complete @@ -34,7 +36,6 @@ sub jq_get { push @returned, schema('daemon')->resultset('Admin') ->new_result({ $job->get_columns, type => $job_type }); } - return @returned; } @@ -50,7 +51,6 @@ sub jq_getlocal { push @returned, schema('daemon')->resultset('Admin') ->new_result({ $job->get_columns, type => $job_type }); } - return @returned; } @@ -64,6 +64,39 @@ sub jq_queued { })->get_column('device')->all; } +sub jq_log { + my @returned = (); + + my $rs = schema('netdisco')->resultset('Admin')->search({}, { + order_by => { -desc => [qw/entered device action/] }, + rows => 50, + }); + + while (my $job = $rs->next) { + my $job_type = setting('job_types')->{$job->action} or next; + push @returned, schema('daemon')->resultset('Admin') + ->new_result({ $job->get_columns, type => $job_type }); + } + return @returned; +} + +sub jq_userlog { + my $user = shift; + my @returned = (); + + my $rs = schema('netdisco')->resultset('Admin')->search({ + username => $user, + finished => { '>' => \"(now() - interval '5 seconds')" }, + }); + + while (my $job = $rs->next) { + my $job_type = setting('job_types')->{$job->action} or next; + push @returned, schema('daemon')->resultset('Admin') + ->new_result({ $job->get_columns, type => $job_type }); + } + return @returned; +} + sub jq_lock { my $job = shift; my $fqdn = hostfqdn || 'localhost'; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm index 27767ce9..e4e5e916 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/AdminTask/JobQueue.pm @@ -6,6 +6,7 @@ use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; use App::Netdisco::Web::Plugin; +use App::Netdisco::JobQueue 'jq_log'; register_admin_task({ tag => 'jobqueue', @@ -28,16 +29,9 @@ ajax '/ajax/control/admin/jobqueue/delall' => require_role admin => sub { }; ajax '/ajax/content/admin/jobqueue' => require_role admin => sub { - my $set = schema('netdisco')->resultset('Admin') - ->with_times - ->search({}, { - order_by => { -desc => [qw/entered device action/] }, - rows => 50, - }); - content_type('text/html'); template 'ajax/admintask/jobqueue.tt', { - results => $set, + results => [ jq_log ], }, { layout => undef }; }; diff --git a/Netdisco/lib/App/Netdisco/Web/PortControl.pm b/Netdisco/lib/App/Netdisco/Web/PortControl.pm index fb41835f..d4a182e8 100644 --- a/Netdisco/lib/App/Netdisco/Web/PortControl.pm +++ b/Netdisco/lib/App/Netdisco/Web/PortControl.pm @@ -5,7 +5,7 @@ use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; -use App::Netdisco::JobQueue 'jq_insert'; +use App::Netdisco::JobQueue qw/jq_insert jq_userlog/; ajax '/ajax/portcontrol' => require_role port_control => sub { send_error('No device/port/field', 400) @@ -62,21 +62,20 @@ ajax '/ajax/portcontrol' => require_role port_control => sub { }; ajax '/ajax/userlog' => require_login sub { - my $rs = schema('netdisco')->resultset('Admin')->search({ - username => session('logged_in_user'), - action => [qw/location contact portcontrol portname vlan power - discover macsuck arpnip/], - finished => { '>' => \"(now() - interval '5 seconds')" }, - }); + my @jobs = jq_userlog( session('logged_in_user') ); my %status = ( - 'done' => [ - map {s/\[\]/<empty>/; $_} - $rs->search({status => 'done'})->get_column('log')->all + 'done' => [ + map {s/\[\]/<empty>/; $_} + map { $_->log } + grep { $_->status eq 'done' } + @jobs ], 'error' => [ - map {s/\[\]/<empty>/; $_} - $rs->search({status => 'error'})->get_column('log')->all + map {s/\[\]/<empty>/; $_} + map { $_->log } + grep { $_->status eq 'error' } + @jobs ], ); diff --git a/Netdisco/lib/auto/App/Netdisco b/Netdisco/lib/auto/App/Netdisco deleted file mode 120000 index 99fb8743..00000000 --- a/Netdisco/lib/auto/App/Netdisco +++ /dev/null @@ -1 +0,0 @@ -../../../share \ No newline at end of file diff --git a/Netdisco/share/views/ajax/admintask/jobqueue.tt b/Netdisco/share/views/ajax/admintask/jobqueue.tt index 77fd4076..aa034549 100644 --- a/Netdisco/share/views/ajax/admintask/jobqueue.tt +++ b/Netdisco/share/views/ajax/admintask/jobqueue.tt @@ -1,4 +1,4 @@ -[% IF NOT results.has_rows %] +[% IF NOT results.size %]
The job queue is empty.
[% ELSE %] @@ -17,7 +17,7 @@ - [% WHILE (row = results.next) %] + [% FOREACH row IN results %]