#!/usr/bin/env perl 
#----------------------------------------------------------------------------
#
# Name: otfr_stats_rs.pl
#
# This perl script fills part of a database record in the "otfr_request_stats"
# relation using information gleaned from an OTFC request file.
# This script uses these inputs to update the database fields:
#
#    resp_time - time at which response file is sent back to DADS
#    output_file_count - count of output files sent back to DADS
#    output_data_set_size - size of output file set (in KBytes)
#    cal_success - T (true) if calibration completed OK
#
# Usage:
#	% otfr_stats_rs.pl otfcid disk_space out_file_count cal_status resp_time
#
#	Note that the following environment variables (logicals) are required:
#       ARCH_SERVER and ARCH_DB
#
# History:
# Date     OPR      Who         Reason
# -------- -------- ----------  ---------------------------------------------
# 12/21/00 43165    MSwam       first version
# 04/04/03 43915    WMiller     DADS 10.0 support
# 03/25/10 64274    MSwam       Replace ST_DBlib with DBI
#
#----------------------------------------------------------------------------
# adjust the Perl include dir using PATH
$PATH=      $ENV{"PATH"};
@parts = split /:/, $PATH;
while (defined($val = pop(@parts))) {
   if (substr($val,0,1) eq "/") { unshift(@INC, $val)}
}

# get the external variables
$ARCH_SERVER=      $ENV{"ARCH_SERVER"};
$ARCH_DB=      lc($ENV{"ARCH_DB"});

$usage = "USAGE: otfc_stats_rs.pl otfcid rootname disk_space out_file_count cal_success resp_time";

require 'printmsg.pl';  # PrintMsg routine
require 'do_dbi_pkg.pl';    # RunSql routine

# begin

    #
    # verify arguments are present
    # (#argv=0 when there is exactly one argument)
    #
    if ($#ARGV != 5) {  
        die "$usage\n";
    }
    $otfc_id    = $ARGV[0];
    $rootname   = uc($ARGV[1]);
    $disk_space = $ARGV[2];
    $out_file_count = $ARGV[3];
    $cal_success = $ARGV[4];
    $resp_time = $ARGV[5];

    #
    # initialize database
    PrintMsg ("I","--- start --- update response in otfr_request_stats ----------");
	 
    my $EXIT_FAILURE = 1;
    $db = DoDBIopen( $ARCH_SERVER, $ARCH_DB, $EXIT_FAILURE);

    $query = <<"EOQ";
UPDATE otfr_request_stats
SET    ors_req_response_time = \'$resp_time\',
       ors_out_file_count = $out_file_count,
       ors_out_data_set_size = $disk_space,
       ors_cal_success = \'$cal_success\'
WHERE  ors_otfr_request_id = \'$otfc_id\'
AND    ors_data_set_name = \'$rootname\'
EOQ

    $count = DoDBI( $db, $query );
    if ($count == 1) {
        PrintMsg("I","otfr_stats_rs: 1 record updated");
    }
    else {
        PrintMsg("E","otfr_stats_rs: $count record updated\n");
    }

#---------------------------------------------------------------------
# end of all queries
#---------------------------------------------------------------------
    DoDBIclose($db);
    PrintMsg ("I","---  end  --- update response in otfr_request_stats ----------");
    exit( 0);  # EXIT_SUCCESS
