#!/p/bin/perl

#
# Directory configuration
#

$top_size = 20;
$totalsize = 0.0;

while (<STDIN>) {
	chop;
	if (/^L/) {			# Open connection
		($x, $pid, $remotehost, $locality, $account, $email, @date) = split;
		if ($remotehost =~ /^\[/) { $remotehost = "ip-address"; }
		$remotehost =~ tr/[A-Z]/[a-z]/;
#		$date=join(@date, ' ');
		($login{$pid . " " . $remotehost} = $email) =~ s/'//g;
#		$hosts{$remotehost}++;
#		push (@hosts, $remotehost);
		$totalusers++;
	}
	elsif (/^O/) {			# Close connection
#		($x, $pid, $remotehost, $bytes, $time) = split;
#		$remotehost =~ tr/[A-Z]/[a-z]/;
#		$logout{$pid . " " . $remotehost} = $time;
	}
	elsif (/^R/) {			# Retrieve file
		($x, $pid, $remotehost, $path, $size, $transfertime, $file) = split;
		if ($remotehost =~ /^\[/) { $remotehost = "ip-address"; }
		$remotehost =~ tr/[A-Z]/[a-z]/;
		$totalfiles++;
		$totalsize+=$size;
		if ($transfertime > 0 && $transfertime < 86400) {
			$totaltransfertime+=$transfertime;
			$retrieve_time {$remotehost} += $transfertime;
		}
		$hosts {$remotehost}++;
		$files {$remotehost} += $size;
		if ($file =~ /^\/pub\//) {
			$file =~ tr/[A-Z]/[a-z]/;
			(@dirs) = split(/\//, $file);
			$area_size{$dirs[2]}+=$size;
			$area_count{$dirs[2]}++;
		}
	}
	elsif (/^S/) {			# Store file
		($x, $pid, $remotehost, $size, $file) = split;
		if ($remotehost =~ /^\[/) { $remotehost = "ip-address"; }
		if ($file =~ /^\/pub\//) {
			$file =~ tr/[A-Z]/[a-z]/;
			(@dirs) = split(/\//, $file);
		} else {
			$dirs[2] = "Others";
		}
		$upload{$dirs[2]} .= sprintf("%s\n\t%d bytes\n\t%s (%s)\n", $file, $size, $login{$pid . " " . $remotehost}, $remotehost);;
	}
	elsif (/^C/) {			# Command
	}
}

#
# Daily domain statistics
#

foreach $host (keys %hosts) {
	next if ($host =~ /^\[/);
	(@domains) = split(/\./, $host);
	$toplevel_count{$domains[$#domains]}+=$hosts{$host};
	$toplevel_bytes{$domains[$#domains]}+=$files{$host};
	$toplevel_time{$domains[$#domains]}+=$retrieve_time{$host};
}

printf ("FUNET Archive nic.funet.fi statistics

Daily transmission statistics:

	Files transmitted			%12d
	Bytes transmitted			%12.0f
	Average KB/s rate (per user)		%12.2f
	Average KB/s rate (daily)		%12.2f
	Number of users				%12d
	Bytes transmitted per user		%12d
", $totalfiles, $totalsize, ($totaltransfertime==0)?0:(($totalsize/1024)/$totaltransfertime), ($totalsize==0)?0:(($totalsize/1024)/86400.0), $totalusers, ($totalusers==0)?0:$totalsize/$totalusers);

print "\nTop $top_size of archive sections:

						---- Percent  of ----
Archive section		Bytes sent  Files sent  Bytes sent Files sent
----------------------- ----------- ----------- ---------- ----------\n";

sub area_sort { $area_size{$a} < $area_size{$b} ? 1 : $area_size{$a} > $area_size{$b} ? -1 : 0; }

$count = 0;

foreach $area (sort area_sort keys %area_size) {
	if ($count < $top_size) {
		printf ("%-20.20s    %9d   %9d     %5.2f %%    %5.2f %%\n", $area, $area_size{$area}, $area_count{$area}, ($area_size{$area}/$totalsize)*100, ($area_count{$area}/$totalfiles)*100);
	}
	$count++;
}

printf ("
Top $top_size of domains:

	Number of   Number of 	Average	   Percent of  Percent of
Domain 	files sent  bytes sent	xmit rate  files sent  bytes sent
------- ----------- ----------- ---------- ----------- ----------\n");

sub top_level_sort { $toplevel_bytes{$a} < $toplevel_bytes{$b} ? 1 : $toplevel_bytes{$a} > $toplevel_bytes{$b} ? -1 : 0; }

$count = 0;

foreach $domain (sort top_level_sort keys %toplevel_bytes) {
	if ($count < $top_size) {
		printf ("%-6.6s  %9d   %9d   %5.2f KB/s   %5.2f %%     %5.2f %%\n", $domain, $toplevel_count{$domain}, $toplevel_bytes{$domain}, ($toplevel_time{$domain}==0)?0:($toplevel_bytes{$domain}/1024)/$toplevel_time{$domain}, ($toplevel_count{$domain}/$totalfiles)*100, ($toplevel_bytes{$domain}/$totalsize)*100);
	}
	$count++;
}

close STDOUT;

exit 0;
