DS/Transformer/StatCounter.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 120 CC 9 SCAR 23.2
CC
9
Cov
0%
_update_counts
30.0
print
6.0
_print
6.0
CRAP 10.2
1 #!perl
2
3 # ########################################################################## #
4 # Title: Gather counts of selected fields
5 # Creation date: 2007-03-05
6 # Author: Michael Zedeler
7 # Description: Counts number of occurences of various field values
8 # Data Stream class
9 # Data transformer
10 # File: $Source: /data/cvs/lib/DSlib/lib/DS/Transformer/StatCounter.pm,v $
11 # Repository: kronhjorten
12 # State: $State: Exp $
13 # Documentation: inline
14 # Recepient: -
15 # ########################################################################## #
16
17 package DS::Transformer::StatCounter;
18
19 use base qw{ DS::Transformer };
20
21 use strict;
22 use Carp::Assert;
23 use IO::Handle;
24
25 our ($VERSION) = $DS::VERSION;
26 our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/;
27
28
29 sub new {
30 my ( $class, $typespec, $source, $count_config ) = @_;
31
32 my $self = $class->SUPER::new( $typespec, $source );
33
34 $self->{count_config} = $count_config;
35 $self->{stats} = {};
36
37 return $self;
38 }
39
40 sub process {
41 my( $self, $row ) = @_;
42
43 $self->_update_counts( $row, $self->{count_config}, $self->{stats} );
44
45 return $row;
46 }
47
48 sub _update_counts {
49 my ( $self, $row, $field_list, $stats ) = @_;
50
51 for( my $i = 0; $i <= $#$field_list; $i++ ) {
52
53 # Find out which field to process
54 my $field = ${$field_list}[$i];
55
56 # Create any missing data structures
57 # (yes - this could be done once and would speed up the processing a lot)
58 unless( defined $stats->{$field} ) {
59 $stats->{$field} = {};
60 }
61
62 unless( defined $stats->{$field}->{ $row->{$field} } ) {
63 $stats->{$field}->{ $row->{$field} } = { stats => { count => 0 } };
64 }
65
66 # This is where the actual statistics are being stored
67 $stats->{$field}->{ $row->{$field} }->{stats}->{count}++;
68
69 # If there are any extra subfields, add the next one to the structure
70 if( $i < $#$field_list) {
71 unless( defined( $stats->{$field}->{ $row->{$field} }->{subfield} ) ) {
72 $stats->{$field}->{ $row->{$field} }->{subfield} = {};
73 }
74 $stats = $stats->{$field}->{ $row->{$field} }->{subfield};
75 }
76 }
77 }
78
79 # Method to print statistics
80 sub print {
81 my( $self, $fh ) = @_;
82
83 unless( defined( $fh ) ) {
84 $fh = new IO::Handle;
85 #TODO Error check: did we get a file handle on STDOUT?
86 $fh->fdopen(fileno(STDOUT),"w");
87 }
88
89 $fh->print(join("\t\t", @{$self->{count_config}}), "\n");
90
91 return $self->_print( $fh, $self->{stats}, '' );
92 }
93
94 # Recursive print of statistics
95 sub _print {
96 my( $self, $fh, $stats, $indent ) = @_;
97
98 foreach my $field (sort keys %{$stats}) {
99 foreach my $value (sort keys %{$stats->{$field}}) {
100 $fh->print("$indent$value\t", $stats->{$field}->{$value}->{stats}->{count}, "\n");
101 if(defined($stats->{$field}->{$value}->{subfield})) {
102 $self->_print( $fh, $stats->{$field}->{$value}->{subfield}, "$indent\t\t" );
103 }
104 }
105 }
106 }
107
108 sub print_terse_sum {
109 my( $self, $fh ) = @_;
110
111 unless( defined( $fh ) ) {
112 $fh = new IO::Handle;
113 #TODO Error check: did we get a file handle on STDOUT?
114 $fh->fdopen(fileno(STDOUT),"w");
115 }
116
117 $fh->print($self->terse_sum_line(), "\n");
118 }
119
120 sub terse_sum_line {
121 my( $self ) = @_;
122
123 my $ts = $self->terse_sum();
124 my $line = '';
125
126 foreach my $field (@{$self->{count_config}}) {
127 $line .= sprintf("%6s % 6d % 6d ", $field, $ts->{$field}->{count}, $ts->{$field}->{sum});
128 }
129
130 return $line;
131 }
132
133 sub terse_sum {
134 my( $self ) = @_;
135
136 my $result = {};
137 foreach my $field (@{$self->{count_config}}) {
138 $result->{$field} = {
139 count => 0,
140 sum => 0
141 };
142 }
143
144 $self->_terse_sum( $self->{stats}, $result );
145
146 return $result;
147 }
148
149 # Recursive print of statistics
150 sub _terse_sum {
151 my( $self, $stats, $result ) = @_;
152
153 foreach my $field (keys %{$stats}) {
154 foreach my $value (keys %{$stats->{$field}}) {
155 $result->{$field}->{count}++;
156 $result->{$field}->{sum} += $stats->{$field}->{$value}->{stats}->{count};
157 if(defined($stats->{$field}->{$value}->{subfield})) {
158 $self->_terse_sum( $stats->{$field}->{$value}->{subfield}, $result );
159 }
160 }
161 }
162 }
163
164 1;