#!/usr/bin/perl
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2001 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.

use File::Basename;
use FindBin '$Bin';
use Getopt::Std;
use Slash::Utility;
use Slash::DB;

my $PROGNAME = basename($0);
(my $PREFIX = $Bin) =~ s|/[^/]+/?$||;

my %opts;
$opts{'u'} ||= $ARGV[0] || 'slashdot';

createEnvironment($opts{'u'});

my $slashdb = getCurrentDB();
my $constants = getCurrentStatic();

my $sleep = $ARGV[1] || 10;
my $rows = $ARGV[2] || 50;


my $log=$slashdb->sqlSelectMany("host_addr,uid,op,dat,
                     date_format(ts,\"\%H:\%i\") as ts,id",
                     "accesslog","id > ?",
                     "ORDER BY ts DESC");

my $proc=$slashdb->sqlShowProcessList();


my ($id)=$slashdb->sqlSelect("max(id)","accesslog"); 
my $starttime=time;
my $startsq=getQuestions();



while(1) {
	$proc->execute();
	print "\n\n";
	# system("clear");
	$active=0;
	undef @p;
	while(my $S=$proc->fetchrow_hashref() ) {
		next if $$S{Info} eq "accesslog";
		next if $$S{Command} eq "Sleep";
		next if $$S{Info} eq "SHOW PROCESSLIST";
		next unless $$S{Info};

		$active++;
		my $o=$$S{Time}." ";
		$o.=" " if $$S{Time} < 10;

		$$S{Info}=~s/\n//g;
             	$$S{Info}=~s/[^_\'\-\+\.A-Z0-9\(\) ,=\|]//gi;
		$$S{Info}=~s/(\s)+/ /g;
		$$S{Info}=~s/, /,/g;
		$$S{Info}=~s/\) /\)/g;
		$$S{Info}=~s/DATE_ADD\((.*)\)//g;
		$$S{Info}=~s/DATE_FORMAT\((.*)\)//g;
		$$S{Info}=~s/SELECT/S/;
		$o.=substr($$S{Info},0,76);
		push @p,$o;

	}
	$proc->finish();

	open P, "/proc/loadavg";
	@la=split " ",<P>;
	$l="$la[0] $la[1] $la[2]";
	close P;

	my $oldsec=$sec;

	$oldsq=$sq;
	$oldsq||=1;
	$sq=getQuestions();
	$thissq=$sq - $oldsq;
	$totalsq=$sq - $startsq;

    	$sec=time - $starttime;
	my $timepassed=$sec - $oldsec;
	$sec||=1;
	$timepassed||=1;

	$log->bind_param(1,$id);
	$log->execute;

	my $oldtotal=$total;
    	$total+=$log->rows;
	my $thistotal=$total - $oldtotal;
	$total||=1;
	$thistotal||=1;

	print "Total:$sec ".
		"pages:$total ".
		"avg:".sprintf("%.2f",$total/$sec)." ".
		"sql:$totalsq ".
		"avg:".sprintf("%.2f",$totalsq/$sec)." ".
		"pp:".sprintf("%.2f",$totalsq/$total )."\n".

	       "  Now:$timepassed ".
		"pages:$thistotal ".
		"avg:".	sprintf("%.2f",$thistotal/$timepassed)." ".
		"sql:$thissq ".
		"avg:". sprintf("%.2f",$thissq/$timepassed)." ".
		"pp:".sprintf("%.2f",$thissq/$thistotal)."\n";
		
        print "  Sql:$active/".$proc->rows()." | $l\n";
        	

    	while(my $U=$log->fetchrow_hashref()) {
	        my $w="$$U{ts} $$U{host_addr} ($$U{uid})";
	        $w.= "\t" if length $w < 24;
	        $w.= "\t$$U{op}";
       	 	$w.= "\t" if length($$U{op}) < 8;
	        $w.= "\t$$U{dat}\n";
	        # print $w unless $ARGV[0] eq "sec";
	        $id=$$U{id} if $$U{id} > $id;
    	}
    	$proc->finish();

                  
	my $x=0;
 	foreach (sort{ $b <=> $a }@p ) {
		$x++;
		next if $x > $rows;
		print "$_\n";
        }
	print "\n";

	sleep $sleep;
}



sub getQuestions
{
	my $c=$slashdb->sqlShowStatus();
	$c->execute();
	while (my ($q,$a)=$c->fetchrow() ) {
		$r=$a if $q eq "Questions";
	}
	$c->finish();
	return $r;

}
