package AccessLogger;
use strict;



sub new {
 ## constructor
my %vars;
my %totals;
my %types;
my %restotals;
my %groups;
my %queries = ();
my %inst = ();
my $class = shift;
my $self = {};
 $self->{itype} = shift || "institution";
 $self->{cid} = shift;
 $self->{vcid} = shift;
 $self->{iid} = shift;
 $self->{mode} = shift || "0"; # if set to something other than 0 the module is in a debug mode
 $self->{inst} = \%inst;
 $self->{restotals} = \%restotals;
 bless $self;


return $self;
};


sub DESTROY {
 my $self = shift;
 $self->{itype} = undef;
 $self->{cid} = undef;
 $self->{vcid} = undef;
 $self->{iid} = undef;
 $self->{filename} = undef;
 $self->{inst} = undef;
 $self->{restotals} = undef;
};

sub getInst {
 my $self = shift;
 return $self->{inst};
};

sub add {
 my $self = shift;
 my $ln = shift;
 my $ipt = shift;

 my %inst = %{$self->{inst}};
 my %restot = %{$self->{restotals}};
my @line = split (/\t/,$ln);
 my $servertype = $line[2];
 my $groupid = $line[13];
 my $ln_type = $line[15];
 my $cid = $line[9];
 my $vcid = $line[11];
 my $iid = $line[10];
 my $loc_ip = $line[5];
 my $ln_user = $line[7];
 my $ln_profile = $line[8];
 my $ln_qs = "";
 my $resize = "";
 my $user;

 if ($self->{itype} =~ /user/i) {
	$user = $ln_user;
 } else {
	$user = $ln_profile;
 };
 $user = $ipt if $ipt ne "";
 ## total query types

 $user =~ s/ /_/ig;


next if ($cid ne $self->{cid} && $cid ne "UNAVAILABLE");
next if ($vcid ne $self->{vcid} && $vcid ne "UNAVAILABLE");
next if ($iid ne $self->{iid} && $iid ne "UNAVAILABLE");
#print "\n$user - $servertype- $cid : $self->{cid} | $self->{vcid} : $vcid | $self->{iid} : $iid\r\n";

  if (!defined $restot{$user."\t".$servertype}) {$restot{$user."\t".$servertype} = 1 } else {$restot{$user."\t".$servertype}++; };
  $inst{$user} = 1;

   $self->{restotals} = \%restot;
   $self->{inst} = \%inst;

};

sub getQueries {
  my $self = shift;
  my $user = shift;
  my $filter = shift || 0;
  my %inst = %{$self->{inst}};
  my %restot = %{$self->{restotals}};
  my $ret;
  $user = undef if $user eq "";

$ret .= "<BR><BR><h2>Access: By Login</h2><TABLE border=1><TR><TD><B>User</B></TD><TD><B>JVA Logins</B></TD><TD><B>Browser Logins</B></TD><TD><B>Totals</B></TD></TR>\n";

  my %totals = ("JVA"=>0,"BROWSER"=>0,"TOTAL"=>0);
  for my $ins (keys %inst) {
  next if ($ins !~ /^$user$/ && $user ne "");
    $ret .= "<TR><TD><B>$ins</B></TD>";
    $ret .= "<TD>";
    $ret .= $restot{"$ins\tJVA"};
    $ret .= "<i>0</i>" if !defined $restot{"$ins\tJVA"};
    $ret .= "</TD>";
    $ret .= "<TD>";
    $ret .= $restot{"$ins\tBrowser"};
    $ret .= "<i>0</i>" if !defined $restot{"$ins\tBrowser"};
    $ret .= "</TD>";
    $ret .= "<TD>".($restot{"$ins\tBrowser"} + $restot{"$ins\tJVA"})."</TD>";
    $totals{"JVA"} += $restot{"$ins\tJVA"};
    $totals{"TOTAL"} += $restot{"$ins\tJVA"} + $restot{"$ins\tBrowser"};
    $totals{"Browser"} += $restot{"$ins\tBrowser"};
    $ret .= "</TR>\n";
  };
    $ret .= "<TR><TD><B>Totals:</B></TD><TD><B>".$totals{"JVA"}."</B></TD><TD><B>".$totals{"Browser"}."</B></TD><TD><B>".$totals{"TOTAL"}."</B></TD></TR></table>";


return $ret;

};

sub numerically {$a <=>$b;};

1;
