in reply to Code refactoring challenge
sub LastDalLoad { my $ContentsLoadMsg = shift; # setting log/sdf_misloader1.log may fail if SDF_LOGDIR is set ? $logFile = !$MISValue{ MIS_DBTYPE } || $MISValue{ MIS_DBTYPE } eq + 'db2' ? $logFile = "$ENV{ MIS_ROOT }/log/loadmsg_1.log" : $logFile = "$ENV{ MIS_ROOT }/log/sdf_misloader1.log"; my @Result; my $Command; my $indLines; my $finishedLine; my $beginningLine; my $found; my @beginningTime; my @finishedTime; my $maxSecondsAnHour; my @datePart; my @checkDate; my @checkTime; my $beginningInSeconds; my $finishedInSeconds; my $actualInSeconds; my $timeDiff; my $startDiff; my $status = $TRUE; # # Check the existance of the logfile # if( ( $statusLoadmsg = &FileExists( $logFile ) ) == $FALSE ) { &ResCode("WARNING"); &BrowseCode("File \"$logFile\" not found"); 
 &SummaryCode("WARNING"); return $WARNING; } # # read whole log file # if( &readFileContents( $logFile, $ContentsLoadMsg ) == $FALSE ) { &ResCode("E_LOADMSG_ERROR"); &BrowseCode("Could not open \"$logFile\""); 
 &SummaryCode("E_MISLOAD_ERROR"); return $FALSE; } # # find out last load # $finishedLine = ""; $beginningLine = ""; $indLines = $#{ $ContentsLoadMsg }; if(!$MISValue{ MIS_DBTYPE } || $MISValue{ MIS_DBTYPE } eq 'db2') { $beginload = 'The utility is beginning'; $endload = 'The utility has finished'; } else { $beginload = 'SDFMISLD01'; $endload = 'SDFMISLD02'; } foreach $ind (reverse(0 .. $#{ $ContentsLoadMsg })) { last if($finishedLine && $beginningLine ); if( ${ $ContentsLoadMsg }[ $ind ] =~ /\Q$endload\E/ ) { $finishedLine = ${ $ContentsLoadMsg }[ $ind ] . " " . ${ +$ContentsLoadMsg }[ $ind + 1 ]; } elsif( ${ $ContentsLoadMsg }[ $ind ] =~ /\Q$beginload\E/ ) { $beginningLine = ${ $ContentsLoadMsg }[ $ind ] . " " . ${ + $ContentsLoadMsg }[ $in d + 1 ]; } } # # Last load # my ($bday, $bmon, $byear, $bhour, $bmin, $bsec); my ($eday, $emon, $eyear, $ehour, $emin, $esec); if(!$MISValue{ MIS_DBTYPE } || $MISValue{ MIS_DBTYPE } eq 'db2') { ($bday, $bmon, $byear, $bhour, $bmin, $bsec) = $beginningLine =~ /"(\d){2}-(\d){2}-(\d){4} (\d){2}:(\d){2} +:(\d){2}/; #" ($eday, $emon, $eyear, $ehour, $emin, $esec) = $finishedLine =~ /"(\d){2}-(\d){2}-(\d){4} (\d){2}:(\d){2}: +(\d){2}/; #" } else { ($byear, $bmon, $bday, $bhour, $bmin, $bsec) = $beginningLine =~ /^(.){10} (\d){4}-(\d){2}-(\d)}-(\d){2}\. +(\d){2}\.(\d){2}/; ($eyear, $emon, $eday, $ehour, $emin, $esec) = $finishedLine =~ /^(.){10} (\d){4}-(\d){2}-(\d)}-(\d){2}\.( +\d){2}\.(\d){2}/; } use Time::Local; my ($timebegin, $timeend); if(!$beginningLine) { $timebegin = 0; } elsif(!$beginningLine) { $timeend = 0; } else { $timebegin = timelocal($bsec, $bmin, $bhour, $bday, $bmon - 1 +, $byear); $timeend = timelocal($esec, $emin, $ehour, $eday, $emon - 1 +, $eyear); } my $bdiffhours = (time() - $timebegin)/3600; my $ediffhours = (time() - $timeend )/3600; if( $bdiffhours > $Configuration{ MIS_MAX_DALLOADTIMEPASS_H } ) { &ResCode("WARNING"); &BrowseCode("Last DAL load operation was started more than $C +onfiguration{ MIS_MAX_DA LLOADTIMEPASS_H } hours ago"); $status = $WARNING; } else { &ResCode("OK"); &BrowseCode("Last DAL load operation started at " . timelocal +($timebegin)); } if( $ediffhours > $Configuration{ MIS_MAX_DALLOADTIMEPASS_H } ) { &ResCode("WARNING"); &BrowseCode("Last DAL load operation ended more than $Configu +ration{ MIS_MAX_DALLOADT IMEPASS_H } hours ago"); $status = $WARNING; } else { &ResCode("OK"); &BrowseCode("Last DAL load operation ended at " . timelocal($ +timeend)); } 
 if( $status == $TRUE ) { &SummaryCode("OK"); } elsif( $status == $WARNING ) { &SummaryCode("WARNING"); } else { &SummaryCode("E_MISLOAD_ERROR"); } return $status; }
C.
|
|---|