<commit_before>package EnsEMBL::Web::Object::Tools;

use strict;
use warnings;
no warnings "uninitialized";

use EnsEMBL::Web::SpeciesDefs;
use Storable qw(nfreeze thaw);
use IO::Compress::Gzip qw(gzip $GzipError);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use Bio::Root::IO;
use Data::Dumper;

use base qw(EnsEMBL::Web::Object);

sub caption { return 'Tools'; }
sub short_caption { return "Tools"; }

sub long_caption {
  my $self = shift;
  return 'Tools' if $self->action ne 'BlastResults';

  my $caption =  sprintf ('<h1>Results for %s: <span class="small"><a href ="%s">[Change ticket]</a></span></h1>',
    $self->hub->param('tk'),
    $self->hub->url({ action => 'Summary', tk => undef})
  );
}

sub session_id    { my $self = shift; return $self->hub->session->session_id || undef; }
sub user_id       { my $self = shift; return $self->hub->user ? $self->hub->user->user_id : undef; }
sub species_defs  { return new EnsEMBL::Web::SpeciesDefs; }

sub ticket { my $self = shift; return $self->Obj->{'_ticket'} || undef;}

sub hive_adaptor {
  my $self = shift;
   
  my $adaptor = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new(
    -user   => $self->species_defs->DATABASE_WRITE_USER,
    -pass   => $self->species_defs->DATABASE_WRITE_PASS,
    -host   => $self->species_defs->multidb->{'DATABASE_WEB_HIVE'}{'HOST'},
    -port   => $self->species_defs->multidb->{'DATABASE_WEB_HIVE'}{'PORT'},
    -dbname => $self->species_defs->multidb->{'DATABASE_WEB_HIVE'}{'NAME'},     
  );

  return $adaptor;
}

sub create_ticket {
  my $self = shift;

  my $now = $self->get_time_now;
  my $owner_id = $self->user_id ? 'user_'. $self->user_id : 'session_' . $self->session_id;
  my $name = $self->get_unique_ticket_name; 
  my $job_id = $self->rose_manager('Job')->get_job_id_by_name($self->{'_analysis'});
  my $description = $self->{'_description'};
  my $site_type = $self->species_defs->ENSEMBL_SITETYPE;

  # First create and save ticket
  my $ticket = $self->rose_manager('Ticket')->create_empty_object;
  $ticket->ticket_name($name);
  $ticket->owner_id($owner_id);
  $ticket->job_type_id($job_id);
  $ticket->created_at($now);
  $ticket->modified_at($now);
  $ticket->status('Queued');
  $ticket->site_type($site_type);
  $ticket->ticket_desc($description);

  # Then create analysis object - this contains params needed to run job
  # create serialised and gzipped version of the analysis object to store in ticket DB.
  my $serialised_analysis = $self->serialise;
  $ticket->analysis(object => $$serialised_analysis);
 
  # Finally save objects
  $ticket->save(cascade => 1);

  return $ticket; 
}

sub submit_job {
  my ($self, $ticket)  = @_;

  my $analysis_adaptor  = $self->rose_manager('Analysis');
  my $serialised_object = $analysis_adaptor->retrieve_analysis_object($ticket->ticket_id); 
  my $analysis_object = $self->deserialise($serialised_object);
  $analysis_object->create_jobs($ticket);
  
  return;
}

sub display_status {
  my ($self, $job_status) = @_;
  
  my %status_lookup = (
    'SEMAPHORED'   => 'Pending',
    'READY'        => 'Pending', 
    'BLOCKED'      => 'Pending',
    'CLAIMED'      => 'Pending',
    'COMPILATION'  => 'Pending',
    'PRE_CLEANUP'  => 'Pending',
    'FETCH_INPUT'  => 'Pending',
    'RUN'          => 'Running',  
    'WRITE_OUTPUT' => 'Parsing',
    'POST_CLEANUP' => 'Parsing',
    'DONE'         => 'Completed',  
    'FAILED'       => 'Failed',
    'PASSED_ON'    => 'Failed',
  );
  return $status_lookup{$job_status};
}

sub fetch_current_tickets {
  my $self = shift;
  my (@user_tickets, @session_tickets, $all_tickets);

  if ($self->user_id) {
   @user_tickets = @{$self->rose_manager('Ticket')->fetch_all_tickets_by_user($self->user_id) || []};
  }

  @session_tickets = @{$self->rose_manager('Ticket')->fetch_all_tickets_by_session($self->session_id) || []};
  $all_tickets = [@user_tickets, @session_tickets];

  return $all_tickets;
}

sub fetch_ticket_by_name {
  my ($self, $ticket_name) = @_;
  return unless $ticket_name;

  my $ticket = shift @{$self->rose_manager('Ticket')->fetch_by_ticket_name($ticket_name)};

  if(!$ticket){$ticket = 'The requested ticket "'.$ticket_name.'" could not be found.'
    .' Please be aware that all tickets are deleted after 7 days unless you save'
    .' them to a user account.';
  }

  return $ticket;
}

sub check_submission_status {
  my ($self, $ticket) = @_;
  return $ticket->status unless ref $ticket->sub_job;

  # Set ticket status to be that of the sub job that has progressed the least    
  my %status_priority = (
    'Queued'   => 0,
    'Pending'   => 1,
    'Running'   => 2,
    'Parsing'   => 3,
    'Completed' => 4,
    'Failed'    => 5,
    'Deleted'   => 6,    
  );
 
  my $status;
  foreach my $job (@{$ticket->sub_job}){  
    my $display_status = $self->get_hive_job_status($job->sub_job_id);
    $status = $status_priority{$display_status} << $status_priority{$status} ? $display_status : $status; 
  } 

  # update status in ticket db
  my $now = $self->get_time_now;
  $ticket->status($status);
  $ticket->modified_at($now);
  $ticket->analysis->modified_at($now);
  foreach (@{$ticket->sub_job}){ $_->modified_at($now); } 
  $ticket->save(cascade => 1);

  return $status;
}

<commit_msg>see comment in edit<commit_after>package EnsEMBL::Web::Object::Tools;

use strict;
use warnings;
no warnings "uninitialized";

use EnsEMBL::Web::SpeciesDefs;
use Storable qw(nfreeze thaw);
use IO::Compress::Gzip qw(gzip $GzipError);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use Bio::Root::IO;
use Data::Dumper;

use base qw(EnsEMBL::Web::Object);

sub caption { return 'Tools'; }
sub short_caption { return "Tools"; }

sub long_caption {
  my $self = shift;
  return 'Tools' if $self->action ne 'BlastResults';

  my $caption =  sprintf ('<h1>Results for %s: <span class="small"><a href ="%s">[Change ticket]</a></span></h1>',
    $self->hub->param('tk'),
    $self->hub->url({ action => 'Summary', tk => undef})
  );
}

sub session_id    { my $self = shift; return $self->hub->session->session_id || undef; }
sub user_id       { my $self = shift; return $self->hub->user ? $self->hub->user->user_id : undef; }
sub species_defs  { return new EnsEMBL::Web::SpeciesDefs; }

sub ticket { my $self = shift; return $self->Obj->{'_ticket'} || undef;}

sub hive_adaptor {
  my $self = shift;
   
  my $adaptor = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new(
    -user   => $self->species_defs->DATABASE_WRITE_USER,
    -pass   => $self->species_defs->DATABASE_WRITE_PASS,
    -host   => $self->species_defs->multidb->{'DATABASE_WEB_HIVE'}{'HOST'},
    -port   => $self->species_defs->multidb->{'DATABASE_WEB_HIVE'}{'PORT'},
    -dbname => $self->species_defs->multidb->{'DATABASE_WEB_HIVE'}{'NAME'},     
  );

  return $adaptor;
}

sub create_ticket {
  my $self = shift;

  my $now = $self->get_time_now;
  my $owner_id = $self->user_id ? 'user_'. $self->user_id : 'session_' . $self->session_id;
  my $name = $self->get_unique_ticket_name; 
  my $job_id = $self->rose_manager('Job')->get_job_id_by_name($self->{'_analysis'});
  my $description = $self->{'_description'};
  my $site_type = $self->species_defs->ENSEMBL_SITETYPE;

  # First create and save ticket
  my $ticket = $self->rose_manager('Ticket')->create_empty_object;
  $ticket->ticket_name($name);
  $ticket->owner_id($owner_id);
  $ticket->job_type_id($job_id);
  $ticket->created_at($now);
  $ticket->modified_at($now);
  $ticket->status('Queued');
  $ticket->site_type($site_type);
  $ticket->ticket_desc($description);

  # Then create analysis object - this contains params needed to run job
  # create serialised and gzipped version of the analysis object to store in ticket DB.
  my $serialised_analysis = $self->serialise;
  $ticket->analysis(object => $$serialised_analysis);
 
  # Finally save objects
  $ticket->save(cascade => 1);

  return $ticket; 
}

sub submit_job {
  my ($self, $ticket)  = @_;

  my $analysis_adaptor  = $self->rose_manager('Analysis');
  my $serialised_object = $analysis_adaptor->retrieve_analysis_object($ticket->ticket_id); 
  my $analysis_object = $self->deserialise($serialised_object);
  $analysis_object->create_jobs($ticket);
  
  return;
}

sub display_status {
  my ($self, $job_status) = @_;
  
  my %status_lookup = (
    'SEMAPHORED'   => 'Pending',
    'READY'        => 'Pending', 
    'BLOCKED'      => 'Pending',
    'CLAIMED'      => 'Pending',
    'COMPILATION'  => 'Pending',
    'PRE_CLEANUP'  => 'Pending',
    'FETCH_INPUT'  => 'Pending',
    'RUN'          => 'Running',  
    'WRITE_OUTPUT' => 'Parsing',
    'POST_CLEANUP' => 'Parsing',
    'DONE'         => 'Completed',  
    'FAILED'       => 'Failed',
    'PASSED_ON'    => 'Failed',
  );
  return $status_lookup{$job_status};
}

sub fetch_current_tickets {
  my $self = shift;
  my (@user_tickets, @session_tickets, $all_tickets);

  if ($self->user_id) {
   @user_tickets = @{$self->rose_manager('Ticket')->fetch_all_tickets_by_user($self->user_id) || []};
  }

  @session_tickets = @{$self->rose_manager('Ticket')->fetch_all_tickets_by_session($self->session_id) || []};
  $all_tickets = [@user_tickets, @session_tickets];

  return $all_tickets;
}

sub fetch_ticket_by_name {
  my ($self, $ticket_name) = @_;
  return unless $ticket_name;

  my $ticket = shift @{$self->rose_manager('Ticket')->fetch_by_ticket_name($ticket_name)};

  if(!$ticket){$ticket = 'The requested ticket "'.$ticket_name.'" could not be found.'
    .' Please be aware that all tickets are deleted after 7 days unless you save'
    .' them to a user account.';
  }

  return $ticket;
}

sub check_submission_status {
  my ($self, $ticket) = @_;
  return $ticket->status unless ref $ticket->sub_job;

  # Set ticket status to be that of the sub job that has progressed the least    
  my %status_priority = (
    'Queued'   => 0,
    'Pending'   => 1,
    'Running'   => 2,
    'Parsing'   => 3,
    'Completed' => 4,
    'Failed'    => 5,
    'Deleted'   => 6,    
  );
 
  my $status;
  foreach my $job (@{$ticket->sub_job}){  
    my $display_status = $self->get_hive_job_status($job->sub_job_id);
    $status = $status_priority{$display_status} << $status_priority{$status} ? $display_status : $status; 
  } 

  # update status in ticket db
  my $now = $self->get_time_now;
  $ticket->status($status);
  $ticket->modified_at($now);
  if ($status eq 'Completed' || $status eq 'Failed'){
    # If ticket is complete set all modified/created dates for data belonging to this ticket to be 
    # the same - that way all data will be in equivalent partitions and will be removed at same time
    $ticket->analysis->modified_at($now);
    foreach (@{$ticket->sub_job}){ $_->modified_at($now); } 
    foreach (@{$ticket->result}){ $_->created_at($now); }
  }
  $ticket->save(cascade => 1);

  return $status;
}


{'fraction_non_alphanumeric': 0.17349865827704267, 'fraction_numerical': 0.002220782825946146, 'mean_word_length': 4.048108360579168, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 237, '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': '1071969', 'n_tokens_mistral': 4051, 'n_tokens_neox': 3862, 'n_words': 811}