Index: trunk/tools/czarplot.pl
===================================================================
--- trunk/tools/czarplot.pl	(revision 28718)
+++ trunk/tools/czarplot.pl	(revision 28719)
@@ -5,10 +5,8 @@
 
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
-use File::Temp qw(tempfile);
 use POSIX qw/strftime/;
 
 use czartool::CzarDb;
-use czartool::Gnuplot;
-
+use czartool::Czarplot;
 
 my $czarDbName = "czardb";
@@ -19,7 +17,9 @@
 my $begin = undef;
 my $end = undef;
+my $path = undef;
 my $verbose = undef;
 my $histogram = undef;
 my $timeSeries = undef;
+my $savingToFile = undef;
 
 GetOptions (
@@ -30,4 +30,5 @@
         "begin|b=s" => \$begin,
         "end|e=s" => \$end,
+        "output|o=s" => \$path,
         "histogram|h" => \$histogram,
         "timeseries|t" => \$timeSeries,
@@ -37,24 +38,19 @@
 print "\n";
 if (!$histogram) {
-    print "* OPTIONAL: plot histogram                  -h                          (default=on)\n";
-}
+    print "* OPTIONAL: plot histogram                  -h                          (default=off)\n";}
 if (!$timeSeries) {
-    print "* OPTIONAL: plot timeseries                 -t                          (default=on)\n";
-} 
+    print "* OPTIONAL: plot timeseries                 -t                          (default=on)\n";} 
 if (!$label) {
-    print "* OPTIONAL: choose a label                  -l <labellName>             (default='all_labels')\n";
-}
+    print "* OPTIONAL: choose a label                  -l <labellName>             (default='all_labels')\n";}
 if (!$stage) {
-    print "* OPTIONAL: choose a stage                  -s <chip|cam|warp|etc>      (default=none)\n";
-}
+    print "* OPTIONAL: choose a stage                  -s <chip|cam|warp|etc>      (default=none)\n";}
 if (!$interval) {
-    print "* OPTIONAL: choose time interval in past    -i <'1 hour'|'1 day'|etc>   (default=interval since 7am this morning)\n";
-} 
+    print "* OPTIONAL: choose time interval in past    -i <'1 hour'|'1 day'|etc>   (default=interval since 7am this morning)\n";} 
 if (!$begin) {
-    print "* OPTIONAL: choose a begin time             -d <datetime>               (default=7am this morning)\n";
-} 
+    print "* OPTIONAL: choose a begin time             -d <datetime>               (default=7am this morning)\n";} 
 if (!$end) {
-    print "* OPTIONAL: choose an end time              -e <datetime>               (default=now)\n";
-} 
+    print "* OPTIONAL: choose an end time              -e <datetime>               (default=now)\n";} 
+if (!$path) {
+    print "* OPTIONAL: choose an output location       -o <path>                   (default=none)\n";} 
 
 print "\n";
@@ -62,11 +58,14 @@
 # default values
 if (!$label) {$label = "all_labels";}
-if (!$histogram && !$timeSeries) {$timeSeries = 1;}
+if (!$histogram && !$timeSeries) {$timeSeries = 1; $histogram = 1;}
 if (!$verbose) {$verbose = 0;}
 if (!$save_temps) {$save_temps = 0;}
+if (!$path) {$savingToFile = 0; $path = ".";}
+else {$savingToFile = 1;}
 
 my $czarDb = new czartool::CzarDb($czarDbName, "ippdb01", "ipp", "ipp", $verbose);
-my $gnuplot = new czartool::Gnuplot("%Y%m%d-%H%M%S", "X11", "histo.pbm");
 $czarDb->setDateFormat("%Y%m%d-%H%i%s");
+
+my $czarplot = new czartool::Czarplot($czarDb, "%Y%m%d-%H%M%S", $savingToFile ? "png" : "X11", $path, $save_temps);
 
 my @allStages = ("chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist");
@@ -79,58 +78,5 @@
 }
 
-if ($histogram) {plotHistogram($label, $begin, $end);}
-if ($timeSeries) {plotTimeSeries($label, $stage, $begin, $end); }
+if ($timeSeries) {$czarplot->createTimeSeries($label, $stage, $begin, $end);}
+if ($histogram) {$czarplot->createHistogram($label, $begin, $end);}
 
-###########################################################################
-#
-# Plots a time series for all stages for this label
-#
-###########################################################################
-sub plotTimeSeries {
-    my ($label, $selectedStage, $beginTime, $endTime) = @_;
-
-    my ($minX, $maxX, $minY, $maxY, $timeDiff);
-    $minX = 999999999;
-    $maxX = -9999999999;
-    $minY = 999999999;
-    $maxY = -99999999;
-
-    my $stages = undef;
-
-    if (!$selectedStage) {$stages = \@allStages;}
-    else {$stages = ["$selectedStage"]};
-
-    my %gnuplotFiles;
-    foreach $stage (@{$stages}) {
-        $gnuplotFiles{$stage} = $czarDb->createTimeSeriesData($save_temps, $label, $stage, $beginTime, $endTime, \$minX, \$maxX, \$minY, \$maxY, \$timeDiff);
-    }
-
-    if (!$selectedStage) {$gnuplot->plotAllStagesTimeSeries($label, $beginTime, $endTime, $maxX, $minX, $maxY, $minY, $timeDiff, \%gnuplotFiles);}
-    else {$gnuplot->plotSingleTimeSeries($gnuplotFiles{$selectedStage}, $selectedStage, $label, $beginTime, $endTime, $maxX, $minX, $maxY, $minY, $timeDiff);}
-
-}
-
-###########################################################################
-#
-# Plots a histogram of stuff processed in provided interval and label for all stages
-#
-###########################################################################
-sub plotHistogram {
-    my ($label, $beginTime, $endTime) = @_;
-
-    my ($tempFile, $filePath) = tempfile( "/tmp/czartool_gnuplot_histogram.XXXX", UNLINK => !$save_temps);
-    my ($processed, $pending, $faults);
-
-    foreach $stage (@allStages) {
-
-        $czarDb->countProcessedPendingAndFaults($label, $stage, $beginTime, $endTime, \$processed, \$pending, \$faults);
-        print $tempFile "$stage $processed, $pending, $faults\n";
-    }
-
-    close($tempFile);
-
-    $gnuplot->plotHistogram($filePath, $label, $beginTime, $endTime);
-}
-
-
-
Index: trunk/tools/czartool/CzarDb.pm
===================================================================
--- trunk/tools/czartool/CzarDb.pm	(revision 28718)
+++ trunk/tools/czartool/CzarDb.pm	(revision 28719)
@@ -72,4 +72,23 @@
        $query->execute;
 }
+
+###########################################################################
+#
+# Sets priority for this label
+#
+###########################################################################
+sub setLabelPriority {
+    my ($self, $label, $priority) = @_;
+
+    my $query = $self->{_db}->prepare(<<SQL);
+     UPDATE current_labels 
+         SET priority = $priority 
+         WHERE label LIKE '$label' 
+         AND server LIKE 'stdscience';
+SQL
+
+    $query->execute;
+}
+
 ###########################################################################
 #
@@ -79,4 +98,7 @@
 sub updateCurrentLabels {
     my ($self, $server, $labels) = @_;
+
+    my $size = scalar @{$labels};
+    if ($size < 1) {return;}
 
     my $query = $self->{_db}->prepare(<<SQL);
@@ -181,4 +203,5 @@
 
         my ($timestamp, $pending, $faults, $processed) = @row;
+    #    print  "'$timestamp' '$pending' '$faults' '$processed'\n";
         print $tempFile "$timestamp $pending $faults $processed\n";
     }
@@ -298,5 +321,5 @@
 
     my $currentRevision = -1;
-    my $latestRevision = 6;
+    my $latestRevision = 7;
 
     while ($currentRevision != $latestRevision) {
@@ -311,4 +334,5 @@
         elsif ($currentRevision == 4) {$self->createRevision_5();}
         elsif ($currentRevision == 5) {$self->createRevision_6();}
+        elsif ($currentRevision == 6) {$self->createRevision_7();}
     }
 }
@@ -476,4 +500,24 @@
 #######################################################################################
 # 
+# Create revision 7 of the database 
+#
+#######################################################################################
+sub createRevision_7 {
+    my ($self) = @_;
+
+    print "* Creating revision 7 of '$self->{_dbName}'\n";
+
+    my $query = $self->{_db}->prepare(<<SQL);
+    ALTER TABLE current_labels 
+        ADD COLUMN priority INT NOT NULL DEFAULT 0;
+SQL
+
+    $query->execute;
+
+    $self->setRevision(7);
+}
+
+#######################################################################################
+# 
 # Sets current revision of ippToPsps database
 #
Index: trunk/tools/czartool/Czarplot.pm
===================================================================
--- trunk/tools/czartool/Czarplot.pm	(revision 28719)
+++ trunk/tools/czartool/Czarplot.pm	(revision 28719)
@@ -0,0 +1,249 @@
+#!/usr/bin/perl -w
+package czartool::Czarplot;
+
+use warnings;
+use strict;
+
+use File::Temp qw(tempfile);
+
+my @allStages = ("chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist");
+
+
+###########################################################################
+#
+# Constructor
+#
+###########################################################################
+sub new {
+    my $class = shift;
+    my $self = {
+        _czarDb => shift,
+        _dateFormat => shift,
+        _outputFormat => shift,
+        _outputPath => shift,
+        _save_temps => shift,
+    };
+
+    bless $self, $class;
+    return $self;
+}
+
+###########################################################################
+#
+# Generates a suitable filename for this plot
+#
+###########################################################################
+sub createImageFileName {
+    my ($self, $label, $suffix) = @_;
+
+    my $prefix = $self->{_outputPath} ? $self->{_outputPath} : ".";
+
+    return $prefix . "/czarplot_" . $label . "_$suffix.png";
+}
+
+###########################################################################
+#
+# Plots a time series for all stages for this label
+#
+###########################################################################
+sub createTimeSeries {
+    my ($self, $label, $selectedStage, $beginTime, $endTime) = @_;
+
+    my $outputFile = createImageFileName($self, $label, "t");
+
+    my ($minX, $maxX, $minY, $maxY, $timeDiff);
+    $minX = 999999999;      
+    $maxX = -9999999999;
+    $minY = 999999999;          
+    $maxY = -99999999;
+
+    my $stages = undef;                 
+
+    if (!$selectedStage) {$stages = \@allStages;}
+    else {$stages = ["$selectedStage"]};        
+
+    my %gnuplotFiles;
+    my $stage = undef;
+    foreach $stage (@{$stages}) {
+        $gnuplotFiles{$stage} =  $self->{_czarDb}->createTimeSeriesData($self->{_save_temps}, $label, $stage, $beginTime, $endTime, \$minX, \$maxX, \$minY, \$maxY, \$timeDiff);
+    }                                                           
+
+    if (!$selectedStage) {plotAllStagesTimeSeries($self, $outputFile, $label, $beginTime, $endTime, $maxX, $minX, $maxY, $minY, $timeDiff, \%gnuplotFiles);}                      
+    else {plotSingleTimeSeries($self, $gnuplotFiles{$selectedStage}, $outputFile, $selectedStage, $label, $beginTime, $endTime, $maxX, $minX, $maxY, $minY, $timeDiff);}
+}                                                    
+
+###########################################################################
+#
+# Plots a histogram of stuff processed in provided interval and label for all stages
+#
+###########################################################################
+sub createHistogram {
+    my ($self, $label, $beginTime, $endTime) = @_;
+
+    my $outputFile = createImageFileName($self, $label, "h");
+
+    my ($tempFile, $inputFile) = tempfile( "/tmp/czartool_gnuplot_histogram.XXXX", UNLINK => !$self->{_save_temps});
+    my ($processed, $pending, $faults);
+    my $stage = undef;
+    foreach $stage (@allStages) {
+
+        $self->{_czarDb}->countProcessedPendingAndFaults($label, $stage, $beginTime, $endTime, \$processed, \$pending, \$faults);
+        print $tempFile "$stage $processed, $pending, $faults\n";
+    }
+
+    close($tempFile);
+
+    plotHistogram($self, $inputFile, $outputFile, $label, $beginTime, $endTime);
+}
+
+
+###########################################################################
+#
+# Sets the output path 
+#
+###########################################################################
+sub setOutputFormat {
+    my ($self, $outputFormat) = @_;
+    $self->{_outputFormat} = $outputFormat if defined($outputFormat);
+}           
+
+###########################################################################
+#
+# Sets the output type 
+#
+###########################################################################
+sub setOutputPath {
+    my ($self, $outputPath) = @_;
+    $self->{_outputPath} = $outputPath if defined($outputPath);
+}           
+
+###########################################################################
+#
+# Sets the date format to be used
+#
+###########################################################################
+sub setDateFormat {
+    my ($self, $dateFormat) = @_;
+    $self->{_dateFormat} = $dateFormat if defined($dateFormat);
+    return $self->{_dateFormat};
+}       
+
+###########################################################################
+#
+# Plots a time-series of pending/faults
+#
+###########################################################################
+sub plotAllStagesTimeSeries {
+    my ($self, $outputFile, $label, $fromTime, $toTime, $maxX, $minX, $maxY, $minY, $timeDiff, $gnuplotFiles) = @_;
+
+    $maxY = $maxY + ($maxY/10);
+    $minY = $minY - ($minY/10);
+    if ($maxY == 0) {$maxY = 1;}
+    if ($maxY == 0) {$maxY = 1;}
+    if ($minY == 0) {$minY = -1;}
+
+    my $divX = $timeDiff/2;
+
+    open (GP, "|/usr/bin/gnuplot -persist") or die "no gnuplot";
+    use FileHandle;
+    GP->autoflush(1);
+
+    print GP
+        "set term $self->{_outputFormat};" .
+        "set output \"$outputFile\";" .
+        "set title \"All stages for '$label' since $fromTime\";" .
+        "set key left top;" .
+        "set xdata time;" .
+        "set timefmt \"$self->{_dateFormat}\";" .
+        #"set xrange [\"$minX\":\"$maxX\"];" .
+        #"set yrange [$minY:$maxY];" .
+        "set format x \"%m/%d %H:%M\";" .
+        "set xtics \"$minX\", $divX, \"$maxX\";" .
+        "set grid xtics;" .
+        "set xlabel \"Time\";" .
+        "set ylabel \"Exposures\";" .
+        "plot ";
+
+    my $firstIn = 1;
+    foreach my $stage (keys %$gnuplotFiles) {
+        if (!$firstIn) {print GP ",";}
+        print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"$stage\" with lines";
+        $firstIn = 0;
+    }
+
+    print GP "\n";
+    close GP;
+}
+
+###########################################################################
+#
+# Plots a time-series of pending/faults
+#
+###########################################################################
+sub plotSingleTimeSeries {
+    my ($self, $inputFile, $outputFile, $stage, $label, $fromTime, $toTime, $maxX, $minX, $maxY, $minY, $timeDiff) = @_;
+
+    $maxY = $maxY + ($maxY/10);
+    $minY = $minY - ($minY/10);
+    if ($maxY == 0) {$maxY = 1;}
+    if ($maxY == 0) {$maxY = 1;}
+    if ($minY == 0) {$minY = -1;}
+
+    my $divX = $timeDiff/2;
+
+    open (GP, "|/usr/bin/gnuplot -persist") or die "no gnuplot";
+    use FileHandle;
+    GP->autoflush(1);
+
+    print GP
+        "set term $self->{_outputFormat};" .
+        "set output \"$outputFile\";" .
+        "set title \"'$stage' stage for '$label' since $fromTime\";" .
+        "set key left top;" .
+        "set xdata time;" .
+        "set timefmt \"$self->{_dateFormat}\";" .
+        "set xrange [\"$minX\":\"$maxX\"];" .
+        "set yrange [$minY:$maxY];" .
+        "set format x \"%m/%d %H:%M\";" .
+        "set xtics \"$minX\", $divX, \"$maxX\";" .
+        "set grid;" .
+        "set xlabel \"Time\";" .
+        "set ylabel \"Exposures\";" .
+        "plot '$inputFile' using 1:4 title \"processed\" with lines lt 2," .
+        "'$inputFile' using 1:2 title \"pending\" with lines lt 3," .
+        "'$inputFile' using 1:3 title \"faults\" with lines lt 1\n";
+    close GP;
+}
+
+###########################################################################
+#
+# Plots a histogram of processed stuff
+#
+###########################################################################
+sub plotHistogram {
+    my ($self, $inputFile, $outputFile, $label, $fromTime, $toTime) = @_;
+
+    open (GP, "|/usr/bin/gnuplot -persist") or die "no gnuplot";
+    use FileHandle;
+    GP->autoflush(1);
+
+    print GP
+        "set term $self->{_outputFormat};" .
+        "set output \"$outputFile\";" .
+        "set title \"Processing status for $label since $fromTime\";" .
+        "set grid;" .
+        "set boxwidth;" .
+        "set style data histogram;" .
+        "set style histogram cluster gap 3;" .
+        "set style fill solid border -1;" .
+#        "set boxwidth 1.5;" .
+        "set bmargin 5;" .
+#        "set xtic rotate by -90;" .
+#        "plot '$inputFile' using 2:xtic(1) notitle\n";
+        "set ylabel \"Exposures\";" .
+        "plot '$inputFile' using 2:xtic(1) title \"Processed\" lt 2, '' using 3 title \"Pending\" lt 3, '' using 4 title \"Faults\" lt 1\n";
+
+    close GP;
+}
+
+1;
Index: trunk/tools/czartool/Gnuplot.pm
===================================================================
--- trunk/tools/czartool/Gnuplot.pm	(revision 28718)
+++ 	(revision )
@@ -1,163 +1,0 @@
-#!/usr/bin/perl -w
-
-use warnings;
-use strict;
-
-package czartool::Gnuplot;
-
-###########################################################################
-#
-# Constructor
-#
-###########################################################################
-sub new {
-    my $class = shift;
-    my $self = {
-        _dateFormat => shift,
-        _outputFormat => shift,
-        _outputPath => shift,
-    };
-
-    bless $self, $class;
-    return $self;
-}
-
-###########################################################################
-#
-# Sets the ouput path to be X11 
-#
-###########################################################################
-sub setOutputFormat {
-    my ($self, $outputFormat, $outputPath) = @_;
-    $self->{_outputFormat} = $outputFormat if defined($outputFormat);
-    $self->{_outputPath} = $outputPath if defined($outputPath);
-}           
-
-###########################################################################
-#
-# Sets the date format to be used
-#
-###########################################################################
-sub setDateFormat {
-    my ($self, $dateFormat) = @_;
-    $self->{_dateFormat} = $dateFormat if defined($dateFormat);
-    return $self->{_dateFormat};
-}       
-
-###########################################################################
-#
-# Plots a time-series of pending/faults
-#
-###########################################################################
-sub plotAllStagesTimeSeries {
-    my ($self, $label, $fromTime, $toTime, $maxX, $minX, $maxY, $minY, $timeDiff, $gnuplotFiles) = @_;
-
-    $maxY = $maxY + ($maxY/10);
-    $minY = $minY - ($minY/10);
-    if ($maxY == 0) {$maxY = 1;}
-    if ($maxY == 0) {$maxY = 1;}
-    if ($minY == 0) {$minY = -1;}
-
-    my $divX = $timeDiff/2;
-
-    open (GP, "|/usr/bin/gnuplot -persist") or die "no gnuplot";
-    use FileHandle;
-    GP->autoflush(1);
-
-    print GP
-        "set term $self->{_outputFormat};" .
-        "set output \"$self->{_outputPath}\";" .
-        "set title \"All stages for '$label' since $fromTime\";" .
-        "set xdata time;" .
-        "set timefmt \"$self->{_dateFormat}\";" .
-        #"set xrange [\"$minX\":\"$maxX\"];" .
-        #"set yrange [$minY:$maxY];" .
-        "set format x \"%m/%d %H:%M\";" .
-        "set xtics \"$minX\", $divX, \"$maxX\";" .
-        "set grid xtics;" .
-        "set xlabel \"Time\";" .
-        "set ylabel \"Exposures\";" .
-        "plot ";
-
-    my $firstIn = 1;
-    foreach my $stage (keys %$gnuplotFiles) {
-        if (!$firstIn) {print GP ",";}
-        print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"$stage\" with lines";
-        $firstIn = 0;
-    }
-
-    print GP "\n";
-    close GP;
-}
-
-###########################################################################
-#
-# Plots a time-series of pending/faults
-#
-###########################################################################
-sub plotSingleTimeSeries {
-    my ($self, $filePath, $stage, $label, $fromTime, $toTime, $maxX, $minX, $maxY, $minY, $timeDiff) = @_;
-
-    $maxY = $maxY + ($maxY/10);
-    $minY = $minY - ($minY/10);
-    if ($maxY == 0) {$maxY = 1;}
-    if ($maxY == 0) {$maxY = 1;}
-    if ($minY == 0) {$minY = -1;}
-
-    my $divX = $timeDiff/2;
-
-    open (GP, "|/usr/bin/gnuplot -persist") or die "no gnuplot";
-    use FileHandle;
-    GP->autoflush(1);
-
-    print GP
-        "set term $self->{_outputFormat};" .
-        "set output \"$self->{_outputPath}\";" .
-        "set title \"'$stage' stage for '$label' since $fromTime\";" .
-        "set xdata time;" .
-        "set timefmt \"$self->{_dateFormat}\";" .
-        "set xrange [\"$minX\":\"$maxX\"];" .
-        "set yrange [$minY:$maxY];" .
-        "set format x \"%m/%d %H:%M\";" .
-        "set xtics \"$minX\", $divX, \"$maxX\";" .
-        "set grid;" .
-        "set xlabel \"Time\";" .
-        "set ylabel \"Exposures\";" .
-        "plot '$filePath' using 1:4 title \"processed\" with lines lt 2," .
-        "'$filePath' using 1:2 title \"pending\" with lines lt 3," .
-        "'$filePath' using 1:3 title \"faults\" with lines lt 1\n";
-    close GP;
-}
-
-###########################################################################
-#
-# Plots a histogram of processed stuff
-#
-###########################################################################
-sub plotHistogram {
-    my ($self, $filePath, $label, $fromTime, $toTime) = @_;
-
-    open (GP, "|/usr/bin/gnuplot -persist") or die "no gnuplot";
-    use FileHandle;
-    GP->autoflush(1);
-
-    print GP
-        "set term $self->{_outputFormat};" .
-        "set output \"$self->{_outputPath}\";" .
-        "set title \"Processing status for $label since $fromTime\";" .
-        "set grid;" .
-        "set boxwidth;" .
-        "set style data histogram;" .
-        "set style histogram cluster gap 3;" .
-        "set style fill solid border -1;" .
-#        "set boxwidth 1.5;" .
-        "set bmargin 5;" .
-        "set xtic rotate by -90;" .
-#        "plot '$filePath' using 2:xtic(1) notitle\n";
-        "set ylabel \"Exposures\";" .
-        "plot '$filePath' using 2:xtic(1) title \"Processed\" lt 2, '' using 3 title \"Pending\" lt 3, '' using 4 title \"Faults\" lt 1\n";
-
-    close GP;
-}
-
-1;
Index: trunk/tools/czartool/Gpc1Db.pm
===================================================================
--- trunk/tools/czartool/Gpc1Db.pm	(revision 28718)
+++ trunk/tools/czartool/Gpc1Db.pm	(revision 28719)
@@ -73,4 +73,24 @@
 ###########################################################################
 #
+# Returns priority for this label
+#
+###########################################################################
+sub getPriority {
+    my ($self, $label) = @_;
+
+    my $query = $self->{_db}->prepare(<<SQL);
+    SELECT priority 
+        FROM Label 
+        WHERE label LIKE '$label' 
+SQL
+
+        $query->execute;
+    my $priority = scalar $query->fetchrow_array();
+    if (!$priority) {return 50000;} # assume labels not given priority in gpc1 Db have highest priority
+    return $priority;
+}
+
+###########################################################################
+#
 # Returns count of exposures with this state for this label
 #
Index: trunk/tools/roboczar.pl
===================================================================
--- trunk/tools/roboczar.pl	(revision 28718)
+++ trunk/tools/roboczar.pl	(revision 28719)
@@ -4,4 +4,5 @@
 use strict;
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use POSIX qw/strftime/;
 
 # local classes
@@ -9,13 +10,17 @@
 use czartool::Gpc1Db;
 use czartool::Pantasks;
+use czartool::Czarplot;
 
 
 my $period = 60;
 my $czarDbName = "czardb"; # TODO variables for other Db stuff, host etc
-my $save_temps = 1;
+my $save_temps = 0;
 
 my $czarDb = new czartool::CzarDb($czarDbName, "ippdb01", "ipp", "ipp");
 my $gpc1Db = new czartool::Gpc1Db("gpc1", "ippdb01", "ippuser", "ippuser");
 my $pantasks = new czartool::Pantasks();
+my $czarplot = new czartool::Czarplot($czarDb, "%Y%m%d-%H%M%S", "png", "~/", $save_temps);
+$czarDb->setDateFormat("%Y%m%d-%H%i%s");
+
 my @stages = ("chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist");
 
@@ -79,5 +84,4 @@
     my $full;
     my $faults;
-    my $currentTime;
     my $stage;
     my ($totalNew,$totalFull,$totalFaults);
@@ -88,4 +92,7 @@
     my $rows = undef;
     my $row = undef;
+    my $begin = undef;
+    my $end = undef;
+    my $priority = undef;
 
     while (1) {
@@ -94,4 +101,22 @@
         updateLabels();
         if (!$czarDb->getCurrentLabels("stdscience", \$rows)) {next;}
+
+        my $size = @{$rows};
+        if($size < 1) {
+            
+            print "* WARNING: no stdscience labels found in Db\n";
+            next;
+        }
+
+        # get priority
+        foreach $row ( @{$rows} ) {
+            my ($label) = @{$row};
+            $priority = $gpc1Db->getPriority($label);
+            $czarDb->setLabelPriority($label, $priority);
+        }
+
+        # sort out times
+        $begin =  strftime('%Y-%m-%d 07:00',localtime);
+        $end = $czarDb->getNowTimestamp();
 
         foreach $stage (@stages) {
@@ -112,5 +137,4 @@
                 $full = $gpc1Db->countExposures($label, $stage, "full");
                 $faults = $gpc1Db->countFaults($label, $stage);
-                $currentTime = time();
 
                 #printf("%ld, %s, %s, %d, %d\n", $currentTime, $label, $stage, $new, $faults);
@@ -125,8 +149,15 @@
         }
 
+        print "* Generating plots\n";
         foreach $row ( @{$rows} ) {
             my ($label) = @{$row};
+
+            $czarplot->createTimeSeries($label, undef, $begin, $end);
+            $czarplot->createHistogram($label, $begin, $end);
+
             #routineChecks($label, "1 HOUR");
         }
+            $czarplot->createTimeSeries("all_labels", undef, $begin, $end);
+            $czarplot->createHistogram("all_labels", $begin, $end);
         print "--------------------------------------------------------------------------\n";
         print "* Going to sleep\n";
