# $HeadURL: http://svn.mzet.net/psist/Psist.pm $ # $Author: zet $ # $Date: 2006-08-24 15:39:25 +0200 (Thu, 24 Aug 2006) $ # $Revision: 12 $ package Psist; use strict; use Time::Local; sub new { my $type = shift; my %args = @_; my $self = { profile => $args{profile}, outfile => $args{outfile}, silent => $args{silent}, verbose => 1, version => $args{version}, history => 30, start => time, now => 0, jids => {}, nicks => {}, snicks => [], lines => 0, days => [], hours => [] }; my ($mday,$mon,$year) = (localtime(time))[3,4,5]; $self->{now} = timelocal(0, 0, 0, $mday, $mon, $year); my $i = 0; my $j = 0; while($i < $self->{history}) { while($j < 4) { $self->{days}[$i][$j] = 0; $j++; } $j = 0; $i++; } $i = 0; while($i < 24) { $self->{hours}[$i] = 0; $i++; } bless($self, $type); return $self; } sub run { my $self = shift; my $time; print "psist v$self->{version} - PSI Statistics Generator\n\n" unless $self->{silent}; $self->load_config(); $self->parse_history(); $self->gen_html(); $time = time - $self->{start}; print "\n",'DONE. Generated in ',sprintf('%02d:%02d:%02d', $time / 3600, ($time % 3600) / 60, ($time % 3600) % 60),"\n"; } sub load_config { my $self = shift; my $row; my $i; my $nick; my $jid; print "loading contacts...\n" unless $self->{silent}; open(CONFIGXML, $self->{profile} . "/accounts.xml"); while($row = ) { if($row =~ /]*>(.*?)<\/name>/) { $nick = $1; } if($row =~ /]*>(.*?)<\/jid>/) { if(length($1) == 0) { next; } if (length($nick) == 0) { $nick = $jid; } $jid = $1; $self->{jids}{$jid} = $nick; $self->{nicks}{$nick}{lines} = 0; $self->{nicks}{$nick}{linest}[0] = 0; $self->{nicks}{$nick}{linest}[1] = 0; $self->{nicks}{$nick}{linest}[2] = 0; $self->{nicks}{$nick}{linest}[3] = 0; $self->{nicks}{$nick}{to} = 0; $self->{nicks}{$nick}{sfrom} = 0; $self->{nicks}{$nick}{sto} = 0; $self->{nicks}{$nick}{last} = 0; } } close(CONFIGXML); } sub parse_history { my $self = shift; my ($jidf,$nick); my ($row,$ts,$tsfull,$dago,$dp); print "parsing history data...\n" unless $self->{silent}; foreach my $jid (keys %{$self->{jids}}) { $nick = $self->{jids}{$jid}; $jidf = $jid; $jidf =~ s/@/_at_/; $jidf =~ s/\+/%2b/; open(HISTFIL, $self->{profile} . '/history/' . $jidf . '.history') or next; print ' >> "',$jid,'" (',$nick,')',"\n" if $self->{verbose}; while($row = ) { # row processing if($row =~ /^\|(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\|1\|([a-z]+)\|.{4}\|(.*)/) { $ts = timelocal(0, 0, 0, $3, $2 - 1, $1); $tsfull = timelocal($6, $5, $4, $3, $2 - 1, $1); $dp = ($4 < 6) ? 0 : (($4 < 12) ? 1 : (($4 < 18) ? 2 : 3)); $dago = int(($self->{now} - $ts) / 86400); if($dago < $self->{history}) { $self->{days}[$dago][$dp]++; } $self->{hours}[$4]++; $self->{lines}++; $self->{nicks}{$nick}{lines}++; $self->{nicks}{$nick}{linest}[$dp]++; if($7 eq 'to') { $self->{nicks}{$nick}{to}++; } if($self->{nicks}{$nick}{last} < $tsfull) { $self->{nicks}{$nick}{last} = $tsfull; } } } close(HISTFIL); } print "sorting contacts by lines count...\n" unless $self->{silent}; @{$self->{snicks}} = sort { $self->{nicks}{$b}{lines} <=> $self->{nicks}{$a}{lines} } keys %{$self->{nicks}}; } sub gen_html { my $self = shift; my ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time)); my ($max_d,$max_h,$sum_h,$i,$sum); my ($time,$nick); $mon++; $year += 1900; # max for days $max_d = 1; $i = $self->{history}; while($i > 0) { $i--; $sum = $self->{days}[$i][0] + $self->{days}[$i][1] + $self->{days}[$i][2] + $self->{days}[$i][3]; $max_d = ($sum > $max_d) ? $sum : $max_d; } # max and sum for hours $max_h = 0; $sum_h = 0; $i = 0; while($i < 24) { $sum_h += $self->{hours}[$i]; $max_h = ($self->{hours}[$i] > $max_h) ? $self->{hours}[$i] : $max_h; $i++; } $max_h = ($max_h == 0) ? 1 : $max_h; $sum_h = ($sum_h == 0) ? 1 : $sum_h; print "generating HTML file...\n" unless $self->{silent}; open(HTMLFIL, '> '.$self->{outfile}); print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',$self->translate('PSI Statistics'),'',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '
',"\n"; print HTMLFIL '',$self->translate('PSI Statistics'),'

',"\n"; print HTMLFIL $self->translate('Statistics have been generated at '),'"',sprintf('%02d.%02d.%d %02d:%02d:%02d', $mday, $mon, $year, $hour, $min, $sec),'"


',"\n"; # daily activity print HTMLFIL '
',$self->translate('Daily activity'),'
',"\n"; print HTMLFIL '',"\n"; $i = $self->{history}; while($i > 0) { $i--; $sum = $self->{days}[$i][0] + $self->{days}[$i][1] + $self->{days}[$i][2] + $self->{days}[$i][3]; print HTMLFIL '',"\n"; } print HTMLFIL '',"\n"; $i = $self->{history}; while($i > 0) { $i--; print HTMLFIL '',"\n"; } print HTMLFIL '
',$sum,'
',"\n"; print HTMLFIL '',$sum,'
',"\n"; print HTMLFIL '',$sum,'
',"\n"; print HTMLFIL '',$sum,'
',"\n"; print HTMLFIL '',$sum,'
',"\n"; print HTMLFIL '
',$i,'
',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '
0-5 = 0-56-11 = 6-1112-17 = 12-1718-23 = 18-23

',"\n"; # most active times print HTMLFIL '
',$self->translate('Most active times'),'
',"\n"; print HTMLFIL '',"\n"; $i = 0; while($i < 24) { print HTMLFIL '',"\n"; $i++; } print HTMLFIL '',"\n"; $i = 0; while($i < 24) { print HTMLFIL '',"\n"; $i++; } print HTMLFIL '
',sprintf('%.1f', $self->{hours}[$i] * 100 / $sum_h),'%
',$self->{hours}[$i],'
',$i,'

',"\n"; # contacts print HTMLFIL '
',$self->translate('Contacts'),'
',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; $i = 0; foreach $nick (@{$self->{snicks}}) { $i++; if($self->{nicks}{$nick}{lines} > 0) { $sum = $self->{nicks}{$nick}{linest}[0] + $self->{nicks}{$nick}{linest}[1] + $self->{nicks}{$nick}{linest}[2] + $self->{nicks}{$nick}{linest}[3]; $sum = ($sum > 0) ? $sum : 1; my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($self->{nicks}{$nick}{last})); $year += 1900; $mon++; print HTMLFIL ''; print HTMLFIL ''; print HTMLFIL '',"\n"; } } print HTMLFIL '
 ',$self->translate('Nick'),'',$self->translate('Posts'),'',$self->translate('Time'),'',$self->translate('To'),'',$self->translate('From'),'',$self->translate('Last message'),'
',$i,'', $nick, '',$self->{nicks}{$nick}{lines},''; print HTMLFIL '',$self->{nicks}{$nick}{linest}[0],''; print HTMLFIL '',$self->{nicks}{$nick}{linest}[1],''; print HTMLFIL '',$self->{nicks}{$nick}{linest}[2],''; print HTMLFIL '',$self->{nicks}{$nick}{linest}[3],''; print HTMLFIL '',sprintf('%.2f', $self->{nicks}{$nick}{to} * 100 / $self->{nicks}{$nick}{lines}),'%',sprintf('%.2f', 100 - $self->{nicks}{$nick}{to} * 100 / $self->{nicks}{$nick}{lines}),'%',sprintf('%02d.%02d.%d %02d:%02d:%02d', $mday, $mon, $year, $hour, $min, $sec),'

',"\n"; # footer print HTMLFIL $self->translate('Total number of lines'),': ',$self->{lines},'

',"\n"; print HTMLFIL '',"\n"; print HTMLFIL $self->translate('Stats generated by'),' psist v',$self->{version},'
',"\n"; print HTMLFIL 'psist by Michal Zbortek (zet)
',"\n"; $time = time - $self->{start}; print HTMLFIL $self->translate('Stats generated in'),' ',sprintf('%02d:%02d:%02d', $time / 3600, ($time % 3600) / 60, ($time % 3600) % 60),"\n"; print HTMLFIL '
',"\n"; print HTMLFIL '
',"\n"; print HTMLFIL '',"\n"; print HTMLFIL '',"\n"; close(HTMLFIL); } sub translate { my $self = shift; my $message = shift; return $message; } return 1;