| 1 | package Ukigumo::Server::Command::Report; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use 5.010001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use Amon2::Declare; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use URI::WithBase; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use Data::Page::NoTotalEntries; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use Data::Validator; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use Ukigumo::Server::Command::Branch; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use Compress::Zlib; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use Encode; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use List::MoreUtils qw/uniq/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | sub get_last_status { | ||||||||||||
| 17 | 0 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
get_last_status: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 18 | 0 | state $rule = Data::Validator->new( | |||||||||||
| 19 | project => { isa => 'Str' }, | ||||||||||||
| 20 | branch => { isa => 'Str' }, | ||||||||||||
| 21 | ); | ||||||||||||
| 22 | 0 | my $args = $rule->validate(@_); | |||||||||||
| 23 | |||||||||||||
| 24 | my $itr = c->db->search_by_sql( | ||||||||||||
| 25 | q{SELECT status FROM report INNER JOIN branch ON (report.report_id=branch.last_report_id) WHERE | ||||||||||||
| 26 | branch.project = ? AND | ||||||||||||
| 27 | branch.branch = ? | ||||||||||||
| 28 | ORDER BY report_id DESC LIMIT 1}, | ||||||||||||
| 29 | 0 | [$args->{project}, $args->{branch}], | |||||||||||
| 30 | ); | ||||||||||||
| 31 | 0 | $itr->suppress_object_creation(1); | |||||||||||
| 32 | |||||||||||||
| 33 | 0 | ▶ | my $row = $itr->next || {}; | ||||||||||
|
Condition0%0/2
$itr->next || {}
MC/DC0%0/1
$itr->next || {}
$itr->next
Truth table0%0/2
A
A= $itr->next
| |||||||||||||
| 34 | 0 | $row->{status}; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | sub recent_list { | ||||||||||||
| 38 | 0 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
recent_list: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 39 | 0 | state $rule = Data::Validator->new( | |||||||||||
| 40 | limit => { isa => 'Int', default => 50 }, | ||||||||||||
| 41 | page => { isa => 'Int', default => 1 }, | ||||||||||||
| 42 | ); | ||||||||||||
| 43 | 0 | my $args = $rule->validate(@_); | |||||||||||
| 44 | |||||||||||||
| 45 | my $itr = c->db->search_by_sql( | ||||||||||||
| 46 | q{SELECT branch.project, branch.branch, report.report_id, report.revision, report.status, report.ctime, report.elapsed_time_sec | ||||||||||||
| 47 | FROM report INNER JOIN branch ON (branch.branch_id=report.branch_id) | ||||||||||||
| 48 | ORDER BY report_id DESC | ||||||||||||
| 49 | LIMIT ? OFFSET ?}, | ||||||||||||
| 50 | 0 | [$args->{limit} + 1, $args->{limit}*($args->{page}-1)], | |||||||||||
| 51 | ); | ||||||||||||
| 52 | 0 | $itr->suppress_object_creation(1); | |||||||||||
| 53 | 0 | my $reports = [$itr->all]; | |||||||||||
| 54 | 0 | my $has_next = do { | |||||||||||
| 55 | 0 | ▶ | if (@$reports == $args->{limit}+1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 0 | pop @$reports; | |||||||||||
| 57 | 0 | 1; | |||||||||||
| 58 | } else { | ||||||||||||
| 59 | 0 | 0; | |||||||||||
| 60 | } | ||||||||||||
| 61 | }; | ||||||||||||
| 62 | my $pager = Data::Page::NoTotalEntries->new( | ||||||||||||
| 63 | has_next => $has_next, | ||||||||||||
| 64 | entries_per_page => $args->{limit}, | ||||||||||||
| 65 | current_page => $args->{page}, | ||||||||||||
| 66 | 0 | entries_on_this_page => scalar @$reports, | |||||||||||
| 67 | ); | ||||||||||||
| 68 | 0 | ▶ | return wantarray ? ($reports, $pager) : $reports; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | sub failure_list { | ||||||||||||
| 72 | 0 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
failure_list: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 73 | 0 | state $rule = Data::Validator->new( | |||||||||||
| 74 | limit => { isa => 'Int', default => 50 }, | ||||||||||||
| 75 | page => { isa => 'Int', default => 1 }, | ||||||||||||
| 76 | ); | ||||||||||||
| 77 | 0 | my $args = $rule->validate(@_); | |||||||||||
| 78 | |||||||||||||
| 79 | my $itr = c->db->search_by_sql( | ||||||||||||
| 80 | q{SELECT branch.project, branch.branch, report.report_id, report.revision, report.status, report.ctime, report.elapsed_time_sec | ||||||||||||
| 81 | FROM report INNER JOIN branch ON (branch.branch_id=report.branch_id) | ||||||||||||
| 82 | WHERE NOT report.status = 1 | ||||||||||||
| 83 | ORDER BY report_id DESC | ||||||||||||
| 84 | 0 | LIMIT ? OFFSET ?}, [$args->{limit} + 1, $args->{limit}*($args->{page}-1)], | |||||||||||
| 85 | ); | ||||||||||||
| 86 | 0 | $itr->suppress_object_creation(1); | |||||||||||
| 87 | 0 | my $reports = [$itr->all]; | |||||||||||
| 88 | 0 | my $has_next = do { | |||||||||||
| 89 | 0 | ▶ | if (@$reports == $args->{limit}+1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | pop @$reports; | |||||||||||
| 91 | 0 | 1; | |||||||||||
| 92 | } else { | ||||||||||||
| 93 | 0 | 0; | |||||||||||
| 94 | } | ||||||||||||
| 95 | }; | ||||||||||||
| 96 | my $pager = Data::Page::NoTotalEntries->new( | ||||||||||||
| 97 | has_next => $has_next, | ||||||||||||
| 98 | entries_per_page => $args->{limit}, | ||||||||||||
| 99 | current_page => $args->{page}, | ||||||||||||
| 100 | 0 | entries_on_this_page => scalar @$reports, | |||||||||||
| 101 | ); | ||||||||||||
| 102 | 0 | ▶ | return wantarray ? ($reports, $pager) : $reports; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | } | ||||||||||||
| 104 | |||||||||||||
| 105 | sub list { | ||||||||||||
| 106 | 0 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
list: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 107 | 0 | state $rule = Data::Validator->new( | |||||||||||
| 108 | branch_id => { isa => 'Int' }, | ||||||||||||
| 109 | limit => { isa => 'Int', default => 50 }, | ||||||||||||
| 110 | page => { isa => 'Int', default => 1 }, | ||||||||||||
| 111 | ); | ||||||||||||
| 112 | 0 | my $args = $rule->validate(@_); | |||||||||||
| 113 | |||||||||||||
| 114 | my $itr = c->db->search(report => { | ||||||||||||
| 115 | branch_id => $args->{branch_id}, | ||||||||||||
| 116 | }, { | ||||||||||||
| 117 | limit => $args->{limit} + 1, | ||||||||||||
| 118 | 0 | offset => $args->{limit}*($args->{page}-1), | |||||||||||
| 119 | order_by => 'report_id DESC', | ||||||||||||
| 120 | columns => [qw/report_id revision status ctime elapsed_time_sec/], | ||||||||||||
| 121 | }); | ||||||||||||
| 122 | 0 | $itr->suppress_object_creation(1); | |||||||||||
| 123 | |||||||||||||
| 124 | 0 | my $reports = [$itr->all]; | |||||||||||
| 125 | 0 | my $has_next = do { | |||||||||||
| 126 | 0 | ▶ | if (@$reports == $args->{limit}+1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | 0 | pop @$reports; | |||||||||||
| 128 | 0 | 1; | |||||||||||
| 129 | } else { | ||||||||||||
| 130 | 0 | 0; | |||||||||||
| 131 | } | ||||||||||||
| 132 | }; | ||||||||||||
| 133 | |||||||||||||
| 134 | my $pager = Data::Page::NoTotalEntries->new( | ||||||||||||
| 135 | has_next => $has_next, | ||||||||||||
| 136 | entries_per_page => $args->{limit}, | ||||||||||||
| 137 | current_page => $args->{page}, | ||||||||||||
| 138 | 0 | entries_on_this_page => scalar @$reports, | |||||||||||
| 139 | ); | ||||||||||||
| 140 | 0 | ▶ | return wantarray ? ($reports, $pager) : $reports; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | sub search { | ||||||||||||
| 144 | 0 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
search: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 145 | 0 | state $rule = Data::Validator->new( | |||||||||||
| 146 | project => { isa => 'Str' }, | ||||||||||||
| 147 | branch => { isa => 'Str' }, | ||||||||||||
| 148 | revision => { isa => 'Str' }, | ||||||||||||
| 149 | limit => { isa => 'Int', default => 20 }, | ||||||||||||
| 150 | ); | ||||||||||||
| 151 | 0 | my $args = $rule->validate(@_); | |||||||||||
| 152 | 0 | my %where = map { $_ => $args->{$_} } qw(project branch revision); | |||||||||||
| 153 | |||||||||||||
| 154 | my $itr = c->db->search_by_sql( | ||||||||||||
| 155 | q{SELECT status, report_id, report.ctime FROM report INNER JOIN branch ON (branch.branch_id=report.branch_id) WHERE | ||||||||||||
| 156 | project = ? AND | ||||||||||||
| 157 | branch = ? AND | ||||||||||||
| 158 | revision = ? | ||||||||||||
| 159 | ORDER BY report_id DESC LIMIT ? | ||||||||||||
| 160 | 0 | }, [$args->{project}, $args->{branch}, $args->{revision}, $args->{limit}]); | |||||||||||
| 161 | 0 | $itr->suppress_object_creation(1); | |||||||||||
| 162 | |||||||||||||
| 163 | 0 | [$itr->all]; | |||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | sub insert { | ||||||||||||
| 167 | 0 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
insert: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 168 | 0 | state $rule = Data::Validator->new( | |||||||||||
| 169 | project => { isa => 'Str' }, | ||||||||||||
| 170 | branch => { isa => 'Str' }, | ||||||||||||
| 171 | status => { isa => 'Int' }, | ||||||||||||
| 172 | repo => { isa => 'Str', optional => 1 }, | ||||||||||||
| 173 | revision => { isa => 'Str', optional => 1 }, | ||||||||||||
| 174 | body => { isa => 'Str', optional => 1 }, | ||||||||||||
| 175 | vc_log => { isa => 'Str', optional => 1 }, | ||||||||||||
| 176 | compare_url => { isa => 'Str', optional => 1 }, | ||||||||||||
| 177 | elapsed_time_sec => { isa => 'Int', optional => 1 }, | ||||||||||||
| 178 | ); | ||||||||||||
| 179 | 0 | my $args = $rule->validate(@_); | |||||||||||
| 180 | |||||||||||||
| 181 | 0 | my $txn = c->db->txn_scope; | |||||||||||
| 182 | |||||||||||||
| 183 | my $branch_id = Ukigumo::Server::Command::Branch->find_or_create( | ||||||||||||
| 184 | project => $args->{project}, | ||||||||||||
| 185 | branch => $args->{branch}, | ||||||||||||
| 186 | 0 | ); | |||||||||||
| 187 | 0 | my $report_id = do { | |||||||||||
| 188 | 0 | my %params = %$args; | |||||||||||
| 189 | 0 | delete $params{project}; | |||||||||||
| 190 | 0 | delete $params{branch}; | |||||||||||
| 191 | |||||||||||||
| 192 | 0 | c->db->fast_insert(report => $class->_compress_text_data({ | |||||||||||
| 193 | %params, | ||||||||||||
| 194 | ctime => time(), | ||||||||||||
| 195 | branch_id => $branch_id, | ||||||||||||
| 196 | })); | ||||||||||||
| 197 | }; | ||||||||||||
| 198 | |||||||||||||
| 199 | 0 | c->db->update(branch => { | |||||||||||
| 200 | last_report_id => $report_id, | ||||||||||||
| 201 | }, { | ||||||||||||
| 202 | branch_id => $branch_id, | ||||||||||||
| 203 | }); | ||||||||||||
| 204 | |||||||||||||
| 205 | 0 | ▶ | if (defined c->config->{max_num_of_reports_by_branch}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 206 | my $last = [ c->db->search_named(q{ SELECT report_id FROM report WHERE branch_id = :branch_id ORDER BY report_id DESC LIMIT :limit }, { | ||||||||||||
| 207 | limit => c->config->{max_num_of_reports_by_branch}, | ||||||||||||
| 208 | 0 | branch_id => $branch_id, | |||||||||||
| 209 | }) ]->[-1]; | ||||||||||||
| 210 | |||||||||||||
| 211 | 0 | c->db->delete('report', { | |||||||||||
| 212 | report_id => { '<' => $last->report_id }, | ||||||||||||
| 213 | branch_id => $branch_id, | ||||||||||||
| 214 | }); | ||||||||||||
| 215 | } | ||||||||||||
| 216 | |||||||||||||
| 217 | 0 | ▶ | if (defined c->config->{max_num_of_reports}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 218 | my $last = [ c->db->search_named(q{ SELECT report_id FROM report ORDER BY report_id DESC LIMIT :limit }, { | ||||||||||||
| 219 | limit => c->config->{max_num_of_reports}, | ||||||||||||
| 220 | 0 | }) ]->[-1]; | |||||||||||
| 221 | |||||||||||||
| 222 | 0 | my $should_delete_rows_itr = c->db->search('report', { | |||||||||||
| 223 | report_id => { '<' => $last->report_id }, | ||||||||||||
| 224 | }); | ||||||||||||
| 225 | |||||||||||||
| 226 | 0 | my @branch_ids; | |||||||||||
| 227 | 0 | ▶ | while (my $delete_row = $should_delete_rows_itr->next) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 228 | 0 | push @branch_ids, $delete_row->branch_id; | |||||||||||
| 229 | 0 | $delete_row->delete; | |||||||||||
| 230 | } | ||||||||||||
| 231 | |||||||||||||
| 232 | 0 | for my $branch_id (uniq @branch_ids) { | |||||||||||
| 233 | 0 | my $branch = c->db->single('branch', {branch_id => $branch_id}); | |||||||||||
| 234 | 0 | ▶ | unless ($class->find(report_id => $branch->last_report_id)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 235 | 0 | $branch->delete; | |||||||||||
| 236 | } | ||||||||||||
| 237 | } | ||||||||||||
| 238 | } | ||||||||||||
| 239 | |||||||||||||
| 240 | 0 | $txn->commit; | |||||||||||
| 241 | |||||||||||||
| 242 | 0 | return $report_id; | |||||||||||
| 243 | } | ||||||||||||
| 244 | |||||||||||||
| 245 | sub get_url { | ||||||||||||
| 246 | 0 | ▶ | my ($class, $id) = @_; | ||||||||||
|
Subroutine
get_url: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 247 | 0 | my $c = c(); | |||||||||||
| 248 | 0 | my $uri = URI::WithBase->new($c->uri_for("/report/$id"), $c->req->uri); | |||||||||||
| 249 | 0 | $uri->abs->as_string; | |||||||||||
| 250 | } | ||||||||||||
| 251 | |||||||||||||
| 252 | sub find { | ||||||||||||
| 253 | 0 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
find: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 254 | 0 | state $rule = Data::Validator->new( | |||||||||||
| 255 | report_id => { isa => 'Int' }, | ||||||||||||
| 256 | ); | ||||||||||||
| 257 | 0 | my $args = $rule->validate(@_); | |||||||||||
| 258 | |||||||||||||
| 259 | 0 | local c->db->{suppress_row_objects} = 1; | |||||||||||
| 260 | my $report = $class->_uncompress_text_data(c->db->single_by_sql( | ||||||||||||
| 261 | q{SELECT branch.project, branch.branch, report.* FROM report INNER JOIN branch ON (report.branch_id=branch.branch_id) WHERE report_id=?}, | ||||||||||||
| 262 | 0 | [$args->{report_id}] | |||||||||||
| 263 | )); | ||||||||||||
| 264 | |||||||||||||
| 265 | 0 | return $report; | |||||||||||
| 266 | } | ||||||||||||
| 267 | |||||||||||||
| 268 | sub _compress_text_data { | ||||||||||||
| 269 | 0 | ▶ | my ($self, $row) = @_; | ||||||||||
|
Subroutine
_compress_text_data: not called CC=4 SCAR=30.0
| |||||||||||||
| 270 | 0 | ▶ | c->config->{enable_compression} or return $row; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 271 | |||||||||||||
| 272 | 0 | for my $type (qw/vc_log body/) { | |||||||||||
| 273 | 0 | ▶ | if (defined(my $content_text = $row->{$type})) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 274 | 0 | $row->{$type} = __compress($content_text); | |||||||||||
| 275 | } | ||||||||||||
| 276 | } | ||||||||||||
| 277 | |||||||||||||
| 278 | 0 | $row; | |||||||||||
| 279 | } | ||||||||||||
| 280 | |||||||||||||
| 281 | sub _uncompress_text_data { | ||||||||||||
| 282 | 0 | ▶ | my ($self, $row) = @_; | ||||||||||
|
Subroutine
_uncompress_text_data: not called CC=4 SCAR=30.0
| |||||||||||||
| 283 | |||||||||||||
| 284 | 0 | ▶ | return unless $row; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 285 | 0 | ▶ | c->config->{enable_compression} or return $row; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 286 | |||||||||||||
| 287 | 0 | for my $type (qw/vc_log body/) { | |||||||||||
| 288 | # For new Encode.pm | ||||||||||||
| 289 | 0 | eval { $row->{$type} = decode_utf8(__uncompress($row->{$type})) }; | |||||||||||
| 290 | } | ||||||||||||
| 291 | |||||||||||||
| 292 | 0 | $row; | |||||||||||
| 293 | } | ||||||||||||
| 294 | |||||||||||||
| 295 | sub __compress { | ||||||||||||
| 296 | 0 | ▶ | my $bytes = Compress::Zlib::memGzip(\ encode_utf8($_[0])) ; | ||||||||||
|
Subroutine
__compress: not called CC=2 SCAR=17.9
| |||||||||||||
| 297 | 0 | ▶ | if (length($bytes) < length($_[0])) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 298 | 0 | return $bytes; | |||||||||||
| 299 | } | ||||||||||||
| 300 | 0 | $_[0]; | |||||||||||
| 301 | } | ||||||||||||
| 302 | |||||||||||||
| 303 | |||||||||||||
| 304 | sub __uncompress { | ||||||||||||
| 305 | # Only uncompress with gzip header | ||||||||||||
| 306 | 0 | ▶ | my $item = shift; | ||||||||||
|
Subroutine
__uncompress: not called CC=3 SCAR=24.8
| |||||||||||||
| 307 | |||||||||||||
| 308 | 0 | ▶ | return unless $item; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 309 | |||||||||||||
| 310 | 0 | ▶ | if ( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 311 | substr($item, 0, length(IO::Compress::Gzip::Constants::GZIP_MINIMUM_HEADER)) eq | ||||||||||||
| 312 | IO::Compress::Gzip::Constants::GZIP_MINIMUM_HEADER | ||||||||||||
| 313 | ) { | ||||||||||||
| 314 | 0 | return Compress::Zlib::memGunzip(\$item) ; | |||||||||||
| 315 | } | ||||||||||||
| 316 | 0 | $item; | |||||||||||
| 317 | } | ||||||||||||
| 318 | |||||||||||||
| 319 | 1 | 1; | |||||||||||
| 320 | |||||||||||||