/[Frey]/trunk/lib/Frey/Shell/Log.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/Shell/Log.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 894 by dpavlin, Wed Dec 24 21:42:20 2008 UTC revision 902 by dpavlin, Thu Dec 25 14:01:40 2008 UTC
# Line 4  use Moose; Line 4  use Moose;
4  extends 'Frey';  extends 'Frey';
5  with 'Frey::Web';  with 'Frey::Web';
6    
7  use DateTimeX::Easy;  use DateTime::Locale;
8    
9  has log_command => (  has log_command => (
10          is => 'rw',          is => 'rw',
# Line 23  has to_datetime => ( Line 23  has to_datetime => (
23          isa => 'Str',          isa => 'Str',
24  );  );
25    
26    # we don't use DateTime because it's huge overhead for every log line
27    my $month2nr;
28    {
29            my @months = @{ DateTime::Locale->load('en_US')->month_abbreviations };
30            warn "# months ", join(',',@months);
31    
32            foreach my $i ( 0 .. $#months ) {
33                    $month2nr->{ lc($months[$i]) } = $i + 1;
34            }
35    }
36    
37  sub as_sponge {  sub as_sponge {
38          my ($self) = @_;          my ($self) = @_;
39    
# Line 31  sub as_sponge { Line 42  sub as_sponge {
42    
43          my @rows;          my @rows;
44    
45          my $from = DateTimeX::Easy->parse( $self->from_datetime ) if $self->from_datetime;          my $from = $self->from_datetime;
46          my $to   = DateTimeX::Easy->parse( $self->to_datetime   ) if $self->to_datetime;          my $to   = $self->to_datetime;
47    
48          warn "# from: $from" if $from;          my $stats = {
49          warn "# to: $to" if $to;                  from => $from,
50                    to   => $to,
51            };
52    
53            warn "# stats ",$self->dump( $stats );
54    
55            my $yyyy = (localtime(time))[5] + 1900;
56    
57          open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!";          open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!";
58          while(<$fh>) {          while(<$fh>) {
# Line 43  sub as_sponge { Line 60  sub as_sponge {
60    
61                  if ( m{^(\w\w\w) (\d\d) (\d\d:\d\d:\d\d) (.+)} ) {                  if ( m{^(\w\w\w) (\d\d) (\d\d:\d\d:\d\d) (.+)} ) {
62                          my $message = $4;                          my $message = $4;
63                          my $dt = DateTimeX::Easy->parse("2008-$1-$2 $3");                          my $mm = $month2nr->{lc($1)} || die "month $1 unknown";
64                          if ( $from ) {                          my $dt = sprintf("%04d-%02d-%02d %s", $yyyy, $mm, $2, $3, $4);
65                                  next if $dt < $from;                          if ( $from && $dt lt $from ) {
66                                    $stats->{before_from}++;
67                                    next;
68                          }                          }
69                          if ( $to ) {                          if ( $to && $dt gt $to ) {
70                                  next if $dt > $to;                                  $stats->{after_to}++;
71                                    next;
72                          }                          }
73                          push @rows, [ $dt->ymd . ' ' . $dt->hms, $message ];                          push @rows, [ $dt, $message ];
74                            $stats->{rows}++;
75                  } else {                  } else {
76                          warn "# skip $_\n";                          warn "# skip $_\n";
77                            $stats->{skipped}++;
78                  }                  }
79          }          }
80    
81            warn "# stats ",$self->dump( $stats );
82    
83          return {          return {
84                  NAME => [ 'date', 'message' ],                  NAME => [ 'date', 'message' ],
85                  rows => \@rows,                  rows => [ sort { $a->[0] cmp $b->[0] } @rows ],
86                    stats => $stats,
87          }          }
88  }  }
89    

Legend:
Removed from v.894  
changed lines
  Added in v.902

  ViewVC Help
Powered by ViewVC 1.1.26