#!/usr/bin/perl
# How much articles take my peers in percent
# crontab:
# zgrep ' final ' /news/log/OLD/news.notice.1.gz | awk '{print $6 " " $11 " " $13 " " $15}' | grep -v :  > /tmp/ratio && /news/bin/ratio.pl

my $tmpfile='/tmp/ratio';
my (@in,%articles,%accept,%reject);
my %res;

open(F, "< $tmpfile");
while (my $data=<F>) {
	push @in, $data;
}
close(F);

for (@in) {
	my ($host,$c,$a,$r)=split(/ /);
	$articles{$host} += $c;
	$accept{$host} += $a;
	$reject{$host} += $r;
}


foreach my $peer (sort keys %accept) {
	next if ($accept{$peer} == 0||$reject{$peer}==0);
	my $ratio=sprintf("%.2f", ($accept{$peer}/$articles{$peer})*100);

	$res{$peer}=$ratio;
}

print "How much articles take my peers in percent\n";
print "Ratio(%)\tPeername\n";
foreach my $peer (sort {$res{$b} <=> $res{$a}} keys %res) {
	print "$res{$peer}\t\t$peer\n";
}
