RedisTop/Component.pm

filter: Statementstmt 71.1% 74 / 104 Branchbran 60.5% 23 / 38 Conditioncond 28.5% 4 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 78.5% 11 / 14 Podpod 0.0% 0 / 13 totaltotal 58.6% 112 / 191 CC 32 SCAR 27.2
CC
32
Cov
65%
average
56.0
total
20.0
stat_values
13.0
CRAP 15.2
« RedisTop/PerformStat.pm ↑ Summary RedisTop/Perform.pm »
1 package App::RedisTop::Component;
2 2 ▶use Term::ANSIColor qw/colored/;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
5 0 my $class = shift;
6  
7 0 my %args = @_;
8 my $self = bless {
9 redis_config => $args{redis_config},
10 0 }, $class;
11 }
12  
13 15 ▶sub redis_config {
Subroutine redis_config: called CC=2 SCAR=6.9
Pod undocumented
14 15 my ($self, $value) = @_;
15 15 ▶ if($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 9 6 15
16 9 $self->{redis_config} = $value;
17 }
18 15 return $self->{redis_config};
19 }
20  
21 8 ▶sub stat_values {
Subroutine stat_values: called CC=11 SCAR=25.7
Pod undocumented
22 8 my ($self, $stat, $prev_stat) = @_;
23 8 my @results;
24 8 for my $row (@{$self->{items}}) {
25 14 ▶ my $stat_key = ref $row->{stat_key} eq 'CODE' ? $row->{stat_key}->($stat->{redis_version}) : $row->{stat_key};
Branch: 1 branch
BranchTrueFalseTotal
ref $row->{'stat_key'} eq 'CODE' ? : 1 13 14
26 14 my $value;
27 14 ▶ if ($row->{diff}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($row->{'diff'}) { } 6 8 14
elsif ($row->{'denominator_key'}) { } 2 6 8
elsif ($row->{'data'}) { } 0 6 6
28 6 ▶ my $prev_uptime = $prev_stat->{uptime_in_seconds} || 0;
Condition50%1/2
$prev_stat->{'uptime_in_seconds'} || 0
l!l
60
MC/DC0%0/1
$prev_stat->{'uptime_in_seconds'} || 0
$prev_stat->{'uptime_in_seconds'}
Truth table50%1/2
A
A= $prev_stat->{'uptime_in_seconds'}
Aresult
11
00
29 6 ▶ my $prev_value = $prev_stat->{$stat_key} || 0;
Condition50%1/2
$prev_stat->{$stat_key} || 0
l!l
60
MC/DC0%0/1
$prev_stat->{$stat_key} || 0
$prev_stat->{$stat_key}
Truth table50%1/2
A
A= $prev_stat->{$stat_key}
Aresult
11
00
30 6 eval {
31 $value
32 = ( $stat->{$stat_key} - $prev_value )
33 6 / ( $stat->{uptime_in_seconds} - $prev_uptime);
34 };
35 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
36 0 $value = $stat->{$stat_key};
37 }
38 } elsif($row->{denominator_key}) {
39 2 ▶ if($self->redis_config && $self->redis_config->{$row->{denominator_key}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->redis_config and $self->redis_config->{$row->{'denominator_key'}}) { } 2 0 2
Condition33%1/3
$self->redis_config and $self->redis_config->{$row->{"denominator_key"}}
!ll&&!rl&&r
002
MC/DC0%0/2
$self->redis_config and $self->redis_config->{$row->{"denominator_key"}}
$self->redis_config $self->redis_config->{$row->{"denominator_key"}}
Truth table33%1/3
A and B
A= $self->redis_configB= $self->redis_config->{$row->{"denominator_key"}}
ABresult
0X0
100
111
40 2 $value = sprintf("%.2f", ($stat->{$stat_key} / $self->redis_config->{$row->{denominator_key}}) * 100);
41 } else {
42 0 $value = "-1";
43 }
44 } elsif($row->{data}) {
45 0 $value = $row->{data}->();
46 } else {
47 6 $value = $stat->{$stat_key};
48 }
49 14 push @results, $value;
50 }
51 8 return @results;
52 }
53  
54 12 ▶sub unit {
Subroutine unit: called CC=5 SCAR=16.1
Pod undocumented
55 12 my ($self, $value) = @_;
56 12 ▶ $value ||= 0;
Condition50%1/2
$value ||= 0
l!l
120
MC/DC0%0/1
$value ||= 0
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
57  
58 12 my $round = '';
59 12 my @units = qw/K M G T P/;
60 12 unshift @units, ' ';
61 12 for my $unit (@units) {
62 15 ▶ if($value / 1000 < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value / 1000 < 1) 12 3 15
63 12 ▶ $round = 2 if index($value, '.') > 0; # float
Branch: 1 branch
BranchTrueFalseTotal
if index($value, ".") > 0 3 9 12
64 12 return sprintf("%.${round}f%s", $value, $unit);
65 }
66 3 $value = $value / 1000;
67 3 $round = 2;
68 }
69 }
70  
71 18 ▶sub separator { colored("|", "blue") }
Subroutine separator: called CC=1 SCAR=0.0
Pod undocumented
72  
73 9 ▶sub header {
Subroutine header: called CC=3 SCAR=11.1
Pod undocumented
74 9 my ($self) = @_;
75 9 my $title = $self->{group};
76 9 my $diff = $self->component_width - length($title);
77 9 my $left = $diff / 2;
78 9 my $right = $diff - $left;
79  
80 9 my $line = '-' x $left . $title . '-' x $right;
81 9 my $line_len = length($line);
82 9 ▶ if($line_len > $self->component_width-1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line_len > $self->component_width - 1) { } 3 6 9
elsif ($line_len < $self->component_width - 1) { } 0 6 6
83 3 $line = substr($line, 0, length($line)-1);
84 } elsif($line_len < $self->component_width-1) {
85 0 $line .= '-';
86 }
87 9 return colored("${line} ", "blue");
88 }
89  
90 9 ▶sub sub_header {
Subroutine sub_header: called CC=1 SCAR=0.0
Pod undocumented
91 9 my ($self) = @_;
92 9 my $format = "%" . $self->{width} . "s";
93 9 my $line = join(' ', map { sprintf($format, $_->{name}) } @{$self->{items}});
94 9 return colored("${line}", "cyan") . $self->separator;
95 }
96  
97 48 ▶sub line {
Subroutine line: called CC=1 SCAR=0.0
Pod undocumented
98 48 my ($self, $spt) = @_;
99 48 my $item_len = scalar @{$self->{items}};
100 48 my $line = join(' ', map { $spt x $self->{width} } 1..$item_len);
101 48 return colored("${line} ", "blue");
102 }
103  
104 24 ▶sub component_width {
Subroutine component_width: called CC=1 SCAR=0.0
Pod undocumented
105 24 my ($self) = @_;
106 24 my $line = $self->line('-');
107 24 return length($self->colorstrip($self->line('-')));
108 }
109  
110 24 ▶sub colorstrip {
Subroutine colorstrip: called CC=3 SCAR=11.1
Pod undocumented
111 24 my ($self, @string) = @_;
112 24 for my $string (@string) {
113 24 $string =~ s{ \e\[ [\d;]* m }{}xmsg;
114 }
115 24 ▶ return wantarray ? @string : join q{}, @string;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 24 24
116 }
117  
118 9 ▶sub body {
Subroutine body: called CC=4 SCAR=13.9
Pod undocumented
119 9 my ($self, $stat, $prev_stat) = @_;
120 9 my @cols;
121 9 for my $value ($self->stat_values($stat, $prev_stat)) {
122 16 ▶ my $stat = ($self->{unit}) ? $self->unit($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'unit'} ? : 12 4 16
123 16 ▶ my $format = ($self->{round}) ? "%" . $self->{width} . ".1f" : "%" . $self->{width} . "s" ;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'round'} ? : 2 14 16
124 16 push @cols, sprintf($format, $stat);
125 }
126 9 my $line = join(' ', @cols);
127 9 return $line . $self->separator;
128 }
129  
130 0 ▶sub average {
Subroutine average: not called CC=7 SCAR=40.3
Pod undocumented
131 0 my ($self, $values, $instance_count) = @_;
132 0 ▶ $values ||= [ map { '' } @{$self->{items}} ];
Condition0%0/2
$values ||= [map({"";} @{$$self{"items"};})]
l!l
00
MC/DC0%0/1
$values ||= [map({"";} @{$$self{"items"};})]
$values
Truth table0%0/2
A
A= $values
Aresult
11
00
133  
134 0 my @cols;
135 0 for my $value (@$values) {
136 0 my $avg_value = 0;
137 0 ▶ $avg_value = $value / $instance_count if $value && $instance_count;
Branch: 1 branch
BranchTrueFalseTotal
if $value and $instance_count 0 0 0
Condition0%0/3
$value and $instance_count
!ll&&!rl&&r
000
MC/DC0%0/2
$value and $instance_count
$value $instance_count
Truth table0%0/3
A and B
A= $valueB= $instance_count
ABresult
0X0
100
111
138  
139 0 ▶ my $stat = ($self->{unit}) ? $self->unit($avg_value) : $avg_value;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'unit'} ? : 0 0 0
140 0 ▶ my $format = ($self->{round}) ? "%" . $self->{width} . ".1f" : "%" . $self->{width} . "s" ;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'round'} ? : 0 0 0
141 0 push @cols, sprintf($format, $stat);
142 }
143 0 my $line = join(' ', @cols);
144 0 return $line . $self->separator;
145 }
146  
147 0 ▶sub total {
Subroutine total: not called CC=4 SCAR=30.0
Pod undocumented
148 0 my ($self, $values) = @_;
149  
150 0 my @cols;
151 0 for my $value (@$values) {
152 0 my $stat = '';
153 0 ▶ if($self->{total}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"total"}) 0 0 0
154 0 ▶ $stat = ($self->{unit}) ? $self->unit($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'unit'} ? : 0 0 0
155 }
156 0 my $format = "%" . $self->{width} . "s";
157 0 push @cols, sprintf($format, $stat);
158 }
159 0 my $line = join(' ', @cols);
160 0 return $line . $self->separator;
161 }
162  
163 2 1;
« RedisTop/PerformStat.pm ↑ Summary RedisTop/Perform.pm »