Changeset 228

Show
Ignore:
Timestamp:
07/31/08 08:45:46
Author:
dan
Message:

Changes: Added handling of time zones. The data is stamped in GMT in the database, so we want to be able to move the time frame to be EST for us east coasters.
Added command line option -TimeZone? to allow for compensation away from GMT.

Sub: TabulatePlatformResults()
Changes: Added a query URL to allow the ability to drill down on a sensor/platform to see the data for the time period.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • obskml/trunk/QAQC/CalcPlatformUptimePercentage.pl

    r217 r228  
    11####################################################################################################### 
    22#Revisions 
     3#Rev: 1.2.0.0 
     4#Author: DWR 
     5#Changes: Added handling of time zones. The data is stamped in GMT in the database, so we want to be able to move the time 
     6# frame to be EST for us east coasters. 
     7# Added command line option -TimeZone to allow for compensation away from GMT. 
     8# 
     9#Sub: TabulatePlatformResults() 
     10#Changes: Added a query URL to allow the ability to drill down on a sensor/platform to see the data for the time period. 
     11# 
    312#Rev: 1.1.0.0 
    413#Author: DWR 
     
    3039#of shell commands. 
    3140 
    32 use constant MICROSOFT_PLATFORM => 1
     41use constant MICROSOFT_PLATFORM => 0
    3342 
    3443#1 enables the various debug print statements, 0 turns them off. 
    3544use constant USE_DEBUG_PRINTS   => 0; 
    3645 
     46use constant SECONDS_PER_DAY => ( 24 * 60 * 60 ); 
    3747###path config############################################# 
    3848my $strDBName     = ''; 
     
    5969            "TstProfFeed=s", 
    6070            "UpdateDatabase:s", 
    61             "Date:s" ); 
     71            "Date:s",  
     72            "TimeZone:s" ); 
    6273 
    6374my $strWorkingDir   = $CommandLineOptions{"WorkingDir"}; 
     
    7283              "--TstProfFeed provides the url where the test_profiles.xml file resides.\n". 
    7384              "--UpdateDatabase specifies if the metrics table in the database is to be updated. Values are \"yes\" to update, or \"no\". This value is optional, default is \"no\" \n". 
    74               "--Date specifies the day, in YYYY-MM-DD format, to get the stats for, this is optional and if not provided the default is the last full day( date '1 day ago')\n" ); 
     85              "--Date specifies the day, in YYYY-MM-DD format, to get the stats for, this is optional and if not provided the default is the last full day( date '1 day ago')\n". 
     86              "--TimeZone optional timezone argument. The data dates in the DB are all in GMT. Options are EASTERN, CENTRAL, MOUNTAIN, PACIFIC. Default is EASTERN."); 
     87               
    7588} 
    7689#Optional command line arguments. 
     
    8295 
    8396my $strDate         = $CommandLineOptions{"Date"}; 
     97#DWR v1.2.0.0 
     98my $strEndDate; 
    8499if( !length( $strDate ) ) 
    85100{ 
     
    88103    $strDate = `date --d=\"1 days ago\" +%Y-%m-%d`; 
    89104    chomp( $strDate ); 
     105     
     106    $strEndDate = `date +%Y-%m-%d`; 
     107    chomp( $strEndDate ); 
    90108  } 
    91109  else 
     
    93111    $strDate = `\\UnixUtils\\usr\\local\\wbin\\date.exe --d=\"1 days ago\" +%Y-%m-%d`; 
    94112    chomp( $strDate );    
     113 
     114    $strEndDate = `\\UnixUtils\\usr\\local\\wbin\\date.exe +%Y-%m-%d`; 
     115    chomp( $strEndDate ); 
    95116  }   
    96117} 
    97  
     118my $strTimeZone = $CommandLineOptions{"TimeZone"}; 
     119if( length( $strTimeZone ) == 0 ) 
     120
     121  $strTimeZone = 'EASTERN'; 
     122
    98123########################################################### 
    99124 
     
    179204 
    180205 
    181 my $strBeginDateRange = $strDate.'T00:00:00'; 
    182 my $strEndDateRange   = $strDate.'T24:00:00'; 
     206######################################################################################################### 
     207#DWR v1.2.0.0 
     208#Add support for time zone handling. 
     209 
     210#if you want to reference other time zones, add them here 
     211my %time_zone = ('GMT',0,'EST',-5,'EDT',-4,'CST',-6,'CDT',-5,'MST',-7,'MDT',-6,'PST',-8,'PDT',-7); 
     212#print 'time_zone='.$time_zone{$strTimeZone}."\n"; 
     213 
     214#daylight savings time consideration 
     215my ($temp_sec,$temp_min,$temp_hour,$temp_mday,$temp_mon,$temp_year,$temp_wday,$temp_yday,$isdst) = localtime(time); 
     216 
     217#correct $strTimeZone depending on whether $isdst is set for EASTERN,etc 
     218if ($strTimeZone eq 'EASTERN')  
     219
     220  if ($isdst)  
     221  {  
     222    $strTimeZone = 'EDT'; 
     223  }  
     224  else 
     225  {  
     226    $strTimeZone = 'EST';  
     227  } 
     228
     229if ($strTimeZone eq 'CENTRAL') 
     230
     231  if ($isdst) 
     232  {  
     233    $strTimeZone = 'CDT';  
     234  }  
     235  else 
     236  {  
     237    $strTimeZone = 'CST';  
     238  } 
     239
     240if ($strTimeZone eq 'MOUNTAIN') 
     241
     242  if ($isdst) 
     243  {  
     244    $strTimeZone = 'MDT'; 
     245  } 
     246  else 
     247  { 
     248    $strTimeZone = 'MST';  
     249  } 
     250
     251if ($strTimeZone eq 'PACIFIC') 
     252
     253  if ($isdst) 
     254  {  
     255    $strTimeZone = 'PDT';  
     256  }  
     257  else 
     258  {  
     259    $strTimeZone = 'PST';  
     260  } 
     261
     262my $iTimeZoneShift = -1*$time_zone{$strTimeZone}; 
     263 
     264######################################################################################################### 
    183265 
    184266my %PlatformIDs; 
     
    205287                my $iUpdateInterval = sprintf( "%d", $obs->find('UpdateInterval') ); 
    206288       
    207       my $strStart = $strDate."T00:00:00"; 
    208       my $strEnd   = $strDate."T24:00:00"; 
    209       QueryPlatformSensorReportCount( $DB, $strPlatformID, $strStart, $strEnd, $strObsName, $iUpdateInterval, \%PlatformIDs ); 
     289      my $strStart = $strDate.'T00:00:00'; 
     290      my $strEnd   = $strEndDate.'T00:00:00'; 
     291      #DWR v1.2.0.0 
     292      #Added $iTimeZoneShift to compensate for time zones. Measurements in the DB are all stored in GMT. 
     293      QueryPlatformSensorReportCount( $DB, $strPlatformID, $strStart, $strEnd, $strObsName, $iUpdateInterval, \%PlatformIDs, $iTimeZoneShift ); 
    210294    }#obstypes 
    211295    #PlotPlatformResults( $DB, $strPlatformID, \%PlatformIDs, $strWorkingDir ); 
     
    244328# 5. $SensorIDs is a reference to a hash which will be populated as: key=Sensor ID from sensor.m_type_id column, value=Sensor Name from sensor.short_name column. 
    245329######################################################################################################################## 
    246 sub QueryReportingSensorsForPlatform #( $DB, $strPlatformID, $strStartDate, $strEndDate, \%SensorIDs ) 
     330=comment 
     331sub QueryReportingSensorsForPlatform #( $DB, $strPlatformID, $strStartDate, $strEndDate, \%SensorIDs, $iTimeZoneShift ) 
    247332{ 
    248333  my $DBI           = shift @_; 
     
    251336  my $strEndDate    = shift @_; 
    252337  my $SensorIDs     = shift @_; 
    253     
     338  my $iTimeZoneShift= shift @_; 
     339 
     340  #m_date >= '$strStartDate' AND m_date < '$strEndDate' AND  
     341 
    254342  my $strSQL = "SELECT DISTINCT(sensor.m_type_id), sensor.short_name  
    255343                FROM multi_obs 
    256344                LEFT JOIN sensor on sensor.row_id=multi_obs.sensor_id 
    257                 WHERE m_date >= '$strStartDate' AND m_date < '$strEndDate' AND platform_handle = '$strPlatform' 
     345                WHERE  
     346                WHERE datetime(m_date) >= datetime('$strStartDate','-$iTimeZoneShift hours')  AND  
     347                      datetime(m_date) < datetime('$strEndDate','-$iTimeZoneShift hours')     AND  
     348                  platform_handle = '$strPlatform' 
    258349                ORDER BY sensor.row_id ASC;"; 
    259350   
     
    277368  return( $iCnt ); 
    278369} 
    279  
     370=cut 
    280371######################################################################################################################## 
    281372# QueryPlatformSensorReportCount 
     
    288379# 6. $PlatformInfo is a reference to a hash which will be populated with per platform/sensor information. 
    289380######################################################################################################################## 
    290 sub QueryPlatformSensorReportCount #( $DB, $strPlatformID, $strStart, $strEnd, $strSensorName, $iUpdateInterval, \%PlatformIDs
    291 { 
    292   my ( $DB, $strPlatformID, $strStart, $strEnd, $strSensorName, $iUpdateInterval, $PlatformIDs ) = @_; 
     381sub QueryPlatformSensorReportCount #( $DB, $strPlatformID, $strStart, $strEnd, $strSensorName, $iUpdateInterval, \%PlatformIDs, $iTimeZoneShift
     382{ 
     383  my ( $DB, $strPlatformID, $strStart, $strEnd, $strSensorName, $iUpdateInterval, $PlatformIDs,$iTimeZoneShift ) = @_; 
    293384      
    294385  #This query will return the number of entries of the sensor type given for the given platform and date range. 
    295386  #In other words how many times do we have an entry for that sensor for that platform during that date/time interval. 
     387  # m_date >= '$strStart' AND m_date < '$strEnd'      AND  
     388 
     389  #DWR v1.2.0.0 
     390  #Added time zone adjustment into WHERE clause for start/end date range. 
    296391  my $strSQL = "SELECT COUNT( sensor.m_type_id ) 
    297392                FROM multi_obs  
    298393                LEFT JOIN sensor on sensor.row_id=multi_obs.sensor_id 
    299                 WHERE platform_handle = '$strPlatformID' AND m_date >= '$strStart' AND m_date < '$strEnd' AND sensor.short_name = '$strSensorName';"; 
     394                WHERE   
     395                  platform_handle = '$strPlatformID'                                  AND  
     396                  sensor.short_name = '$strSensorName'                                AND 
     397                  datetime(m_date) >= datetime('$strStart','$iTimeZoneShift hours')   AND   
     398                  datetime(m_date) < datetime('$strEnd','$iTimeZoneShift hours');";       
    300399 
    301400  my $hSt = $DB->prepare( $strSQL ); 
     
    514613      my $fSensorAvg  = 0; 
    515614      my $iMaxCnt     = 0; 
     615 
     616      #DWR v1.2.0.0 
     617      #Build link to query data for sensor 
     618      my $strURL = 'http://nautilus.baruch.sc.edu/~dramage_prod/cgi-bin/DumpPlatformSensorReport.php?'; 
     619       
    516620      if( $iRowCnt == 0 ) 
    517621      { 
     
    552656      #DWR v1.1.0.0 
    553657      my $fAvg = $fSensorAvg / $iDayCnt; 
     658      if( $iUpdateInterval == 0 ) 
     659      { 
     660        $iUpdateInterval = 1; 
     661      } 
    554662      $fSensorAvg = ($fAvg / $iUpdateInterval) * 100.0 ; 
    555663      if( $iSensorCnt == 0 ) 
     
    561669        $strRow = $strRow.','; 
    562670      }  
     671      #DWR v1.2.0.0 
     672      #Build the url for our drill down into the database for this platform/sensor data. 
     673      my $iUpdatesInSeconds = SECONDS_PER_DAY / $iUpdateInterval; 
     674      $strURL .= "PLATFORMID=$strPlatformID&OBSERVATION=$Sensor&UPDATEINTERVAL=$iUpdatesInSeconds&STARTDATE=$strDate&ENDDATE=$strEndDate&TIMEZONE=EASTERN"; 
    563675      my $strSensorAvg = sprintf( "%.2f",$fSensorAvg ); 
    564       $strRow = $strRow."$strSensorAvg($fAvg/$iUpdateInterval)"; 
     676      $strRow = $strRow."$strSensorAvg($fAvg/$iUpdateInterval);$strURL"; 
     677       
    565678      if( $iUpdateDatabase ) 
    566679      { 
    567680        AddRecordToDatabase( $DB, $strPlatformID, $Sensor, $strSensorAvg, $strStartDate ); 
    568681      } 
     682       
    569683      $iSensorCnt++; 
    570684    }#foreach sensor