Index: freenet/scripts/path.pl
===================================================================
RCS file: /cvsroot/freenet/freenet/scripts/path.pl,v
retrieving revision 1.8
diff -u -r1.8 path.pl
--- freenet/scripts/path.pl	1 Jun 2002 06:31:13 -0000	1.8
+++ freenet/scripts/path.pl	4 Jun 2002 17:27:23 -0000
@@ -13,6 +13,7 @@
 # Enjoy graph.ps! 
 ########################################################################
 use strict;
+use File::Spec;
 
 $|++;  #non-buffered IO
 
@@ -34,29 +35,25 @@
 my @datafiles = grep {-f "$dir/$_" and /^\d+\.\d+\.\d+\.\d+$/} readdir(DIR); 
 close DIR;
 
-&usage ("no datafiles in directory $dir; please specify a directory") unless @datafiles;
+&usage ("no datafiles in directory $dir; please specify a directory") 
+    unless @datafiles;
+
+do { summarize(map { File::Spec->catdir($dir, $_); } @datafiles); 
+     exit(0); } if($request =~ m/summarize/i);
 
 my @lines = ();
 
 print "Reading: ";
-#suck data from the files
+
+# Suck data from the files
 foreach my $file (@datafiles) {
     print "$file ";
-    open INFILE, "$dir/$file" || die "cannot open $dir/$file: $!";
-    while (<INFILE>) {
-	my @parts = split /\s+/;
-	next unless ($parts[3] eq $request) && ($parts[1] eq "received"); #skip lines that don't have the right ID, also show only the "received" not sent.
-	my ($toNodeAddr, $toNodePort) = $parts[4] =~ /^tcp\/(.*):(\d+)/;
-	# I'm totally ignoring the port of this node.
-	# WARNING:  This means that the script WILL BE WRONG if a person is 
-	# running two nodes on different ports on the same IP address.  They will
-	# be treated as the same node.
-	$parts[4] = $toNodeAddr;
-	push @parts, $file;  #add the sender's IP to the list
-	push @lines, \@parts;
-    }
-    close INFILE;
+    my $full_filename = File::Spec->catdir($dir, $file);
+
+    my @results = parseFile($full_filename);
+    push(@lines, @results);
 }
+
 print "\n";
 
 my @path = qw();       # Array of "Step" objects.
@@ -196,14 +193,86 @@
 
 print OUT "}\n";  # End DOT output
 
-print "Message type counts:\n";
-foreach (sort keys %type_count) {
-    print "$_ -> $type_count{$_}\n";
-}
-
+messageTypeCounts(\%type_count);
 exit(0);  # We're done.
 
 #############################################################################
+
+sub summarize {
+    my @files = @_;
+
+    my %ips     = ();
+    my %types   = ();
+    my %reqs    = ();
+    my $records = 0;
+    my $fcount  = scalar(@files);
+
+    print "Reading: ";
+    foreach my $file (@files) {
+	open(FILE, $file) or die "Cannot open $file for reading: $!\n";
+	print "$file ";
+
+	while(my $line = <FILE>) {
+	    $records++;
+	    my @chunks = split(/\s+/, $line);
+	    $reqs{$chunks[3]}++;
+	    $types{$chunks[2]}++;
+	    $chunks[4] =~ m/tcp\/(\d+\.\d+\.\d+\.\d+)(:.*)?/;
+	    $ips{$1}++ if($1);
+	} # End while
+    } # End foreach
+    
+    print "\nSummary of $fcount files:\n";
+    print scalar(keys(%ips)), " unique IP addresses.\n";
+    print scalar(keys(%reqs)), " distinct requests.\n";
+    print scalar(keys(%types)), " distinct request types composed of:\n";
+    messageTypeCounts(\%types);
+
+    return 1;
+} # End summarize
+
+# Takes a filename, returns an array of references to arrays.
+sub parseFile {
+    my $filename = shift;
+
+    open INFILE, "$filename" || die "cannot open $filename: $!";
+    my @lines = qw();
+
+    while (<INFILE>) {
+	my @parts = split /\s+/;
+
+	# skip lines that don't have the right ID. 
+	# also show only the "received" not sent.
+	next unless ($parts[3] eq $request) && ($parts[1] eq "received"); 
+	my ($toNodeAddr, $toNodePort) = $parts[4] =~ /^tcp\/(.*):(\d+)/;
+
+	# I'm totally ignoring the port of this node.
+	# WARNING:  This means that the script WILL BE WRONG if a person is 
+	# running two nodes on different ports on the same IP address.  
+	# They will be treated as the same node.
+	$parts[4] = $toNodeAddr;
+	push @parts, $filename;  #add the sender's IP to the list
+	push @lines, \@parts;
+    }
+
+    close INFILE;
+    return @lines;
+} # End parseFile
+
+sub pad { 
+    my($str, $l) = @_;   
+    $str .= " " while(length($str) < $l);
+    return $str; 
+}
+
+sub messageTypeCounts {
+    my $hashref = shift;
+    
+    print "Message type counts:\n";
+    foreach my $key (sort(keys(%{$hashref}))) {
+	print pad($key, 25), $hashref->{$key}, "\n";
+    } 
+} # End messageTypeCounts
 
 sub reformat {
     # Take an IP address with an optional port and return a string that we
