ETLp/ItemBuilder.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 155 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ETLp::ItemBuilder;
2  
3 use MooseX::Declare;
4  
5 =head1 NAME
6  
7 ETLp::ItemBuilder - Builds the pipeline of items to execute
8  
9 =head1 DESCRIPTION
10  
11 The pipeline is effectivey an array of anonymous methods that will be
12 called when the pipline execution begins
13  
14 =head1 METHODS
15  
16 =head2 new
17  
18 =head3 parameters
19  
20 * plugins: Required. A hashref of item types types and the name of
21 the plugin that executes that type
22 * pipeline_type: Required. Either "iteration" or "serial"
23 * include_env: Optional. Whether the environment variables should be
24 substituted into the configuration. Defaults to false (0)
25 * env_config: Optional The environment variables (a hashref)
26 * app_root: Required. The application root directory
27
28 =head3 returns
29  
30 an ETLp::ItemBuilder object
31  
32 =cut
33  
34 class ETLp::ItemBuilder with ETLp::Role::Config {
35 use ETLp::Exception;
36 use Data::Dumper;
37 use UNIVERSAL::require;
38 use Try::Tiny;
39 use Clone qw(clone);
40 use File::Basename;
41  
42 has 'plugins' => (is => 'ro', isa => 'HashRef', required => 1);
43 has 'pipeline_type' => (is => 'ro', isa => 'Str', required => 1);
44 has 'allow_env_vars' =>
45 (is => 'ro', isa => 'Bool', required => 0, default => 0);
46 has 'env_conf' =>
47 (is => 'ro', isa => 'HashRef', required => 0, default => sub { {} });
48 has 'app_root' => (is => 'ro', isa => 'Maybe[Str]', required => 1);
49 has 'config' => (is => 'ro', isa => 'HashRef', required => 1);
50  
51 # Recursively navigate the items, replacing the placeholders
52 # with their values
53
54 method _parse_item(HashRef $config, $item, Maybe [Str] $filename) {
55 my $app_root = $self->app_root;
56 my %master_config = (%{$self->env_conf}, %{$config->{config}});
57 $self->logger->debug("Master config: " . Dumper(\%master_config));
58 if (ref $item eq 'HASH') {
59 foreach my $item_key (keys %$item) {
60 if (!ref $item->{$item_key}) {
61 if ($item->{$item_key} =~ /%app_root%/) {
62 $item->{$item_key} =~ s/%app_root%/$app_root/g;
63 }
64
65 if ($filename) {
66 if ($item->{$item_key} =~ /%filename%/) {
67 $item->{$item_key} =~ s/%filename%/$filename/g;
68 }
69
70 if ($item->{$item_key} =~ /%basename\(filename\)%/) {
71 my $base_filename = basename($filename);
72 $item->{$item_key} =~
73 s/%basename\(filename\)%/$base_filename/g;
74 }
75 }
76 foreach my $config_key (keys %master_config) {
77 if ($item->{$item_key} =~ /%$config_key%/) {
78 my $env_value = $master_config{$config_key};
79 $item->{$item_key} =~ s/%$config_key%/$env_value/g;
80 }
81 }
82 } else {
83 $self->_parse_item($config, $item->{$item_key}, $filename);
84 }
85 }
86 } elsif (ref $item eq 'ARRAY') {
87 for (my $j = 0 ; $j < @$item ; $j++) {
88 $self->_parse_item($config, $item->[$j], $filename);
89 }
90 }
91
92 return $item;
93 }
94  
95 # Go through the config section and the items, replacing the placeholders
96 # with their values
97 method _parse_config(HashRef $config, Maybe [Str] $filename){
98 my $app_root = $self->app_root;
99  
100 # Firstly, replace app_root and filename throughout the config section
101 foreach my $config_key (keys %{$config->{config}}) {
102  
103 # replace the app_root placeholder
104 if ($config->{config}->{$config_key} =~ /%app_root%/) {
105 $config->{config}->{$config_key} =~ s/%app_root%/$app_root/g;
106 }
107  
108 # replace the environment placeholders if we are allowed to
109 foreach my $env_config_key (keys %{$self->env_conf}) {
110 if ($config->{config}->{$config_key} =~ /%$env_config_key%/)
111 {
112 my $env_value = $self->env_conf->{$env_config_key};
113 $config->{config}->{$config_key} =~
114 s/%$env_config_key%/$env_value/g;
115 }
116 }
117 }
118
119 return $config;
120 }
121  
122 =head2 build_pipeline
123  
124 Build an array of the pipeline items
125  
126 =head3 parameters
127  
128 * void
129
130 =head3 returns
131  
132 An arrayref of Item hashrefs. Each hashref consists of the following keys:
133  
134 * name. The name of the item
135 * type. The type of the item
136 * plugin. The plugin that will execute the pipeline item
137 * sub. An anonymous subroutine that executes the pipeline item
138 * item. The original item hash (minus the name and type)
139 * parsed_item. The item hash with the placeholders
140  
141 =cut
142  
143 method build_pipeline {
144 my $app_config = clone($self->config);
145 my @items;
146 foreach my $phase (qw/pre_process process post_process/) {
147 if (exists($app_config->{$phase}->{item})) {
148 foreach my $item (@{$app_config->{$phase}->{item}}) {
149 my $name = delete $item->{name}
150 || ETLpException->throw(error => "Item has no name: " .
151 Dumper($item));
152 my $type = delete $item->{type}
153 || ETLpException->throw(error => "Item has no type: " .
154 Dumper($item));
155 my $plugin = $self->plugins->{$type}
156 || ETLpException->throw(error =>
157 "No such plugin for type $type: " .
158 Dumper($item));
159  
160 my $sub = sub {
161 my $filename = shift;
162  
163 my $parsed_config =
164 $self->_parse_config(clone($app_config), $filename);
165
166 my $parsed_item = $self->_parse_item(
167 $parsed_config, clone($item), $filename
168 );
169  
170 #if ($self->allow_env_vars) {
171 # # We don't want to log any sensitive information
172 # # from the environment file
173 # $self->logger->debug("$name item:" . Dumper($item));
174 # $self->logger->debug(
175 # "$name config:" . Dumper($app_config));
176 #} else {
177 # # If there is any senstive information in the
178 # # application config, then it will appear in the
179 # # log if the threshold <= debug. Users should
180 # # not put sensitive info there.
181 # $self->logger->debug(
182 # "$name item:" . Dumper($parsed_item));
183 # $self->logger->debug(
184 # "$name config:" . Dumper($parsed_config));
185 #}
186
187 $self->logger->debug("$name item:" . Dumper($item));
188 $self->logger->debug("$name config:" . Dumper($app_config));
189
190 $self->logger->debug(
191 "Parsed $name item:" . Dumper($parsed_item));
192 $self->logger->debug(
193 "Parsed $name config:" . Dumper($parsed_config));
194  
195 $plugin->require;
196 my $etlp_obj = $plugin->new(
197 item => $parsed_item,
198 config => $parsed_config,
199 original_item => $item,
200 env_conf => $self->env_conf,
201 );
202  
203 if ($filename) {
204 my $file_proc =
205 $self->audit->item->create_file_process(
206 basename($filename));
207
208 try {
209 $filename = $etlp_obj->run($filename);
210 }
211 catch {
212 my $error = $_;
213 $file_proc->update_message(''.$error);
214 $file_proc->update_status('failed');
215 $error->rethrow if (ref $error);
216 ETLpException->throw(error =>
217 "Processing of file: $filename failed: $_");
218 };
219
220 # A warning won't raise an exception, so don't
221 # overwrite a warning with a success
222 unless ($file_proc->details->status->status_name eq
223 'warning') {
224 $file_proc->update_status('succeeded');
225 }
226 return $filename;
227 } else {
228 $etlp_obj->run;
229 }
230 };
231  
232 push @items,
233 {
234 'name' => $name,
235 'type' => $type,
236 'plugin' => $plugin,
237 'runner' => $sub,
238 'item' => $item,
239 'phase' => $phase
240 };
241 }
242 }
243 }
244 return \@items;
245 }
246 }
247  
248 =head1 ROLES CONSUMED
249  
250 * ETLp::Role::Config
251
252 =head1 LICENSE AND COPYRIGHT
253  
254 Copyright 2010 Redbone Systems Ltd
255  
256 This program is free software; you can redistribute it and/or modify it
257 under the terms of either: the GNU General Public License as published
258 by the Free Software Foundation; or the Artistic License.
259  
260 The terms are in the LICENSE file that accompanies this application
261