/** * A method to test the the getDaysInSpan() method. */ function testGetDaysInSpan() { $oDaySpan = new OA_Admin_DaySpan(); $oTestStartDate = new Date('1999-12-31 12:34:56'); $oDaySpan->setSpanDays($oTestStartDate, $oTestStartDate); $this->assertEqual($oDaySpan->getDaysInSpan(), 1); $oTestStartDate = new Date('1999-12-31 12:34:56'); $oTestEndDate = new Date('2000-01-01 12:43:32'); $oDaySpan->setSpanDays($oTestStartDate, $oTestEndDate); $this->assertEqual($oDaySpan->getDaysInSpan(), 2); $oTestStartDate = new Date('1999-12-31 12:34:56'); $oTestEndDate = new Date('2000-03-01 12:43:32'); $oDaySpan->setSpanDays($oTestStartDate, $oTestEndDate); $this->assertEqual($oDaySpan->getDaysInSpan(), 1 + 31 + 29 + 1); // 31st of Dec + All of Jan + All of Feb (leap year!) + 1st of Mar }
/** * A private method to calculate an approximate value of how badly a * high-priority campaign is mis-delivering so far "today". * * @access private * @param array $aCampaignData An array of campaign data. * @return float The difference in the predicted final delivered impressions of a high-priority * campaign (based on "today's" data) and booked number of impressions the campaign * has. Positive for over-delivery, negative for under-delivery. */ function _calculateTodaysMisdelivery($aCampaignData) { // Only calculate mis-delivery if start and end dates present if (empty($aCampaignData['campaign_start']) || empty($aCampaignData['campaign_end'])) { return false; } // Calculate the number of days the campaign should run over $oCampaignDaySpan =& $this->_rangeFromCampaign($aCampaignData); $campaignDays = $oCampaignDaySpan->getDaysInSpan(); // Calulate the number of days over which the campaign has been running $oCampaignRunningDaySpan = new OA_Admin_DaySpan(); $oBeginDate = new Date($aCampaignData['campaign_start']); $oEndDate = new Date($aCampaignData['stats_most_recent_day']); $oCampaignRunningDaySpan->setSpanDays($oBeginDate, $oEndDate); $runningDays = $oCampaignRunningDaySpan->getDaysInSpan() - 1; if ($runningDays <= 0) { // The campaign has not started running yet $percentDiff = 0; return $percentDiff; } // Ensure the number of days the campaign has been running for makes // sense, and also set the number or hours that the campaign has been // running for "today" if ($runningDays > $campaignDays) { // The campaign has been running longer than it was expected to! $runningDays = $campaignDays; $runningHours = 0; } else { $runningHours = $aCampaignData['stats_most_recent_hour']; } // Calculate how many impressions were delivered "yesterday" up to the same point // in time as we are at "now" $counter = 1; $yesterdaysImpressionsToSameHourAsNow = 0; foreach ($aCampaignData['yesterdays_impressions_by_hour'] as $aHour) { if ($counter > $runningHours) { // Have gone past the same hour as "now", stop adding break; } $yesterdaysImpressionsToSameHourAsNow += $aHour['campaign_impressions']; } // Calculate how many days are remaining for the campaign $remainingDays = $campaignDays - $runningDays; // Calculate how many impressions were delivered up until the end // of "yesterday" $campaignImpressionsToLastNight = $aCampaignData['campaign_impressions'] - $aCampaignData['todays_impressions']; // Calculate the percent difference and return $percentDiff = $this->_calculateTodaysPercentDifference($aCampaignData['todays_impressions'], $aCampaignData['yesterdays_impressions'], $yesterdaysImpressionsToSameHourAsNow, $remainingDays, $campaignImpressionsToLastNight, $aCampaignData['campaign_booked_impressions']); return $percentDiff; }