#! /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)
#
$Copy="(c) 1997 Gerhard.Winkler\@univie.ac.at";

$RELDATE = "Tue Jan 14 01:03:57 MET 1997";
$RELEASE = "V1.2";

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

$WORKDIR   = "/home/news/status";          # 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
$THRESHOLD = 100;   # don't plot sites which have sent less than $THRESHOLD
                    # articles
$GNUPLOT   = "/opt/gnu/bin/gnuplot";       # we need gnuplot
$PPMTOGIF  = "/opt/gnu/bin/ppmtogif";      # we need ppmtogif (pbmplus 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
ppmtogif (from pbmplus): $PPMTOGIF

\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];

#
# 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);
   $cmd = "$GNUPLOT << EOF
      cd \"$WORKDIR\"
      set term pbm
      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);
   print "converting picture format for: $PIC1\n" if ($opt_d);
   $cmd = "$PPMTOGIF < $WORKDIR/$TMPPIC > $PIC1-$hostname.gif 2>/dev/null";
   system($cmd);


}


# 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
      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);
   print "converting picture format for: $PIC2\n" if ($opt_d);
   $cmd = "$PPMTOGIF < $WORKDIR/$TMPPIC > $PIC2-$hostname.gif 2>/dev/null";
   system($cmd);

}



# 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
      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);
   print "converting picture format for: $PIC3\n" if ($opt_d);
   $cmd = "$PPMTOGIF < $WORKDIR/$TMPPIC > $PIC3-$hostname.gif 2>/dev/null";
   system($cmd);


}



# 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
      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);
   print "converting picture format for: $PIC4\n" if ($opt_d);
   $cmd = "$PPMTOGIF < $WORKDIR/$TMPPIC > $PIC4-$hostname.gif 2>/dev/null";
   system($cmd);

}




# 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 }

