#!/usr/bin/perl

#NAME
#  DailyStats - Extract statistics from JC's tune finder logs.

#SYNOPSIS
#  DailyStats &

# DESCRIPTION
#  This  script  grovels  through  the logfiles produced by JC's Tune
#  Finder scripts and generates some statistics.   What  is  produced
#  depends on what I want at the moment.

#  This script should be run in the log directory, wherever it may be
#  on this system.  After running this program, you  can  delete  the
#  *.log files.  They do tend to grow.

# AUTHOR
#  John Chamber <jc@trillian.mit.edu>


$| = 1;
$H = $ENV{HOME};
push @INC, ".","sh","$H/sh";
require "Vopt.pm";
($me = $0) =~ s'.*/'' unless defined($me);
&Vopt($ENV{"V_$me"} || $ENV{"D_$me"} || $ENV{"T_$me"} || 1);

$maxfiles = 999 if $V>1;

unless (open(S,">>dailystats")) {
	print V "$me: Can't write dailystats ($!)\n" if $V>0;
	exit $|;
}

file:
	for $f (glob "*.*") {
		print V "$me: File \"$f\"\n" if $V>2;
		++$files;
		unless (($base,$suff) = ($f = /^(.*)\.([^.]+)$/)) {
			print V "$me: File \"$f\"   ignored.\n" if $V>1;
			next file;
		}
		next file if ($suff eq log);	# Ignore logfiles.
		next file if ($base !~ /\d$/);	# Ignore non-request files.
		$t = lc($t);
		unless (open(F,$f)) {
			print V "$me: Can't read \"$f\" ($!)\n" if $V>0;
			next logfile;
		}
		print V "$me: File \"$f\" opened.\n" if $V>2;
		for $l (<F>) {
			print V "$me: $l" if $V>2;
			##	H: 1999-10-17 01:05:46 UT
			##	D:	1999-10-20 15:21:50
			$matches = 0;
			if ($l  =~ /^[DH]:/) {
				print V "$me: $l" if $V>2;
			}
			if (($x,$y,$m,$d) = ($l =~ /^([HD]):\s*(\d+)-(\d+)-(\d+)\s.*/)) {
				$V = ($y==1999 && $m==11 && $d==20) ? 3 : 1;
				print V "$me: Got x=$x y=$y m=$m d=$d t=$t.\n" if $V>2;
				unless (defined $dt{"$y-$m-$d-find"}) {$dt{"$y-$m-$d-find"} = 0}
				unless (defined $dt{"$y-$m-$d-tune"}) {$dt{"$y-$m-$d-tune"} = 0}
				$y = substr("0000$y",-4,4);
				$m = substr("00$m",-2,2);
				$d = substr("00$d",-2,2);
				$ymd = "$y-$m-$d";
				print V "$me: === x=$x y=$y m=$m d=$d t=$t.\n" if $V>2;
				$k = "$ymd-$t";
				++$dt{$k};
				$xx = $dt{$k};
				print V "$me: \"$k\" is $xx.\n" if $V>2;
				$ff = $dt{"$ymd-find"};
				$tt = $dt{"$ymd-tune"};
				print V "$me: $ymd $ff finds $ff tunes.\n" if $V>2;
				++$matches;
			}
		}
		print V "$me: File \"$f\"\ $matches matches.\n" if $V>2;
	}
print V "$me: File \"$f\"\ $matches matches.\n" if $V>2;

date:
	for $k (sort keys %dt) {
		print V "$me: Key \"$k\"\n" if $V>1;
		if (($y,$m,$d,$t) = ($k =~ /^(\d+)-(\d+)-(\d+)-(\w+)$/)) {
			$V = ($y==1999 && $m==11 && $d==20) ? 3 : 1;
			if ($t eq 'find') {
				$finds = $dt{$k};
				print V "$me: $y-$m-$d $finds finds $tunes tunes.\n" if $V>2;
			} elsif ($t eq 'tune') {
				$tunes = $dt{$k};
				print V "$me: $y-$m-$d $finds finds $tunes tunes.\n" if $V>2;
				$fx = int(($finds + 5) / 10);
				$tx = int(($tunes + 5) / 10);
				$lx = 1 + (($fx > $tx) ? $fx : $tx);
				$ly = int(($lx + 9) / 10);
				$graph = '|' . ('....:....|' x $ly);
				substr($graph,$tx,1) = 'T';
				substr($graph,$fx,1) = 'F';
				substr($graph,$fx,1) = '#' if $tx == $fx;
				printf S "$y$m$d %6d %6d $graph\n",$finds,$tunes;
				$finds = $tunes = 0;
			} else {
			}
		} else {
			print V "$me: Can't parse key \"$k\"\n" if $V>0;
		}
	}
print S "\n";
close S;

exit 0;
