<commit_before>package CSG::Mapper::Job::Factory::Implementation::csg;

use CSG::Base qw(cmd file);
use CSG::Constants;
use CSG::Mapper::Util qw(parse_time);

use Moose;

Readonly::Scalar my $PREFIX     => q{/usr/cluster/bin};
Readonly::Scalar my $SACCT_CMD  => File::Spec->join($PREFIX, 'sacct');
Readonly::Scalar my $SQUEUE_CMD => File::Spec->join($PREFIX, 'squeue');
Readonly::Scalar my $SBATCH_CMD => File::Spec->join($PREFIX, 'sbatch');
Readonly::Scalar my $SCANCEL_CMD => File::Spec->join($PREFIX, 'scancel');

Readonly::Scalar my $JOB_ELAPSED_TIME_FORMAT   => $SACCT_CMD . q{ -j %d -X -n -o elapsed};
Readonly::Scalar my $JOB_STATE_CMD_FORMAT      => $SACCT_CMD . q{ -j %d -X -n -o state%%20};
Readonly::Scalar my $JOB_TIME_REMAINING_FORMAT => $SQUEUE_CMD . q{ -h -o %%L -j %d};
Readonly::Scalar my $JOB_OUTPUT_REGEXP         => qr/^Submitted batch job (?<jobid>\d+)$/i;

Readonly::Hash my %JOB_STATES => (
  RUNNING   => 'running',
  COMPLETED => 'completed',
  FAILED    => 'failed',
  REQUEUED  => 'requeued',
  CANCELLED => 'cancelled',
  TIMEOUT   => 'timeout',
);

has 'job_id'            => (is => 'rw', isa => 'Int',       predicate => 'has_job_id');
has 'job_output_regexp' => (is => 'ro', isa => 'RegexpRef', default   => sub {return $JOB_OUTPUT_REGEXP});
has 'job_submit_cmd'    => (is => 'ro', isa => 'Str',       default   => sub {return $SBATCH_CMD});
has 'job_kill_cmd'      => (is => 'ro', isa => 'Str',       default   => sub {return $SCANCEL_CMD});

sub elapsed {
  my ($self) = @_;
  my $cmd = sprintf $JOB_ELAPSED_TIME_FORMAT, $self->job_id;
  chomp(my $time = capture(EXIT_ANY, $cmd));
  return parse_time($time);
}

sub state {
  my ($self) = @_;
  my $cmd = sprintf $JOB_STATE_CMD_FORMAT, $self->job_id;
  chomp(my $state = capture(EXIT_ANY, $cmd));
  $state =~ s/^\s+|\s+$//g;
  return $JOB_STATES{$state};
}

sub _time_remaining {
  return capture(sprintf($JOB_TIME_REMAINING_FORMAT, shift->job_id));
}

no Moose;
__PACKAGE__->meta->make_immutable;

1;
<commit_msg>the job reason can include more than just the keyword we are looking for<commit_after>package CSG::Mapper::Job::Factory::Implementation::csg;

use CSG::Base qw(cmd file);
use CSG::Constants;
use CSG::Mapper::Util qw(parse_time);

use Moose;

Readonly::Scalar my $PREFIX     => q{/usr/cluster/bin};
Readonly::Scalar my $SACCT_CMD  => File::Spec->join($PREFIX, 'sacct');
Readonly::Scalar my $SQUEUE_CMD => File::Spec->join($PREFIX, 'squeue');
Readonly::Scalar my $SBATCH_CMD => File::Spec->join($PREFIX, 'sbatch');
Readonly::Scalar my $SCANCEL_CMD => File::Spec->join($PREFIX, 'scancel');

Readonly::Scalar my $JOB_ELAPSED_TIME_FORMAT   => $SACCT_CMD . q{ -j %d -X -n -o elapsed};
Readonly::Scalar my $JOB_STATE_CMD_FORMAT      => $SACCT_CMD . q{ -j %d -X -n -o state%%20};
Readonly::Scalar my $JOB_TIME_REMAINING_FORMAT => $SQUEUE_CMD . q{ -h -o %%L -j %d};
Readonly::Scalar my $JOB_OUTPUT_REGEXP         => qr/^Submitted batch job (?<jobid>\d+)$/i;

Readonly::Hash my %JOB_STATES => (
  RUNNING   => 'running',
  COMPLETED => 'completed',
  FAILED    => 'failed',
  REQUEUED  => 'requeued',
  CANCELLED => 'cancelled',
  TIMEOUT   => 'timeout',
);

has 'job_id'            => (is => 'rw', isa => 'Int',       predicate => 'has_job_id');
has 'job_output_regexp' => (is => 'ro', isa => 'RegexpRef', default   => sub {return $JOB_OUTPUT_REGEXP});
has 'job_submit_cmd'    => (is => 'ro', isa => 'Str',       default   => sub {return $SBATCH_CMD});
has 'job_kill_cmd'      => (is => 'ro', isa => 'Str',       default   => sub {return $SCANCEL_CMD});

sub elapsed {
  my ($self) = @_;
  my $cmd = sprintf $JOB_ELAPSED_TIME_FORMAT, $self->job_id;
  chomp(my $time = capture(EXIT_ANY, $cmd));
  return parse_time($time);
}

sub state {
  my ($self) = @_;
  my $cmd = sprintf $JOB_STATE_CMD_FORMAT, $self->job_id;
  chomp(my $state = capture(EXIT_ANY, $cmd));
  $state =~ s/\s+([\w]+)\s+(?:.*)?/$1/;
  return $JOB_STATES{$state};
}

sub _time_remaining {
  return capture(sprintf($JOB_TIME_REMAINING_FORMAT, shift->job_id));
}

no Moose;
__PACKAGE__->meta->make_immutable;

1;

{'fraction_non_alphanumeric': 0.192619926199262, 'fraction_numerical': 0.0017220172201722018, 'mean_word_length': 4.378306878306878, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624630', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1592, 'n_words': 353}