#!/usr/bin/perl

# syntax: grep \* output-from-extendprobe|sort|./track-extends.pl

while(<>) {
#  print "Examining: $_";
  chomp;
  if (/\*\*\* (\S+) (.*)/) {
    if ($fp ne $1) {
      # new relay, so now is the time to finish processing the old $fp.
      if ($failsecond{$fp} >= 5) {
        $denom = $success{$fp} + $failsecond{$fp};
        $ratio{$fp} = $failsecond{$fp} / $denom;
#        print "Recorded ratio for $fp as $failsecond{$fp} / $denom = $ratio{$fp}\n";
      }
    }
    $fp = $1;
    if ($2 =~ /.*success.*/) {
      $success{$fp}++;
#      print "success for $fp now $success{$fp}\n";
    } elsif ($2 =~ /.*first hop.*/) {
      $failfirst{$fp}++;
#      print "failfirst for $fp now $failfirst{$fp}\n";
    } elsif ($2 =~ /.*second hop.*/) {
      $failsecond{$fp}++;
#      print "failsecond for $fp now $failsecond{$fp}\n";
    }
  }
}

foreach my $key (sort { $ratio{$b} <=> $ratio{$a}
                                   ||
                        $failsecond{$b} <=> $failsecond{$a}
                                   ||
                                $a cmp $b
                      } keys %ratio) {
  $denom = $success{$key} + $failsecond{$key};
  print "ratio $key as $failsecond{$key} / $denom = $ratio{$key}\n";
}

