Index: /trunk/ippToPsps/perl/checkOdmStatus.pl
===================================================================
--- /trunk/ippToPsps/perl/checkOdmStatus.pl	(revision 28897)
+++ /trunk/ippToPsps/perl/checkOdmStatus.pl	(revision 28897)
@@ -0,0 +1,148 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use LWP::UserAgent;
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use XML::LibXML;
+use File::Temp qw(tempfile);
+use ippToPsps::IppToPspsDb;
+
+my $singleBatch = undef;
+my $verbose = undef;
+my $save_temps = undef;
+
+GetOptions(
+        'batch|b=s' => \$singleBatch,
+        'verbose|v' => \$verbose,
+        'save_temps|s' => \$save_temps
+        );
+
+if (!defined $singleBatch) {
+    print "* OPTIONAL: a single batch                  -b                   (default = none)\n";
+}
+if (!defined $verbose) {
+    $verbose = 0;
+    print "* OPTIONAL: run in verbose mode             -v                   (default = $verbose)\n";
+}
+if (!defined $save_temps) {
+    $save_temps = 0;
+    print "* OPTIONAL: keep temp files                 -t                   (default = $save_temps)\n";
+}
+
+
+my $ippToPspsDb = new ippToPsps::IppToPspsDb("ippToPsps", "ippdb01", "ipp", "ipp", $verbose, $save_temps);
+my $fromFilter = "2010-08-01";
+my $toFilter = "2099-12-31";
+my $odmUrl = "http://web01.psps.ifa.hawaii.edu/a01/OdmWebService/OdmWebService.asmx/GetBatchStatus";
+my $ua = LWP::UserAgent->new;
+$ua->timeout(15);
+$ua->env_proxy;
+
+
+process();
+
+#######################################################################################
+# 
+# Loops through all processed exposures and checks against the ODM, then deletes if necessary 
+# 
+########################################################################################
+sub process {
+
+    my $batches;
+    my $numOfBatches;
+
+    if (defined $singleBatch ) { $numOfBatches = $ippToPspsDb->getSingleBatch($singleBatch, \$batches);}
+    else { $numOfBatches = $ippToPspsDb->getBatchList(\$batches);}
+
+    if ($numOfBatches < 1) {return 0;}
+
+    print "\n";
+    printf("+--------------+--------------+----------------+---------------+---------+\n");
+    printf("|    Batch     |  Exposure ID | Loaded to ODM? | Merge worthy? | Merged? |\n");
+    printf("+--------------+--------------+----------------+---------------+---------+\n");
+
+    # loop round batches
+    my $batch;
+    my $numChecked = 0;
+    foreach $batch ( @{$batches} ) {
+        my ($expId, $batchId, $surveyType) =  @{$batch};
+
+if (checkBatch($expId, $batchId, $surveyType)) {$numChecked++;}
+
+    }
+    printf("+--------------+--------------+----------------+---------------+---------+\n");
+
+    printf( "* Successfully checked %d batch%s out of %d\n", $numChecked, ($numChecked==1) ? "" : "es", $numOfBatches);
+
+}
+
+######################################################################################y
+# 
+# Check a single batch
+# 
+########################################################################################
+sub checkBatch {
+    my ($expId, $batchId, $surveyType) = @_;
+
+
+    my $batchFilter = sprintf("B%08d", $batchId);
+    my $statusFilter = "*";
+
+    my $response = $ua->post($odmUrl,
+            [batchNameFilter => $batchFilter,
+            statusFilter => $statusFilter,
+            fromFilter => $fromFilter,
+            toFilter => $toFilter]
+            );
+
+    # '200' is the 'OK' response
+    if ($response->code != 200) {
+
+        print "Problem connecting to web service for '$batchFilter', HTTP response status: ".$response->status_line."\n";
+        return;
+    }
+    #        print( "HTTP response status: ".$response->content."\n" );
+
+    my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_odmXml.XXXX", UNLINK => !$save_temps);
+    print $tempFile $response->content;
+    close($tempFile);
+
+    my $loadedToOdm;
+    my $mergeWorthy;
+    my $mergeCompleted;
+
+    parseXml($tempName, \$loadedToOdm, \$mergeWorthy, \$mergeCompleted);
+    printf( "| %11s  | %10d   |    %6s      |    %6s     | %6s  |\n", 
+            $batchFilter, 
+            $expId, 
+            $loadedToOdm ? "yes" : "no", 
+            $mergeWorthy ? "yes" : "no", 
+            $mergeCompleted ? "yes" : "no");
+
+    $ippToPspsDb->updateODMStatus($batchId, $expId, $loadedToOdm, $mergeWorthy, $mergeCompleted);
+}
+
+######################################################################################y
+# 
+# Parses ODM XML
+# 
+########################################################################################
+sub parseXml {
+    my ($xmlFile, $loadedToOdm, $mergeWorthy, $mergeCompleted) = @_;
+
+    my $parser = XML::LibXML->new;
+    my $doc = $parser->parse_file($xmlFile);
+    my $xc = XML::LibXML::XPathContext->new($doc);
+    $xc->registerNs('ArrayOfOdmBatchState', 'PanSTARRS.Services.OdmWebService');
+    my $result = $xc->findvalue('//ArrayOfOdmBatchState:Message');
+    ${$loadedToOdm} = 0;
+    ${$mergeWorthy} = 0;
+    ${$mergeCompleted} = 0;
+
+    if ($result =~ m/LoadStarted/) { ${$loadedToOdm} = 1;}
+    if ($result =~ m/MergeWorthy/) { ${$loadedToOdm} = 1; ${$mergeWorthy} = 1;}
+    if ($result =~ m/Merge[1-9]Completed/) { ${$loadedToOdm} = 1; ${$mergeWorthy} = 1; ${$mergeCompleted} = 1;}
+}
+
Index: unk/ippToPsps/perl/cleanup.pl
===================================================================
--- /trunk/ippToPsps/perl/cleanup.pl	(revision 28896)
+++ 	(revision )
@@ -1,168 +1,0 @@
-#!/usr/bin/env perl
-
-use warnings;
-use strict;
-
-use LWP::UserAgent;
-use PS::IPP::Config 1.01 qw( :standard );
-use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
-use Pod::Usage qw( pod2usage );
-use XML::LibXML;
-use File::Temp qw(tempfile);
-use DBI;
-use constant DB_SOCKET => '/var/run/mysqld/mysqld.sock';
-
-
-my $save_temps = 0;
-# get user args
-#GetOptions(
-#        'batchid|d=s' => \$batchId,
-#        'odmurl|u=s' => \$odmUrl,
-#        ) or pod2usage( 2 );
-
-#pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-
-# tell off user for not providing proper args
-#pod2usage(
-#        -msg => "\n   Required options:\n\n".
-#        "--batchid <batchid>\n".
-#        "\n   Optional:\n\n".
-#        "--odmurl <URL to ODM eg http://web01.psps.ifa.hawaii.edu/a01/OdmWebService/OdmWebService.asmx/GetBatchStatus>\n".
-#        -exitval => 3
-#        ) unless
-#defined $batchId and
-#defined $odmUrl;
-
-
-process();
-
-#######################################################################################
-# 
-# Loops through all processed exposures and checks against the ODM, then deletes if necessary 
-# 
-########################################################################################
-sub process {
-
-my $ippToPspsDb = connectToDb("ippToPsps", "ippdb01", "ipp", "ipp") or return 0;
-
-    my $query = $ippToPspsDb->prepare(<<SQL);
-
-    SELECT exp_id, batch_id, survey_id 
-        FROM batches 
-        WHERE processed = 1 
-        AND on_datastore = 1;
-SQL
-
-    $query->execute;
-my $batchFilter;
-my $statusFilter = "*";
-my $fromFilter = "2001-01-01";
-my $toFilter = "2099-12-31";
-
-my $odmUrl = "http://web01.psps.ifa.hawaii.edu/a01/OdmWebService/OdmWebService.asmx/GetBatchStatus";
-my $ua = LWP::UserAgent->new;
-my $loadedToOdm;
-my $mergeWorthy;
-my $mergeCompleted;
-$ua->timeout(15);
-$ua->env_proxy;
-
-# loop round exposures
-while (my @row = $query->fetchrow_array()) {
-    my ($expId, $batchId, $surveyType) = @row;
-
-    $batchFilter = sprintf("%s_P2_J%06d*", $surveyType, $batchId);
-
-    my $response = $ua->post($odmUrl,
-            [batchNameFilter => $batchFilter,
-            statusFilter => $statusFilter,
-            fromFilter => $fromFilter,
-            toFilter => $toFilter]
-            );
-
-    # '200' is the 'OK' response
-    if ($response->code != 200) {
-
-        print "Problem connecting to web service for $batchId\n"; 
-        print( "HTTP response status: ".$response->status_line."\n" );
-        next;
-    }
-
-    my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_odmXml.XXXX", UNLINK => !$save_temps);
-    print $tempFile $response->content;
-    close($tempFile);
-
-    parseXml($tempName, \$loadedToOdm, \$mergeWorthy, \$mergeCompleted);
-    print "$batchId, $expId = " . $loadedToOdm . " " . $mergeWorthy . " " . $mergeCompleted . "\n";
-    updateDb($ippToPspsDb, $batchId, $expId, $loadedToOdm, $mergeWorthy, $mergeCompleted);
-
-    #if ( $response->content =~ m/MergeWorthy/i) {print "$batchId is MergeWorthy\n";}
-    #else {print "$batchId is NOT MergeWorthy\n";};
-
-}
-
-$ippToPspsDb->disconnect();
-}
-
-#######################################################################################
-#
-# Updates an existing database record 
-#
-#######################################################################################
-sub updateDb {
-    my ($db, $batchId, $expId, $loadedToOdm, $mergeWorthy, $mergeCompleted) = @_;
-
-    my $query = $db->prepare(<<SQL);
-
-    UPDATE batches 
-        SET loaded_to_ODM = $loadedToOdm, merge_worthy = $mergeWorthy, merged = $mergeCompleted 
-        WHERE batch_id = $batchId 
-        AND exp_id = $expId;
-SQL
-
-        $query->execute; # TODO check response of these
-}
-
-######################################################################################y
-# 
-# Connects to a db
-# 
-########################################################################################
-sub connectToDb {
-    my ($dbname,$dbserver,$dbuser,$dbpass) = @_;
-
-
-    my $db = DBI->connect("DBI:mysql:database=${dbname};host=${dbserver};" .
-            "mysql_socket=" . DB_SOCKET(),
-            ${dbuser},${dbpass},
-            { RaiseError => 1, AutoCommit => 1}
-            );
-
-    printf("* Connection to '$dbname' on '$dbserver': %s\n", $db ? "success" : "FAILED ($DBI::errstr)");
-
-    return $db;
-}
-
-######################################################################################y
-# 
-# Parses ODM XML
-# 
-########################################################################################
-sub parseXml {
-    my ($xmlFile, $loadedToOdm, $mergeWorthy, $mergeCompleted) = @_;
-
-    my $parser = XML::LibXML->new;
-    my $doc = $parser->parse_file($xmlFile);
-    my $xc = XML::LibXML::XPathContext->new($doc);
-    $xc->registerNs('ArrayOfOdmBatchState', 'PanSTARRS.Services.OdmWebService');
-    my $result = $xc->findvalue('//ArrayOfOdmBatchState:Message');
-
-    ${$loadedToOdm} = 0;
-    ${$mergeWorthy} = 0;
-    ${$mergeCompleted} = 0;
-
-    if ($result =~ m/LoadStarted/) { ${$loadedToOdm} = 1;}
-    if ($result =~ m/MergeWorthy/) { ${$loadedToOdm} = 1; ${$mergeWorthy} = 1;}
-    if ($result =~ m/Merge[1-9]Completed/) { ${$loadedToOdm} = 1; ${$mergeWorthy} = 1; ${$mergeCompleted} = 1;}
-}
-
