IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 29093


Ignore:
Timestamp:
Sep 1, 2010, 12:33:59 PM (16 years ago)
Author:
rhenders
Message:

Neatened code and now deletes from file system when a batch has reached a state of 'merged'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/perl/checkOdmStatus.pl

    r29082 r29093  
    55
    66use LWP::UserAgent;
     7use IPC::Cmd 0.36 qw( can_run run );
     8use IPC::Cmd 0.36 qw( can_run run );
    79use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
    810use XML::LibXML;
     
    1719my $toTime = undef;
    1820my $product = undef;
     21my $filePath = undef;
    1922
    2023
     
    2427        'to|t=s' => \$toTime,
    2528        'product|p=s' => \$product,
     29        'location|l=s' => \$filePath,
    2630        'verbose|v' => \$verbose,
    2731        'save_temps|s' => \$save_temps
     
    4145    $toTime = "2099-12-31";
    4246    print "* OPTIONAL: to time                         -t <dateTime>        (default = $toTime)\n";
     47}
     48if (!defined $filePath) {
     49    print "* OPTIONAL: location for files to be deleted    -l <path>\n";
    4350}
    4451if (!defined $verbose) {
     
    7279    my $numOfBatches;
    7380
    74     if (defined $singleBatch ) { $numOfBatches = $ippToPspsDb->getSingleBatchOnDatastore($singleBatch, \$batches);}
    75     else { $numOfBatches = $ippToPspsDb->getListOfBatchesOnDatastore(\$batches, $fromTime, $toTime);}
     81    if (defined $singleBatch ) { $numOfBatches = $ippToPspsDb->getBatch($singleBatch, \$batches);}
     82    else { $numOfBatches = $ippToPspsDb->getBatches(\$batches, $fromTime, $toTime);}
    7683
    7784    if ($numOfBatches < 1) {return 0;}
     
    8491    # loop round batches
    8592    my $batch;
     93    my $numBatchesToCheck = 0;
    8694    my $numChecked = 0;
     95    my $numRemovedFromDatastore = 0;
     96    my $numDeleted = 0;
    8797    foreach $batch ( @{$batches} ) {
    88         my ($timestamp, $expId, $batchId, $surveyType, $deleted) =  @{$batch};
    89 
    90         if (checkBatch($timestamp, $expId, $batchId, $surveyType, $deleted)) {$numChecked++;}
    91 
     98        my ($timestamp, $expId, $batchId, $surveyType, $deleted, $dvoDb, $processed, $onDatastore, $loadedToOdm, $mergeWorthy, $merged) =  @{$batch};
     99
     100        if (!$processed) {next;}
     101
     102        $numBatchesToCheck++;
     103
     104        my $batchName = getBatchName($batchId);
     105
     106        # if not merged then update by polling ODM for status
     107        if (!$merged) {
     108
     109            if (checkODM($batchName, \$loadedToOdm, \$mergeWorthy, \$merged)) {$numChecked++;}
     110        }
     111
     112        # delete from datastore
     113        if (defined $product && !$deleted && $loadedToOdm && $mergeWorthy) {
     114
     115            $deleted = $datastore->remove($batchName);
     116            if ($deleted) {
     117                $ippToPspsDb->setBatchAsDeleted($batchId, $expId);
     118                $numRemovedFromDatastore++;
     119            }
     120        }
     121
     122        # if merged and already removed from datastore then delete files
     123        if (defined $filePath && $merged && $deleted) {
     124
     125            print "$command\n";
     126            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     127                run(command => $command, verbose => $verbose);
     128            if (!$success) {print "* Unable to run: $command\n";}
     129            else {$numDeleted++;}
     130        }
     131
     132        # update database
     133        $ippToPspsDb->updateODMStatus($batchId, $expId, $loadedToOdm, $mergeWorthy, $merged, $deleted);
     134        printf( "| %18s  | %11s  | %10d   |    %6s      |    %6s     | %6s  |  %5s   |\n",
     135                $timestamp,
     136                $batchName,
     137                $expId,
     138                $loadedToOdm ? "yes" : "no",
     139                $mergeWorthy ? "yes" : "no",
     140                $merged ? "yes" : "no",
     141                $deleted ? "yes" : "no");
    92142    }
     143
    93144    printf("+----------------------+--------------+--------------+----------------+---------------+---------+----------+\n");
    94145
    95     printf( "* Successfully checked %d batch%s out of %d\n", $numChecked, ($numChecked==1) ? "" : "es", $numOfBatches);
    96 
    97 }
    98 
    99 ######################################################################################y
     146    printf( "* Successfully checked %d batch%s out of %d\n", $numChecked, ($numChecked==1) ? "" : "es", $numBatchesToCheck);
     147    printf( "* Successfully removed %d batch%s from the datastore\n", $numRemovedFromDatastore, ($numRemovedFromDatastore==1) ? "" : "es");
     148    printf( "* Successfully deleted %d batch%s from local file system\n", $numDeleted, ($numDeleted==1) ? "" : "es");
     149
     150}
     151
     152########################################################################################
     153#
     154# Returns batch name from batch_id TODO needs to be a batch class
     155#
     156########################################################################################
     157sub getBatchName {
     158    my ($batchId) = @_;
     159
     160    return sprintf("B%08d", $batchId);
     161}
     162
     163########################################################################################
    100164#
    101165# Check a single batch
    102166#
    103167########################################################################################
    104 sub checkBatch {
    105     my ($timestamp, $expId, $batchId, $surveyType, $deleted) = @_;
    106 
    107 
    108     my $batchFilter = sprintf("B%08d", $batchId);
     168sub checkODM {
     169    my ($batchName, $loadedToOdm, $mergeWorthy, $merged) = @_;
     170
    109171    my $statusFilter = "*";
    110172
    111173    my $response = $ua->post($odmUrl,
    112             [batchNameFilter => "%".$batchFilter,
     174            [batchNameFilter => "%".$batchName,
    113175            statusFilter => $statusFilter,
    114176            fromFilter => "2010-01-01",
     
    119181    if ($response->code != 200) {
    120182
    121         print "Problem connecting to web service for '$batchFilter', HTTP response status: ".$response->status_line."\n";
    122         return;
     183        print "Problem connecting to web service for '$batchName', HTTP response status: ".$response->status_line."\n";
     184        return 0;
    123185    }
    124186    #        print( "HTTP response status: ".$response->content."\n" );
     
    128190    close($tempFile);
    129191
    130     my $loadedToOdm = 0;
    131     my $mergeWorthy = 0;
    132     my $mergeCompleted = 0;
    133 
    134     parseXml($tempName, \$loadedToOdm, \$mergeWorthy, \$mergeCompleted);
    135 
    136     # delete from datastore
    137     if(defined $product) {
    138    
    139         if (!$deleted && $loadedToOdm && $mergeWorthy) {
    140        
    141             $deleted = $datastore->remove($batchFilter);
    142             if ($deleted) {
    143                 $ippToPspsDb->setBatchAsDeleted($batchId, $expId);
    144             }
    145         }
    146     }
    147    
    148     printf( "| %18s  | %11s  | %10d   |    %6s      |    %6s     | %6s  |  %5s   |\n",
    149             $timestamp,
    150             $batchFilter,
    151             $expId,
    152             $loadedToOdm ? "yes" : "no",
    153             $mergeWorthy ? "yes" : "no",
    154             $mergeCompleted ? "yes" : "no",
    155             $deleted ? "yes" : "no");
    156 
    157     $ippToPspsDb->updateODMStatus($batchId, $expId, $loadedToOdm, $mergeWorthy, $mergeCompleted, $deleted);
    158 
     192    ${$loadedToOdm} = 0;
     193    ${$mergeWorthy} = 0;
     194    ${$merged} = 0;
     195
     196    parseXml($tempName, $loadedToOdm, $mergeWorthy, $merged);
     197
     198    return 1;
    159199}
    160200
     
    165205########################################################################################
    166206sub parseXml {
    167     my ($xmlFile, $loadedToOdm, $mergeWorthy, $mergeCompleted) = @_;
     207    my ($xmlFile, $loadedToOdm, $mergeWorthy, $merged) = @_;
    168208
    169209    my $parser = XML::LibXML->new;
     
    174214    ${$loadedToOdm} = 0;
    175215    ${$mergeWorthy} = 0;
    176     ${$mergeCompleted} = 0;
     216    ${$merged} = 0;
    177217
    178218    if ($result =~ m/LoadStarted/) { ${$loadedToOdm} = 1;}
    179219    if ($result =~ m/MergeWorthy/) { ${$loadedToOdm} = 1; ${$mergeWorthy} = 1;}
    180     if ($result =~ m/Merge[1-9]Completed/) { ${$loadedToOdm} = 1; ${$mergeWorthy} = 1; ${$mergeCompleted} = 1;}
    181 }
    182 
     220    if ($result =~ m/Merge[1-9]Completed/) { ${$loadedToOdm} = 1; ${$mergeWorthy} = 1; ${$merged} = 1;}
     221}
     222
Note: See TracChangeset for help on using the changeset viewer.