| 395 | | |
|---|
| 396 | | |
|---|
| 397 | | #DWR v1.2.0.0 |
|---|
| 398 | | #Added time zone adjustment into WHERE clause for start/end date range. |
|---|
| 399 | | my $strSQL = "SELECT COUNT( sensor.m_type_id ) |
|---|
| 400 | | FROM multi_obs |
|---|
| 401 | | LEFT JOIN sensor on sensor.row_id=multi_obs.sensor_id |
|---|
| 402 | | WHERE |
|---|
| 403 | | platform_handle = '$strPlatformID' AND |
|---|
| 404 | | sensor.short_name = '$strSensorName' AND |
|---|
| 405 | | datetime(m_date) >= datetime('$strStart','$iTimeZoneShift hours') AND |
|---|
| 406 | | datetime(m_date) < datetime('$strEnd','$iTimeZoneShift hours');"; |
|---|
| 407 | | |
|---|
| 408 | | my $iCnt = -1; |
|---|
| 409 | | |
|---|
| 410 | | my $hSt = $DB->prepare( $strSQL ); |
|---|
| 411 | | if( defined $hSt ) |
|---|
| 412 | | { |
|---|
| 413 | | if( $hSt->execute( ) ) |
|---|
| 414 | | { |
|---|
| 415 | | $iCnt = $hSt->fetchrow_array(); |
|---|
| 416 | | my $strDay = substr( $strStart, 0, 10); |
|---|
| 417 | | %$PlatformIDs->{Platform}{$strPlatformID}{Sensor}{$strSensorName}{Day}{$strDay}{Count} = $iCnt; |
|---|
| 418 | | %$PlatformIDs->{Platform}{$strPlatformID}{Sensor}{$strSensorName}{UpdateInterval} = $iUpdateInterval; |
|---|
| | 404 | #$strPlatformID = 'usf.C14.IMET'; |
|---|
| | 405 | |
|---|
| | 406 | #DWR v1.3.0.0 |
|---|
| | 407 | #Query the m_type_id to alleviate the need below to JOIN on the sensor table. |
|---|
| | 408 | my $iType = GetMTypeFromObsType ( $DB, $strSensorName, $strPlatformID, 1);#($dbh, $strObsName, $strPlatformHandle, $iSOrder ) |
|---|
| | 409 | |
|---|
| | 410 | if( defined( $iType ) ) |
|---|
| | 411 | { |
|---|
| | 412 | #DWR v1.2.0.0 |
|---|
| | 413 | #Added time zone adjustment into WHERE clause for start/end date range. |
|---|
| | 414 | #DWR v1.3.0.0 |
|---|
| | 415 | #Reworked the SQL query for speed. Dropped the join the the sensor table since we are now |
|---|
| | 416 | #looking up the sensor m_type_id above. |
|---|
| | 417 | my $strSQL = "SELECT COUNT( m_type_id ) |
|---|
| | 418 | FROM multi_obs |
|---|
| | 419 | WHERE |
|---|
| | 420 | multi_obs.m_type_id = $iType AND |
|---|
| | 421 | m_date >= strftime( '%Y-%m-%dT%H:00:00',datetime('$strStart','$iTimeZoneShift hours') ) AND |
|---|
| | 422 | m_date < strftime( '%Y-%m-%dT%H:00:00', datetime('$strEnd','$iTimeZoneShift hours') ) AND |
|---|
| | 423 | platform_handle = '$strPlatformID';"; |
|---|
| | 424 | # my $strSQL = "SELECT COUNT( sensor.m_type_id ) |
|---|
| | 425 | # FROM multi_obs |
|---|
| | 426 | # LEFT JOIN sensor on sensor.row_id=multi_obs.sensor_id |
|---|
| | 427 | # WHERE |
|---|
| | 428 | # platform_handle = '$strPlatformID' AND |
|---|
| | 429 | # sensor.short_name = '$strSensorName' AND |
|---|
| | 430 | # datetime(m_date) >= datetime('$strStart','$iTimeZoneShift hours') AND |
|---|
| | 431 | # datetime(m_date) < datetime('$strEnd','$iTimeZoneShift hours');"; |
|---|
| | 432 | |
|---|
| | 433 | |
|---|
| | 434 | my $hSt = $DB->prepare( $strSQL ); |
|---|
| | 435 | if( defined $hSt ) |
|---|
| | 436 | { |
|---|
| | 437 | if( $hSt->execute( ) ) |
|---|
| | 438 | { |
|---|
| | 439 | $iCnt = $hSt->fetchrow_array(); |
|---|
| | 440 | my $strDay = substr( $strStart, 0, 10); |
|---|
| | 441 | %$PlatformIDs->{Platform}{$strPlatformID}{Sensor}{$strSensorName}{Day}{$strDay}{Count} = $iCnt; |
|---|
| | 442 | %$PlatformIDs->{Platform}{$strPlatformID}{Sensor}{$strSensorName}{UpdateInterval} = $iUpdateInterval; |
|---|
| | 443 | } |
|---|
| | 444 | else |
|---|
| | 445 | { |
|---|
| | 446 | my $strErr = $hSt->errstr; |
|---|
| | 447 | print( "QueryPlatformSensorReportCount::ERROR: Failed execute: $strErr\n SQLStatement: $strSQL\n"); |
|---|
| | 448 | } |
|---|
| 422 | | my $strErr = $hSt->errstr; |
|---|
| 423 | | print( "QueryPlatformSensorReportCount::ERROR: Failed execute: $strErr\n SQLStatement: $strSQL\n"); |
|---|
| 424 | | } |
|---|
| 425 | | } |
|---|
| 426 | | else |
|---|
| 427 | | { |
|---|
| 428 | | print( "QueryPlatformSensorReportCount::ERROR: Unable to prepare SQL statement: $strSQL.\n"); |
|---|
| 429 | | } |
|---|
| 430 | | |
|---|
| | 452 | print( "QueryPlatformSensorReportCount::ERROR: Unable to prepare SQL statement: $strSQL.\n"); |
|---|
| | 453 | } |
|---|
| | 454 | } |
|---|
| | 455 | #No m_type_id for the sensor on the platform. We'll create a placeholder and give a count of 0. |
|---|
| | 456 | else |
|---|
| | 457 | { |
|---|
| | 458 | my $strDay = substr( $strStart, 0, 10); |
|---|
| | 459 | %$PlatformIDs->{Platform}{$strPlatformID}{Sensor}{$strSensorName}{Day}{$strDay}{Count} = 0; |
|---|
| | 460 | %$PlatformIDs->{Platform}{$strPlatformID}{Sensor}{$strSensorName}{UpdateInterval} = $iUpdateInterval; |
|---|
| | 461 | } |
|---|
| | 836 | sub GetMTypeFromObsType |
|---|
| | 837 | { |
|---|
| | 838 | my ($dbh, $strObsName, $strPlatformHandle, $iSOrder ) = @_; |
|---|
| | 839 | |
|---|
| | 840 | my $strSOrder = ''; |
|---|
| | 841 | if( defined $iSOrder ) |
|---|
| | 842 | { |
|---|
| | 843 | $strSOrder = "sensor.s_order = $iSOrder AND"; |
|---|
| | 844 | } |
|---|
| | 845 | my $strSQL = "SELECT DISTINCT(sensor.m_type_id) FROM m_type, m_scalar_type, obs_type, sensor, platform |
|---|
| | 846 | WHERE sensor.m_type_id = m_type.row_id AND |
|---|
| | 847 | m_scalar_type.row_id = m_type.m_scalar_type_id AND |
|---|
| | 848 | obs_type.row_id = m_scalar_type.obs_type_id AND |
|---|
| | 849 | platform.row_id = sensor.platform_id AND |
|---|
| | 850 | $strSOrder |
|---|
| | 851 | obs_type.standard_name = '$strObsName' AND |
|---|
| | 852 | platform.platform_handle = '$strPlatformHandle';"; |
|---|
| | 853 | my $iMType = -1; |
|---|
| | 854 | my $sth = $dbh->prepare( $strSQL ); |
|---|
| | 855 | if( defined $sth ) |
|---|
| | 856 | { |
|---|
| | 857 | if( $sth->execute() ) |
|---|
| | 858 | { |
|---|
| | 859 | $iMType = $sth->fetchrow(); |
|---|
| | 860 | } |
|---|
| | 861 | else |
|---|
| | 862 | { |
|---|
| | 863 | my $strErr = $sth->errstr; |
|---|
| | 864 | print( "ERROR::$strErr\n"); |
|---|
| | 865 | } |
|---|
| | 866 | } |
|---|
| | 867 | else |
|---|
| | 868 | { |
|---|
| | 869 | print( "ERROR::Unable to prepare SQL statement: $strSQL\n"); |
|---|
| | 870 | } |
|---|
| | 871 | $sth->finish(); |
|---|
| | 872 | return( $iMType ); |
|---|
| | 873 | } |
|---|