# -*-perl-*-

# $Id$

# Display status from sushi::status

# $Log$
# Revision 1.5  2012/02/20 22:17:15  koczan
# format fix
#
# Revision 1.4  2010/03/22 23:33:39  koczan
# clean up status output
#
# Revision 1.3  2005/11/01 22:40:57  thomas
# change the way messages are printed
# eliminate warnings with -d:ptkdb
#
# Revision 1.2  2002/07/23 16:02:28  thomas
# version 0.2.5
#
# Revision 1.1  2002/07/10 14:01:29  thomas
# sushi-0.2.4
#


package Sushi::Command::Status;

use strict;
use Getopt::Long;
use Time::localtime;

###############################################################################

sub main {
  my ($class) = shift;

  my $args = {};
  my ($res, $help);
  Getopt::Long::Configure ("bundling");
  if (!($res = GetOptions (
			   'schedule_group|g=s', \$args->{schedule_group},
			   'daemonStatus',       \$args->{daemonStatus},
			   'workerStatus',       \$args->{workerStatus},
			   'groupStatus',        \$args->{groupStatus},
			   'help|h',             \$help
			  ))) {
    $main::log->error (&help);
    return 0;
  }

  defined $args->{schedule_group} or $args->{schedule_group}= shift @ARGV;

  if (!$args->{daemonStatus} &&
      !$args->{workerStatus} &&
      !$args->{groupStatus}) {
    # Print everything if nothing specified...
    $args->{daemonStatus} = 1;
    $args->{workerStatus} = 1;
    $args->{groupStatus}  = 1;
  }

  print "Gathering status.  Please wait...\n";
  my $now = time;
  my $status = Sushi::Status::status ($args);
  print "Data gathering took " . (time - $now) . " seconds.\n";

  printstatus ($args, $status);

  return;
}

###############################################################################

sub printstatus {
  my $args = shift;
  my $status = shift;

  print "\nSushi status as of: " . ctime (time) . "\n\n";

  if ($args->{daemonStatus}) {
    print "Daemon status:\n";

    if (defined $status->{sushid_host}) {
      my $t1 = ctime ($status->{sushid_start_time});
      my $t2 = ctime ($status->{sushid_last_log_msg});
      print <<__Eof__
The daemon appears to be running...
Host: $status->{sushid_host}
Pid: $status->{sushid_pid}
Daemon started:  $t1
Last log update:  $t2

__Eof__
	;
    } else {
      print "There is currently no sushi daemon running...\n";
    }
  }

  if ($args->{workerStatus}) {
    print "Worker Status:\n";
    foreach my $worker (keys %{$status->{workers}}) {
      printf "%-30s last logged %s\n",
	$worker, ctime ($status->{workers}->{$worker});
    }
    print "\n";
  }

  if ($args->{groupStatus}) {
    my $firststart = ctime ($status->{group_firststart});
    my $laststart  = ctime ($status->{group_laststart});
    my $lastdone   = ctime ($status->{group_lastdone});
    my $typetext = "";
    foreach (sort keys %{$status->{group_type}}) {
      $typetext .= "  $_: $status->{group_type}->{$_}\n";
    }
    print <<__Eof__
Group status for group: $status->{schedule_group}
First atom started: $firststart
Most recent start: $laststart
Most recent atom update: $lastdone

Total atoms in group: $status->{group_total_atoms}
Atom types:
$typetext
Atom states:
  Pending: $status->{group_state}->{Pending}
  Scheduled: $status->{group_state}->{Scheduled}
  Active: $status->{group_state}->{Active} (out of 150 possible)
  Success: $status->{group_state}->{Success}
  Fail: $status->{group_state}->{Fail}
  Unknown (should be 0): $status->{group_state}->{Unknown}

__Eof__
      ;
  }
}

###############################################################################

sub aliases {
  return ();
}

###############################################################################

sub printhelp {
  print help();
  return 1;
}

sub shortHelp {
  return "Display status of today's group"
}

sub help {
  my $help = <<__Eof__

status <group_name>

__Eof__
;
}

###############################################################################

1;
