lib/Monitoring/Reporter/Web.pm

filter: Statementstmt 28.3% 47 / 166 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 36.1% 13 / 36 Podpod 100% 2 / 2 totaltotal 22.9% 62 / 270 CC 28 SCAR 31.5
CC
28
Cov
21%
_handle_request
90.0
__ANON__
42.0
_init_tt
30.0
CRAP 23.4
« lib/Monitoring/Reporter.pm ↑ Summary lib/Monitoring/Reporter/Web/Plugin/Demo.pm »
1 package Monitoring::Reporter::Web;
2 {
3 1 $Monitoring::Reporter::Web::VERSION = '0.01';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Monitoring::Reporter::Web::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: a plack based webinterface to Monitoring::Reporter
9  
10 1 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20 # use Carp;
21 # use English qw( -no_match_vars );
22 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Module::Pluggable;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use Monitoring::Reporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # extends ...
32 # has ...
33 1 has '_key' => (
34 'is' => 'ro',
35 'isa' => 'Str',
36 'lazy' => 1,
37 'builder' => '_init_key',
38 );
39  
40 1 has '_fields' => (
41 'is' => 'rw',
42 'isa' => 'ArrayRef',
43 'lazy' => 1,
44 'builder' => '_init_fields',
45 );
46  
47 1 has '_finder' => (
48 'is' => 'rw',
49 'isa' => 'Module::Pluggable::Object',
50 'lazy' => 1,
51 'builder' => '_init_finder',
52 'accessor' => 'finder',
53 );
54  
55 1 has '_plugins' => (
56 'is' => 'rw',
57 'isa' => 'HashRef[Monitoring::Reporter::Web::Plugin]',
58 'lazy' => 1,
59 'builder' => '_init_plugins',
60 'accessor' => 'plugins',
61 );
62  
63 1 has '_tt' => (
64 'is' => 'ro',
65 'isa' => 'Template',
66 'lazy' => 1,
67 'builder' => '_init_tt',
68 );
69  
70 1 has 'mr' => (
71 'is' => 'rw',
72 'isa' => 'Monitoring::Reporter',
73 'lazy' => 1,
74 'builder' => '_init_zr',
75 );
76  
77 # with ...
78 1 with qw(Config::Yak::LazyConfig Log::Tree::Logger);
79  
80 0 ▶sub _log_facility { return 'mreporter-web'; }
Subroutine _log_facility: not called CC=1 SCAR=6.9
81 0 ▶sub _config_locations { return [qw(conf /etc/mreporter)]; }
Subroutine _config_locations: not called CC=1 SCAR=6.9
82  
83 # initializers ...
84 0 ▶sub _init_fields {
Subroutine _init_fields: not called CC=1 SCAR=6.9
85 0 return [qw(mode key)];
86 }
87  
88 0 ▶sub _init_key {
Subroutine _init_key: not called CC=1 SCAR=6.9
89 0 my $self = shift;
90  
91 0 return $self->config()->get('Monitoring::Reporter::Key', { Default => '', }, );
92 }
93  
94 0 ▶sub _init_finder {
Subroutine _init_finder: not called CC=1 SCAR=6.9
95 0 my $self = shift;
96  
97 # The finder is the class that finds our available plugins
98 0 my $Finder = Module::Pluggable::Object::->new( 'search_path' => 'Monitoring::Reporter::Web::Plugin' );
99  
100 0 return $Finder;
101 } ## end sub _init_finder
102  
103 0 ▶sub _init_zr {
Subroutine _init_zr: not called CC=1 SCAR=6.9
104 0 my $self = shift;
105  
106 0 my $ZR = Monitoring::Reporter::->new({
107 'config' => $self->config(),
108 'logger' => $self->logger(),
109 'warn_unattended' => $self->config()->get('Monitoring::Reporter::WarnUnattended', { Default => 0, } ),
110 'warn_unsupported' => $self->config()->get('Monitoring::Reporter::WarnUnsupported', { Default => 0, } ),
111 });
112  
113 0 return $ZR;
114 }
115  
116 0 ▶sub _init_plugins {
Subroutine _init_plugins: not called CC=4 SCAR=30.0
117 0 my $self = shift;
118  
119 0 my $plugin_ref = {};
120 0 PLUGIN: foreach my $class_name ( $self->finder()->plugins() ) {
121 ## no critic (ProhibitStringyEval)
122 0 my $eval_status = eval "require $class_name;";
123 ## use critic
124 0 ▶ if ( !$eval_status ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_status) 0 0 0
125 0 $self->logger()->log( message => 'Failed to require ' . $class_name . ': ' . $@, level => 'warning', );
126 0 next;
127 }
128 0 my $arg_ref = $self->config()->get($class_name);
129 0 $arg_ref->{'logger'} = $self->logger();
130 0 $arg_ref->{'config'} = $self->config();
131 0 $arg_ref->{'tt'} = $self->_tt();
132 0 $arg_ref->{'mr'} = $self->mr();
133 0 ▶ if ( $arg_ref->{'disabled'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"disabled"}) 0 0 0
134 0 $self->logger()->log( message => 'Skipping disabled plugin: ' . $class_name, level => 'debug', );
135 0 next PLUGIN;
136 }
137 0 ▶ try {
Subroutine __ANON__: not called CC=4 SCAR=30.0
138 0 my $Plugin = $class_name->new($arg_ref);
139  
140 0 my $alias = $Plugin->alias();
141 0 ▶ if ( $alias && !exists( $plugin_ref->{$alias} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias and not exists $plugin_ref->{$alias}) 0 0 0
Condition0%0/3
$alias and not exists $plugin_ref->{$alias}
!ll&&!rl&&r
000
MC/DC0%0/2
$alias and not exists $plugin_ref->{$alias}
$alias not exists $plugin_ref->{$alias}
Truth table0%0/3
A and not(B)
A= $aliasB= not exists $plugin_ref->{$alias}
ABresult
0X0
100
111
142 0 $plugin_ref->{$alias} = $Plugin;
143 0 $self->logger()->log( message => 'Initialized Plugin: ' . $class_name . ' as ' . $alias, level => 'debug', );
144 0 foreach my $field ( @{ $Plugin->fields() } ) {
145 0 push( @{ $self->_fields() }, $field );
146 }
147 } ## end if ( $alias && !exists...)
148 } ## end try
149 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 $self->logger()->log( message => 'Failed to initialize plugin ' . $class_name . ' w/ error: ' . $_, level => 'warning', );
151 0 };
152 } ## end foreach my $class_name ( $self...)
153  
154 0 return $plugin_ref;
155 } ## end sub _init_plugins
156  
157 0 ▶sub _init_tt {
Subroutine _init_tt: not called CC=5 SCAR=34.0
158 0 my $self = shift;
159  
160 0 my $dist_dir;
161 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 0 $dist_dir = File::ShareDir::dist_dir('Monitoring-Reporter');
163 0 };
164 0 my @inc = ( 'share/tpl', '../share/tpl', );
165 0 ▶ if($dist_dir && -d $dist_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist_dir and -d $dist_dir) 0 0 0
Condition0%0/3
$dist_dir and -d $dist_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$dist_dir and -d $dist_dir
$dist_dir -d $dist_dir
Truth table0%0/3
A and B
A= $dist_dirB= -d $dist_dir
ABresult
0X0
100
111
166 0 push(@inc, $dist_dir.'/tpl');
167 }
168 0 my $cfg_dir = $self->config()->get('Monitoring::Reporter::TemplatePath');
169 0 ▶ if($cfg_dir && -d $cfg_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg_dir and -d $cfg_dir) 0 0 0
Condition0%0/3
$cfg_dir and -d $cfg_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$cfg_dir and -d $cfg_dir
$cfg_dir -d $cfg_dir
Truth table0%0/3
A and B
A= $cfg_dirB= -d $cfg_dir
ABresult
0X0
100
111
170 0 unshift(@inc,$cfg_dir);
171 }
172  
173 my $tpl_config = {
174 INCLUDE_PATH => [ @inc ],
175 POST_CHOMP => 1,
176 FILTERS => {
177 'substr' => [
178 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 0 my ( $context, $len ) = @_;
180  
181 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
182 0 my $str = shift;
183 0 ▶ if ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
184 0 $str = substr $str, 0, $len;
185 }
186 0 return $str;
187 }
188 0 },
189 1,
190 ],
191 0 ▶ 'ucfirst' => sub { my $str = shift; return ucfirst($str); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 ▶ 'localtime' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 0 my $str = shift;
194 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($str);
195 0 $year += 1900;
196 0 $mon++;
197 0 return sprintf('%02d.%02d.%04d %02d:%02d:%02d', $mday, $mon, $year, $hour, $min, $sec);
198 },
199 0 ▶ 'sev2btn' => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
200 0 my $str = shift;
201 0 ▶ return 'btn-info' if $str =~ m/^information$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^information$/i 0 0 0
202 0 ▶ return 'btn-warning' if $str =~ m/^warning$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^warning$/i 0 0 0
203 0 ▶ return 'btn-warning' if $str =~ m/^average$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^average$/i 0 0 0
204 0 ▶ return 'btn-danger' if $str =~ m/^high$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^high$/i 0 0 0
205 0 ▶ return 'btn-danger' if $str =~ m/^disaster$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^disaster$/i 0 0 0
206 },
207 },
208 0 };
209 0 my $TT = Template::->new($tpl_config);
210  
211 0 return $TT;
212 }
213  
214 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod documented
215 0 my $self = shift;
216  
217 # init param filter list
218 0 $self->plugins();
219  
220 0 return 1;
221 } ## end sub BUILD
222  
223 # your code here ...
224 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
225 0 my $self = shift;
226 0 my $env = shift;
227  
228 0 my $plack_request = Plack::Request::->new($env);
229 0 my $request = $self->_filter_params($plack_request);
230  
231 # log request and ip
232 0 $self->_log_request($request);
233  
234 0 return $self->_handle_request($request);
235 } ## end sub run
236  
237 0 ▶sub _filter_params {
Subroutine _filter_params: not called CC=3 SCAR=24.8
238 0 my $self = shift;
239 0 my $request = shift;
240  
241 0 my $params = $request->parameters();
242  
243 0 my $request_ref = {};
244 0 foreach my $key ( @{ $self->_fields() } ) {
245 0 ▶ if ( defined( $params->{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{$key}) 0 0 0
246 0 $request_ref->{$key} = $params->{$key};
247 }
248 }
249  
250 # add the remote_addr
251 0 $request_ref->{'remote_addr'} = $request->address();
252  
253 0 return $request_ref;
254 } ## end sub _filter_params
255  
256 0 ▶sub _handle_request {
Subroutine _handle_request: not called CC=9 SCAR=45.0
257 0 my $self = shift;
258 0 my $request = shift;
259  
260 0 ▶ my $mode = $request->{'mode'} || 'list_triggers';
Condition0%0/2
$request->{'mode'} || 'list_triggers'
l!l
00
MC/DC0%0/1
$request->{'mode'} || 'list_triggers'
$request->{'mode'}
Truth table0%0/2
A
A= $request->{'mode'}
Aresult
11
00
261 0 my $key = $request->{'key'};
262  
263 # Check API key
264 0 ▶ if($self->_key() && (!$key || $key ne $self->_key())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_key and !$key || $key ne $self->_key) 0 0 0
Condition0%0/6
$self->_key and !$key || $key ne $self->_key
!ll&&!rl&&r
000
not $key or $key ne $self->_key
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->_key and !$key || $key ne $self->_key
$self->_key not $key $key ne $self->_key
Truth table0%0/4
A and not(B) or C
A= $self->_keyB= not $keyC= $key ne $self->_key
ABCresult
0XX0
1000
11X1
1011
265 0 return [ 400, [ 'Content-Type', 'text/plain' ], ['Bad Request - Invalid key'] ];
266 }
267  
268 # Handle request
269 0 ▶ if ( $mode && $self->plugins()->{$mode} && ref( $self->plugins()->{$mode} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode and $self->plugins->{$mode} and ref $self->plugins->{$mode}) { } 0 0 0
Condition0%0/6
$mode and $self->plugins->{$mode} and ref $self->plugins->{$mode}
!ll&&!rl&&r
000
$mode and $self->plugins->{$mode}
!ll&&!rl&&r
000
MC/DC0%0/3
$mode and $self->plugins->{$mode} and ref $self->plugins->{$mode}
$mode $self->plugins->{$mode} ref $self->plugins->{$mode}
Truth table0%0/4
A and B and C
A= $modeB= $self->plugins->{$mode}C= ref $self->plugins->{$mode}
ABCresult
0XX0
10X0
1100
1111
270 0 ▶ if ( my $resp = $self->plugins()->{$mode}->execute($request) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $resp = $self->plugins->{$mode}->execute($request)) { } 0 0 0
271 0 return $resp;
272 }
273 else {
274 0 return [ 500, [ 'Content-Type', 'text/plain' ], ['Processing Error'] ];
275 }
276 } ## end if ( $mode && $self->plugins...)
277 else {
278 0 return [ 400, [ 'Content-Type', 'text/html' ], [$self->_list_plugins()] ];
279 }
280  
281 0 return 1;
282 } ## end sub _handle_request
283  
284 0 ▶sub _list_plugins {
Subroutine _list_plugins: not called CC=1 SCAR=6.9
285 0 my $self = shift;
286 0 my $request = shift;
287  
288 0 my $body = '<html><body><h1>Bad Request - Command not found</h1><h2>Available Plugins</h2><ul>';
289 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
290 0 foreach my $plugin (sort keys %{ $self->plugins() } ) {
291 0 my $alias = $self->plugins->{$plugin}->alias();
292 0 $body .= '<li><a href="?mode='.$alias.'">'.$plugin.'</a>';
293 }
294 0 };
295 0 $body .= '</ul></body></html>';
296  
297 0 return $body;
298 }
299  
300 0 ▶sub _log_request {
Subroutine _log_request: not called CC=1 SCAR=6.9
301 0 my $self = shift;
302 0 my $request_ref = shift;
303  
304 0 my $remote_addr = $request_ref->{'remote_addr'};
305  
306 # turn key => value pairs into smth. like key1=value1,key2=value2,...
307 0 my $args = join( q{,}, map { $_ . q{=} . $request_ref->{$_} } keys %{$request_ref} );
308  
309 0 $self->logger()->log( message => 'New Request from ' . $remote_addr . '. Args: ' . $args, level => 'debug', );
310  
311 0 return 1;
312 } ## end sub _log_request
313  
314 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
315 1 __PACKAGE__->meta->make_immutable;
316  
317 1 1;
318  
319 __END__
« lib/Monitoring/Reporter.pm ↑ Summary lib/Monitoring/Reporter/Web/Plugin/Demo.pm »