blib/lib/oEdtk/Tracking2.pm

filter: Statementstmt 21.0% 46 / 219 Branchbran 1.1% 1 / 88 Conditioncond 0.0% 0 / 56 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 56.0% 14 / 25 Podpod 100% 7 / 7 totaltotal 15.9% 68 / 427 CC 70 SCAR 54.6
CC
70
Cov
13%
track
552.0
new
420.0
_caller_name
56.0
CRAP 235.3
« lib/oEdtk/Run.pm ↑ Summary lib/oEdtk/DBAdmin.pm »
1 package oEdtk::Tracking; ## no critic (Modules::RequireFilenameMatchesPackage)
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $WARN2LOGGER; # imported from oEdtk::logger below
6  
7 1 my ( $_TRACK_SIG, $_TRACK_TRK, @_TRACK_JSON );
8  
9 # xxx TO CHANGE : test if SQLite table already exist before creation
10  
11 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=3.9
12 0 ▶ $SIG{'__WARN__'} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
13 0 ▶ if ($WARN2LOGGER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oEdtk::Tracking::WARN2LOGGER) { } 0 0 0
14 0 &logger( 4, $_[0] );
15 }
16 else {
17 0 warn $_[0];
18 }
19  
20 # when Tracking object is defined, $_TRACK_TRK is defined so that events can be catch and sent to tracker
21 0 ▶ if ( defined $_TRACK_TRK && $_TRACK_SIG =~ /warn/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_TRACK_TRK and $_TRACK_SIG =~ /warn/i) 0 0 0
Condition0%0/3
defined $_TRACK_TRK and $_TRACK_SIG =~ /warn/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_TRACK_TRK and $_TRACK_SIG =~ /warn/i
defined $_TRACK_TRK $_TRACK_SIG =~ /warn/i
Truth table0%0/3
A and B
A= defined $_TRACK_TRKB= $_TRACK_SIG =~ /warn/i
ABresult
0X0
100
111
22  
23 # http://perldoc.perl.org/functions/warn.html
24 0 my $message = $_[0];
25 0 chomp($message);
26 0 $_TRACK_TRK->track( 'Warn', 1, $message );
27 }
28 1 };
29  
30 12 ▶ $SIG{'__DIE__'} = sub {
Subroutine __ANON__: called CC=2 SCAR=10.1
31 12 ▶ if ( defined $_TRACK_TRK ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_TRACK_TRK) 0 12 12
32 0 my $message = $_[0];
33 0 chomp($message);
34 0 $_TRACK_TRK->track( 'Halt', 1, $message );
35 }
36 12 die $_[0];
37 1 };
38 }
39  
40 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 ▶use oEdtk::Main;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use oEdtk::Config qw(config_read);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use oEdtk::DBAdmin qw(db_connect create_table_TRACKING);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use oEdtk::logger qw(logger $WARN2LOGGER);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use oEdtk::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 #use JSON;
53 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 1 our $VERSION = 2.1131;
56 1 our @ISA = qw(Exporter);
57 1 our @EXPORT_OK = qw(stats_iddest stats_week stats_month);
58  
59 #my @JSON_TRACE;
60  
61 0 ▶sub new {
Subroutine new: not called CC=20 SCAR=60.4
Pod documented
62  
63 # example call
64 # $TRK = oEdtk::Tracking->new($ARGV[1] // '',
65 # ED_USER => "utest",
66 # ED_CORP => "Society",
67 # keys => ['T_CALLID', 'T_TYPE', 'O_TT_TYP', 'O_TT_ID', 'T_PARAM', 'O_DESIGN', 'T_STATUT' ]
68 # );
69  
70 0 my ( $class, $ED_SOURCE, %params ) = @_;
71  
72 # An explicit empty source means "no input file" and must stay empty; only
73 # an omitted source falls back to the command line.
74 0 ▶ if ( !defined $ED_SOURCE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ED_SOURCE) 0 0 0
75 0 ▶ $ED_SOURCE = $ARGV[1] || $ARGV[0] || '';
Condition0%0/5
$ARGV[1] || $ARGV[0] || ''
l!l
00
$ARGV[1] or $ARGV[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$ARGV[1] || $ARGV[0] || ''
$ARGV[1] $ARGV[0]
Truth table0%0/3
A or B
A= $ARGV[1]B= $ARGV[0]
ABresult
1X1
011
000
76 }
77 0 ▶ if ( $ED_SOURCE =~ /^\-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ED_SOURCE =~ /^\-/) 0 0 0
78 0 ▶ $ED_SOURCE = $ARGV[0] // '';
Condition0%0/2
$ARGV[0] // ""
l!l
00
MC/DC0%0/1
$ARGV[0] // ""
$ARGV[0]
Truth table0%0/2
A
A= $ARGV[0]
Aresult
11
00
79 }
80 0 my $cfg = config_read('EDTK_DB');
81  
82 # Load the dictionary to normalize entity names.
83 0 my $EDTK_TRACK_DICT = oEdtk::Dict->new( $cfg->{'EDTK_DICO'}, { invert => 1 } );
84  
85 0 my $EDTK_TRACK_MODE = uc( $cfg->{'EDTK_TRACK_MODE'} );
86 0 ▶ if ( $EDTK_TRACK_MODE eq 'NONE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($EDTK_TRACK_MODE eq "NONE") 0 0 0
87 0 warn "INFO: Tracking is currently disabled... (see EDTK_TRACK_MODE in config file)\n";
88  
89 # Return a dummy object if tracking is disabled.
90 0 return bless { EDTK_TRACK_DICT => $EDTK_TRACK_DICT, mode => $EDTK_TRACK_MODE }, $class;
91 }
92  
93 0 my $table = $cfg->{'EDTK_DBI_TRACKING'};
94 0 my $dbh = db_connect( $cfg, 'EDTK_DBI_DSN', { AutoCommit => 1 } );
95  
96 # XXX Should we ensure there is at least one key defined?
97 0 $cfg->{'EDTK_TRACKING_KEYS'} =~ s/\s+//g;
98 0 $cfg->{'EDTK_TRACKING_KEYS'} =~ s/[()"']+//g;
99  
100 my @userkeys =
101 0 ▶ ( split( /,/, uc( $cfg->{'EDTK_TRACKING_KEYS'} ), $cfg->{'EDTK_MAX_USER_KEY'} ) ) // [];
Condition0%0/2
split(/,/, uc $cfg->{"EDTK_TRACKING_KEYS"}, $cfg->{"EDTK_MAX_USER_KEY"}) // []
l!l
00
MC/DC0%0/1
split(/,/, uc $cfg->{"EDTK_TRACKING_KEYS"}, $cfg->{"EDTK_MAX_USER_KEY"}) // []
split(/,/, uc $cfg->{"EDTK_TRACKING_KEYS"}, $cfg->{"EDTK_MAX_USER_KEY"})
Truth table0%0/2
A
A= split(/,/, uc $cfg->{"EDTK_TRACKING_KEYS"}, $cfg->{"EDTK_MAX_USER_KEY"})
Aresult
11
00
102  
103 # print Dumper(@userkeys);
104  
105 # my $keys = $params{'keys'} || [];
106  
107 # if (@$keys > $cfg->{'EDTK_MAX_USER_KEY'}) {
108 # die "ERROR: too many tracking keys: got " . @$keys . ", max " .
109 # $cfg->{'EDTK_MAX_USER_KEY'};
110 # }
111  
112 # Check that all the keys are at most 8 characters long, and otherwise
113 # truncate them. Also ensure we don't have the same key several times.
114 0 my %seen = ();
115 0 ▶ foreach ( my $i = 0 ; $i <= $#userkeys ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#userkeys) 0 0 0
116 0 ▶ if ( length( $userkeys[$i] ) > 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $userkeys[$i] > 8) 0 0 0
117 0 $userkeys[$i] =~ s/^(.{8}).*$/$1/;
118 0 warn "userkeys[$i] = $userkeys[$i]\n";
119 0 warn "INFO: column \"\U$_\E\" too long, truncated to \"$userkeys[$i])\"\n";
120 }
121 0 ▶ if ( exists( $seen{ $userkeys[$i] } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$userkeys[$i]}) 0 0 0
122 0 die "ERROR: duplicate column \"$userkeys[$i])\"";
123 }
124  
125 # push(@userkeys, $key);
126 0 $seen{ $userkeys[$i] } = 1;
127 }
128  
129 # my @userkeys = ();
130 # foreach (@$keys) {
131 # my $key = uc($_);
132 # if (length($key) > 8) {
133 # $key =~ s/^(.{8}).*$/$1/;
134 # warn "INFO: column \"\U$_\E\" too long, truncated to \"$key\"\n";
135 # }
136 # if (exists($seen{$key})) {
137 # die "ERROR: duplicate column \"$key\"";
138 # }
139 # push(@userkeys, $key);
140 # $seen{$key} = 1;
141 # }
142  
143 # Extract application name from the script name.
144 0 my $ED_APP = $0;
145 0 $ED_APP =~ s/^.*?[\/\\]?([\w-]+[\.plmex]*$)/$1/;
146  
147 # $ED_APP =~ s/^.*?[\/\\]?([A-Z0-9-_]+)\.pl$/$1/;
148 0 ▶ if ( length($ED_APP) > 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ED_APP > 20) 0 0 0
149 0 $ED_APP =~ s/\.pl$//i;
150 0 $ED_APP =~ /(.{20})$/;
151 0 warn "INFO: application name \"$ED_APP\" too long, truncated to \"$1\"\n";
152 0 $ED_APP = $1;
153 }
154  
155 # Validate the editing mode.
156 0 my $ED_MOD_ED = _validate_edmode( $params{'ED_MOD_ED'} );
157  
158 # Limit username length to 10 characters per the table schema.
159 0 ▶ my $ED_USER = $params{'ED_USER'} || 'None';
Condition0%0/2
$params{'ED_USER'} || 'None'
l!l
00
MC/DC0%0/1
$params{'ED_USER'} || 'None'
$params{'ED_USER'}
Truth table0%0/2
A
A= $params{'ED_USER'}
Aresult
11
00
160 0 ▶ if ( length($ED_USER) > 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ED_USER > 10) 0 0 0
161 0 $ED_USER =~ s/^(.{10}).*$/$1/;
162 0 warn "INFO: username \"$params{'user'}\" too long, truncated to \"$ED_USER\"\n";
163 }
164  
165 # Truncate if necessary, by taking at most 128 characters on the right.
166 0 ▶ if ( length($ED_SOURCE) > 128 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ED_SOURCE > 128) 0 0 0
167 0 $ED_SOURCE = substr( $ED_SOURCE, -128, 128 );
168 }
169  
170 0 my $self = bless {
171 dbh => $dbh,
172 table => $table,
173 keys => \@userkeys,
174 EDTK_TRACK_DICT => $EDTK_TRACK_DICT,
175 ED_SNGL_ID => oe_ID_LDOC(),
176 ED_SEQ => 1,
177 EDTK_TRACK_MODE => $EDTK_TRACK_MODE,
178 ED_MOD_ED => $ED_MOD_ED,
179 ED_USER => $ED_USER,
180 ED_SOURCE => $ED_SOURCE,
181 ED_APP => $ED_APP
182 }, $class;
183  
184 0 ▶ my $entity = $params{'entity'} || $cfg->{'EDTK_CORP'};
Condition0%0/3
$params{'entity'} || $cfg->{'EDTK_CORP'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{'entity'} || $cfg->{'EDTK_CORP'}
$params{'entity'} $cfg->{'EDTK_CORP'}
Truth table0%0/3
A || B
A= $params{'entity'}B= $cfg->{'EDTK_CORP'}
ABresult
1X1
011
000
185 0 $self->set_entity($entity);
186  
187 # Create the table in the SQLite case
188 0 ▶ if ( $dbh->{'Driver'}->{'Name'} eq 'SQLite' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"Driver"}{"Name"} eq "SQLite") 0 0 0
189 0 eval { create_table_TRACKING( $dbh, $table, $cfg->{'EDTK_MAX_USER_KEY'} ); };
190 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
191 0 warn "INFO: Table already exist : $@\n";
192 }
193 }
194  
195 0 $self->track( 'Job', 1, join( ' ', @ARGV ) )
196 ; # keep the join to place all the free parameters in the message area
197 0 ▶ if ( defined $cfg->{'EDTK_TRACK_SIG'} && $cfg->{'EDTK_TRACK_SIG'} !~ /no/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg->{"EDTK_TRACK_SIG"} and not $cfg->{"EDTK_TRACK_SIG"} =~ /no/i) 0 0 0
Condition0%0/3
defined $cfg->{"EDTK_TRACK_SIG"} and not $cfg->{"EDTK_TRACK_SIG"} =~ /no/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cfg->{"EDTK_TRACK_SIG"} and not $cfg->{"EDTK_TRACK_SIG"} =~ /no/i
defined $cfg->{"EDTK_TRACK_SIG"} not $cfg->{"EDTK_TRACK_SIG"} =~ /no/i
Truth table0%0/3
A and not(B)
A= defined $cfg->{"EDTK_TRACK_SIG"}B= not $cfg->{"EDTK_TRACK_SIG"} =~ /no/i
ABresult
0X0
100
111
198 0 $_TRACK_SIG = $cfg->{'EDTK_TRACK_SIG'};
199 0 warn
200 "INFO: tracking catchs SIG messages = '$_TRACK_SIG' (see EDTK_TRACK_SIG : 'warn' for all, 'halt' for die only)\n";
201 0 $_TRACK_TRK = $self;
202 }
203  
204 # if (defined $cfg->{'EDTK_TRACK_JSON'} && $cfg->{'EDTK_TRACK_JSON'} !~/no/i ) {
205 # # $_TRACK_TRK_JSON = $cfg->{'EDTK_TRACK_JSON'};
206 # push (@_TRACK_JSON, $cfg->{'EDTK_TRACK_JSON'});
207 # }
208 # } else {
209 # #$_TRACK_TRK_JSON = "no";
210 # $_TRACK_JSON[0] //= "no";
211 # #push (@_TRACK_JSON, "no");
212 # }
213 # warn "INFO: _TRACK_JSON = " . @_TRACK_JSON ." \n";
214  
215 0 return $self;
216 }
217  
218 0 ▶sub track {
Subroutine track: not called CC=23 SCAR=63.1
Pod documented
219  
220 # example call
221 # $TRK->track('Spool', 1, "K0val", "K1val", "K2val", "Knval" );
222 0 my ( $self, $ED_JOB_EVT, $count, @data ) = @_;
223  
224 0 ▶ return if $self->{'EDTK_TRACK_MODE'} eq 'NONE';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"EDTK_TRACK_MODE"} eq "NONE" 0 0 0
225  
226 0 ▶ $count ||= 1;
Condition0%0/2
$count ||= 1
l!l
00
MC/DC0%0/1
$count ||= 1
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
227  
228 0 my @usercols = @{ $self->{'keys'} };
229 0 ▶ if ( @data > ( @usercols + 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data > @usercols + 1) 0 0 0
230  
231 # max is @usercols nbcol + 1 for message col
232 0 warn "INFO: Too much values : got "
233 . @data
234 . ", expected "
235 . ( @usercols + 1 )
236 . " maximum\n";
237 }
238  
239 # Validate the job event.
240 0 $ED_JOB_EVT = _validate_event($ED_JOB_EVT);
241  
242 # GENERATE SQL REQUEST.
243 my $values = {
244 ED_SNGL_ID => $self->{'ED_SNGL_ID'},
245 ED_SEQ => $self->{'ED_SEQ'}++,
246 ED_TSTAMP => oe_now_time(),
247 ED_USER => $self->{'ED_USER'},
248 ED_APP => $self->{'ED_APP'},
249 ED_CORP => $self->{'entity'},
250 ED_ACCOUNT => $self->{'account'} // "",
251 ED_MOD_ED => $self->{'edmode'},
252 ED_JOB_EVT => $ED_JOB_EVT,
253 ED_OBJ_TYP => $self->{'obj_typ'},
254 ED_OBJ_COUNT => $count,
255 ED_CHILD_TYP => $self->{'child_typ'} // "",
256 ED_CHILD_ID => $self->{'child_id'} // "",
257 ED_PARENT_ID => $self->{'parent_id'} // "",
258  
259 ED_HOST => hostname(),
260 0 ▶ ED_SOURCE => $self->{'ED_SOURCE'}
Condition0%0/8
$self->{"account"} // ""
l!l
00
$self->{"child_typ"} // ""
l!l
00
$self->{"child_id"} // ""
l!l
00
$self->{"parent_id"} // ""
l!l
00
MC/DC0%0/1
$self->{"account"} // ""
$self->{"account"}
MC/DC0%0/1
$self->{"child_typ"} // ""
$self->{"child_typ"}
MC/DC0%0/1
$self->{"child_id"} // ""
$self->{"child_id"}
MC/DC0%0/1
$self->{"parent_id"} // ""
$self->{"parent_id"}
Truth table0%0/2
A
A= $self->{"account"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"child_typ"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"child_id"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"parent_id"}
Aresult
11
00
261 };
262  
263 0 foreach my $i ( 0 .. $#data ) {
264  
265 # add a message column to handle messages and warnings
266 # to ensure compatibility with what exists, we reverse
267 # the data to put the message first while waiting for job_evt
268 ################## PBM DATA NOT POPULATED, TO REVIEW
269 0 ▶ my $val = $data[$i] // "";
Condition0%0/2
$data[$i] // ""
l!l
00
MC/DC0%0/1
$data[$i] // ""
$data[$i]
Truth table0%0/2
A
A= $data[$i]
Aresult
11
00
270 0 ▶ $values->{'ED_MESSAGE'} = $val . " " . ( $values->{'ED_MESSAGE'} // "" );
Condition0%0/2
$values->{"ED_MESSAGE"} // ""
l!l
00
MC/DC0%0/1
$values->{"ED_MESSAGE"} // ""
$values->{"ED_MESSAGE"}
Truth table0%0/2
A
A= $values->{"ED_MESSAGE"}
Aresult
11
00
271  
272 # if there is only one data item, make sure not to put it needlessly in a user column
273 0 ▶ if ( $#data > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#data > 0) 0 0 0
274 0 ▶ if ( defined( $data[$i] ) && length( $data[$i] ) > 128 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data[$i] and length $data[$i] > 128) 0 0 0
Condition0%0/3
defined $data[$i] and length $data[$i] > 128
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data[$i] and length $data[$i] > 128
defined $data[$i] length $data[$i] > 128
Truth table0%0/3
A and B
A= defined $data[$i]B= length $data[$i] > 128
ABresult
0X0
100
111
275 0 warn "INFO: \"$data[$i]\" truncated to 128 characters\n";
276 0 $data[$i] =~ s/^(.{128}).*$/$1/;
277 }
278 0 $values->{"ED_K${i}_NAME"} = $usercols[$i];
279 0 $values->{"ED_K${i}_VAL"} = $val;
280 }
281 }
282  
283 0 ▶ if ( $ED_JOB_EVT eq 'W' || $ED_JOB_EVT eq 'H' ) { # Halt or Warn event
Branch: 2 branches
BranchTrueFalseTotal
if ($ED_JOB_EVT eq 'W' or $ED_JOB_EVT eq 'H') { } 0 0 0
elsif ($ED_JOB_EVT eq 'J') { } 0 0 0
Condition0%0/3
$ED_JOB_EVT eq "W" or $ED_JOB_EVT eq "H"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ED_JOB_EVT eq "W" or $ED_JOB_EVT eq "H"
$ED_JOB_EVT eq "W" $ED_JOB_EVT eq "H"
Truth table0%0/3
A or B
A= $ED_JOB_EVT eq "W"B= $ED_JOB_EVT eq "H"
ABresult
1X1
011
000
284 0 ▶ $values->{'ED_SOURCE'} = $self->{'ED_SOURCE'} if ( $ED_JOB_EVT eq 'H' );
Branch: 1 branch
BranchTrueFalseTotal
if $ED_JOB_EVT eq "H" 0 0 0
285  
286 }
287 elsif ( $ED_JOB_EVT eq 'J' ) { # ED_JOB_EVT event
288 0 $values->{'ED_SOURCE'} = $self->{'ED_SOURCE'};
289  
290 }
291 else {
292 0 undef( $values->{'ED_MESSAGE'} );
293 }
294  
295 # Normalize the message. When the caller supplied none, name the function
296 # that recorded the event so the row stays informative.
297 0 ▶ my $message = $values->{'ED_MESSAGE'} // '';
Condition0%0/2
$values->{"ED_MESSAGE"} // ""
l!l
00
MC/DC0%0/1
$values->{"ED_MESSAGE"} // ""
$values->{"ED_MESSAGE"}
Truth table0%0/2
A
A= $values->{"ED_MESSAGE"}
Aresult
11
00
298 0 $message =~ s/\s+/ /g;
299 0 $message =~ s/^ //;
300 0 $message =~ s/ $//;
301 0 ▶ $message = _caller_name() if $message eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $message eq "" 0 0 0
302 0 $message =~ s/^(.{256}).*$/$1/;
303 0 $values->{'ED_MESSAGE'} = $message;
304  
305 0 my @cols = keys(%$values);
306 0 my $table = $self->{'table'};
307 0 my $sql =
308 "INSERT INTO $table ("
309 . join( ', ', @cols )
310 . ") VALUES ("
311 . join( ', ', ('?') x @cols ) . ")";
312  
313 #warn "sql = $sql \n";
314 #sql = INSERT INTO EDTK_TRACKING (ED_JOB_EVT, ED_APP, ED_CORP, ED_OBJ_TYP, ED_USER, ED_MESSAGE, ED_TSTAMP, ED_SNGL_ID, ED_OBJ_COUNT, ED_MOD_ED, ED_SOURCE, ED_SEQ, ED_HOST) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
315  
316 0 my $dbh = $self->{'dbh'};
317 0 my $sth = $dbh->prepare($sql);
318 0 ▶ $sth->execute( values(%$values) ) or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(values %$values) or die $sth->errstr 0 0 0
319  
320 0 ▶ if ( !$dbh->{'AutoCommit'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dbh->{"AutoCommit"}) 0 0 0
321 0 ▶ $dbh->commit or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->commit or die $dbh->errstr 0 0 0
322 }
323  
324 #if ($_TRACK_JSON[0] ne "no") { push (@_TRACK_JSON, $values) };
325 }
326  
327 # Name of the function that recorded the event. The Tracking package and its
328 # signal-handler closures are skipped, so the caller's code is named instead.
329 0 ▶sub _caller_name {
Subroutine _caller_name: not called CC=7 SCAR=40.3
330 0 for my $depth ( 1 .. 10 ) {
331 0 my @frame = caller($depth);
332 0 ▶ last unless @frame;
Branch: 1 branch
BranchTrueFalseTotal
unless @frame 0 0 0
333 0 ▶ my $name = $frame[3] // '';
Condition0%0/2
$frame[3] // ""
l!l
00
MC/DC0%0/1
$frame[3] // ""
$frame[3]
Truth table0%0/2
A
A= $frame[3]
Aresult
11
00
334 0 ▶ next if $name eq '' || $name =~ /__ANON__/;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" or $name =~ /__ANON__/ 0 0 0
Condition0%0/3
$name eq "" or $name =~ /__ANON__/
l!l&&r!l&&!r
000
MC/DC0%0/2
$name eq "" or $name =~ /__ANON__/
$name eq "" $name =~ /__ANON__/
Truth table0%0/3
A or B
A= $name eq ""B= $name =~ /__ANON__/
ABresult
1X1
011
000
335 0 ▶ next if $name =~ /^oEdtk::Tracking::/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^oEdtk::Tracking::/ 0 0 0
336 0 return $name;
337 }
338 0 return 'main';
339 }
340  
341 0 ▶sub set_entity {
Subroutine set_entity: not called CC=3 SCAR=24.8
Pod documented
342 0 my ( $self, $entity ) = @_;
343  
344 0 ▶ if ( !defined($entity) || length($entity) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $entity or length $entity == 0) 0 0 0
Condition0%0/3
not defined $entity or length $entity == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $entity or length $entity == 0
not defined $entity length $entity == 0
Truth table0%0/3
not(A) or B
A= not defined $entityB= length $entity == 0
ABresult
1X1
011
000
345 0 warn "INFO: Tracking::set_entity() called with an undefined entity!\n";
346 0 return;
347 }
348  
349 # warn "INFO: translate >$entity< \n";
350 0 $entity = $self->{'EDTK_TRACK_DICT'}->translate($entity);
351 0 $self->{'entity'} = $entity;
352  
353 # warn $self->{'entity'}. " \$self->{'entity'}\n";
354 }
355  
356 0 ▶sub end {
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
357 0 my $self = shift;
358 0 $self->track( 'Halt', 1 );
359 }
360  
361 # For each application, for each legal entity, and for each week
362 # the number of documents in the tracking.
363 0 ▶sub stats_week {
Subroutine stats_week: not called CC=4 SCAR=30.0
Pod documented
364  
365 # pass the options as hash keys...
366 0 my ( $dbh, $cfg, $start, $end, $excluded_users ) = @_;
367  
368 0 my $table = $cfg->{'EDTK_STATS_TRACKING'};
369 0 my $innersql =
370 "SELECT ED_CORP, ED_APP, "
371 . "'S' || TO_CHAR(TO_DATE(ED_TSTAMP, 'YYYYMMDDHH24MISS'), 'IW') AS ED_WEEK "
372 . "FROM $table "
373 . "WHERE ED_JOB_EVT = 'D' AND ED_TSTAMP >= ? ";
374 0 my @vals = ($start);
375 0 ▶ if ( defined($end) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end) 0 0 0
376 0 $innersql .= " AND ED_TSTAMP <= ? ";
377 0 push( @vals, $end );
378 }
379  
380 0 ▶ if ( defined $excluded_users ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $excluded_users) 0 0 0
381 0 my @excluded = split( /,\s*/, $excluded_users );
382 0 ▶ for ( my $i = 0 ; $i <= $#excluded ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#excluded) 0 0 0
383 0 $innersql .= " AND ED_USER != ? ";
384 }
385 0 push( @vals, @excluded );
386 }
387  
388 0 my $sql = "SELECT i.ED_CORP, i.ED_APP, i.ED_WEEK, COUNT(*) AS ED_COUNT "
389 . "FROM ($innersql) i GROUP BY ED_CORP, ED_APP, ED_WEEK ";
390  
391 # warn "\nINFO: $sql \n";
392 #SELECT i.ED_CORP, i.ED_APP, i.ED_WEEK, COUNT(*) AS ED_COUNT
393 # FROM (
394 # SELECT ED_CORP, ED_APP, 'S' || TO_CHAR(TO_DATE(ED_TSTAMP, 'YYYYMMDDHH24MISS'), 'IW') AS ED_WEEK
395 # FROM EDTK_TRACKING_2010 WHERE ED_JOB_EVT = 'D' AND ED_TSTAMP >= '20101212'
396 # ) i
397 # GROUP BY ED_CORP, ED_APP, ED_WEEK;
398  
399 0 my $rows = $dbh->selectall_arrayref( $sql, { Slice => {} }, @vals );
400  
401 # use Data::Dumper;
402 # print Dumper($rows);
403 # {
404 # 'ED_COUNT' => '4',
405 # 'ED_APP' => 'FUS-AC007',
406 # 'ED_CORP' => 'CPLTR',
407 # 'ED_WEEK' => 'S51'
408 # },
409  
410 0 return $rows;
411 }
412  
413 0 ▶sub stats_iddest {
Subroutine stats_iddest: not called CC=5 SCAR=34.0
Pod documented
414  
415 # pass the options as hash keys...
416 0 my ( $dbh, $cfg, $start, $end, $excluded_users, $ed_app ) = @_;
417  
418 0 my $table = $cfg->{'EDTK_STATS_TRACKING'};
419 0 my $innersql =
420 "SELECT ED_CORP, ED_K1_VAL AS ED_EMET, ED_K0_VAL AS ED_IDDEST, ED_APP, "
421 . "'S' || TO_CHAR(TO_DATE(ED_TSTAMP, 'YYYYMMDDHH24MISS'), 'IW') AS ED_WEEK "
422 . "FROM $table "
423 . "WHERE ED_JOB_EVT = 'D' AND ED_TSTAMP >= ? ";
424 0 my @vals = ($start);
425 0 ▶ if ( defined($end) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end) 0 0 0
426 0 $innersql .= " AND ED_TSTAMP <= ? ";
427 0 push( @vals, $end );
428 }
429  
430 0 ▶ if ( defined $excluded_users ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $excluded_users) 0 0 0
431 0 my @excluded = split( /,\s*/, $excluded_users );
432 0 ▶ for ( my $i = 0 ; $i <= $#excluded ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#excluded) 0 0 0
433 0 $innersql .= " AND ED_USER != ? ";
434 }
435 0 push( @vals, @excluded );
436 }
437  
438 0 ▶ if ( defined $ed_app ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ed_app) 0 0 0
439 0 $innersql .= " AND ED_APP = ? ";
440 0 push( @vals, $ed_app );
441 }
442  
443 0 my $sql = "SELECT i.ED_CORP, i.ED_EMET, i.ED_IDDEST, i.ED_APP, i.ED_WEEK, COUNT(*) AS ED_COUNT "
444 . "FROM ($innersql) i GROUP BY i.ED_CORP, i.ED_EMET, i.ED_IDDEST, i.ED_APP, i.ED_WEEK ";
445  
446 # warn "INFO: $sql \n";
447 # warn "INFO: @vals \n";
448 # SELECT i.ED_CORP, i.ED_SECTION, i.ED_IDDEST, i.ED_APP, i.ED_WEEK, COUNT(*) AS ED_COUNT
449 # FROM (
450 # SELECT ED_CORP, ED_K1_VAL AS ED_SECTION, ED_K0_VAL AS ED_IDDEST, ED_APP,
451 # 'S' || TO_CHAR(TO_DATE(ED_TSTAMP, 'YYYYMMDDHH24MISS'), 'IW') AS ED_WEEK
452 # FROM EDTK_TRACKING_2010 WHERE ED_JOB_EVT = 'D' AND ED_TSTAMP >= ?
453 # AND ED_TSTAMP <= ? AND ED_USER != ? AND ED_APP = ? ) i
454 # GROUP BY i.ED_CORP, i.ED_SECTION, i.ED_IDDEST, i.ED_APP, i.ED_WEEK
455  
456 0 my $rows = $dbh->selectall_arrayref( $sql, { Slice => {} }, @vals );
457  
458 # use Data::Dumper;
459 # print Dumper($rows);
460 # {
461 # 'ED_COUNT' => '2',
462 # 'ED_APP' => 'CTP-AC001',
463 # 'ED_IDDEST' => '0000428193',
464 # 'ED_CORP' => 'CORP_1',
465 # 'ED_WEEK' => 'S50',
466 # 'ED_EMET' => 'P004'
467 # },
468  
469 0 return $rows;
470 }
471  
472 # For each application, for each E.R., for each legal entity
473 # and for each month, the number of documents in the tracking.
474 0 ▶sub stats_month {
Subroutine stats_month: not called CC=4 SCAR=30.0
Pod documented
475 0 my ( $dbh, $cfg, $start, $end, $excluded_users ) = @_;
476  
477 0 my $table = $cfg->{'EDTK_STATS_TRACKING'};
478 0 my $innersql =
479 "SELECT ED_APP, ED_CORP, ED_K1_VAL AS ED_EMET, "
480 . "'M' || TO_CHAR(TO_DATE(ED_TSTAMP, 'YYYYMMDDHH24MISS'), 'MM') AS ED_MONTH "
481 . "FROM $table WHERE ED_JOB_EVT = 'D' AND ED_TSTAMP >= ? "; # AND ED_K1_NAME = 'SECTION'
482 0 my @vals = ($start);
483  
484 0 ▶ if ( defined($end) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end) 0 0 0
485 0 $innersql .= " AND ED_TSTAMP <= ? ";
486 0 push( @vals, $end );
487 }
488  
489 0 ▶ if ( defined $excluded_users ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $excluded_users) 0 0 0
490 0 my @excluded = split( /,\s*/, $excluded_users );
491 0 ▶ for ( my $i = 0 ; $i <= $#excluded ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#excluded) 0 0 0
492 0 $innersql .= " AND ED_USER != ? ";
493 }
494 0 push( @vals, @excluded );
495 }
496  
497 0 my $sql = "SELECT i.ED_APP, i.ED_CORP, i.ED_EMET, i.ED_MONTH, COUNT(*) AS ED_COUNT "
498 . "FROM ($innersql) i GROUP BY ED_APP, ED_CORP, ED_EMET, ED_MONTH ";
499  
500 0 my $rows = $dbh->selectall_arrayref( $sql, { Slice => {} }, @vals );
501  
502 # use Data::Dumper;
503 # print Dumper($rows);
504 # 'ED_MONTH' => 'M12',
505 # 'ED_COUNT' => '1',
506 # 'ED_CORP' => 'CORP_1',
507 # 'ED_APP' => 'DEV-CAMELEON',
508 # 'ED_EMET' => '37043'
509  
510 0 return $rows;
511 }
512  
513 #my $_PRGNAME;
514  
515 0 ▶sub _validate_event {
Subroutine _validate_event: not called CC=5 SCAR=34.0
516  
517 # Job Event : looking for one of the following :
518 # Job (default), Spool, Document, Line, Warning, Error, Halt (critic), Reject
519 0 my $job = shift;
520  
521 0 ▶ warn "INFO: Halt event in Tracking = $job\n" if ( $job =~ /^H/ );
Branch: 1 branch
BranchTrueFalseTotal
if $job =~ /^H/ 0 0 0
522 0 ▶ if ( !defined $job || $job !~ /^([JSDLWEHR])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $job and $job =~ /^([JSDLWEHR])/) 0 0 0
Condition0%0/3
defined $job and $job =~ /^([JSDLWEHR])/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $job and $job =~ /^([JSDLWEHR])/
defined $job $job =~ /^([JSDLWEHR])/
Truth table0%0/3
A and B
A= defined $jobB= $job =~ /^([JSDLWEHR])/
ABresult
0X0
100
111
523 0 ▶ die "ERROR: Invalid job event : "
Branch: 1 branch
BranchTrueFalseTotal
defined $job ? : 0 0 0
524 . ( defined $job ? $job : '(undef)' ) . "\n"
525 . "\t valid events are : Job / Spool / Document / Line / Warning / Reject / Error / Halt (critic)\n";
526 }
527 0 return $1;
528 }
529  
530 #{
531 #my $_edmode;
532 #
533 # sub display_edmode {
534 # if (!defined $_edmode) {
535 # $_edmode = _validate_edmode(shift);
536 # }
537 # return $_edmode;
538 # }
539  
540 0 ▶sub _validate_edmode {
Subroutine _validate_edmode: not called CC=3 SCAR=24.8
541  
542 # Printing Mode : looking for one of the following :
543 # Undef (default), Batch, Tp, Web, Mail, probinG
544 0 my $edmode = shift;
545  
546 0 ▶ if ( !defined $edmode || $edmode !~ /^([BTMWG])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $edmode and $edmode =~ /^([BTMWG])/) 0 0 0
Condition0%0/3
defined $edmode and $edmode =~ /^([BTMWG])/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $edmode and $edmode =~ /^([BTMWG])/
defined $edmode $edmode =~ /^([BTMWG])/
Truth table0%0/3
A and B
A= defined $edmodeB= $edmode =~ /^([BTMWG])/
ABresult
0X0
100
111
547 0 return 'U';
548 }
549 0 return $1;
550 }
551  
552 #}
553  
554 #END {
555 # #if ($_TRACK_TRK_JSON !~/no/i) {
556 # if ($_TRACK_JSON[0] ne "no") {
557 # my $json = JSON->new;
558 # #print $json->encode(\@_TRACK_JSON);
559 # }
560 #}
561  
562 1 1;
563  
564 =head1 NAME
565  
566 oEdtk::Tracking2 - legacy warn-based production tracking
567  
568 =head1 SYNOPSIS
569  
570 use oEdtk::Tracking;
571  
572 my $trk = oEdtk::Tracking->new($source, entity => 'Corp');
573 $trk->track('Job', 1, @values);
574 $trk->end;
575  
576 =head1 DESCRIPTION
577  
578 Legacy, warn-based duplicate of L<oEdtk::Tracking>. It declares the same package
579 C<oEdtk::Tracking> and the same public interface, but reports its diagnostics
580 through C<warn> instead of the logger, and reads the user keys from the
581 configuration rather than from the constructor arguments. It is kept for
582 reference; use L<oEdtk::Tracking> for new code.
583  
584 =head1 METHODS
585  
586 =head2 new
587  
588 my $trk = oEdtk::Tracking->new($source, %params);
589  
590 Build a tracker. C<$source> names the input, and C<%params> may carry C<entity>,
591 C<edmode>, C<account>, C<obj_typ>, C<child_typ>, C<child_id> and C<parent_id>.
592 An omitted C<$source> falls back to C<@ARGV>; an explicit empty string means no
593 input file and is kept. The user keys come from the C<EDTK_TRACKING_KEYS>
594 configuration. When C<EDTK_TRACK_MODE> is C<NONE> an inert object is returned.
595  
596 =head2 track
597  
598 $trk->track($job, $count, @data);
599  
600 Record one event: the job event, the number of objects, and the values of the
601 configured user keys (an optional trailing value is the message). When no
602 message is supplied, C<ED_MESSAGE> names the function that recorded the event.
603  
604 =head2 set_entity
605  
606 $trk->set_entity($entity);
607  
608 Normalise C<$entity> through the dictionary and store it.
609  
610 =head2 end
611  
612 $trk->end;
613  
614 Record the final C<Halt> event.
615  
616 =head1 FUNCTIONS
617  
618 =head2 stats_iddest
619  
620 my $rows = stats_iddest($dbh, $cfg, $start, $end, $excluded_users, $ed_app);
621  
622 Per recipient destination, per week, the number of documents tracked between
623 C<$start> and C<$end>.
624  
625 =head2 stats_week
626  
627 my $rows = stats_week($dbh, $cfg, $start, $end, $excluded_users);
628  
629 Per application, per legal entity and per ISO week, the number of documents
630 tracked during the period.
631  
632 =head2 stats_month
633  
634 my $rows = stats_month($dbh, $cfg, $start, $end, $excluded_users);
635  
636 Per application, per legal entity and per month, the number of documents tracked
637 during the period.
638  
639 The following subs are internal helpers and are not part of the public API:
640 C<_validate_event> and C<_validate_edmode>.
641  
642 =head1 AUTHOR
643  
644 The oEdtk team.
645  
646 =cut
« lib/oEdtk/Run.pm ↑ Summary lib/oEdtk/DBAdmin.pm »