#! /usr/local/bin/perl --    # -*-Perl-*-
#
# inflow-plot  - plot data collected by inflow-collect
#
# History: This program is based on some ideas (and source code lines) of
#          Felix Kuglers, SWITCH, inflow-package and could be part of it.
#
# 970102 V1.0   released
# 970113 V1.2   aligned default paths and renumbered version (FK)
# 970221 V1.3   changed gnuplot command to plot color
# 970421 V1.3.1 added ImageMagick support (FK)
# 970421 V1.3.2 enhanced file naming (FK)
#
$Copy="(c) 1997 Gerhard.Winkler\@univie.ac.at";

$RELDATE = "Mon Apr 21 14:35:31 MET DST 1997";
$RELEASE = "V1.3.2";

# ---- begin config section -------------------------------------------------

$WORKDIR      = "/opt/www/htdocs/cug/news/status/pics"; # dir to write pictures
$HOURDIR      = "/home/news/inflow/hour";     # dir of hourly summaries
$PIC1         = "$WORKDIR/arthourpic";        # articles per hour
$PIC2         = "$WORKDIR/artcumpic";         # articles cumulative
$PIC3         = "$WORKDIR/volhourpic";        # volume per hour
$PIC4         = "$WORKDIR/volcumpic";         # volume cumulative
$WANTHOSTNAME = 0;                            # add -host to outfile name ?
$THRESHOLD    = 300;   # don't plot sites which have sent less than $THRESHOLD
                       # articles
#
# This script needs helper applications, which you can download from many good
# file servers. ImageMagick is a successor of pbmplus package. URL examples:
# gnuplot: 
#   http://sunsite.cnlab-switch.ch/ftp/mirror/gnu/gnuplot-3.5.tar.gz
# ppmtogif: 
#   ftp://sunsite.cnlab-switch.ch/mirror/X11/R5-contrib/pbmplus10dec91.tar.Z
# convert:
#   ftp://sunsite.cnlab-switch.ch/mirror/X11/contrib/applications/ImageMagick/ImageMagick-3.8.2.tar.gz
#
$GNUPLOT   = "/usr/local/bin/gnuplot -rv";    # we need gnuplot
$PPMTOGIF  = "";                              # we need ppmtogif (pbmplus pkg)
$IMCONVERT = "/usr/local/bin/convert";        # or convert (ImageMagick pkg)
# ---- end config section ----------------------------------------------------

# don't change this; gnuplot makes cd and uses relative filenames
$TMPFILE   = "inflow-plot.tmp";   # temp for plot data (relative to $WORKDIR)
$TMPPIC    = "inflow-plot.pbm";   # temp for plot pics (relative to $WORKDIR)

require "getopts.pl";
require "ctime.pl";
#require "timelocal.pl";

($path,$0) = ($0 =~ /^(.*)\/([^\/]+)$/);                # strip path...

$Usage="$0    -  $Copy

Release $RELEASE  of $RELDATE

Use data produced by inflow-collect and produce some plotfiles
Output are some gif pictures

Usage:  $0 [-dht] 

Parameters:

   -d:            turn on verbosity; for debugging only
   -h:            This help.
   -t:            threshold; don't plot sites which have sent less than
                  threshold articles (up to plot time)

required helper programs:

gnuplot:                           $GNUPLOT
one of ppmtogif (from pbmplus):    $PPMTOGIF
    or convert (from ImageMagick): $IMCONVERT

\n";

#
# initialisation ----------------------------------------------------
#

&Getopts('dht');

if ($opt_h) { print "$Usage"; exit 0; }

chdir($WORKDIR);

$date = `date +%y%m%d`;
chop($date);
$fqdn = &gethostandfqdn;
$timenow = time;
$thisweekday = (localtime($timenow))[6];
$thisday = (localtime($timenow))[3];
$thishour = (localtime($timenow))[2];
$thisminute = (localtime($timenow))[1];
$suffix = $WANTHOSTNAME ? "-$hostname" : "";
#
# process files ----------------------------------------------------
#

opendir(DIR,"$HOURDIR") || die "can't opendir $HOURDIR\n";
$pat = "inflow.sum." . $thisweekday . "-";
@files = grep(/$pat/,readdir(DIR));
closedir(DIR);

while ($curfile = shift(@files)) {
   print "reading $curfile\n" if ($opt_d);
   $curfile = $HOURDIR . "/" . $curfile;
   open(FIL,"$curfile") || die "can't open file $curfile\n";
   $filetime=$timenow;
   while(<FIL>) {
      chop;
      ($a,$b,$filetime,$filedate) = split if (/timenow:/);
      last if ($filetime < ($timenow-86400));
      $filehour = (localtime($filetime))[2];
      if (/^pn/) {
         ($a,$filesite,$fileart,$filevol) = split;
         $allsites{$filesite}++;
         $allhours{$filehour}++;
         $articles{$filehour,$filesite} = $fileart;
         $volume{$filehour,$filesite} = $filevol;
      }

   }
   close(FIL);
}

&total;
&reducesite if ($opt_t);
&plotartperhour;
&plotartcum;
&plotvolperhour;
&plotvolcum;

unlink("$TMPFILE");
unlink("$TMPPIC");



# reducesite
#----------------------------------------------------------------------
# delete sites from array which have less than threshold articles
sub reducesite {
   foreach $h (keys %allhours) {
      foreach $s (keys %allsites) {
         $sum{$s} += $articles{$h,$s};
      }
   }
   foreach $s (keys %sum) {
      if ($sum{$s} < $THRESHOLD) {
         print "deleted: $s (articles: $sum{$s} )\n" if ($opt_d);
         delete $sum{$s};
         delete $allsites{$s};
      }
   }

}



# total
#----------------------------------------------------------------------
# compute summary of all sites
sub total {
   foreach $h (keys %allhours) {
      foreach $s (keys %allsites) {
         $articles{$h,"TOTAL"} += $articles{$h,$s};
         $volume{$h,"TOTAL"} += $volume{$h,$s};
      }
   }
   $allsites{"TOTAL"}++;
}




# plotartperhour
#---------------------------------------------------------------------- 
# construct plot; articles per site per hour
sub plotartperhour {
   open(OUT,">$TMPFILE");
   foreach $h (sort numerically keys %allhours) {
      print OUT "$h";
      foreach $s (sort keys %allsites) {
         $val = $articles{$h,$s};
         if (!$val) {$val = 0};
         print OUT " $val";
      }
      print OUT "\n";
   }
   close(OUT);

   $plotstr = "plot ";
   $row = 2;
   foreach $s (sort keys %allsites) {
      $plotstr .= "\"$TMPFILE\" using 1:$row title \"$s\" with lines,";
      $row++;
   }
   chop($plotstr);
#     gnuplot*background: black
#     gnuplot*textColor: white
#     gnuplot*borderColor: white
#     gnuplot*axisColor: white
   $cmd = "$GNUPLOT << EOF
      cd \"$WORKDIR\"
      set term pbm color
      set output \"$TMPPIC\"
      set xrange [0:23]
      set title \"Articles per hour for different sites ($fqdn: $date)\"
      set xlabel \"hour of day\"
      set ylabel \"articles\"
      $plotstr
EOF";

   system($cmd);
   &TMP2gif($PIC1);

}


# plotartcum
#----------------------------------------------------------------------
# construct plot; articles cumulative per site
sub plotartcum {
   open(OUT,">$TMPFILE");
   foreach $h (sort numerically keys %allhours) {
      print OUT "$h";
      foreach $s (sort keys %allsites) {
         $val = $articles{$h,$s};
         if (!$val) {$val = 0};
         $cum{$s} += $val;
      }
      foreach $s (sort keys %cum) {
         print OUT " $cum{$s}";
      }
      print OUT "\n";
   }
   close(OUT);

   $plotstr = "plot ";
   $row = 2;
   foreach $s (sort keys %allsites) {
      $plotstr .= "\"$TMPFILE\" using 1:$row title \"$s\" with lines,";
      $row++;
   }
   chop($plotstr);
   $cmd = "$GNUPLOT << EOF
      cd \"$WORKDIR\"
      set term pbm color
      set output \"$TMPPIC\"
      set xrange [0:23]
      set title \"Articles cumulative for different sites ($fqdn: $date)\"
      set xlabel \"hour of day\"
      set ylabel \"articles\"
      $plotstr
EOF";

   system($cmd);
   &TMP2gif($PIC2);

}



# plotvolperhour
#---------------------------------------------------------------------- 
# construct plot; volume per site per hour
sub plotvolperhour {
   open(OUT,">$TMPFILE");
   foreach $h (sort numerically keys %allhours) {
      print OUT "$h";
      foreach $s (sort keys %allsites) {
         $val = $volume{$h,$s};
         if (!$val) {$val = 0} else {$val /= 1000000};
         print OUT " $val";
      }
      print OUT "\n";
   }
   close(OUT);

   $plotstr = "plot ";
   $row = 2;
   foreach $s (sort keys %allsites) {
      $plotstr .= "\"$TMPFILE\" using 1:$row title \"$s\" with lines,";
      $row++;
   }
   chop($plotstr);
   $cmd = "$GNUPLOT << EOF
      cd \"$WORKDIR\"
      set term pbm color
      set output \"$TMPPIC\"
      set xrange [0:23]
      set title \"Volume per hour for different sites ($fqdn: $date)\"
      set xlabel \"hour of day\"
      set ylabel \"volume (MB)\"
      $plotstr
EOF";

   system($cmd);
   &TMP2gif($PIC3);

}



# plotvolcum
#----------------------------------------------------------------------
# construct plot; volume cumulative per site
sub plotvolcum {
   open(OUT,">$TMPFILE");
   foreach $h (sort numerically keys %allhours) {
      print OUT "$h";
      foreach $s (sort keys %allsites) {
         $val = $volume{$h,$s};
         if (!$val) {$val = 0} else {$val /= 1000000};
         $vcum{$s} += $val;
      }
      foreach $s (sort keys %vcum) {
         print OUT " $vcum{$s}";
      }
      print OUT "\n";
   }
   close(OUT);

   $plotstr = "plot ";
   $row = 2;
   foreach $s (sort keys %allsites) {
      $plotstr .= "\"$TMPFILE\" using 1:$row title \"$s\" with lines,";
      $row++;
   }
   chop($plotstr);
   $cmd = "$GNUPLOT << EOF
      cd \"$WORKDIR\"
      set term pbm color
      set output \"$TMPPIC\"
      set xrange [0:23]
      set title \"Volume cumulative for different sites ($fqdn: $date)\"
      set xlabel \"hour of day\"
      set ylabel \"volume (MB)\"
      $plotstr
EOF";

   system($cmd);
   &TMP2gif($PIC4);


}




# gethostandfqdn
#---------------------------------------------------------------------- 
# construct fully qualified domain name...
sub gethostandfqdn {
    chop($str=`uname -n`);
    if ($str =~ /\./) {             # str is fqdn
        $fqdn = $str;
        ($hostname) = ($str =~ /^([^.]+)\./);
    } else {                        # str is simple hostname
        $hostname = $str;
        $str = `/bin/grep domain /etc/resolv.conf`;
        $str =~ /domain\s*(\S+)$/;
        $fqdn = $hostname . "." . $1;
    }
}

# numerically
#----------------------------------------------------------------------
# sort numerically
sub numerically { $a <=> $b }


# TMP2gif
# --------------------------------------------------------------------
# convert TMPPIC to GIF-format picture: takes output filename as arg
sub TMP2gif {
    local($outpic) = @_;
    print "converting picture format for: $outpic\n" if ($opt_d);
    if (-e $PPMTOGIF) {
	print "using converter $PPMTOGIF\n" if ($opt_d);
	$cmd = "$PPMTOGIF < $WORKDIR/$TMPPIC > $outpic$suffix.gif 2>/dev/null";
    } 
    elsif (-e $IMCONVERT) {
	print "using converter $IMCONVERT\n" if ($opt_d);
	$cmd = "$IMCONVERT $WORKDIR/$TMPPIC gif:$outpic$suffix.gif 2>/dev/null";
    }
    system($cmd);
}
