TT/Render.pm

filter: Statementstmt 61.0% 61 / 100 Branchbran 50.0% 17 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 47.0% 8 / 17 Podpod 100% 4 / 4 totaltotal 56.8% 91 / 160 CC 30 SCAR 36.2
CC
30
Cov
58%
uniq
132.0
uniq_list
15.5
join_hash_list
8.0
CRAP 37.3
« TT/Object.pm ↑ Summary TT/Input/Nagios.pm »
1 package Monitoring::TT::Render;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Monitoring::TT::Log qw/error warn info debug trace log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #####################################################################
10  
11 =head1 NAME
12  
13 Monitoring::TT::Render - Render Helper Functions
14  
15 =head1 DESCRIPTION
16  
17 All functions from this render helper can be used in templates
18  
19 =cut
20  
21 #####################################################################
22  
23 =head1 METHODS
24  
25 =head2 die
26  
27 die(error message)
28  
29 die with an hopefully useful error message
30  
31 =cut
32 sub die {
33 0 ▶ my( $msg ) = @_;
Subroutine die: not called CC=1 SCAR=6.9
Pod documented
34 0 return croak($msg);
35 }
36  
37 #####################################################################
38  
39 =head2 _warn
40  
41 _warn(message)
42  
43 =cut
44 sub _warn {
45 0 ▶ return warn(@_);
Subroutine _warn: not called CC=1 SCAR=6.9
46 }
47  
48 #####################################################################
49  
50 =head2 _die
51  
52 _die(message)
53  
54 =cut
55 sub _die {
56 0 ▶ return __PACKAGE__::die(@_);
Subroutine _die: not called CC=1 SCAR=6.9
57 }
58  
59 #####################################################################
60  
61 =head2 _info
62  
63 _info(message)
64  
65 =cut
66 sub _info {
67 0 ▶ return info(@_);
Subroutine _info: not called CC=1 SCAR=6.9
68 }
69  
70 #####################################################################
71  
72 =head2 _error
73  
74 _error(message)
75  
76 =cut
77 sub _error {
78 0 ▶ return error(@_);
Subroutine _error: not called CC=1 SCAR=6.9
79 }
80  
81 #####################################################################
82  
83 =head2 _debug
84  
85 _debug(message)
86  
87 =cut
88 sub _debug {
89 0 ▶ return debug(@_);
Subroutine _debug: not called CC=1 SCAR=6.9
90 }
91  
92 #####################################################################
93  
94 =head2 _trace
95  
96 _trace(message)
97  
98 =cut
99 sub _trace {
100 0 ▶ return trace(@_);
Subroutine _trace: not called CC=1 SCAR=6.9
101 }
102  
103 #####################################################################
104  
105 =head2 _log
106  
107 _log(lvl, message)
108  
109 =cut
110 sub _log {
111 0 ▶ return log(@_);
Subroutine _log: not called CC=1 SCAR=6.9
112 }
113  
114 #####################################################################
115  
116 =head2 uniq
117  
118 uniq(objects, attr)
119 uniq(objects, attr, name)
120  
121 returns list of uniq values for one attr of a list of objects
122  
123 ex.:
124  
125 get uniq list of group items
126 uniq(hosts, 'group')
127  
128 get uniq list of test tags
129 uniq(hosts, 'tag', 'test')
130  
131 =cut
132 sub uniq {
133 0 ▶ my( $objects, $attrlist , $name ) = @_;
Subroutine uniq: not called CC=11 SCAR=48.8
Pod documented
134 0 ▶ croak('expected list of objects') unless ref $objects eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $objects eq "ARRAY" 0 0 0
135 0 my $uniq = {};
136 0 for my $o (@{$objects}) {
137 0 for my $attr (@{_list($attrlist)}) {
138 0 ▶ if($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
139 0 ▶ next unless defined $o->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{$attr} 0 0 0
140 0 ▶ next unless defined $o->{$attr}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{$attr}{$name} 0 0 0
141 0 for my $v (split(/\s*\|\s*|\s*,\s*/mx, $o->{$attr}->{$name})) {
142 0 $uniq->{$v} = 1;
143 }
144 } else {
145 0 ▶ next unless defined $o->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{$attr} 0 0 0
146 0 my $tmp = $o->{$attr};
147 0 ▶ if(ref $tmp ne 'ARRAY') { my @tmp = split(/\s*,\s*/mx,$tmp); $tmp = \@tmp; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tmp ne "ARRAY") 0 0 0
148 0 for my $a (@{$tmp}) {
149 0 $uniq->{$a} = 1;
150 }
151 }
152 }
153 }
154 0 my @list = keys %{$uniq};
155 0 return \@list;
156 }
157  
158 #####################################################################
159  
160 =head2 uniq_list
161  
162 uniq_list(list1, list2, ...)
163  
164 returns list of uniq values in all lists
165  
166 =cut
167 sub uniq_list {
168 8 ▶ return join_hash_list(@_) if defined $_[0] and ref $_[0] eq 'HASH';
Subroutine uniq_list: called CC=11 SCAR=27.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and ref $_[0] eq "HASH" 0 8 8
Condition33%1/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
080
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
169 8 my $uniq = {};
170 8 for my $list (@_) {
171 8 ▶ if(ref $list eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $list eq 'HASH') { } 0 8 8
elsif (ref $list eq 'ARRAY') { } 2 6 8
elsif (ref $list eq '') { } 6 0 6
172 0 for my $i (keys %{$list}) {
173 0 $i =~ s/^\s+//mx;
174 0 $i =~ s/\s+$//mx;
175 0 $uniq->{$i} = 1;
176 }
177 }
178 elsif(ref $list eq 'ARRAY') {
179 2 for my $i (@{$list}) {
180 6 my $tmp = uniq_list($i);
181 6 ▶ if(ref $tmp eq '') { $tmp = [split(/\s*,\s*/mx, $tmp)]; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tmp eq "") 0 6 6
182 6 for my $k (@{$tmp}) {
183 6 $k =~ s/^\s+//mx;
184 6 $k =~ s/\s+$//mx;
185 6 $uniq->{$k} = 1;
186 }
187 }
188 }
189 elsif(ref $list eq '') {
190 6 $list =~ s/^\s+//mx;
191 6 $list =~ s/\s+$//mx;
192 6 $uniq->{$list} = 1;
193 }
194 else {
195 0 croak('unexpected objects type in uniq_list()'.(ref $list));
196 }
197 }
198 8 my @items = sort keys %{$uniq};
199 8 return \@items;
200 }
201  
202 #####################################################################
203  
204 =head2 join_hash_list
205  
206 join_hash_list($hashlist, $exceptions)
207  
208 returns list csv list for hash but leave out exceptions
209  
210 =cut
211 sub join_hash_list {
212 2 ▶ my($hash, $exceptions) = @_;
Subroutine join_hash_list: called CC=8 SCAR=20.8
Pod documented
213 2 ▶ return "" unless defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 0 2 2
214 2 my $list = [];
215 2 for my $key (sort keys %{$hash}) {
216 5 my $skip = 0;
217 5 for my $ex (@{_list($exceptions)}) {
218 3 ▶ if($key =~ m/$ex/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /$ex/mx) 1 2 3
219 1 $skip = 1;
220 1 last;
221 }
222 }
223 5 ▶ next if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 1 4 5
224 4 for my $val (@{_list($hash->{$key})}) {
225 6 ▶ if($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 4 2 6
226 4 push @{$list}, $key.'='.$val;
227 } else {
228 2 push @{$list}, $key;
229 }
230 }
231 }
232 2 $list = uniq_list($list);
233 2 return join(', ', sort @{$list});
234 }
235  
236 #####################################################################
237 sub _list {
238 9 ▶ my($data) = @_;
Subroutine _list: called CC=3 SCAR=11.0
239 9 ▶ return([]) unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 2 7 9
240 7 ▶ return($data) if ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "ARRAY" 5 2 7
241 2 return([$data]);
242 }
243 #####################################################################
244  
245 =head1 AUTHOR
246  
247 Sven Nierlein, 2013, <sven.nierlein@consol.de>
248  
249 =cut
250  
251 5 1;
« TT/Object.pm ↑ Summary TT/Input/Nagios.pm »