<commit_before>#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use Text::Template;
use LWP::UserAgent;
use Plack::Builder;

$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;

sub copr_info {
  my $ua = LWP::UserAgent->new;
  $ua->timeout(10);
  $ua->env_proxy;
  $ua->ssl_opts(SSL_verify_mode => 0x00);
  
  my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/");
  my $result_v5 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v5/monitor/");

  my $json = JSON->new->allow_nonref;

  my $dec_result_v4 = $json->decode($result_v4->decoded_content);
  my $dec_result_v5 = $json->decode($result_v5->decoded_content);
  my $info = {};

  foreach(@{$dec_result_v4->{'packages'}}) {
    my $package = $_->{'pkg_name'};
    my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
    my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
    $info->{$package}->{'v4_version'} = $version if $status eq "succeeded";
  }

  foreach(@{$dec_result_v5->{'packages'}}) {
    my $package = $_->{'pkg_name'};
    my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
    my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
    $info->{$package}->{'v5_version'} = $version if $status eq "succeeded";
  }

  foreach my $key (%{$info}) {
    next if(ref($key) eq 'HASH');

    my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/master/src/pkg-rpm/$key/$key.spec");
    my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m;
    if($version =~ /%\{version\}/) {
      $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m;
    }

    my $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m;
    $version = "$version-$release";
    $info->{$key}->{'git_version'} = $version;
  }

  return $info;
}

sub info2html {
  my $info = copr_info();
  my $table_entries="";
  foreach my $key (%{$info}) {
    next if(ref($key) eq 'HASH');
    my $fill_row;
    if($info->{$key}->{'v4_version'} eq $info->{$key}->{'v5_version'} && $info->{$key}->{'v4_version'} eq $info->{$key}->{git_version}) {
      $fill_row = "success";
    }
    else {
      $fill_row = "danger";
    }

    $table_entries .= "<tr class=\"$fill_row\">
    <td>$key</td>
    <td>$info->{$key}->{'v4_version'}</td>
    <td>$info->{$key}->{'v5_version'}</td>
    <td>$info->{$key}->{'git_version'}</td>
    </tr>";
  }

  return $table_entries;
}

sub build_html {
  my $data = {
    title => "SPB Copr Stats",
    table_entries => info2html()
  };
  my $template = Text::Template->new(
    TYPE => 'FILE',
    SOURCE => 'template.html.tt'
  );
  return $template->fill_in(HASH => $data);
}

my $app = sub {
  return [
    '200',
    [ 'Content-Type' => 'text/html'],
    [ build_html() ],
  ];
};

builder {
    enable "Static", path => qr!^(/css|/js)!;
      $app;
}<commit_msg>allow server to handle static files<commit_after>#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use Text::Template;
use LWP::UserAgent;
use Plack::Builder;

$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;

sub copr_info {
  my $ua = LWP::UserAgent->new;
  $ua->timeout(10);
  $ua->env_proxy;
  $ua->ssl_opts(SSL_verify_mode => 0x00);
  
  my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/");
  my $result_v5 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v5/monitor/");

  my $json = JSON->new->allow_nonref;

  my $dec_result_v4 = $json->decode($result_v4->decoded_content);
  my $dec_result_v5 = $json->decode($result_v5->decoded_content);
  my $info = {};

  foreach(@{$dec_result_v4->{'packages'}}) {
    my $package = $_->{'pkg_name'};
    my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
    my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
    $info->{$package}->{'v4_version'} = $version if $status eq "succeeded";
  }

  foreach(@{$dec_result_v5->{'packages'}}) {
    my $package = $_->{'pkg_name'};
    my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
    my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
    $info->{$package}->{'v5_version'} = $version if $status eq "succeeded";
  }

  foreach my $key (%{$info}) {
    next if(ref($key) eq 'HASH');

    my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/master/src/pkg-rpm/$key/$key.spec");
    my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m;
    if($version =~ /%\{version\}/) {
      $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m;
    }

    my $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m;
    $version = "$version-$release";
    $info->{$key}->{'git_version'} = $version;
  }

  return $info;
}

sub info2html {
  my $info = copr_info();
  my $table_entries="";
  foreach my $key (%{$info}) {
    next if(ref($key) eq 'HASH');
    my $fill_row;
    if($info->{$key}->{'v4_version'} eq $info->{$key}->{'v5_version'} && $info->{$key}->{'v4_version'} eq $info->{$key}->{git_version}) {
      $fill_row = "success";
    }
    else {
      $fill_row = "danger";
    }

    $table_entries .= "<tr class=\"$fill_row\">
    <td>$key</td>
    <td>$info->{$key}->{'v4_version'}</td>
    <td>$info->{$key}->{'v5_version'}</td>
    <td>$info->{$key}->{'git_version'}</td>
    </tr>";
  }

  return $table_entries;
}

sub build_html {
  my $data = {
    title => "SPB Copr Stats",
    table_entries => info2html()
  };
  my $template = Text::Template->new(
    TYPE => 'FILE',
    SOURCE => 'template.html.tt'
  );
  return $template->fill_in(HASH => $data);
}

my $app = sub {
  return [
    '200',
    [ 'Content-Type' => 'text/html'],
    [ build_html() ],
  ];
};

builder {
    enable "Static", path => qr!^(/css|/js)!, pass_through => 1;
      $app;
}
{'fraction_non_alphanumeric': 0.2438899333447274, 'fraction_numerical': 0.017603828405400788, 'mean_word_length': 3.6334125098970707, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 132, 'https://': 2, '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': '3473400', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2350, 'n_words': 371}