| 1 | package Transform::Alert::TemplateGrp; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | our $VERSION = '1.00'; # VERSION | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: Base class for Transform::Alert template groups | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use sanity; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use MooX::Types::MooseLike::Base qw(Bool Str ArrayRef RegexpRef HashRef InstanceOf ConsumerOf Maybe); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Template 2.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Data::Dump 'pp'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use File::Slurp 'read_file'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use String::Escape qw(elide printable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use Module::Load; # yes, using both Class::Load and Module::Load, as M:L will load files | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use Module::Metadata; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 7 | has in_group => ( | |||||||||||||||||||||||||||
| 20 | is => 'rwp', | ||||||||||||||||||||||||||||
| 21 | isa => InstanceOf['Transform::Alert::InputGrp'], | ||||||||||||||||||||||||||||
| 22 | weak_ref => 1, | ||||||||||||||||||||||||||||
| 23 | handles => [ 'log' ], | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | 7 | has regexp => ( | |||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||
| 27 | isa => Maybe[RegexpRef], | ||||||||||||||||||||||||||||
| 28 | required => 1, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | 7 | has munger => ( | |||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||
| 32 | isa => ArrayRef[Str], | ||||||||||||||||||||||||||||
| 33 | predicate => 1, | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | 7 | has outputs => ( | |||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | isa => HashRef[ConsumerOf['Transform::Alert::Output']], | ||||||||||||||||||||||||||||
| 38 | required => 1, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 42 | 0 | my ($orig, $self) = (shift, shift); | |||||||||||||||||||||||||||
| 43 | 0 | my $hash = shift; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | $hash = { $hash, @_ } unless ref $hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # temp hash with output objects | ||||||||||||||||||||||||||||
| 47 | 0 | my $outs = delete $hash->{output_objs}; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # replace OutputNames with Outputs | ||||||||||||||||||||||||||||
| 50 | 0 | my $outputs = delete $hash->{outputname}; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | $outputs = [ $outputs ] unless (ref $outputs eq 'ARRAY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | $hash->{outputs} = { map { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $_ => ($outs->{$_} || die "OutputName '$_' doesn't have a matching Output block!") | ||||||||||||||||||||||||||
|
Condition0%0/2
$$outs{$_} || die("OutputName '${_}' doesn't have a matching Output block!")
MC/DC0%0/1
$$outs{$_} || die("OutputName '${_}' doesn't have a matching Output block!")
$$outs{$_}
Truth table0%0/2
A
A= $$outs{$_}
| |||||||||||||||||||||||||||||
| 54 | } @$outputs }; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # read template file | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ($hash->{templatefile}) { $hash->{regexp} = read_file( delete $hash->{templatefile} ); } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 58 | 0 | elsif ($hash->{template}) { $hash->{regexp} = delete $hash->{template}; } | |||||||||||||||||||||||||||
| 59 | 0 | else { $hash->{regexp} = undef; } | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # work with inline templates (and file above) | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($hash->{regexp} && not ref $hash->{regexp}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$hash{"regexp"} and not ref $$hash{"regexp"}
MC/DC0%0/2
$$hash{"regexp"} and not ref $$hash{"regexp"}
$$hash{"regexp"}
not ref $$hash{"regexp"}
Truth table0%0/3
A and not(B)
A= $$hash{"regexp"}B= not ref $$hash{"regexp"}
| |||||||||||||||||||||||||||||
| 63 | 0 | my $tmpl_text = $hash->{regexp}; | |||||||||||||||||||||||||||
| 64 | 0 | $tmpl_text =~ s/^\s+|\s+$//g; # remove leading/trailing spaces | |||||||||||||||||||||||||||
| 65 | 0 | $tmpl_text =~ s/\r//g; # make sure it works for all line-endings | |||||||||||||||||||||||||||
| 66 | 0 | $tmpl_text = '^'.$tmpl_text.'$'; | |||||||||||||||||||||||||||
| 67 | 0 | $hash->{regexp} = qr/$tmpl_text/; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # munger class | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (my $munger = delete $hash->{munger}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | # variable parsing | ||||||||||||||||||||||||||||
| 73 | 0 | my ($file, $class, $fc, $method); | |||||||||||||||||||||||||||
| 74 | 0 | ($fc, $method) = split /-\>/, $munger, 2; | |||||||||||||||||||||||||||
| 75 | 0 | ($file, $class) = split /\s+/, $fc, 2; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | unless ($class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | my $info = Module::Metadata->new_from_file($file); | |||||||||||||||||||||||||||
| 79 | 0 | $class = ($info->packages_inside)[0]; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | die "No packages found in $file!" unless $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | $method ||= 'munge'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$method ||= "munge"
MC/DC0%0/1
$method ||= "munge"
$method
Truth table0%0/2
A
A= $method
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | load $file; | |||||||||||||||||||||||||||
| 85 | 0 | $hash->{munger} = [ $class, $method ]; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | $orig->($self, $hash); | |||||||||||||||||||||||||||
| 89 | 7 | }; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub send_all { | ||||||||||||||||||||||||||
|
Subroutine
send_all: not called CC=9 SCAR=43.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 0 | my ($self, $vars) = @_; | |||||||||||||||||||||||||||
| 93 | 0 | my $log = $self->log; | |||||||||||||||||||||||||||
| 94 | 0 | $log->debug('Processing outputs...'); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | $log->trace('Variables (pre-munged):'); | |||||||||||||||||||||||||||
| 97 | 0 | $log->trace( join "\n", map { ' '.$_ } split(/\n/, pp $vars) ); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Munge the data if configured | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($self->munger) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | my ($class, $method) = @{ $self->munger }; | |||||||||||||||||||||||||||
| 102 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 0 | $vars = $class->$method($vars, $self); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | unless ($vars) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | $log->debug('Munger cancelled output'); | |||||||||||||||||||||||||||
| 107 | 0 | return 1; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | $log->trace('Variables (post-munge):'); | |||||||||||||||||||||||||||
| 111 | 0 | $log->trace( join "\n", map { ' '.$_ } split(/\n/, pp $vars) ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Support multiple outputs, if the munger sent them | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | $vars = [ $vars ] unless (ref $vars eq 'ARRAY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my $tt = Template->new(); | |||||||||||||||||||||||||||
| 118 | 0 | foreach my $v (@$vars) { | |||||||||||||||||||||||||||
| 119 | 0 | foreach my $out_key (keys %{ $self->outputs }) { | |||||||||||||||||||||||||||
| 120 | 0 | $log->debug('Looking at Output "'.$out_key.'"...'); | |||||||||||||||||||||||||||
| 121 | 0 | my $out = $self->outputs->{$out_key}; | |||||||||||||||||||||||||||
| 122 | 0 | my $out_str = ''; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | $tt->process($out->template, $v, \$out_str) || do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $log->error('TT error for "$out_key": '.$tt->error); | |||||||||||||||||||||||||||
| 126 | 0 | $log->warn('Output error... bailing out of this process cycle!'); | |||||||||||||||||||||||||||
| 127 | 0 | $self->close_all; | |||||||||||||||||||||||||||
| 128 | 0 | return; | |||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # send alert | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | unless ($out->opened) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | $log->debug('Opening output connection'); | |||||||||||||||||||||||||||
| 134 | 0 | $out->open; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | $log->info('Sending alert for "'.$out_key.'"'); | |||||||||||||||||||||||||||
| 137 | 0 | $log->info(' Output message: '.printable(elide($out_str, int(2.5 ** $log->level) )) ); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | unless ($out->send(\$out_str)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | $log->warn('Output error... bailing out of this process cycle!'); | |||||||||||||||||||||||||||
| 141 | 0 | $self->close_all; | |||||||||||||||||||||||||||
| 142 | 0 | return; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | return 1; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub close_all { | ||||||||||||||||||||||||||
|
Subroutine
close_all: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 0 | $_->close for (values %{ $self->outputs }); | |||||||||||||||||||||||||||
| 153 | 0 | return 1; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 7 | 42; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||