<commit_before>#!/usr/bin/env perl

use strict;
use warnings;

use Bio::EnsEMBL::Hive::URLFactory;
use JSON::XS;
use HTML::Template;
use Data::Dumper;

my $json_data = shift @ARGV || '{"url":["mysql://ensro@127.0.0.1:2912/mp12_compara_nctrees_69b"], "logic_name":["infernal"]}';
my $details_template = "../static/analysis_details.html"; ## TODO: use BASEDIR or something similar

my $url = decode_json($json_data)->{url}->[0];
my $logic_name = decode_json($json_data)->{logic_name}->[0];

my $dbConn = Bio::EnsEMBL::Hive::URLFactory->fetch($url);

my $response = {
		status => "ok",
		analysis_info => "",
	       };

if (defined $dbConn) {
  my $analysis;
  eval {
    $analysis = $dbConn->get_AnalysisAdaptor()->fetch_by_logic_name_or_url($logic_name);
  };
  if ($@) {
    $response->{status} = formError();
  }
  if (! defined $analysis) {
    $response->{status} = formError();
  }
  $response->{analysis_info} = formAnalysisInfo($analysis);
} else {
  $response->{status} = formError();
}

my $json = JSON::XS->new->indent(0);
print $json->encode($response);

sub formError {
  return "I have lost connection to the database. Please connect again";
}

sub formAnalysisInfo {
  my ($analysis) = @_;
  my $analysis_stats = $analysis->stats();

  my $parameters = $analysis->parameters();
  $parameters =~ s/,/,<br \/>/g;
  my $info;
  $info->{logic_name}        = "analysis/" . $analysis->logic_name();
  $info->{module}            = "analysis/" . $analysis->module();
  $info->{parameters}        = "analysis/" . $parameters, #$analysis->parameters();
  $info->{id}                = "analysis/" . $analysis->dbID();
  $info->{priority}          = "analysis_stats/" . $analysis_stats->priority();
  $info->{batch_size}        = "analysis_stats/" . $analysis_stats->batch_size();
  $info->{can_be_empty}      = "analysis_stats/" . $analysis_stats->can_be_empty();
  $info->{resource_class_id} = "anslysis_stats/" . $analysis_stats->resource_class_id();
  $info->{hive_capacity}     = "analysis_stats/" . $analysis_stats->hive_capacity();

  my $template = HTML::Template->new(filename => $details_template);
  $template->param(%$info);

  return $template->output();
}
<commit_msg>Improvements in the details section<commit_after>#!/usr/bin/env perl

use strict;
use warnings;

use Bio::EnsEMBL::Hive::URLFactory;
use JSON::XS;
use HTML::Template;
use Data::Dumper;

my $json_data = shift @ARGV || '{"url":["mysql://ensro@127.0.0.1:2912/mp12_compara_nctrees_69b"], "logic_name":["infernal"]}';
my $details_template = "../static/analysis_details.html"; ## TODO: use BASEDIR or something similar

my $url = decode_json($json_data)->{url}->[0];
my $logic_name = decode_json($json_data)->{logic_name}->[0];

my $dbConn = Bio::EnsEMBL::Hive::URLFactory->fetch($url);

my $response = {
		status => "ok",
		analysis_info => "",
	       };

if (defined $dbConn) {
  my $analysis;
  eval {
    $analysis = $dbConn->get_AnalysisAdaptor()->fetch_by_logic_name_or_url($logic_name);
  };
  if ($@) {
    $response->{status} = formError();
  }
  if (! defined $analysis) {
    $response->{status} = formError();
  }
  $response->{analysis_info} = formAnalysisInfo($analysis);
} else {
  $response->{status} = formError();
}

my $json = JSON::XS->new->indent(0);
print $json->encode($response);

sub formError {
  return "I have lost connection to the database. Please connect again";
}

sub formAnalysisInfo {
  my ($analysis) = @_;
  my $analysis_stats = $analysis->stats();

  my $info;
  $info->{logic_name}        = $analysis->logic_name();
  $info->{module}            = $analysis->module();
  $info->{parameters}        = template_mappings_PARAMS($analysis,
							"parameters");
  $info->{id}                = $analysis->dbID();
  $info->{priority}          = template_mappings_SELECT($analysis_stats,
							"priority",
							build_values({1=>[0,20]}));

  $info->{batch_size}        = template_mappings_SELECT($analysis_stats,
							"batch_size",
							build_values({1=>[0,9],10=>[10,90],100=>[100,1000]}));

  $info->{can_be_empty}      = template_mappings_SELECT($analysis_stats,
							"can_be_empty",
							build_values({1=>[0,1]}));

  $info->{resource_class_id} = $analysis_stats->resource_class_id();
  $info->{hive_capacity}     = template_mappings_SELECT($analysis_stats,
							"hive_capacity",
							build_values({1=>[-1,9],10=>[10,90],100=>[100,1000]}));

  my $template = HTML::Template->new(filename => $details_template);
  $template->param(%$info);

  return $template->output();
}

sub template_mappings_SELECT {
  my ($obj, $method, $vals) = @_;
  my $curr_val = $obj->$method;
  return [map {{is_current => $curr_val == $_, $method."_value" => $_}} @$vals];
}

sub template_mappings_PARAMS {
  my ($obj, $method) = @_;
  my $curr_raw_val = $obj->$method;
  my $curr_val = eval $curr_raw_val;
  my $vals;
  for my $param (keys %$curr_val) {
    push @$vals, {"key" => $param, "value" => $curr_val->{$param}};
  }
  return $vals;
}

sub build_values {
  my ($ranges) = @_;
  my @vals;
  for my $step (keys %$ranges) {
    for (my $i = $ranges->{$step}->[0]; $i <= $ranges->{$step}->[1]; $i+=$step) {
      push @vals, $i;
    }
  }
  return [@vals];
}
{'fraction_non_alphanumeric': 0.20228195706826532, 'fraction_numerical': 0.015664281570295882, 'mean_word_length': 4.070588235294117, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '638577', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1844, 'n_words': 326}