root/lang/perl/Log-Analyze/trunk/lib/Log/Analyze.pm @ 17777

Revision 17777, 3.1 kB (checked in by miki, 6 years ago)

import Log::Analyze

Line 
1package Log::Analyze;
2
3use strict;
4use warnings;
5use Carp;
6
7our $VERSION = '0.00002';
8
9sub new {
10    my $class = shift;
11
12    return bless {
13        tree      => {},
14        matrix    => [],
15        node_name => [],
16    }, $class;
17}
18
19sub analyze {
20    my $self    = shift;
21    my $nodes   = shift;
22    my $command = shift;
23
24    $command ||= 'count';
25    my $eval = q|$self->{tree}|;
26    for (@$nodes) {
27        $_ ||= "";
28        $eval .= q|->{'| . $_ . q|'}|;
29    }
30    if ( $command eq 'count' ) {
31        $eval .= q|++|;
32    }
33    if ( $command eq 'sum' ) {
34        my $num = shift;
35        if ( !$num ) {
36            carp("'sum' needs one numeric parameter");
37            $num = 0;
38        }
39        $eval .= q|+=| . $num;
40    }
41    if ( ref($command) eq 'CODE' ) {
42        my $result = $command->( $nodes, shift );
43        $result = quotemeta($result);
44        $eval .= q|='| . $result . q|'|;
45    }
46    eval $eval;
47}
48
49sub tree {
50    my $self = shift;
51    return $self->{tree};
52}
53
54sub matrix {
55    my $self = shift;
56    $self->_walk_tree( $self->{tree} );
57    my @sorted =
58      sort { join( ' ', @$a ) cmp join( ' ', @$b ) } @{ $self->{matrix} };
59    return \@sorted;
60}
61
62sub _walk_tree {
63    my $self = shift;
64    my $node = shift;
65
66    my $node_name = $self->{node_name};
67    while ( my ( $key, $value ) = each %$node ) {
68        push @$node_name, $key;
69        if ( ref($value) eq 'HASH' ) {
70            $self->_walk_tree($value);
71            pop @$node_name;
72        }
73        else {
74            push @{ $self->{matrix} }, [ @$node_name, $value ];
75            pop @$node_name;
76        }
77    }
78}
79
801;
81__END__
82
83=head1 NAME
84
85Log::Analyze -
86
87=head1 SYNOPSIS
88
89=head2 default pattern(count up)
90
91  use Log::Analyze;
92 
93  my $parser = Log::Analyze->new;
94 
95  #----------------
96  # count
97  #----------------
98  while(<LOG>){
99      chomp $_;
100      my @f = split(/\t/, $_);
101      $parser->analyze([$f[1],$[2],$f[3]...], "count");
102  }
103 
104  my $hash_ref  = $parser->tree;
105  my $array_ref = $parser->matrix;
106
107=head2 sum pattern(count up)
108
109  #----------------
110  # sum
111  #----------------
112  while(<LOG>){
113      chomp $_;
114      my @f = split(/\t/, $_);
115      $parser->analyze([$f[1],$[2],$f[3]...], "sum" => $f[10]);
116  }
117
118=head2 custom pattern(set coderef)
119
120  #----------------
121  # custom 
122  #----------------
123  while(<LOG>){
124      chomp $_;
125      my @f = split(/\t/, $_);
126      $parser->analyze([$f[1],$[2],$f[3]...], $coderef => $argsref);
127  }
128 
129  $code = sub {
130      my $tree_data = shift;
131      my $args_ref  = shift;
132      ....
133  };
134
135=head1 DESCRIPTION
136
137Log::Analyze is simple log analysis module.
138
139Usually, a task of log analysis is simply "count" the records,
140or "sum" the value of a particular field in every log records.
141
142Furthermore, you sometimes expect more difficult practice that become your custom code.
143
144It makes these tasks very simple.
145
146=head1 METHODS
147
148=head2 new()
149
150=head2 analyze()
151
152=head2 tree()
153
154=head2 matrix()
155
156=head1 AUTHOR
157
158takeshi miki E<lt>miki@cpan.orgE<gt>
159
160=head1 LICENSE
161
162This library is free software; you can redistribute it and/or modify
163it under the same terms as Perl itself.
164
165=head1 SEE ALSO
166
167=cut
Note: See TracBrowser for help on using the browser.