rplantiko

Find time gaps in job logs

Dec 15th, 2020 (edited)
873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.72 KB | None | 0 0
  1. # Find time gaps in a joblog, sorting by time differences between successive records
  2. # Entries of the Job Log are of the form
  3. #
  4. # |15.12.2020|04:07:17|Datei ATRON_STAMMDATEN.CSV enthält 720 Fehler; folgende "Arten" (Alle=ZAFM_LIEF_STAT1): | ZAFM | 156 | S |
  5. # |15.12.2020|04:07:17|720 Fehler mit ZAFM-011: Doppelter GTIN & bei Lieferant & Zeilennummer &.               | ZAFM | 157 | S |
  6. # |15.12.2020|04:47:22|Insgesamt wurden 158757 Zeilen abgespeichert aus Datei ATRON_STAMMDATEN.CSV.            | ZAFM | 009 | S |
  7. #
  8. # Log lines which don't start with date and time like these are ignored
  9.  
  10. use DateTime;
  11.  
  12. use constant {
  13. # Constant filename:
  14. # Use fixed filename, or $ARGV[0] from the command line
  15.   JOBLOG_FILENAME    => $ARGV[0] || "C:\\Temp\\joblog.txt",
  16. # Show only the top 20 time gap entries:  
  17.   TOP                => $ARGV[1] || 20,
  18. # Change the regex DATE_TIME_PATTERN to catch the date and time for different log formats:
  19.   DATE_TIME_PATTERN  => qr/^\|(\d{2})\.(\d{2})\.(\d{4})\|(\d{2}):(\d{2}):(\d{2})/
  20. };
  21.  
  22. open JOBLOG, "<", JOBLOG_FILENAME or die "Can't open ${ \JOBLOG_FILENAME }: $!\n";
  23.  
  24. my @log = ();
  25. my $last_time = 0;
  26. foreach my $line ( <JOBLOG> ) {
  27.   next unless my @dt = ( $line =~ DATE_TIME_PATTERN );
  28.   my $this_time = new DateTime(
  29.     day    => $dt[0],
  30.     month  => $dt[1],
  31.     year   => $dt[2],
  32.     hour   => $dt[3],
  33.     minute => $dt[4],
  34.     second => $dt[5]
  35.   );
  36.   if ($last_time) {
  37.     push @log, [ $this_time->epoch - $last_time->epoch, $line ];
  38.   }
  39.   $last_time = $this_time;
  40. }
  41.  
  42. # In Perl, "@a = sort @a" sorts @a inplace internally (no copying):
  43. @log = sort { $b->[0] <=> $a->[0] } @log;
  44.  
  45. foreach $a (@log[0..TOP-1]) {
  46.   printf( "%5d %s", $a->[0], $a->[1] );
  47. }
  48.  
  49. close JOBLOG;
Add Comment
Please, Sign In to add comment