/**
  * A method to set the cached vertsion of the template to expire after
  * a time span
  *
  * @param Date_Span $oSpan
  */
 function setCacheLifetime($oSpan)
 {
     $this->cache_lifetime = $oSpan->toSeconds();
     $this->caching = 2;
 }
Exemple #2
0
 /**
  * A private method to calculate average values when an operation interval
  * has more than one targeting value.
  *
  * @access private
  * @param array      $aValues  The array of arrays of values to calculate the
  *                             averages from.
  * @param PEAR::Date $oEndDate The end date/time of the operation interval,
  *                             to be used for those values where no expiration
  *                             date is set.
  * @return array The array of "average" values.
  */
 function _calculateAverages($aValues, $oEndDate)
 {
     if (empty($aValues) || !is_array($aValues)) {
         return array();
     }
     reset($aValues);
     while (list(, $aAdValues) = each($aValues)) {
         if (empty($aAdValues) || !is_array($aAdValues)) {
             return array();
         }
         if (count($aAdValues) != 10) {
             return array();
         }
     }
     if (empty($oEndDate) || !is_a($oEndDate, 'Date')) {
         return array();
     }
     $counter = 0;
     $totalSeconds = 0;
     $aResult = array('ad_required_impressions' => 0, 'ad_requested_impressions' => 0, 'ad_priority' => 0, 'ad_priority_factor' => 0, 'ad_priority_factor_limited' => 0, 'ad_past_zone_traffic_fraction' => 0, 'average' => true);
     reset($aValues);
     while (list(, $aAdValues) = each($aValues)) {
         if ($counter == 0) {
             $aResult['interval_start'] = $aAdValues['interval_start'];
             $aResult['interval_end'] = $aAdValues['interval_end'];
         }
         $oCreatedDate = new Date($aAdValues['created']);
         if (is_null($aAdValues['expired'])) {
             $oExpiredDate = new Date();
             $oExpiredDate->copy($oEndDate);
         } else {
             $oExpiredDate = new Date($aAdValues['expired']);
         }
         $oSpan = new Date_Span();
         $oSpan->setFromDateDiff($oCreatedDate, $oExpiredDate);
         $seconds = $oSpan->toSeconds();
         $aResult['ad_required_impressions'] += $aAdValues['ad_required_impressions'] * $seconds;
         $aResult['ad_requested_impressions'] += $aAdValues['ad_requested_impressions'] * $seconds;
         $aResult['ad_priority'] += $aAdValues['ad_priority'] * $seconds;
         $aResult['ad_priority_factor'] += $aAdValues['ad_priority_factor'] * $seconds;
         $aResult['ad_past_zone_traffic_fraction'] += $aAdValues['ad_past_zone_traffic_fraction'] * $seconds;
         if ($aAdValues['ad_priority_factor_limited'] == 1) {
             $aResult['ad_priority_factor_limited'] = 1;
         }
         $counter++;
         $totalSeconds += $seconds;
     }
     $aResult['ad_required_impressions'] /= $totalSeconds;
     $aResult['ad_requested_impressions'] /= $totalSeconds;
     $aResult['ad_priority'] /= $totalSeconds;
     $aResult['ad_priority_factor'] /= $totalSeconds;
     $aResult['ad_past_zone_traffic_fraction'] /= $totalSeconds;
     return $aResult;
 }
Exemple #3
0
 /**
  * A method to store data about the times that various Maintenance
  * processes ran.
  *
  * @param Date $oStart The time that the script run started.
  * @param Date $oEnd The time that the script run ended.
  * @param mixed $oUpdateTo PEAR::Date representing the end of the last
  *                         operation interval ID that has been updated,
  *                         or NULL, in the case that this information
  *                         does not actually apply, and only the
  *                         start/end dates of the process run are
  *                         relevant.
  * @param string $tableName The name of the log_matinenance_* table to log into.
  *                          Must NOT be a complete table name, ie. no prefix.
  * @param boolean $setOperationInterval Should the operation intverval value be
  *                                      logged, or not?
  * @param string $runTypeField Optional name of DB field to hold $type value.
  * @param integer $type Optional type of process run performed.
  */
 function setProcessLastRunInfo($oStart, $oEnd, $oUpdateTo, $tableName, $setOperationInterval, $runTypeField = null, $type = null)
 {
     $aConf = $GLOBALS['_MAX']['CONF'];
     // Test input values $oStart and $oEnd are dates
     if (!is_a($oStart, 'Date') || !is_a($oEnd, 'Date')) {
         return false;
     }
     // Test $oUpdateTo is a date, or null
     if (!is_a($oUpdateTo, 'Date')) {
         if (!is_null($oUpdateTo)) {
             return false;
         }
     }
     // Test $setOperationInterval value is a boolean
     if (!is_bool($setOperationInterval)) {
         return false;
     }
     // Prepare the duraction to log from the start and end dates
     $oDuration = new Date_Span();
     $oStartDateCopy = new Date();
     $oStartDateCopy->copy($oStart);
     $oEndDateCopy = new Date();
     $oEndDateCopy->copy($oEnd);
     $oDuration->setFromDateDiff($oStartDateCopy, $oEndDateCopy);
     // Prepare the logging query
     $tableName = $this->_getTablename($tableName);
     $query = "\n            INSERT INTO\n                {$tableName}\n                (\n                    start_run,\n                    end_run,";
     if ($setOperationInterval) {
         $query .= "\n                    operation_interval,";
     }
     $query .= "\n                    duration";
     if (!is_null($runTypeField) && !is_null($type)) {
         $query .= ",\n                    {$runTypeField}";
     }
     if (!is_null($oUpdateTo)) {
         $query .= ",\n                    updated_to";
     }
     $query .= "\n                )\n            VALUES\n                (\n                    '" . $oStart->format('%Y-%m-%d %H:%M:%S') . "',\n                    '" . $oEnd->format('%Y-%m-%d %H:%M:%S') . "',";
     if ($setOperationInterval) {
         $query .= "\n                    {$aConf['maintenance']['operationInterval']},";
     }
     $query .= "\n                    " . $oDuration->toSeconds();
     if (!is_null($runTypeField) && !is_null($type)) {
         $query .= ",\n                    {$type}";
     }
     if (!is_null($oUpdateTo)) {
         $query .= ",\n                    '" . $oUpdateTo->format('%Y-%m-%d %H:%M:%S') . "'";
     }
     $query .= "\n                )";
     OA::debug('- Logging maintenance process run information into ' . $tableName, PEAR_LOG_DEBUG);
     $rows = $this->oDbh->exec($query);
     if (PEAR::isError($rows)) {
         return false;
     } else {
         return $rows;
     }
 }
 /**
  * A private method for calculating the required and requested impressions,
  * priority factor, and past zone traffic fraction for an ad/zone pair, based on
  * data taken from the data_summary_ad_zone_assoc table. The values may be simply
  * those extracted from the database table, or they may be "average" values,
  * based on the length of time each of a number of different rows was active for
  * in the delivery engine.
  *
  * @access private
  *
  * @param array $aPastPriorityResult A reference to an array of arrays indexed
  *                                   by ad ID, and then zone ID, with each sub-array
  *                                   returned containing the details above.
  * @param array $aResult An array containing the results of a database query, with
  *                       the ad_id, zone_id, required_impressions, requested_impressions,
  *                       priority_factor, past_zone_traffic_fraction, created and expired
  *                       columns from the data_summary_ad_zone_assoc table, representing
  *                       these values for a complete (single) operation interval.
  *                       May optionally contain the operation_interval,
  *                       operation_interval_id, interval_start and interval_end details.
  * @param Date $oDate The current Date object, taken from the OA_ServiceLocator.
  * @param array $aPastDeliveryResult Optional array of arrays indexed by ad ID, and then
  *                                   zone ID, containing details on which ad/zone
  *                                   combinations already have had their average past
  *                                   priority information calculated (if any).
  */
 function _calculateAveragePastPriorityValues(&$aPastPriorityResult, $aResult, $oDate, $aPastDeliveryResult = null)
 {
     if (is_array($aResult) && !empty($aResult)) {
         // Loop through the results, and ensure that an array exists for each
         // ad ID, zone ID pair, and also store the initial values of the data
         foreach ($aResult as $aRow) {
             if (!isset($aPastPriorityResult[$aRow['ad_id']])) {
                 $aPastPriorityResult[$aRow['ad_id']] = array();
             }
             if (!isset($aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']])) {
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']] = array('ad_id' => $aRow['ad_id'], 'zone_id' => $aRow['zone_id'], 'required_impressions' => $aRow['required_impressions'], 'requested_impressions' => $aRow['requested_impressions'], 'to_be_delivered' => $aRow['to_be_delivered'], 'priority_factor' => $aRow['priority_factor'], 'past_zone_traffic_fraction' => $aRow['past_zone_traffic_fraction']);
                 if (isset($aRow['operation_interval'])) {
                     $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['operation_interval'] = $aRow['operation_interval'];
                     $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['operation_interval_id'] = $aRow['operation_interval_id'];
                     $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['interval_start'] = $aRow['interval_start'];
                     $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['interval_end'] = $aRow['interval_end'];
                 }
             }
         }
         // As more than one row of past priority information may have been found
         // in the data, re-loop over the array of results, to see if the values
         // changed during operation interval the data represents
         foreach ($aResult as $aRow) {
             // Compare set values from above, and if there is a difference,
             // set values to zero, and mark as requiring calculation of the
             // averate impressions requested and priority factor
             if (($aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['required_impressions'] != $aRow['required_impressions'] || $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['requested_impressions'] != $aRow['requested_impressions'] || $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['priority_factor'] != $aRow['priority_factor']) && !$aPastDeliveryResult[$aRow['ad_id']][$aRow['zone_id']]['pastPriorityFound']) {
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['required_impressions'] = 0;
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['requested_impressions'] = 0;
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['to_be_delivered'] = 0;
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['priority_factor'] = 0;
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['average'] = true;
                 unset($aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['pastPriorityFound']);
             } else {
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['pastPriorityFound'] = true;
             }
         }
         // Loop again, summing in the impressions, priorities (scaled by the length of time
         // they were active for) to the values, for those ad/zone combinations that require it
         foreach ($aResult as $aRow) {
             // Does this value need to be used to calculate the average?
             if (!empty($aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['average']) && !$aPastDeliveryResult[$aRow['ad_id']][$aRow['zone_id']]['pastPriorityFound']) {
                 // Add the variable values to the array, multiplied by the number of seconds the
                 // values were active for over the "hour" (not exact, in the event that the
                 // maintenance script is not run spot on the hour, but close enough)
                 $oCreatedDate = new Date($aRow['created']);
                 if (is_null($aRow['expired'])) {
                     $oExpiredDate = new Date();
                     $oExpiredDate->copy($oDate);
                 } else {
                     $oExpiredDate = new Date($aRow['expired']);
                 }
                 $oSpan = new Date_Span();
                 $oSpan->setFromDateDiff($oCreatedDate, $oExpiredDate);
                 $seconds = $oSpan->toSeconds();
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['required_impressions'] += $aRow['required_impressions'] * $seconds;
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['requested_impressions'] += $aRow['requested_impressions'] * $seconds;
                 $aPastPriorityResult[$aRow['ad_id']][$aRow['zone_id']]['priority_factor'] += $aRow['priority_factor'] * $seconds;
             }
         }
         // Calculate the average impressions requested and priority factor used, for those
         // ad/zone combinations that require it
         if (!empty($aPastPriorityResult)) {
             foreach ($aPastPriorityResult as $a => $aAd) {
                 if (is_array($aAd) && count($aAd) > 0) {
                     foreach ($aAd as $z => $aZone) {
                         if (!empty($aPastPriorityResult[$a][$z]['average']) && !$aPastPriorityResult[$a][$z]['pastPriorityFound']) {
                             $aPastPriorityResult[$a][$z]['required_impressions'] /= SECONDS_PER_HOUR;
                             $aPastPriorityResult[$a][$z]['requested_impressions'] /= SECONDS_PER_HOUR;
                             $aPastPriorityResult[$a][$z]['priority_factor'] /= SECONDS_PER_HOUR;
                             $aPastPriorityResult[$a][$z]['pastPriorityFound'] = true;
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * The implementation of the OA_Task::run() method that performs
  * the required task of logging the completion of the MSE process.
  *
  * @param PEAR::Date $oEndDate Optional date/time representing the end of the tasks.
  */
 function run($oEndDate = null)
 {
     $oServiceLocator =& OA_ServiceLocator::instance();
     $oNowDate =& $oServiceLocator->get('now');
     if (is_null($oEndDate)) {
         $oEndDate = new Date();
     }
     // Prepare the duraction to log from the start and end dates
     $oDuration = new Date_Span();
     $oStartDateCopy = new Date();
     $oStartDateCopy->copy($oNowDate);
     $oEndDateCopy = new Date();
     $oEndDateCopy->copy($oEndDate);
     $oDuration->setFromDateDiff($oStartDateCopy, $oEndDateCopy);
     $message = '- Logging the completion of the maintenance statistics run';
     $this->oController->report .= "{$message}.\n";
     OA::debug($message, PEAR_LOG_DEBUG);
     // Determine the type of MSE completion logging required
     if ($this->oController->updateFinal && $this->oController->updateIntermediate) {
         // Need to log that both the intermediate and final tables were updated;
         // however, need to ensure that we log the correct "updated to" times
         $oUpdateIntermediateToDate = new Date();
         $oUpdateIntermediateToDate->copy($this->oController->oUpdateIntermediateToDate);
         $oUpdateFinalToDate = new Date();
         $oUpdateFinalToDate->copy($this->oController->oUpdateFinalToDate);
         if ($oUpdateIntermediateToDate->equals($oUpdateFinalToDate)) {
             // The dates are the same, log info with one row
             $doLog_maintenance_statistics = OA_Dal::factoryDO('log_maintenance_statistics');
             $doLog_maintenance_statistics->start_run = $oNowDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->end_run = $oEndDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->duration = $oDuration->toSeconds();
             $doLog_maintenance_statistics->adserver_run_type = OX_DAL_MAINTENANCE_STATISTICS_UPDATE_BOTH;
             $doLog_maintenance_statistics->updated_to = $this->oController->oUpdateIntermediateToDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->insert();
         } else {
             // The dates are not the same, log info with two rows
             $doLog_maintenance_statistics = OA_Dal::factoryDO('log_maintenance_statistics');
             $doLog_maintenance_statistics->start_run = $oNowDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->end_run = $oEndDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->duration = $oDuration->toSeconds();
             $doLog_maintenance_statistics->adserver_run_type = OX_DAL_MAINTENANCE_STATISTICS_UPDATE_OI;
             $doLog_maintenance_statistics->updated_to = $this->oController->oUpdateIntermediateToDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->insert();
             $doLog_maintenance_statistics = OA_Dal::factoryDO('log_maintenance_statistics');
             $doLog_maintenance_statistics->start_run = $oNowDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->end_run = $oEndDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->duration = $oDuration->toSeconds();
             $doLog_maintenance_statistics->adserver_run_type = OX_DAL_MAINTENANCE_STATISTICS_UPDATE_HOUR;
             $doLog_maintenance_statistics->updated_to = $this->oController->oUpdateFinalToDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->insert();
         }
     } else {
         if ($this->oController->updateIntermediate) {
             $doLog_maintenance_statistics = OA_Dal::factoryDO('log_maintenance_statistics');
             $doLog_maintenance_statistics->start_run = $oNowDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->end_run = $oEndDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->duration = $oDuration->toSeconds();
             $doLog_maintenance_statistics->adserver_run_type = OX_DAL_MAINTENANCE_STATISTICS_UPDATE_OI;
             $doLog_maintenance_statistics->updated_to = $this->oController->oUpdateIntermediateToDate->format('%Y-%m-%d %H:%M:%S');
             $doLog_maintenance_statistics->insert();
         } else {
             if ($this->oController->updateFinal) {
                 $doLog_maintenance_statistics = OA_Dal::factoryDO('log_maintenance_statistics');
                 $doLog_maintenance_statistics->start_run = $oNowDate->format('%Y-%m-%d %H:%M:%S');
                 $doLog_maintenance_statistics->end_run = $oEndDate->format('%Y-%m-%d %H:%M:%S');
                 $doLog_maintenance_statistics->duration = $oDuration->toSeconds();
                 $doLog_maintenance_statistics->adserver_run_type = OX_DAL_MAINTENANCE_STATISTICS_UPDATE_HOUR;
                 $doLog_maintenance_statistics->updated_to = $this->oController->oUpdateFinalToDate->format('%Y-%m-%d %H:%M:%S');
                 $doLog_maintenance_statistics->insert();
             } else {
                 return false;
             }
         }
     }
     // Log the report to the "user log"
     $this->_setMaintenanceStatisticsRunReport($this->oController->report);
     return true;
 }