/[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 901 by dpavlin, Thu Dec 25 12:19:25 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;
   
         sub date_time {  
                 my $dt = shift || return;  
                 $dt->ymd . ' ' . $dt->hms;  
         }  
47    
48          my $stats = {          my $stats = {
49                  from => date_time( $from ),                  from => $from,
50                  to   => date_time( $to ),                  to   => $to,
51          };          };
52    
53          warn "# stats ",$self->dump( $stats );          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>) {
59                  chomp;                  chomp;
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 && $dt < $from ) {                          my $dt = sprintf("%04d-%02d-%02d %s", $yyyy, $mm, $2, $3, $4);
65                            if ( $from && $dt lt $from ) {
66                                  $stats->{before_from}++;                                  $stats->{before_from}++;
67                                  next;                                  next;
68                          }                          }
69                          if ( $to && $dt > $to ) {                          if ( $to && $dt gt $to ) {
70                                  $stats->{after_to}++;                                  $stats->{after_to}++;
71                                  next;                                  next;
72                          }                          }
73                          push @rows, [ date_time( $dt ), $message ];                          push @rows, [ $dt, $message ];
74                          $stats->{rows}++;                          $stats->{rows}++;
75                  } else {                  } else {
76                          warn "# skip $_\n";                          warn "# skip $_\n";

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

  ViewVC Help
Powered by ViewVC 1.1.26