Can you simplify the following:
sub LastDalLoad { my $ContentsLoadMsg = shift; my $logFile = "$ENV{ MIS_ROOT }/log/loadmsg_1.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 status of MIS as stated in mistrap.log # 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 }; while ( $indLines > -1 and ( length( $finishedLine ) == 0 or length( $beginningLine + ) == 0 ) ) { if( ${ $ContentsLoadMsg }[ $indLines ] =~ /The utility has fi +nished/ ) { $finishedLine = ${ $ContentsLoadMsg }[ $indLines ] . " " +. ${ $ContentsLoadMsg }[ $indLines + 1 ]; } elsif( ${ $ContentsLoadMsg }[ $indLines ] =~ /The utility is +beginning/ ) { $beginningLine = ${ $ContentsLoadMsg }[ $indLines ] . " " + . ${ $ContentsLoadMsg } [ $indLines + 1 ]; } $indLines --; } # # last load # @beginningTime = &getLineParts( $beginningLine, 4, 4, "\"" ); @finishedTime = &getLineParts( $finishedLine, 4, 4, "\"" ); # # get actual day and time # ( $day, $month, $year ) = &getActualDate; ( $hour, $minute, $second ) = &getActualTime; # get max seconds of a day $maxSecondsAnHour = &getTimeInSeconds( 23, 59, 59 ) + 1; # # get values of DAL load start : # # get pure date part @datePart = &getLineParts( $beginningTime[0], 1, 2 ); @checkDate = &getLineParts( $datePart[0], 1, 3, "-" ); # get pure time part @checkTime = &getLineParts( $datePart[1], 1, 1, "." ); @checkTime = &getLineParts( $checkTime[0], 1, 3, ":" ); # get time of DAL load start in seconds $beginningInSeconds = &getTimeInSeconds( $checkTime[0], $checkTim +e[1], $checkTime[2] ); # get actual time in seconds $actualInSeconds = &getTimeInSeconds( $hour, $minute, $second +); # get difference in days of actual day and last DAL file load st +art $Command = "db2 \"values (julian_day('$year-$month-$day') - "; $Command .= " julian_day('$checkDate[2]-$checkDate[0 +]-$checkDate[1]'))\" "; if( &executeSQL( $Command, \@Result ) == $FALSE ) { &ResCode("E_DB2_ERROR"); &BrowseCode("It was not possible to execute an SQL command"); 
 &SummaryCode("E_MISLOAD_ERROR"); return $FALSE; } # get the value of the previous SQL call for days difference $startDiff = &getFirstNonBlank( $Result[3] ); # is DAL file from same day if( $startDiff == 0 ) { $timeDiff = $actualInSeconds - $beginningInSeconds; # have more than MIS_MAX_DALLOADTIMEPASS_H hours gone after +the last DAL files was l oaded if( $timeDiff > &getTimeInSeconds( $Configuration{ MIS_MAX_DA +LLOADTIMEPASS_H }, 0, 0 ) ) { &ResCode("WARNING"); &BrowseCode("Last DAL load operation was started more tha +n $Configuration{ MIS_MA X_DALLOADTIMEPASS_H } hours ago"); $status = $WARNING; } else { &ResCode("OK"); &BrowseCode("Last DAL load operation started at @beginnin +gTime"); } } # is DAL file from yesterday ( check if time difference over mid +night is within range ) elsif( $startDiff == 1 ) { $timeDiff = $maxSecondsAnHour - $beginningInSeconds; $timeDiff += $actualInSeconds; # have more than MIS_MAX_DALLOADTIMEPASS_H hours gone after +the last DAL files was l oaded if( $timeDiff > &getTimeInSeconds( $Configuration{ MIS_MAX_DA +LLOADTIMEPASS_H }, 0, 0 ) ) { &ResCode("WARNING"); &BrowseCode("Last DAL load operation was started more tha +n $Configuration{ MIS_MAX_DALLOADTIMEPASS_H } hours ago"); $status = $WARNING; } else { &ResCode("OK"); &BrowseCode("Last DAL load operation started at @beginnin +gTime"); } } # DAL file is more than one day old else { &ResCode("WARNING"); &BrowseCode("Last DAL load operation started at @beginningTim +e"); $status = $WARNING; } # # get values of DAL load end # # get pure date part @datePart = &getLineParts( $finishedTime[0], 1, 2 ); @checkDate = &getLineParts( $datePart[0], 1, 3, "-" ); # get pur time part @checkTime = &getLineParts( $datePart[1], 1, 1, "." ); @checkTime = &getLineParts( $checkTime[0], 1, 3, ":" ); # get time of DAL load start in seconds $finishedInSeconds = &getTimeInSeconds( $checkTime[0], $checkTime +[1], $checkTime[2] ); # get difference in days of actual day and last DAL file load st +art $Command = "db2 \"values (julian_day('$year-$month-$day') - "; $Command .= " julian_day('$checkDate[2]-$checkDate[0 +]-$checkDate[1]'))\" "; if( &executeSQL( $Command, \@Result ) == $FALSE ) { &ResCode("E_DB2_ERROR"); &BrowseCode("It was not possible to execute an SQL command"); 
 &SummaryCode("E_MISLOAD_ERROR"); return $FALSE; } # get the value of the previous SQL call for days difference $startDiff = &getFirstNonBlank( $Result[3] ); # is DAL file from same day if( $startDiff == 0 ) { $timeDiff = $actualInSeconds - $finishedInSeconds; # have more than MIS_MAX_DALLOADTIMEPASS_H hours gone after +the last DAL files was l oaded if( $timeDiff > &getTimeInSeconds( $Configuration{ MIS_MAX_DA +LLOADTIMEPASS_H }, 0, 0 ) ) { &ResCode("WARNING"); &BrowseCode("Last DAL load operation ended more than $Con +figuration{ MIS_MAX_DALLOADTIMEPASS_H } hours ago"); $status = $WARNING; } else { &ResCode("OK"); &BrowseCode("Last DAL load operation ended at @finishedTi +me"); } } # is DAL file from yesterday ( check if time difference over mid +night is within range ) elsif( $startDiff == 1 ) { $timeDiff = $maxSecondsAnHour - $finishedInSeconds; $timeDiff += $actualInSeconds; # have more than MIS_MAX_DALLOADTIMEPASS_H hours gone after +the last DAL files was loaded if( $timeDiff > &getTimeInSeconds( $Configuration{ MIS_MAX_DA +LLOADTIMEPASS_H }, 0, 0 ) ) { &ResCode("WARNING"); &BrowseCode("Last DAL load operation ended more than $Con +figuration{ MIS_MAX_DALLOADTIMEPASS_H } hours ago"); $status = $WARNING; } else { &ResCode("OK"); &BrowseCode("Last DAL load operation ended at @finishedTi +me"); } } # DAL file is more than one day old else { &ResCode("WARNING"); &BrowseCode("Last DAL load operation ended at @finishedTime") +; $status = $WARNING; } 
 if( $status == $TRUE ) { &SummaryCode("OK"); } elsif( $status == $WARNING ) { &SummaryCode("WARNING"); } else { &SummaryCode("E_MISLOAD_ERROR"); } return $status; }
Update Oops, should post data sample maybe. The point of the exercise appears to be, get the last timestamp that a file was loaded into the database, and check its not older than MIS_MAX_DALLOADTIMEPASS_H hours. Sample data:
Update2 In case it wasnt clear, I didnt write this thing originally, I just maintain it.. Which means only change stuff if a) not working, or b) needs new functionality. This means, in particular, that although I would like to go through and remove all the junk like $TRUE, FileExists and other nonsensical pieces, I won't be given the time unless some customer needs it rewritten for whatever reason. (We are currently understaffed as a group of two, and booked up until August or so..)SQL3109N The utility is beginning to load data from file "/home/mis/jer/tMIS/MIS.05.23-TIX2/dalfiles/loadfiles//20050404_120000 +_". SQL3500W The utility is beginning the "LOAD" phase at time "04-29-200 +5 12:26:10.994507". SQL3112W There are fewer input file columns specified than database c +olumns. SQL3519W Begin Load Consistency Point. Input record count = "5". SQL3520W Load Consistency Point was successful. SQL3110N The utility has completed processing. "7" rows were read fr +om the input file. SQL3519W Begin Load Consistency Point. Input record count = "7". SQL3520W Load Consistency Point was successful. SQL3515W The utility has finished the "LOAD" phase at time "04-29-200 +5 12:26:11.761926".
C.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Code refactoring challenge
by merlyn (Sage) on Apr 29, 2005 at 16:11 UTC | |
|
Re: Code refactoring challenge
by dragonchild (Archbishop) on Apr 29, 2005 at 17:43 UTC | |
|
Re: Code refactoring challenge
by tilly (Archbishop) on Apr 29, 2005 at 18:39 UTC | |
|
Re: Code refactoring challenge
by castaway (Parson) on Apr 29, 2005 at 16:02 UTC | |
|
Re: Code refactoring challenge
by cazz (Pilgrim) on Apr 29, 2005 at 17:53 UTC | |
by castaway (Parson) on Apr 30, 2005 at 08:42 UTC | |
|
Re: Lawn refactoring challenge
by Anonymous Monk on May 01, 2005 at 00:24 UTC | |
|
Re: Code refactoring challenge
by Anonymous Monk on Apr 29, 2005 at 21:51 UTC | |
by castaway (Parson) on Apr 30, 2005 at 08:45 UTC | |
by Anonymous Monk on Apr 30, 2005 at 14:58 UTC |