#!/usr/bin/perl5
#
# $Id: graph.pl,v 1.2 1998/05/21 20:46:20 sheneman Exp $
#
##############################################################################
#
# The contents of this file are subject to the Netscape Public License Version
# 1.0 (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at http://www.mozilla.org/NPL/ 
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License. 
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are Copyright (C) 1998 Netscape
# Communications Corporation. All Rights Reserved.
#
##############################################################################
#
# Make the graph data for the website
#

$PATH       = "/u/sheneman/projects/cleanup/current/stats";
$GRAPH_PATH = "$PATH/graphs";

$GRAPH_WIDTH = 60;

sub make_graph {

    ($infile, $outfile, $title) = @_;

    @lines = `cat < /dev/null`;
    @all_lines = `cat $PATH/$infile | /bin/awk '{print $2}'`;
    #@all_lines =  `cat /tmp/$infile | /bin/awk '{print $2}'`;

    $i = 0;
    while ($i <= $GRAPH_WIDTH && @all_lines[$i]) {
	push(@lines,@all_lines[$i]);
	$i++;
    }
    @lines = reverse(@lines);

    open (OUT, ">/tmp/graph");

    $count = 0;
    $biggest = 0;
    foreach $line (@lines) {
	if($count++ > $GRAPH_WIDTH) { last; }

	chop $line;
	($date, $exceptions, $fluff) = split(" ", $line);
	if($exceptions > $biggest) { $biggest = $exceptions; }
	print(OUT "$exceptions\n");
    }
    close(OUT);

    #print("biggest = $biggest\n");

    $gnuplot  = "/opt/bin/gnuplot";
    $ppmtogif = "/opt/bin/ppmtogif";

    $yrange = $biggest + $biggest*(0.25);
    if($yrange <= 0) { $yrange = 5; } 
    $ytics = sprintf("%d", $yrange/10);
    if(!$ytics) { $ytics = 5; }

    open (GNUPLOT, "|$gnuplot");

    print GNUPLOT <<gnuplot_Commands_Done;

       set term pbm color medium
       set output "/tmp/$outfile.ppm"
       set size 1.2, 1.0
       set title "$title"
       set xlabel "$GRAPH_WIDTH Day Span"
       set ylabel "EXCEPTIONS"
       set xrange [0:$count]
       set xtics -$count, 5, 0
       set ytics 0, $ytics, $yrange
       set yrange [0:$yrange]
       set noxzeroaxis
       set noyzeroaxis
       set border
       set nogrid
       set nokey
       plot "/tmp/graph" w linespoints 1

gnuplot_Commands_Done

    close(GNUPLOT);

    system("$ppmtogif /tmp/$outfile.ppm > $GRAPH_PATH/$outfile.gif");
    unlink("/tmp/$outfile.ppm");
    unlink("/tmp/graph");
}

# category 1a
&make_graph("cat1a_nscp.txt",    "cat1a_nscp",    "Category 1a: Netscape Employees");
&make_graph("cat1a_vendors.txt", "cat1a_vendors", "Category 1a: Vendors");
&make_graph("cat1a_interim.txt", "cat1a_interim", "Category 1a: Interim Hires");
&make_graph("cat1a_ic.txt",      "cat1a_ic",      "Category 1a: Independent Contractors")
&make_graph("cat1a_oem.txt",     "cat1a_oem",     "Category 1a: OEM Partners")
&make_graph("cat1a_unknown.txt", "cat1a_unknown", "Category 1a: Unknown Employee Type");

# category 1b
&make_graph("cat1b_nscp.txt",    "cat1b_nscp",    "Category 1b: Netscape Employees");
&make_graph("cat1b_vendors.txt", "cat1b_vendors", "Category 1b: Vendors");
&make_graph("cat1b_interim.txt", "cat1b_interim", "Category 1b: Interim Hires");
&make_graph("cat1b_ic.txt",      "cat1b_ic",      "Category 1b: Independent Contractors")
&make_graph("cat1b_oem.txt",     "cat1b_oem",     "Category 1b: OEM Partners")
&make_graph("cat1b_unknown.txt", "cat1b_unknown", "Category 1b: Unknown Employee Type");

# category 2
&make_graph("cat2.txt",          "cat2",    "Category 2");

# category 3
&make_graph("cat3.txt",          "cat3",    "Category 3");

# category 5
&make_graph("cat5_nscp.txt",    "cat5_nscp",    "Category 5: Netscape Employees");
&make_graph("cat5_vendors.txt", "cat5_vendors", "Category 5: Vendors");
&make_graph("cat5_interim.txt", "cat5_interim", "Category 5: Interim Hires");
&make_graph("cat5_ic.txt",      "cat5_ic",      "Category 5: Independent Contractors")
&make_graph("cat5_oem.txt",     "cat5_oem",     "Category 5: OEM Partners")
&make_graph("cat5_unknown.txt", "cat5_unknown", "Category 5: Unknown Employee Type");

# category 6
&make_graph("cat6.txt",          "cat6",    "Category 6");

# category 7
&make_graph("cat7_nscp.txt",    "cat7_nscp",    "Category 7: Netscape Employees");
&make_graph("cat7_vendors.txt", "cat7_vendors", "Category 7: Vendors");
&make_graph("cat7_interim.txt", "cat7_interim", "Category 7: Interim Hires");
&make_graph("cat7_ic.txt",      "cat7_ic",      "Category 7: Independent Contractors")
&make_graph("cat7_oem.txt",     "cat7_oem",     "Category 7: OEM Partners")
&make_graph("cat7_unknown.txt", "cat7_unknown", "Category 7: Unknown Employee Type");

# category 8
&make_graph("cat8.txt",          "cat8",    "Category 8");

# category 9
&make_graph("cat9_nscp.txt",    "cat9_nscp",    "Category 9: Netscape Employees");
&make_graph("cat9_vendors.txt", "cat9_vendors", "Category 9: Vendors");
&make_graph("cat9_interim.txt", "cat9_interim", "Category 9: Interim Hires");
&make_graph("cat9_ic.txt",      "cat9_ic",      "Category 9: Independent Contractors")
&make_graph("cat9_oem.txt",     "cat9_oem",     "Category 9: OEM Partners")
&make_graph("cat9_unknown.txt", "cat9_unknown", "Category 9: Unknown Employee Type");


# sum
&make_graph("sum.txt",           "sum",     "TOTAL Exceptions in all Categories");















