#!/usr/local/bin/perl
#################################################################
#
# + FtpLocate - FTP search engine
#
# by Distributed System Lab E.E. NCKU Taiwan 2000
# http://ftp.ee.ncku.edu.tw/ftplocate/
#
#################################################################

use CGI; CGI::ReadParse(*input);
unshift(@INC, substr($0, 0, rindex($0,'/')) ); # add prog path to INC
require ("config");

############################# main ##############################

$page=$input{'page'};

open(ULOG, "$USERLOG");
flock(ULOG, $LOCK_SH);
@log=<ULOG>;
flock(ULOG, $LOCK_UN);
close(ULOG);
$hit=0; $miss=0; $total_usedtime=0, $fcount=0; $dcount=0;
$today=timestr(); $today=~s/ \d\d:\d\d:\d\d//;
foreach (@log) {
  chop;
  ($date, $time, $usedtime, $client, $script, $cache, $founditem, $query)=split(/\s/);
  if ( $date eq $today ) {
    $todaycount++;
  }
  if ( $script =~ /^dsearch/ ) {
    $dcount++;
  } else {
    $fcount++;
  }
  if ( $cache eq '+' ) {
    $hit++;
  } else {
    $miss++;
  }
  $total_usedtime=$total_usedtime+$usedtime;
}
$total=$hit+$miss;

print html_header_str("FtpLocate Query History");
print qq|<table width=90%>
<tr><td colspan=3 nowrap>
&nbsp;<font color=#c00000>FtpLocate $STR_history</font>
&nbsp;<font color=#cccccc size=-1>$total</font>
</td>
<td align=center colspan=4>
|;

$n=$#log+1;
($logmin, $logmax)=init_pageline($n, 100, 10, $page, "$ENV{'SCRIPT_NAME'}?x=1");
&print_pageline;

print qq|
</td>
<td align=right>
<a href="javascript:history.go(-1)">
$STR_back</a>
&nbsp;
<a href="$ENV{'SCRIPT_NAME'}">$STR_refresh</a>
&nbsp;
&nbsp;
</td></tr>
<tr bgcolor=#408080>
|;

print qq|<td align=center><font color=#ffffff>$STR_date</font></td>
<td align=center><font color=#ffffff>$STR_time</font></td>
<td align=center><font color=#ffffff>$STR_responsetime</font></td>
<td><font color=#ffffff>$STR_clientaddr</font></td>
<td><font color=#ffffff>$STR_program</font></td>
<td align=center><font color=#ffffff>$STR_cache</font></td>
<td align=center><font color=#ffffff>$STR_founditem</font></td>
<td><font color=#ffffff>$STR_querystring</font></td>
</tr>
|;

$rmax=$#log-$logmin;
$rmin=$#log-$logmax;
for ($i=$rmax; $i>=$rmin; $i--) {
  ($date, $time, $usedtime, $client, $_, $cache, $founditem, $query)=split(/\s/,$log[$i],8);
  ($script,$fsite)=/(.*)\((.*)\)/;
  if ( $script =~ "flserv" ) { $script="flsearch"; }
  $url=dirname($ENV{'SCRIPT_NAME'})."/$script.pl?query=".CGI::escape($query)."&fsite=$fsite";
  $q=safe_query_str($query);
  if ( $nowclient eq $client ) {
    $color="#c00000";  
  } else {
    $color="#000000";  
  }

  print qq|<tr>
<td nowrap><font size=-1>$date</font></td>
<td nowrap><font size=-1>$time</font></td>
<td align=center nowrap><font size=-1>$usedtime</font></td>
<td nowrap><font size=-1 color=$color>$client</font></td>
<td nowrap><font size=-1 color=#999999>$script($fsite)</font></td>
<td align=center nowrap><font size=-1>$cache</font></td>
<td align=center nowrap><font size=-1>$founditem</font></td>
<td nowrap><font size=-1><a href="$url">$q</a></font></td>
</tr>
|;
}

$total=$hit+$miss;
if ( $total==0 ) {
  $hitrate=0;
  $avt1=0;
} else {
  $hitrate=int($hit*100/$total*100+0.5)/100;
  $avt1=int($total_usedtime/$total*100+0.5)/100;
}
if ( $miss==0 ) {
  $avt2=0;
} else {
  $avt2=int($total_usedtime/$miss*100+0.5)/100;
}

print qq|
</table>
<table width=90% cellspacing=0 cellpadding=0>
<tr><td colspan=2><hr size=1></td></tr>
<tr><td align=center colspan=2>
$STR_search_by_file: <font color=#c00000>$fcount</font>
&nbsp;
$STR_search_by_desc: <font color=#c00000>$dcount</font>
&nbsp;
$STR_cachehit: <font color=#c00000>$hit ($hitrate%)</font>
&nbsp;
$STR_cachemiss: <font color=#c00000>$miss</font>
<br>
$STR_average_responsetime: <font color=#c00000>$avt1 $STR_second</font>
&nbsp;
$STR_average_searchtime: <font color=#c00000>$avt2 $STR_second</font>
</td></tr>
<tr><td colspan=2><hr size=1></td></tr>
<tr>
<td width=60% align=center>
|;
print_pageline();

print qq|</td>
<td nowrap align=right>
$STR_dslab
</td></tr>
</table>
</center>
&nbsp;
&nbsp;
</body>
</html>
|;

exit(0);
