blib/lib/Ukigumo/Server/Command/Report.pm

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