Skip to content

Commit

Permalink
Make Wake/Shutdown/Restart buttons buttons honor the location filter #…
Browse files Browse the repository at this point in the history
…336

The buttons in question affect all clients at all locations. It would make more sense if the buttons affected the clients based on the location filter. This will still allow all clients to be affected at once, but also to only affect those that are visible.
  • Loading branch information
kylemhall committed Oct 27, 2023
1 parent a91f744 commit 12d01b2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 28 deletions.
45 changes: 32 additions & 13 deletions lib/Libki/Controller/Administration/API/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -528,16 +528,26 @@ The status change will send a message to each client to initiate shutdown.
=cut

sub shutdown_all : Local : Args(0) {
my ( $self, $c ) = @_;
sub shutdown_all : Local {
my ( $self, $c, $location ) = @_;

my $success = 0;
my $clients = $c->model('DB::Client')->search({ instance => $c->instance });
my $status = $c->setting('ClientShutdownAction') || 'shutdown';
my $success = 1;

my $search;
$search->{instance} = $c->instance;
$search->{location} = $location if $location;
my $clients = $c->model('DB::Client')->search($search);

my $status = $c->setting('ClientShutdownAction') || 'shutdown';

my $count = 0;
while ( my $client = $clients->next() ) {
if ($client->status eq 'online') {
$success = 1 if $client->update( { status => $status } );
if ( $client->update( { status => $status } ) ) {
$count++;
} else {
$success = 0;
}

$c->model('DB::Statistic')->create(
{
Expand All @@ -554,7 +564,7 @@ sub shutdown_all : Local : Args(0) {
}
}

$c->stash( 'success' => $success );
$c->stash( success => $success, count => $count );
$c->forward( $c->view('JSON') );
}

Expand All @@ -565,15 +575,24 @@ The status change will send a message to each client to initiate reboot.
=cut

sub restart_all : Local : Args(0) {
my ( $self, $c ) = @_;
sub restart_all : Local {
my ( $self, $c, $location ) = @_;

my $success = 0;
my $clients = $c->model('DB::Client')->search({ instance => $c->instance });
my $success = 1;

my $search;
$search->{instance} = $c->instance;
$search->{location} = $location if $location;
my $clients = $c->model('DB::Client')->search($search);

my $count = 0;
while ( my $client = $clients->next() ) {
if ($client->status eq 'online') {
$success = 1 if $client->update( { status => 'restart' } );
if ( $client->update( { status => 'restart' } ) ) {
$count++;
} else {
$success = 0;
}

$c->model('DB::Statistic')->create(
{
Expand All @@ -590,7 +609,7 @@ sub restart_all : Local : Args(0) {
}
}

$c->stash( 'success' => $success );
$c->stash( success => $success, count => $count );
$c->forward( $c->view('JSON') );
}

Expand Down
45 changes: 30 additions & 15 deletions root/dynamic/templates/administration/index.tt
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@
<li class="nav-item">
<div class="btn-group" role="group" aria-label="Client actions">
[% IF c.setting('ClientMACAddresses') %]
<a href="#" class="turn-on-button btn btn-success"><i class="fas fa-power-off"></i> [% c.loc("Turn on all clients") %]</a>
<a href="#" class="turn-on-button btn btn-success"><i class="fas fa-power-off"></i> [% c.loc("Turn on clients") %]</a>
[% END %]
<a href="#" class="restart-button btn btn-warning"><i class="fas fa-redo"></i> [% c.loc("Restart all clients") %]</a>
<a href="#" class="shutdown-button btn btn-danger"><i class="fas fa-power-off"></i> [% c.loc("Turn off all clients") %]</a>
<a href="#" class="restart-button btn btn-warning"><i class="fas fa-redo"></i> [% c.loc("Restart clients") %]</a>
<a href="#" class="shutdown-button btn btn-danger"><i class="fas fa-power-off"></i> [% c.loc("Turn off clients") %]</a>
</div>
</li>
</ul>
Expand Down Expand Up @@ -1288,39 +1288,54 @@ $(document).ready(function() {
});

$(".turn-on-button").click(function () {
if ( confirm("[% c.loc("Are you sure you want to turn on all clients?") %]") ) {
$.getJSON( '[% c.uri_for('/administration/api/client/wakeup') %]', function(data) {
const location = window.location_filter || "";

let msg = "[% c.loc("Are you sure you want to turn on all clients?") %]";
if ( location ) msg = "[% c.loc("Are you sure you want to turn on all clients at location ") %]" + location;

if ( confirm( msg ) ) {
$.getJSON( '[% c.uri_for('/administration/api/client/wakeup/') %]' + location, function(data) {
if ( data.success ) {
DisplayMessage( "success", "[% c.loc("Clients turned on.") %]" );
DisplayMessage( "success", "[% c.loc("Clients turned on: ") %]" + data.count );
$("#client-table").dataTable().fnDraw(true);
} else {
DisplayMessage( "error", "[% c.loc("Unable to turn on all clients.") %]" );
DisplayMessage( "error", "[% c.loc("Unable to wake all clients. Clients turned on: ") %]" + data.count );
}
});
}
});

$(".shutdown-button").click(function () {
if ( confirm("[% c.loc("Are you sure you want to turn off all clients?") %]") ) {
$.getJSON( '[% c.uri_for('/administration/api/client/shutdown_all') %]', function(data) {
const location = window.location_filter || "";

let msg = "[% c.loc("Are you sure you want to shutdown all clients?") %]";
if ( location ) msg = "[% c.loc("Are you sure you want to turn off all clients at location ") %]" + location;

if ( confirm( msg ) ) {
$.getJSON( '[% c.uri_for('/administration/api/client/shutdown_all/') %]' + location, function(data) {
if ( data.success ) {
DisplayMessage( "success", "[% c.loc("Clients turned off.") %]" );
DisplayMessage( "success", "[% c.loc("Clients turned off: ") %]" + data.count );
$("#client-table").dataTable().fnDraw(true);
} else {
DisplayMessage( "error", "[% c.loc("Unable to turn off all clients.") %]" );
DisplayMessage( "error", "[% c.loc("Unable to shutdown all clients. Clients turned off: ") %]" + data.count );
}
});
}
});

$(".restart-button").click(function () {
if ( confirm("[% c.loc("Are you sure you want to restart all clients?") %]") ) {
$.getJSON( '[% c.uri_for('/administration/api/client/restart_all') %]', function(data) {
const location = window.location_filter || "";

let msg = "[% c.loc("Are you sure you want to restart all clients?") %]";
if ( location ) msg = "[% c.loc("Are you sure you want to restart all clients at location ") %]" + location;

if ( confirm( msg ) ) {
$.getJSON( '[% c.uri_for('/administration/api/client/restart_all/') %]' + location, function(data) {
if ( data.success ) {
DisplayMessage( "success", "[% c.loc("Clients restarted.") %]" );
DisplayMessage( "success", "[% c.loc("Clients restarted: ") %]" + data.count );
$("#client-table").dataTable().fnDraw(true);
} else {
DisplayMessage( "error", "[% c.loc("Unable to restart all clients.") %]" );
DisplayMessage( "error", "[% c.loc("Unable to restart all clients. Clients restarted: ") %]" + data.count );
}
});
}
Expand Down

0 comments on commit 12d01b2

Please sign in to comment.