rendered paste bodypackage ClassifierStats;
use strict;
use warnings;
use Exporter;
use Date::Manip;
use Data::Dumper;
our @ISA= qw( Exporter );
# these CAN be exported.
our @EXPORT_OK = qw( parse_classifier );
# these are exported by default.
our @EXPORT = qw( parse_classifier );
sub parse_classifier
{
my $conf = shift;
if (! $conf) { warn "Error! no configuration passed to the Module; exiting\n"; exit -1 }
my $mma;
my $ghash={};
my @valid=undef;
opendir(my $dh, $conf->{classifier}) || print "\ncan't opendir ".$conf->{classifier}.": $!";
my @dots = readdir($dh);
closedir $dh;
@dots=grep(/stat.*\.log/, @dots);
foreach (@dots) {
my $tmp=$_;
foreach (@{$conf->{date}}) {
s/(\d\d\d\d)(\d\d)(\d\d)/$1.$2.$3/g;
if ($tmp=~/$_/ || $conf->{list} || ( $tmp eq 'classifier_stat.log' && $_ eq $conf->{today} )) {
push (@valid, "$tmp");
}
}
}
$|++;
foreach (@valid) {
next unless $_;
open(my $fh, '<', $conf->{classifier}."/$_") or print "\ncan't open file ".$conf->{classifier}."/$_: $!";
while (<$fh>) {
next unless /(\d{4}-\d{2}-\d{2})\s*([\d:]+)\s*(.*)\s*=\s*(.*)/;
my $tntdate= UnixDate( ParseDate ($1) , '%d/%m/%Y');
my $intdate= UnixDate( ParseDate ($1) , '%Y/%m/%d');
my $inthour=$2;
my $intidx=$3;
my $value=$4;
$intidx=~s/\s{2,}//og;
$inthour=~s/\s{2,}//og;
$intdate=~s/\s{2,}//og;
$value=~s/\s{2,}//og;
next unless grep(/$tntdate/, @{$conf->{dateex}});
next if ( ! grep(/$intidx/, @{$conf->{incarr}}) && defined $conf->{incarr}[0] && ! defined $conf->{list} ) ;
$inthour=~s/\:\d\d$//g;
if ($intidx && $intdate && $inthour ) {
#$intdate=~s/(\d\d)\/(\d\d)\/(\d\d\d\d)/$3\/$2\/$1/og;
#$intdate="$3/$2/$1";
$ghash->{$intidx}->{$intdate."-".$inthour}=0 unless $ghash->{$intidx}->{$intdate."-".$inthour};
$ghash->{$intidx}->{$intdate."-".$inthour}+=$value ;
}
}
}
############################
print "\nClassifier Stats begin \n" if $conf->{verbose};
my $classifierws = $conf->{workbook}->add_worksheet("Classifier");
my $grow=1;
my $gcol=1;
my $gcsv={};
foreach (keys %$ghash) {
my $idx = $_;
$grow=1;
my $g = $ghash->{$idx};
$classifierws->write(0, $gcol, $idx, $conf->{format});
foreach (sort keys %$g) {
my $gdate = $_;
$classifierws->write($grow, 0, "$gdate", $conf->{format});
$classifierws->write($grow++, $gcol, $ghash->{$idx}->{$gdate}, $conf->{format});
$gcsv->{$gdate}->{$idx}=$ghash->{$idx}->{$gdate} if ($conf->{csv});
$gcsv->{$gdate}->{$idx}=0 if ($conf->{csv} && ! $gcsv->{$gdate}->{$idx});
$mma->{$idx}->{cmax}=0 unless $mma->{$idx}->{cmax};
if ($ghash->{$idx}->{$gdate} >= $mma->{$idx}->{cmax}) {$mma->{$idx}->{cmax}=$ghash->{$idx}->{$gdate};}
if ( ! $mma->{$idx}->{cmin} || $ghash->{$idx}->{$gdate} <= $mma->{$idx}->{cmin} ) {$mma->{$idx}->{cmin}=$ghash->{$idx}->{$gdate};}
$mma->{$idx}->{cavg}+=$ghash->{$idx}->{$gdate};
$mma->{$idx}->{cavgc}++;
}
$gcol++;
}
my $gtitle='';
foreach (sort keys %{$gcsv}) {
my $tmpdate=$_;
my $line=$tmpdate.",";
if (! $gtitle) {
print {$conf->{csvfh}} "############################################################\n" unless $gtitle;
my $tmptit=join(',', sort keys %{$gcsv->{$tmpdate}})."\n";
print {$conf->{csvfh}} 'date,'.$tmptit unless $gtitle;
$gtitle=1;
}
foreach (sort keys %{$gcsv->{$tmpdate}} ) {
$line.=$gcsv->{$tmpdate}->{$_}.",";
}
$line=~s/(.*)?,$/$1/;
print {$conf->{csvfh}} $line."\n";
}
}