AuditAny/AuditContext/Change.pm

filter: Statementstmt 35.1% 52 / 148 Branchbran 4.5% 1 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 34.2% 13 / 38 Podpod 100% 13 / 13 totaltotal 32.5% 79 / 243 CC 21 SCAR 27.3
CC
21
Cov
29%
__ANON__
72.0
arr_arr_ascii_table
30.0
__ANON__
12.0
CRAP 15.3
« AuditAny.pm ↑ Summary AuditAny/Collector/DBIC.pm »
1 package DBIx::Class::AuditAny::AuditContext::Change;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Default 'Change' context object class for DBIx::Class::AuditAny
6  
7 14 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use MooX::Types::MooseLike::Base 0.19 qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 extends 'DBIx::Class::AuditAny::AuditContext';
10  
11 14 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use DBIx::Class::AuditAny::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 DBIx::Class::AuditAny::AuditContext::Change - Default 'Change' context object for DBIC::AuditAny
17  
18 =head1 DESCRIPTION
19  
20 This is the class which represents a single captured change event, which could involve multiple
21 columns.
22  
23 =head1 ATTRIBUTES
24  
25 Docs regarding the API/purpose of the attributes and methods in this class still TBD...
26  
27 =head2 SourceContext
28  
29 The Source context
30  
31 =cut
32 14 has 'SourceContext', is => 'ro', isa => Object, required => 1;
33  
34 =head2 ChangeSetContext
35  
36 The parent ChangeSet
37  
38 =cut
39 0 ▶has 'ChangeSetContext', is => 'rw', isa => Maybe[Object], default => sub{undef};
Subroutine __ANON__: not called CC=1 SCAR=1.2
40  
41  
42 =head2 action
43  
44 The type of action which triggered this change: insert, update or delete, or the special
45 action 'select' which is used to initialize tracked rows in the audit database
46  
47 =cut
48 14 has 'action', is => 'ro', isa => Enum[qw(insert update delete select)], required => 1;
49  
50  
51 =head2 old_columns
52  
53 The column values of the row, -according to the db- *before* the change happens.
54 This should be an empty hashref in the case of 'insert'
55  
56 =cut
57 0 ▶has 'old_columns', is => 'ro', isa => HashRef, lazy => 1, default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
58  
59 =head2 to_columns
60  
61 The column changes specified -by the change- (specified by
62 the client/query). Note that this is different from 'new_columns' and
63 probably doesn't contain all the columns. This should be an empty
64 hashref in the case of 'delete'
65 (TODO: would 'change_columns' a better name than 'to_columns'?)
66  
67 =cut
68 0 ▶has 'to_columns', is => 'ro', isa => HashRef, lazy => 1, default => sub{{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
69  
70 =head2 new_columns
71  
72 The column values of the row, -according to the db- *after* the change happens.
73 This should be an empty hashref in the case of 'delete'
74  
75 =cut
76 0 ▶has 'new_columns', is => 'ro', isa => HashRef, lazy => 1, default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
77  
78 =head2 condition
79  
80 The condition associated with this change, applies to 'update' and 'delete'
81  
82 =cut
83 0 ▶has 'condition', is => 'ro', isa => Ref, lazy => 1, default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
84  
85 =head2 recorded
86  
87 Boolean flag set to true once the change data has been recorded
88  
89 =cut
90 4 ▶has 'recorded', is => 'rw', isa => Bool, default => sub{0}, init_arg => undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
91  
92  
93 =head2 pri_key_value
94  
95 =cut
96 0 ▶has 'pri_key_value', is => 'ro', isa => Maybe[Str], lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 my $self = shift;
98 0 $self->enforce_recorded;
99
100 # TEMP: this is a bridge for converting away from needing Row objects...
101 0 my $merge_cols = { %{$self->old_columns}, %{$self->new_columns} };
102 0 return $self->get_pri_key_value($merge_cols);
103
104 #my $Row = $self->Row || $self->origRow;
105 #return $self->get_pri_key_value($Row);
106 14 };
107  
108 =head2 orig_pri_key_value
109  
110 =cut
111 0 ▶has 'orig_pri_key_value', is => 'ro', isa => Maybe[Str], lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 my $self = shift;
113
114 # TEMP: this is a bridge for converting away from needing Row objects...
115 0 my $merge_cols = { %{$self->new_columns},%{$self->old_columns} };
116 0 return $self->get_pri_key_value($merge_cols);
117
118 #return $self->get_pri_key_value($self->origRow);
119 14 };
120  
121  
122 =head2 change_ts
123  
124 =cut
125 4 ▶has 'change_ts', is => 'ro', isa => InstanceOf['DateTime'], lazy => 1, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
126 4 my $self = shift;
127 4 $self->enforce_unrecorded;
128 4 return $self->get_dt;
129 14 };
130  
131 =head2 start_timeofday
132  
133 =cut
134 4 ▶has 'start_timeofday', is => 'ro', default => sub { [gettimeofday] };
Subroutine __ANON__: called CC=1 SCAR=0.0
135  
136 =head2 change_elapsed
137  
138 =cut
139 4 ▶has 'change_elapsed', is => 'rw', default => sub{undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
140  
141 =head2 column_changes
142  
143 =cut
144 0 ▶has 'column_changes', is => 'ro', isa => HashRef[Object], lazy => 1, default => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
145 0 my $self = shift;
146 0 $self->enforce_recorded;
147
148 0 my $old = $self->old_columns;
149 0 my $new = $self->new_columns;
150
151 # This logic is duplicated in DbicLink2. Not sure how to avoid it, though,
152 # and keep a clean API
153 0 my @changed = ();
154 0 foreach my $col (uniq(keys %$new,keys %$old)) {
155 0 ▶ next if (!(defined $new->{$col}) and !(defined $old->{$col}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new->{$col} or defined $old->{$col} 0 0 0
Condition0%0/3
defined $new->{$col} or defined $old->{$col}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $new->{$col} or defined $old->{$col}
defined $new->{$col} defined $old->{$col}
Truth table0%0/3
A or B
A= defined $new->{$col}B= defined $old->{$col}
ABresult
1X1
011
000
156 next if (
157 defined $new->{$col} and defined $old->{$col} and
158 0 ▶ $new->{$col} eq $old->{$col}
Branch: 1 branch
BranchTrueFalseTotal
if defined $new->{$col} and defined $old->{$col} and $new->{$col} eq $old->{$col} 0 0 0
Condition0%0/6
defined $new->{$col} and defined $old->{$col} and $new->{$col} eq $old->{$col}
!ll&&!rl&&r
000
defined $new->{$col} and defined $old->{$col}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $new->{$col} and defined $old->{$col} and $new->{$col} eq $old->{$col}
defined $new->{$col} defined $old->{$col} $new->{$col} eq $old->{$col}
Truth table0%0/4
A and B and C
A= defined $new->{$col}B= defined $old->{$col}C= $new->{$col} eq $old->{$col}
ABCresult
0XX0
10X0
1100
1111
159 );
160 0 push @changed, $col;
161 }
162
163 0 my %col_context = ();
164 0 my $class = $self->AuditObj->column_context_class;
165 0 foreach my $column (@changed) {
166 my $ColumnContext = $class->new(
167 AuditObj => $self->AuditObj,
168 ChangeContext => $self,
169 column_name => $column,
170 old_value => $old->{$column},
171 0 new_value => $new->{$column},
172 );
173 0 $col_context{$ColumnContext->column_name} = $ColumnContext;
174 }
175
176 0 return \%col_context;
177 14 };
178  
179 0 ▶has 'column_datapoint_values', is => 'ro', isa => HashRef, lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 my $self = shift;
181 #my @Contexts = $self->all_column_changes;
182 0 my @Contexts = values %{$self->column_changes};
183 0 return { map { $_->column_name => $_->local_datapoint_data } @Contexts };
184 14 };
185  
186  
187 0 ▶has 'column_changes_ascii', is => 'ro', isa => Str, lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 my $self = shift;
189 0 my $table = $self->column_changes_arr_arr_table;
190 0 return $self->arr_arr_ascii_table($table);
191 14 };
192  
193 0 ▶has 'column_changes_json', is => 'ro', isa => Str, lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 0 my $self = shift;
195 0 my $table = $self->column_changes_arr_arr_table;
196 0 require JSON;
197 0 return JSON::encode_json($table);
198 14 };
199  
200  
201 has 'column_changes_arr_arr_table', is => 'ro', isa => ArrayRef,
202 0 ▶ lazy => 1, default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
203 0 my $self = shift;
204 0 my @cols = $self->get_context_datapoint_names('column');
205
206 0 my @col_datapoints = values %{$self->column_datapoint_values};
207
208 0 my $table = [\@cols];
209 0 foreach my $col_data (@col_datapoints) {
210 0 ▶ my @row = map { $col_data->{$_} || undef } @cols;
Condition0%0/2
$col_data->{$_} || undef
l!l
00
MC/DC0%0/1
$col_data->{$_} || undef
$col_data->{$_}
Truth table0%0/2
A
A= $col_data->{$_}
Aresult
11
00
211 0 push @$table, \@row;
212 }
213
214 0 return $table;
215 14 };
216  
217  
218  
219 =head1 METHODS
220  
221 =head2 class
222  
223 =head2 ResultSource
224  
225 =head2 source
226  
227 =head2 pri_key_column
228  
229 =head2 pri_key_count
230  
231 =head2 primary_columns
232  
233 =head2 get_pri_key_value
234  
235 =head2 record
236  
237 =head2 action_id
238  
239 =head2 enforce_recorded
240  
241 =head2 enforce_unrecorded
242  
243 =head2 all_column_changes
244  
245 =head2 arr_arr_ascii_table
246  
247 =cut
248 0 ▶sub class { (shift)->SourceContext->class }
Subroutine class: not called CC=1 SCAR=6.9
Pod documented
249 0 ▶sub ResultSource { (shift)->SourceContext->ResultSource }
Subroutine ResultSource: not called CC=1 SCAR=6.9
Pod documented
250 0 ▶sub source { (shift)->SourceContext->source }
Subroutine source: not called CC=1 SCAR=6.9
Pod documented
251 0 ▶sub pri_key_column { (shift)->SourceContext->pri_key_column }
Subroutine pri_key_column: not called CC=1 SCAR=6.9
Pod documented
252 0 ▶sub pri_key_count { (shift)->SourceContext->pri_key_column }
Subroutine pri_key_count: not called CC=1 SCAR=6.9
Pod documented
253 0 ▶sub primary_columns { (shift)->SourceContext->primary_columns }
Subroutine primary_columns: not called CC=1 SCAR=6.9
Pod documented
254 0 ▶sub get_pri_key_value { (shift)->SourceContext->get_pri_key_value(@_) }
Subroutine get_pri_key_value: not called CC=1 SCAR=6.9
Pod documented
255  
256 0 ▶sub _build_tiedContexts {
Subroutine _build_tiedContexts: not called CC=2 SCAR=17.9
257 0 my $self = shift;
258 0 my @Contexts = ( $self->SourceContext );
259 0 ▶ unshift @Contexts, $self->ChangeSetContext if ($self->ChangeSetContext);
Branch: 1 branch
BranchTrueFalseTotal
if $self->ChangeSetContext 0 0 0
260 0 return \@Contexts;
261 }
262 0 ▶sub _build_local_datapoint_data {
Subroutine _build_local_datapoint_data: not called CC=1 SCAR=6.9
263 0 my $self = shift;
264 0 $self->enforce_recorded;
265 0 return { map { $_->name => $_->get_value($self) } $self->get_context_datapoints('change') };
266 }
267  
268 4 ▶sub record {
Subroutine record: called CC=3 SCAR=17.9
Pod documented
269 4 my $self = shift;
270 4 my $new_columns = shift;
271 4 $self->enforce_unrecorded;
272 4 $self->change_ts;
273 0 $self->change_elapsed(sprintf('%.8g',tv_interval($self->start_timeofday)));
274
275 0 ▶ %{$self->new_columns} = %$new_columns if (
Branch: 1 branch
BranchTrueFalseTotal
if ref $new_columns eq "HASH" and scalar keys %$new_columns > 0 0 0 0
Condition0%0/3
ref $new_columns eq "HASH" and scalar keys %$new_columns > 0
!ll&&!rl&&r
000
MC/DC0%0/2
ref $new_columns eq "HASH" and scalar keys %$new_columns > 0
ref $new_columns eq "HASH" scalar keys %$new_columns > 0
Truth table0%0/3
A and B
A= ref $new_columns eq "HASH"B= scalar keys %$new_columns > 0
ABresult
0X0
100
111
276 ref($new_columns) eq 'HASH' and
277 scalar(keys %$new_columns) > 0
278 );
279
280 0 $self->recorded(1);
281 }
282  
283  
284 # action_id exists so collectors can store the action as a shorter id
285 # instead of the full name.
286 0 ▶sub action_id {
Subroutine action_id: not called CC=3 SCAR=24.8
Pod documented
287 0 my $self = shift;
288 0 ▶ my $action = $self->action or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $action = $self->action or return undef 0 0 0
289 0 ▶ my $id = $self->_action_id_map->{$action} or die "Error looking up action_id";
Branch: 1 branch
BranchTrueFalseTotal
my $id = $self->_action_id_map->{$action} or die "Error looking up action_id" 0 0 0
290 0 return $id;
291 }
292  
293 4 ▶has '_action_id_map', is => 'ro', default => sub {{
Subroutine __ANON__: called CC=1 SCAR=0.0
294 4 insert => 1,
295 update => 2,
296 delete => 3
297 14 }}, isa => HashRef[Int];
298  
299  
300  
301 8 ▶sub enforce_unrecorded {
Subroutine enforce_unrecorded: called CC=2 SCAR=7.2
Pod documented
302 8 my $self = shift;
303 8 ▶ die "Error: Audit action already recorded!" if ($self->recorded);
Branch: 1 branch
BranchTrueFalseTotal
if $self->recorded 0 8 8
304 }
305  
306 0 ▶sub enforce_recorded {
Subroutine enforce_recorded: not called CC=2 SCAR=17.9
Pod documented
307 0 my $self = shift;
308 0 ▶ die "Error: Audit action not recorded yet!" unless ($self->recorded);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recorded 0 0 0
309 }
310  
311 0 ▶sub all_column_changes { values %{(shift)->column_changes} }
Subroutine all_column_changes: not called CC=1 SCAR=6.9
Pod documented
312  
313 0 ▶sub arr_arr_ascii_table {
Subroutine arr_arr_ascii_table: not called CC=5 SCAR=34.0
Pod documented
314 0 my $self = shift;
315 0 my $table = shift;
316 0 ▶ die "Supplied table is not an arrayref" unless (ref($table) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table eq "ARRAY" 0 0 0
317
318 0 require Text::TabularDisplay;
319 0 require Text::Wrap;
320
321 0 my $t = Text::TabularDisplay->new;
322
323 0 local $Text::Wrap::columns = 52;
324
325 0 my $header = shift @$table;
326 0 ▶ die "Encounted non-arrayref table row" unless (ref($header) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $header eq "ARRAY" 0 0 0
327
328 0 $t->add(@$header);
329 0 $t->add('');
330
331 0 foreach my $row (@$table) {
332 0 ▶ die "Encounted non-arrayref table row" unless (ref($row) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 0 0 0
333 0 $t->add( map { Text::Wrap::wrap('','',$_) } @$row );
334 }
335
336 0 return $t->render;
337 }
338  
339 14 1;
340  
341 __END__
« AuditAny.pm ↑ Summary AuditAny/Collector/DBIC.pm »