#! /usr/bin/perl -w
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use strict;

# Update the module include path
BEGIN
{
    my $Dir = $0;
    if ( $Dir =~ /(.*)\/.*/ )
    {
	push @INC, "$1";
    }
}
use HawkeyePublish;
use HawkeyeLib;

# Prototypes
sub Init( );
sub RunIt( );
sub RunJobHistory(  );


# Setup the hawkeye stuff
my $Hawkeye;
my %Config = (
	      Verbose => 0,
	      Constraint => "",
	      Owner => "",
	      Long => 0,
	      HistFile => "",
	     );
my $Hash;

# Do it
Init();
RunIt();

sub Init()
{
    HawkeyeLib::DoConfig( );

    $Hawkeye = HawkeyePublish->new;
    $Hawkeye->Quiet( 1 );
    $Hawkeye->AutoIndexSet( 0 );

    # Read the config info..
    my $Tmp;

    $Tmp = HawkeyeLib::ReadConfig( "_long", "false" );
    $Config{Long} = ( $Tmp =~ /true/i ) ? 1 : 0;

    $Tmp = HawkeyeLib::ReadConfig( "_const", "" );
    $Config{Constraint} = $Tmp;

    $Tmp = HawkeyeLib::ReadConfig( "_owner", "" );
    $Config{Owner} = $Tmp;

    $Tmp = HawkeyeLib::ReadConfig( "_file", "" );
    $Config{HistFile} = $Tmp;

    foreach my $Arg ( @ ARGV )
    {
	if ( $Arg =~ /-v/ )
	{
	    $Config{Verbose}++;
	}
    }

}

# Do the real work here...
sub RunIt()
{
    # Start things off
    $Hash = HawkeyeHash->new( \$Hawkeye, "" );

    RunJobHistory( );

    # Ok, summary is done...
    $Hash->Store( );
    $Hawkeye->Publish( );
}

# Run condor_history & parse it's output..
sub RunJobHistory(  )
{
    # Build the command line
    my $Cmd = "condor_history -l";

    # Add in the '-f file'
    if ( $Config{HistFile} ne "" )
    {
	if ( ! -f $Config{HistFile} )
	{
	    print STDERR "History file '$Config{HistFile}' does not exist!\n";
	    exit 1;
	}
	$Cmd .= " -f '$Config{HistFile}'";
    }

    # Add in constraints
    $Cmd .= " -const '$Config{Constraint}'" if ( $Config{Constraint} ne "" );
    $Cmd .= "  '$Config{Owner}'" if ( $Config{Owner} ne "" );
    print STDERR "Running '$Cmd'\n" if ( $Config{Verbose} );

    # Run it
    if ( ! open( HIST, "$Cmd|" ) )
    {
	print STDERR "Can't get history info\n";
	return;
    }

    # Array of interesting stuff...
    my %Interesting =
	( ExitStatus => HawkeyePublish::TypeNumber,
	  NumRestarts => HawkeyePublish::TypeNumber,
	  JobStatus => HawkeyePublish::TypeNumber,
	  Owner => HawkeyePublish::TypeString,
	  JobUniverse => HawkeyePublish::TypeNumber,
	  ClusterId => HawkeyePublish::TypeNumber,
	  ProcId => HawkeyePublish::TypeNumber,
	);

    # Job information..
    my @Jobs;
    my @JobStatusMap = (
			{ Count => 0, String => "Unexpanded", },
			{ Count => 0, String => "Idle", },
			{ Count => 0, String => "Running", },
			{ Count => 0, String => "Removed", },
			{ Count => 0, String => "Completed", },
			{ Count => 0, String => "Held", },
			{ Count => 0, String => "SubmissionError", },
		       );
    my @JobUniverseMap = (
			  { Count => 0, String => "Invalid", },
			  { Count => 0, String => "Standard", },
			  { Count => 0, String => "Pipe", },
			  { Count => 0, String => "Linda", },
			  { Count => 0, String => "PVM", },
			  { Count => 0, String => "Vanilla", },
			  { Count => 0, String => "PVMD", },
			  { Count => 0, String => "Scheduler", },
			  { Count => 0, String => "MPI", },
			  { Count => 0, String => "Globus", },
			  { Count => 0, String => "Java", },
			  { Count => 0, String => "Parallel", },
			 );

    # Parse the output...
    my $CurJob;
    while ( <HIST> )
    {
	s/^\s+//g;
	chomp;

	# Ad separator; sum it up here...
	if ( $_ eq "" )
	{
	    if ( exists $CurJob->{ExitStatus} )
	    {
		push @Jobs, $CurJob;
		if ( exists $CurJob->{JobStatus} )
		{
		    my $JobStatus = $CurJob->{JobStatus};
		    $JobStatusMap[$JobStatus]{Count}++
			if ( $JobStatus <= $#JobStatusMap );
		}
		if ( exists $CurJob->{JobUniverse} )
		{
		    my $Universe = $CurJob->{JobUniverse};
		    $JobUniverseMap[$Universe]{Count}++
			if ( $Universe <= $#JobUniverseMap );
		}
	    }
	    $CurJob = {};
	    next;
	}

	if ( /(\S+)\s*=\s*(.*)/ )
	{
	    my $Attr = $1;
	    my $Value = $2;
	    if ( exists ( $Interesting{$Attr} ) )
	    {
		$Value =~ s/^\"//;
		$Value =~ s/\"$//;
		$CurJob->{$Attr} = $Value;
	    }
	}
    }
    close( HIST );

    # Publish the status summary info
    foreach my $Status ( 0 .. $#JobStatusMap )
    {
	$Hash->Add( "JobStatus_" . $JobStatusMap[$Status]{String},
		    HawkeyePublish::TypeNumber,
		    $JobStatusMap[$Status]{Count} );
    }

    # Publish the Universe summary info
    foreach my $Universe ( 0 .. $#JobUniverseMap )
    {
	$Hash->Add( "JobUniverse_" . $JobUniverseMap[$Universe]{String},
		    HawkeyePublish::TypeNumber,
		    $JobUniverseMap[$Universe]{Count} );
    }

    # Publish it all...
    if ( $Config{Long} )
    {
	foreach my $Job ( @Jobs )
	{
	    my $JobId = $Job->{ClusterId} . "_" . $Job->{ProcId};
	    foreach my $Attr ( keys %{$Job} )
	    {
		next if ( ! exists $Interesting{$Attr} );
		next if ( $Interesting{$Attr} eq "" );
		$Hash->Add( "Job_" . $JobId . "_" . $Attr,
			    $Interesting{$Attr},
			    $Job->{$Attr}
			  );
	    }
	}
    }
}
