#!/usr/bin/env perl

# script to provide a summary of loading status given an upper and lower exposure ID limit

use warnings;
use strict;

use LWP::UserAgent;
use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use File::Temp qw(tempfile);
use ippToPsps::IppToPspsDb;
use ippToPsps::Gpc1Db;
use ippToPsps::DetectionBatch;

my $dvoDb = undef;
my $beginExp = undef;
my $endExp = undef;
my $verbose = undef;
my $checkNeb = undef;
my $save_temps = undef;

GetOptions(
        'startexp|b=s' => \$beginExp,
        'endexpi|e=s' => \$endExp,
        'dvodb|d=s' => \$dvoDb,
        'checkNeb|n' => \$checkNeb,
        'verbose|v' => \$verbose,
        'save_temps|s' => \$save_temps
        );


my $quit = 0;
print "\n*******************************************************************************\n";
print "* \n";
if (@ARGV) {
    $quit=1;
    print "* UNKNKOWN: option                          @ARGV\n";
}
if (!defined $beginExp) {
    $quit = 1;
    print "* REQUIRED: a begin exp_id                              -b <name>\n";
}
if (!defined $endExp) {
    $quit = 1;
    print "* REQUIRED: an end exp_id                               -e <name>\n";
}
if (!defined $dvoDb) {
    $quit = 1;
    print "* REQUIRED: a dvo Db name                               -d <name>\n";
}
if (!defined $checkNeb) {
    $checkNeb = 0;
    print "* OPTIONAL: check nebulous for unprocessed exposures    -n      (default = $checkNeb)\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";
}
print "*\n*******************************************************************************\n";

if ($quit) { exit; }

my $gpc1Db = new ippToPsps::Gpc1Db("gpc1", "ippdb01", "ippuser", "ippuser", $verbose, $save_temps);
my $ippToPspsDb = new ippToPsps::IppToPspsDb("ippToPsps", "ippdb01", "ipp", "ipp", $verbose, $save_temps);

my $exposures;
#if (!$gpc1Db->getExposureListFromDvoDb($dvoDb, \$exposures, 0)) {exit;}

my $totalExposures = $ippToPspsDb->countExposures($beginExp, $endExp, "PSPS_test", "P2", $dvoDb);
my $totalMerged = $ippToPspsDb->countMergedExposures($beginExp, $endExp, "PSPS_test", "P2", $dvoDb);

if(!$ippToPspsDb->getUnmergedExposures(\$exposures, $beginExp, $endExp, "PSPS_test", "P2", $dvoDb)) {

    print "No unmerged exposures\n";
    exit;
}

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $timeStamp = sprintf "%4d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min,$sec;

my $exposure;
my $totalUnmerged = 0;
my $totalMergeWorthy = 0;
my $totalNotMergeWorthy = 0;
my $totalProcessed = 0;
my $totalOnDatastore = 0;
my $totalLoadedToOdm = 0;
my $totalLoadFailed = 0;
my $totalMergeWorthy = 0;
my $totalUnderObjIdLimit = 0;
my $totalUnprocessed = 0;
my $totalUnprocessedAvailableSmfs = 0;
my $remainingOdmFailures = 0;
my $totalUnprocessedMissingSmfs = 0;
my $totalProcessedNotOnDatastore = 0;


open (UNPRO, ">unprocessedExposures.txt") or print "* Problem opening data file for unprocessed exposures\n";
open (ODMFAIL, ">odmFailures.txt") or print "* Problem opening data file for ODM failures\n";
open (NOTONDATASTORE, ">notOnDatastore.txt") or print "* Problem opening data file for exposures not on datastore\n";

if ($checkNeb) {

    open (MISSSMF, ">missingSmfs.txt") or print "* Problem opening data file for missing smfs\n";
    open (NOTMISSSMF, ">notMissingSmfs.txt") or print "* Problem opening data file for missing smfs\n";
}

foreach $exposure ( @{$exposures} ) {
    my ($batchId, $expId) = @{$exposure};

    if ($ippToPspsDb->isExposureMerged($expId)) {next;}

    $totalUnmerged++;
    if ($verbose) {print "$totalUnmerged | $expId \n"; }

    # processed stuff
    if ($ippToPspsDb->isExposureProcessed($expId)) {

        $totalProcessed++;

        if (!$ippToPspsDb->isExposureAlreadyPublished($expId)) {

            $totalProcessedNotOnDatastore++;
            print NOTONDATASTORE "$expId\n";
        }
        else {

            $totalOnDatastore++;

            if ($ippToPspsDb->isExposureLoadedToOdm($expId)) {

                $totalLoadedToOdm++;

                if ($ippToPspsDb->isMergeWorthy($expId)) {

                    $totalMergeWorthy++;
                }
                # not mergeworthy
                else {

                    $totalNotMergeWorthy++;

                    #if ($ippToPspsDb->didLoadFail($expId)) {

                    #    $totalLoadFailed++;
                    #}
                    if ($ippToPspsDb->isMinObjIdUnderLimit($expId)) {

                        $totalUnderObjIdLimit++;
                    }
                    else {

                        $remainingOdmFailures++;
                        print ODMFAIL "$expId\n";
                    }
                }
            }
        }
    }
    # unprocessed stuff
    else {


        $totalUnprocessed++;
        print UNPRO "$expId\n";

        # if flag is set, then attempt to get smf file from nebulous for this unprocessed exposure
        if ($checkNeb) {

            my $detectionBatch = ippToPsps::DetectionBatch->existing(
                    "GPC1",
                    $gpc1Db,
                    $batchId,
                    $ippToPspsDb,
                    ".",
                    $verbose,
                    $save_temps);


            my $path = $detectionBatch->getSmfFile();
            if (!$path) {
                print "no smf file for $expId\n";

                $totalUnprocessedMissingSmfs++;
                print MISSSMF "$expId\n";
            }
            else {

                $totalUnprocessedAvailableSmfs++;
                print NOTMISSSMF "$expId\n";

            }
        }

    }
}

close(UNPRO);
close(ODMFAIL);
close(NOTONDATASTORE);
if ($checkNeb) {

    close(MISSSMF);
    close(NOTMISSSMF);
}
my $totalOnDatastoreNotLoaded = $totalOnDatastore - $totalLoadedToOdm;
print "***********************************************************\n";
print "*    Summary of IPP->PSPS loading as of $timeStamp\n"; 
print "*    DVO Db = '$dvoDb'\n";
print "*    Exposures between $beginExp and $endExp\n";
print "*\n";
print "* Total Exposures                            = $totalExposures\n";
print "*   Merged into PSPS                         = $totalMerged\n";
print "*   Un-merged                                = $totalUnmerged\n";
print "*     Unprocessed by ippToPsps               = $totalUnprocessed\n";
if ($checkNeb) {
    print "*       Missing SMF files                    = $totalUnprocessedMissingSmfs\n";
    print "*       Available SMF files                  = $totalUnprocessedAvailableSmfs\n";
}
print "*     Processed by ippToPsps                 = $totalProcessed\n";
print "*       Not on datastore                     = $totalProcessedNotOnDatastore\n";
print "*       On datastore                         = $totalOnDatastore\n";
print "*         Not loaded to ODM                  = $totalOnDatastoreNotLoaded\n";
print "*         Loaded to ODM                      = $totalLoadedToOdm\n";
print "*           MergeWorthy                      = $totalMergeWorthy\n";
print "*           Not MergeWorthy                  = $totalNotMergeWorthy\n";
print "*             Under -30 dec limit            = $totalUnderObjIdLimit\n";
print "*             Remaining failures             = $remainingOdmFailures\n";
print "*\n";
print "***********************************************************\n";


