Alert/InputGrp.pm

filter: Statementstmt 35.9% 32 / 89 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 4 totaltotal 31.0% 40 / 129 CC 14 SCAR 35.0
CC
14
Cov
29%
process
90.0
__ANON__
20.0
BUILD
6.0
CRAP 33.1
« Alert/Input/IMAP.pm ↑ Summary Alert/Output.pm »
1 package Transform::Alert::InputGrp;
2  
3 6 our $VERSION = '1.00'; # VERSION
4 # ABSTRACT: Base class for Transform::Alert input groups
5  
6 6 ▶use sanity;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use MooX::Types::MooseLike::Base qw(Str Int Num ScalarRef ArrayRef InstanceOf ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use Transform::Alert::TemplateGrp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Class::Load 'load_class';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use String::Escape qw(elide printable);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 6 has daemon => (
19 is => 'rwp',
20 isa => InstanceOf['Transform::Alert'],
21 weak_ref => 1,
22 handles => [ 'log' ],
23 );
24 6 has input => (
25 is => 'ro',
26 isa => ConsumerOf['Transform::Alert::Input'],
27 required => 1,
28 );
29 6 has templates => (
30 is => 'ro',
31 isa => ArrayRef[InstanceOf['Transform::Alert::TemplateGrp']],
32 required => 1,
33 );
34  
35 has interval => (
36 is => 'ro',
37 isa => Int,
38 0 ▶ default => sub { 60 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 6 );
40 has last_finished => (
41 is => 'rw',
42 isa => Num,
43 lazy => 1,
44 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 6 );
46 0 ▶sub time_left {
Subroutine time_left: not called CC=1 SCAR=6.9
Pod undocumented
47 0 my $self = shift;
48 0 time - $self->last_finished + $self->interval;
49 }
50  
51 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
52 0 my ($orig, $self) = (shift, shift);
53 0 my $hash = shift;
54 0 ▶ $hash = { $hash, @_ } unless ref $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash 0 0 0
55  
56 # temp hash with output objects
57 0 my $outs = delete $hash->{output_objs};
58  
59 # create input first
60 0 my $name = delete $hash->{name};
61 0 ▶ my $type = delete $hash->{type} || die "Input '$name' requires a Type!";
Condition0%0/2
delete $$hash{'type'} || die("Input '${name}' requires a Type!")
l!l
00
MC/DC0%0/1
delete $$hash{'type'} || die("Input '${name}' requires a Type!")
delete $$hash{'type'}
Truth table0%0/2
A
A= delete $$hash{'type'}
Aresult
11
00
62 0 my $class = "Transform::Alert::Input::$type";
63 0 load_class $class;
64 $hash->{input} = $class->new(
65 connopts => delete $hash->{connopts}
66 0 );
67  
68 # translate templates
69 0 ▶ $hash->{template} = [ $hash->{template} ] unless (ref $hash->{template} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$hash{"template"} eq "ARRAY" 0 0 0
70 $hash->{templates} = [ map {
71 0 $_->{output_objs} = $outs;
72 0 Transform::Alert::TemplateGrp->new($_);
73 0 } @{ delete $hash->{template} } ];
74  
75 0 $orig->($self, $hash);
76 6 };
77  
78 0 ▶sub BUILD {
Subroutine BUILD: not called CC=2 SCAR=17.9
Pod undocumented
79 0 my $self = shift;
80 0 $_->_set_in_group($self) for (@{ $self->templates });
81 0 $self->input->_set_group($self);
82 };
83  
84  
85 0 ▶sub process {
Subroutine process: not called CC=9 SCAR=45.0
Pod undocumented
86 0 my $self = shift;
87 0 my ($in, $log) = ($self->input, $self->log);
88 0 $log->debug('Processing input...');
89  
90 0 ▶ unless ($in->opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in->opened) 0 0 0
91 0 $log->debug('Opening input connection');
92 0 $in->open;
93 }
94 0 ▶ until ($in->eof) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in->eof) 0 0 0
95 # get a message
96 0 my ($msg, $hash) = $in->get;
97 0 ▶ unless (defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
98 0 $log->warn('Input error... bailing out of this process cycle!');
99 0 $self->close_all;
100 0 return;
101 }
102 0 $log->info(' Found message: '.printable(elide($$msg, int(2.5 ** $log->level) )) );
103  
104 # start the matching process
105 0 my $tmpl_count = 0;
106 0 foreach my $tmpl (@{ $self->templates }) {
107 # input RE templates
108 0 my $vars = {};
109 0 ▶ if ($tmpl->regexp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpl->regexp) 0 0 0
110 0 ▶ next unless ($$msg =~ $tmpl->regexp); # found one
Branch: 1 branch
BranchTrueFalseTotal
unless $$msg =~ $tmpl->regexp 0 0 0
111 0 $vars = { %+ }; # untie
112 }
113 0 $tmpl_count++;
114 0 $tmpl->send_all({
115 t => $vars,
116 p => $hash,
117 });
118 }
119 0 ▶ $log->info(' '.($tmpl_count ? $tmpl_count : 'No').' matching template'.($tmpl_count == 1 ? '' : 's').' found');
Branch: 2 branches
BranchTrueFalseTotal
$tmpl_count ? : 0 0 0
$tmpl_count == 1 ? : 0 0 0
120 }
121 0 $self->close_all;
122  
123 0 return 1;
124 }
125  
126 0 ▶sub close_all {
Subroutine close_all: not called CC=2 SCAR=17.9
Pod undocumented
127 0 my $self = shift;
128 0 my $log = $self->log;
129 0 $log->debug('Closing all I/O for this group');
130  
131 0 $self->input->close;
132 0 $_->close_all for (@{ $self->templates });
133  
134 0 $self->last_finished(time);
135 0 $log->debug('Finish time marker');
136  
137 0 return 1;
138 }
139  
140 6 42;
141  
142 __END__
« Alert/Input/IMAP.pm ↑ Summary Alert/Output.pm »