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