Index: trunk/tools/czartool/Gpc1Db.pm
===================================================================
--- trunk/tools/czartool/Gpc1Db.pm	(revision 28923)
+++ trunk/tools/czartool/Gpc1Db.pm	(revision 28924)
@@ -8,4 +8,32 @@
 use czartool::MySQLDb;
 our @ISA = qw(czartool::MySQLDb);    # inherits from MySQLDb
+
+###########################################################################
+#
+# Returns the right join table and ID to join on for for a given stage TODO make private
+#
+###########################################################################
+sub getJoinTableAndId {
+    my ($self, $stage, $joinTable, $id) = @_;
+
+    ${$id} = $stage."_id";
+
+    if ($stage eq "chip") {${$joinTable}="chipProcessedImfile";}
+    elsif ($stage eq "cam") {${$joinTable}="camProcessedExp";}
+    elsif ($stage eq "fake") {${$joinTable}="fakeProcessedImfile";}
+    elsif ($stage eq "warp") {${$joinTable}="warpSkyfile";}
+    elsif ($stage eq "stack") {${$joinTable}="stackSumSkyfile";}
+    elsif ($stage eq "diff") {${$joinTable}="diffSkyfile";}
+    elsif ($stage eq "magic") {${$joinTable}="magicNodeResult";}
+    elsif ($stage eq "magicDS") {${$id} = "magic_ds_id"; ${$joinTable}="magicDSFile";}
+    elsif ($stage eq "dist") {${$joinTable}="distComponent";}
+    else {
+
+        print "* ERROR: could nod find joinTable and ID for '$stage' stage\n";
+        return 0;
+    }
+
+    return 1;
+}
 
 ###########################################################################
@@ -30,5 +58,4 @@
 }
 
-
 ###########################################################################
 #
@@ -37,21 +64,11 @@
 ###########################################################################
 sub countFaults {
-    my ($self, $label, $stage) = @_;
+    my ($self, $label, $stage, $state) = @_;
 
-
-    my $table = getTableForStage($self, $stage);
-    my $joinTable;
-    my $id = $stage."_id";
-
-    if ($stage eq "chip") {$joinTable="chipProcessedImfile";}
-    elsif ($stage eq "cam") {$joinTable="camProcessedExp";}
-    elsif ($stage eq "fake") {$joinTable="fakeProcessedImfile";}
-    elsif ($stage eq "warp") {$joinTable="warpSkyfile";}
-    elsif ($stage eq "stack") {$joinTable="stackSumSkyfile";}
-    elsif ($stage eq "diff") {$joinTable="diffSkyfile";}
-    elsif ($stage eq "magic") {$joinTable="magicNodeResult";}
-    elsif ($stage eq "magicDS") {$id = "magic_ds_id"; $joinTable="magicDSFile";}
-    elsif ($stage eq "dist") {$joinTable="distComponent";}
-    else {return -1;}
+    my $table = undef;
+    my $joinTable = undef;
+    my $id = undef;
+    $table = getTableForStage($self, $stage);
+    if (!getJoinTableAndId($self, $stage, \$joinTable, \$id)) {return -1;}
 
     my $faultCol =  $joinTable.".fault";
@@ -64,5 +81,5 @@
         WHERE label LIKE '$label'
         AND $faultCol != 0
-        AND $stateCol = 'new'
+        AND $stateCol = '$state'
 SQL
 
@@ -85,5 +102,5 @@
 SQL
 
-        $query->execute;
+    $query->execute;
     my $priority = scalar $query->fetchrow_array();
     if (!$priority) {return 50000;} # assume labels not given priority in gpc1 Db have highest priority
@@ -100,15 +117,47 @@
 
     my $table = getTableForStage($self, $stage);
-    if ($state eq "fault") {return countFaults($stage);}
+    my $query = undef;
+
+    if ($state eq "update") {
+
+        if ($stage eq "dist") {return 0;}
+        my $joinTable = undef;
+        my $id = undef;
+        if (!getJoinTableAndId($self, $stage, \$joinTable, \$id)) {return -1;}
+
+        if ($stage eq "chip" || $stage eq "fake" || $stage eq "warp" || $stage eq "diff" || $stage eq "magicDS") {
+            $query = $self->{_db}->prepare(<<SQL);
+            SELECT COUNT(state)
+                FROM $table JOIN $joinTable 
+                USING($id) 
+                WHERE label LIKE '$label'
+                AND state = 'update' 
+                AND data_state = 'update';
+SQL
+        }
+        else {
+        
+            $query = $self->{_db}->prepare(<<SQL);
+            SELECT COUNT(state)
+                FROM $table JOIN $joinTable 
+                USING($id) 
+                WHERE label LIKE '$label'
+                AND state = 'update'; 
+SQL
+        }
+
+    }
+    else {
+
+        $query = $self->{_db}->prepare(<<SQL);
+        SELECT COUNT(state)  
+            FROM $table 
+            WHERE label LIKE '$label' 
+            AND state = '$state'
+SQL
+    }
 
 
-    my $query = $self->{_db}->prepare(<<SQL);
-    SELECT count(state)  
-        FROM $table 
-        WHERE label LIKE '$label' 
-        AND state = '$state'
-SQL
-
-        $query->execute;
+    $query->execute;
     return scalar $query->fetchrow_array();
 }
