[Bio] / FigKernelPackages / Stats.pm Repository:
ViewVC logotype

Diff of /FigKernelPackages/Stats.pm

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

revision 1.3, Thu Dec 6 13:59:34 2007 UTC revision 1.4, Tue Feb 5 02:11:31 2008 UTC
# Line 12  Line 12 
12  messages. The object is intially created in a blank state. Use the L</Add> method to add a  messages. The object is intially created in a blank state. Use the L</Add> method to add a
13  value to one of the totals. Use the L</AddMessage> method to add a message. The messages  value to one of the totals. Use the L</AddMessage> method to add a message. The messages
14  will be returned as one long string with new-lines separating the individual messages. To  will be returned as one long string with new-lines separating the individual messages. To
15  retrieve a counter value, use its name as a hash key. Thus, C<< $stats->{rows} >> would be  retrieve a counter value, use the L</Ask> method.
 the value of the C<rows> counter. The messages are stored under the key C<Messages>.  
16    
17  =cut  =cut
18    
# Line 48  Line 47 
47  sub new {  sub new {
48      # Get the parameters.      # Get the parameters.
49      my ($class, @names) = @_;      my ($class, @names) = @_;
50        # Put the specified counters into a hash.
51        my %map = map { $_ => 0 } @names;
52      # Create the new statistics object.      # Create the new statistics object.
53      my $self = { Messages => "" };      my $self = { Messages => "", Map => \%map };
     # Put the specified counters into it.  
     for my $name (@names) {  
         $self->{$name} = 0;  
     }  
54      # Bless and return it.      # Bless and return it.
55      bless $self;      bless $self;
56      return $self;      return $self;
# Line 93  Line 90 
90          $value = 1;          $value = 1;
91      }      }
92      # Get the counter's current value. If it doesn't exist, use 0.      # Get the counter's current value. If it doesn't exist, use 0.
93      my $current = $self->{$name};      my $current = $self->{Map}->{$name} || 0;
     if (!$current) {  
         $current = 0;  
     }  
94      # Update the counter by adding the value.      # Update the counter by adding the value.
95      my $retVal = $current + $value;      my $retVal = $current + $value;
96      $self->{$name} = $retVal;      $self->{Map}->{$name} = $retVal;
97      # Return the new value.      # Return the new value.
98      return $retVal;      return $retVal;
99  }  }
# Line 127  Line 121 
121      # Get the parameters.      # Get the parameters.
122      my ($self, $other) = @_;      my ($self, $other) = @_;
123      # Loop through the other object's values, merging them in.      # Loop through the other object's values, merging them in.
124      while (my ($name,$value) = each %{$other}) {      my $otherMap = $other->{Map};
125          if ($name eq "Messages") {      for my $key (keys %{$otherMap}) {
126              $self->AddMessage($value);          Trace("Accumulating $key.") if T(4);
127          } else {          $self->Add($key, $otherMap->{$key});
             $self->Add($name, $value);  
         }  
128      }      }
129        $self->AddMessage($other->{Messages});
130  }  }
131    
132  =head3 Ask  =head3 Ask
# Line 162  Line 155 
155      my ($self, $name) = @_;      my ($self, $name) = @_;
156      # Clear the return value.      # Clear the return value.
157      my $retVal = 0;      my $retVal = 0;
158      # If the counter exists, save its value.      # Get the map.
159      if (exists $self->{$name}) {      my $map = $self->{Map};
160          $retVal = $self->{$name};      # If the counter exists, extract its value. This process insures that
161        # non-existent statistical keys don't get created in the hash.
162        if (exists $map->{$name}) {
163            $retVal = $map->{$name};
164      }      }
165      # Return the result.      # Return the result.
166      return $retVal;      return $retVal;
# Line 217  Line 213 
213      my ($self) = @_;      my ($self) = @_;
214      # Create the return variable.      # Create the return variable.
215      my $retVal = "";      my $retVal = "";
216        # Get the map.
217        my $map = $self->{Map};
218        # Compute the key size.
219        my $keySize = Tracer::Max(map { length $_ } keys %{$map}) + 1;
220        my $statSize = Tracer::Max(map { length "$map->{$_}" } keys %{$map}) + 1;
221      # Loop through the statistics.      # Loop through the statistics.
222      for my $statKey (sort keys %{$self}) {      for my $statKey (sort keys %{$map}) {
         # Only proceed if this is not the message queue.  
         if ($statKey ne "Messages") {  
223              # Add the statistic and its value.              # Add the statistic and its value.
224              my $statValue = $self->{$statKey};          my $statValue = $map->{$statKey};
225              $retVal .= "$statKey\t$statValue\n";          $retVal .= Tracer::Pad($statKey, $keySize) .
226          }                     Tracer::Pad($statValue, $statSize, 'left') . "\n";
227      }      }
228      # Display the messages.      # Display the messages.
229      $retVal .= "\n" . $self->{Messages} . "\n";      $retVal .= "\n" . $self->{Messages} . "\n";
# Line 245  Line 244 
244      my ($self) = @_;      my ($self) = @_;
245      # Create the return variable.      # Create the return variable.
246      my $retVal = "";      my $retVal = "";
247        # Get the map.
248        my $map = $self->{Map};
249      # Loop through the statistics.      # Loop through the statistics.
250      for my $statKey (sort keys %{$self}) {      for my $statKey (sort keys %{$map}) {
         # Only proceed if this is not the message queue.  
         if ($statKey ne "Messages") {  
251              # Add the statistic and its value.              # Add the statistic and its value.
252              my $statValue = $self->{$statKey};          my $statValue = $map->{$statKey};
253              $retVal .= " $statKey = $statValue;";              $retVal .= " $statKey = $statValue;";
254          }          }
     }  
255      # Return the result.      # Return the result.
256      return $retVal;      return $retVal;
257  }  }
258    
259    =head3 Map
260    
261        my $mapHash = $stats->Map();
262    
263    Return a hash mapping each statistical key to its total.
264    
265    =cut
266    
267    sub Map {
268        # Get the parameters.
269        my ($self) = @_;
270        # Return the map.
271        return $self->{Map};
272    }
273    
274  1;  1;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3