|
| 1 | +use lib 'lib'; |
| 2 | + |
| 3 | +use Bailador; |
| 4 | +use DBIish; |
| 5 | + |
| 6 | +Bailador::import; |
| 7 | + |
| 8 | +my %db-options = :user<foo>, :password<bar>, |
| 9 | + :host<localhost>, :port<3306>, :database<cpandatesters>; |
| 10 | + |
| 11 | +get '/' | '/dists' => sub { |
| 12 | + DBIish.install_driver('mysql'); |
| 13 | + my $dbh = DBIish.connect('mysql', |%db-options); |
| 14 | + |
| 15 | + # TODO create a table `dists` and precalc its PASS ratio in a cronjob |
| 16 | + my $sth = $dbh.prepare("SELECT DISTINCT `distname`, `distauth` |
| 17 | + FROM `cpandatesters`.`reports` |
| 18 | + ORDER BY `distname`"); |
| 19 | + $sth.execute; |
| 20 | + my $dist-lines = ''; |
| 21 | + while $sth.fetchrow_hashref -> $/ { |
| 22 | + $dist-lines ~= template 'dist-line.tt', $/ |
| 23 | + } |
| 24 | + $dbh.disconnect(); |
| 25 | + template 'main.tt', { |
| 26 | + :breadcrumb(['Distributions']), |
| 27 | + :content( template 'dists.tt', { :$dist-lines }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +get /^ '/dist/' (.+) / => sub ($distname) { |
| 32 | + DBIish.install_driver('mysql'); |
| 33 | + my $dbh = DBIish.connect('mysql', |%db-options); |
| 34 | + |
| 35 | + my $sth = $dbh.prepare("SELECT `id`,`grade`,`distname`,`distauth`,`distver`,`compver`,`backend`,`osname`,`osver`,`arch` |
| 36 | + FROM `cpandatesters`.`reports` |
| 37 | + WHERE `distname`=?"); |
| 38 | + $sth.execute($distname); |
| 39 | + my %reports; |
| 40 | + my @osnames = <linux mswin32 darwin netbsd openbsd freebsd solaris>; |
| 41 | + my %stats; |
| 42 | + while $sth.fetchrow_hashref -> $/ { |
| 43 | + %stats{$<compver>}{$<osname>}{$<backend>}{$<grade>}++; |
| 44 | + @osnames.push: $<osname> unless $<osname> ~~ any @osnames; |
| 45 | + |
| 46 | + $<distver> = '0' if $<distver> eq '*'; |
| 47 | + $<breadcrumb> = "/dist/$distname"; |
| 48 | + %reports{$<distver>}.push: template 'report-line.tt', $/ |
| 49 | + } |
| 50 | + $dbh.disconnect(); |
| 51 | + for @osnames -> $osname { |
| 52 | + for %stats.keys -> $compver is copy { |
| 53 | + for <moar jvm parrot> -> $backend { |
| 54 | + my $all = [+] %stats{$compver}{$osname}{$backend}.values; |
| 55 | + for <PASS FAIL NA> -> $grade { |
| 56 | + if %stats{$compver}{$osname}{$backend}{$grade} { |
| 57 | + %stats{$compver}{$osname}{$backend}{$grade} /= $all / 100; |
| 58 | + if 0 < %stats{$compver}{$osname}{$backend}{$grade} < 2 { |
| 59 | + %stats{$compver}{$osname}{$backend}{$grade}.=ceiling |
| 60 | + } |
| 61 | + else { |
| 62 | + %stats{$compver}{$osname}{$backend}{$grade}.=floor; |
| 63 | + } |
| 64 | + } |
| 65 | + else { |
| 66 | + %stats{$compver}{$osname}{$backend}{$grade} = 0 |
| 67 | + } |
| 68 | + } |
| 69 | + my $deviation = 100 - [+] %stats{$compver}{$osname}{$backend}.values; |
| 70 | + if -10 < $deviation < 10 { |
| 71 | + my $grade = %stats{$compver}{$osname}{$backend}.sort(*.value).reverse[0].key; |
| 72 | + %stats{$compver}{$osname}{$backend}{$grade} += $deviation; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + my $reports = ''; |
| 79 | + my @distvers = %reports.keys.sort({ Version.new($^b) cmp Version.new($^a)}); |
| 80 | + for @distvers -> $distver { |
| 81 | + $reports ~= '<h4>v' ~ $distver ~ '</h4>' |
| 82 | + ~ template 'report-table.tt', { :report-lines(%reports{$distver}.join("\n")) } |
| 83 | + } |
| 84 | + |
| 85 | + template 'main.tt', { |
| 86 | + :breadcrumb(['Distributions' => '/dists', ~$distname]), |
| 87 | + :content( template 'dist.tt', { |
| 88 | + :stats( template 'stats.tt', [@osnames.sort], $%stats, &template ), |
| 89 | + :report-tables($reports) |
| 90 | + }), |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +get / '/report/' (.+) '/' (\d+) / => sub ($path, $id) { |
| 95 | + DBIish.install_driver('mysql'); |
| 96 | + my $dbh = DBIish.connect('mysql', |%db-options); |
| 97 | + |
| 98 | + my $sth = $dbh.prepare("SELECT * |
| 99 | + FROM `cpandatesters`.`reports` |
| 100 | + WHERE `id`=?"); |
| 101 | + $sth.execute($id); |
| 102 | + if $sth.fetchrow_hashref -> $r { |
| 103 | + my @path = $path.Str.split('/'); |
| 104 | + my $breadcrumb = ["Report $id"]; |
| 105 | + if @path[0] eq 'dist' { |
| 106 | + $breadcrumb.unshift: 'Distributions' => '/dists', @path[1] => "/dist/@path[1]" |
| 107 | + } |
| 108 | + $dbh.disconnect(); |
| 109 | + template 'main.tt', { |
| 110 | + :$breadcrumb, |
| 111 | + :content( template 'report-details.tt', $r, from-json $r<raw>) |
| 112 | + } |
| 113 | + } |
| 114 | + else { |
| 115 | + status 404; |
| 116 | + $dbh.disconnect(); |
| 117 | + return "File not found"; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +get /^ '/' ([ css | js | fonts ] '/' .+) / => sub ($file is copy) { |
| 122 | + content_type 'text/css'; |
| 123 | + $file.=Str; |
| 124 | + $file ~~ s/ '?'.* //; |
| 125 | + return $file.IO.slurp(:enc<ascii>) if $file.IO.f; |
| 126 | +} |
| 127 | + |
| 128 | +post '/report' => sub { |
| 129 | + my $report = from-json request.body; |
| 130 | + |
| 131 | + DBIish.install_driver('mysql'); |
| 132 | + my $dbh = DBIish.connect('mysql', |%db-options); |
| 133 | + |
| 134 | + my $sth = $dbh.prepare("INSERT INTO `cpandatesters`.`reports` |
| 135 | + (`grade`,`distname`,`distauth`,`distver`,`compver`,`backend`,`osname`,`osver`,`arch`,`raw`) |
| 136 | + VALUES (?,?,?,?,?,?,?,?,?,?)"); |
| 137 | + $sth.execute( |
| 138 | + $report<build-passed> && $report<test-passed> ?? 'PASS' !! 'FAIL', |
| 139 | + $report<name>, |
| 140 | + $report<metainfo><authority> // $report<metainfo><author> // $report<metainfo><auth>, |
| 141 | + $report<version>, |
| 142 | + $report<perl><compiler><version>, |
| 143 | + $report<vm><name>, |
| 144 | + $report<distro><name>, |
| 145 | + $report<distro><release> // $report<distro><version>, |
| 146 | + $report<kernel><arch>, |
| 147 | + request.body, |
| 148 | + ); |
| 149 | + |
| 150 | + $dbh.disconnect(); |
| 151 | +} |
| 152 | + |
| 153 | +baile; |
0 commit comments