/** * A method that can be inherited and used by children classes to get the * required date span of a statistics page. * * @param object $oCaller The calling object. Expected to have the * the following class variables: * $oCaller->aPlugins - An array of statistics fields plugins * $oCaller->oStartDate - Will be set by method * $oCaller->spanDays - Will be set by method * $oCaller->spanWeeks - Will be set by method * $oCaller->spanMonths - Will be set by method * @param array $aParams An array of query parameters for * {@link Admin_DA::fromCache()}. */ function getSpan(&$oCaller, $aParams) { $oStartDate = new Date(date('Y-m-d')); $oStartDate->setHour(0); $oStartDate->setMinute(0); $oStartDate->setSecond(0); // Check span using all plugins foreach ($oCaller->aPlugins as $oPlugin) { $aPluginParams = call_user_func(array($oPlugin, 'getHistorySpanParams')); $aSpan = Admin_DA::fromCache('getHistorySpan', $aParams + $aPluginParams); if (!empty($aSpan['start_date'])) { $oDate = new Date($aSpan['start_date']); $oDate->setTZbyID('UTC'); if ($oDate->before($oStartDate)) { $oDate->convertTZ($oStartDate->tz); $oStartDate = new Date($oDate); } } } $oStartDate->setHour(0); $oStartDate->setMinute(0); $oStartDate->setSecond(0); $oNow = new Date(); $oSpan = new Date_Span(new Date($oStartDate), new Date($oNow->format('%Y-%m-%d'))); // Store the span data required for stats display $oCaller->oStartDate = $oStartDate; $oCaller->spanDays = (int) ceil($oSpan->toDays()); $oCaller->spanWeeks = (int) ceil($oCaller->spanDays / 7) + ($oCaller->spanDays % 7 ? 1 : 0); $oCaller->spanMonths = ($oNow->getYear() - $oStartDate->getYear()) * 12 + ($oNow->getMonth() - $oStartDate->getMonth()) + 1; // Set the caller's aDates span in the event that it's empty if (empty($oCaller->aDates)) { $oCaller->aDates['day_begin'] = $oStartDate->format('%Y-%m-%d'); $oCaller->aDates['day_end'] = $oNow->format('%Y-%m-%d'); } }
function getSpendingMoney($accountId, $startDate) { global $badgerDb; $accountManager = new AccountManager($badgerDb); $account = $accountManager->getAccountById($accountId); $account->setType('finished'); $account->setOrder(array(array('key' => 'valutaDate', 'dir' => 'asc'))); $account->setFilter(array(array('key' => 'valutaDate', 'op' => 'ge', 'val' => $startDate), array('key' => 'periodical', 'op' => 'eq', 'val' => false), array('key' => 'exceptional', 'op' => 'eq', 'val' => false))); $sum = new Amount(); $realStartDate = false; while ($currentTransaction = $account->getNextFinishedTransaction()) { if (!$realStartDate) { $realStartDate = $currentTransaction->getValutaDate(); } $sum->add($currentTransaction->getAmount()); } $span = new Date_Span($realStartDate, new Date()); $count = $span->toDays(); if ($count > 0) { $sum->div($count); } return $sum; }
/** * A method to send the "midnight" reports during maintenance - that * is, the delivery information report, showing what the campaign(s) * have delivered since the last time the report was sendt. * * @access private */ function _runReports() { OA::debug(' Starting to send advertiser "campaign delivery" reports.', PEAR_LOG_DEBUG); // Get all advertisers where the advertiser preference is to send reports OA::debug(' - Getting details of advertisers that require reports to be sent.', PEAR_LOG_DEBUG); $doClients = OA_Dal::factoryDO('clients'); $doClients->report = 't'; $doClients->find(); while ($doClients->fetch()) { $aAdvertiser = $doClients->toArray(); // Don't email report by default $sendReport = false; // Has the report interval date been passed? if (empty($aAdvertiser['reportlastdate'])) { $sendReport = true; $oReportLastDate = null; } else { $oNowDate = new Date(); $oReportLastDate = new Date($aAdvertiser['reportlastdate']); $oSpan = new Date_Span(); $oSpan->setFromDateDiff($oReportLastDate, $oNowDate); $daysSinceLastReport = (int) floor($oSpan->toDays()); if ($daysSinceLastReport >= $aAdvertiser['reportinterval']) { $sendReport = true; } } if ($sendReport) { // Send the advertiser's campaign delivery report $oEmail = new OA_Email(); $oEmail->sendCampaignDeliveryEmail($aAdvertiser, $oReportLastDate); } } OA::debug(' Finished sending advertiser "campaign delivery" reports.', PEAR_LOG_DEBUG); }
/** * This method check if time span for given dates is greater that daysIntervalThreshold * * @param PEAR:Date $oStartDate * @param PEAR:Date $oEndDate * @param int $daysIntervalThreshold * @return boolean true if time span in days is greater or equal to daysIntervalThreshold, else false */ function _checkDaysIntervalThreshold($oStartDate, $oEndDate, $daysIntervalThreshold = null) { if (!is_numeric($daysIntervalThreshold)) { $daysIntervalThreshold = $GLOBALS['_MAX']['CONF']['performanceStatistics']['defaultDaysIntervalThreshold']; } $span = new Date_Span(); $span->setFromDateDiff($oStartDate, $oEndDate); return $span->toDays() >= $daysIntervalThreshold; }
/** * A method for obtaining the targeting statistics of an ad or placement * for a date span, where the data is summarised by one of "day", "week", * "month", "dow" or "hour" as specified by the $breakdown parameter. * * @param integer $id The ad or placement ID. * @param string $type Either "ad" or "placement". * @param PEAR::Date $oStartDate The start date of the range required. * @param PEAR::Date $oEndDate The end date the range required. * @param string $breakdown One of "day", "week", "month", "dow" * or "hour" * * @return mixed Returns false in the event of incorrect input, or in the case * of being unable to connect to the database, otherwise, returns * an array of arrays: * * array( * [key] => array( * ['ad_required_impressions'] => integer * ['ad_requested_impressions'] => integer * ['ad_actual_impressions'] => integer * ['zones_forecast_impressions'] => integer * ['zones_actual_impressions'] => integer * ['average'] => boolean * ) * . * . * . * ) * * or: * * array( * [key] => array( * ['placement_required_impressions'] => integer * ['placement_requested_impressions'] => integer * ['placement_actual_impressions'] => integer * ['zones_forecast_impressions'] => integer * ['zones_actual_impressions'] => integer * ['average'] => boolean * ) * . * . * . * ) * * In the above "key" changes with breakdown type. * * For the ad, or placement and date range specified, returns an array for each * $breakdown as required, consisting of the total number of impressions requested * by the ad, or all ads in the placement (for all zones the ads are linked to), * as well as the total number of impressions actually delivered by the ad, or * all ads in the placement (for all zones the ads are linked to). * * The individual ad/zone impressions requested values may need to be * calculated as an "averge" value, in the event that there are multiple, * differing values for an ad in a zone for an operation interval -- in * much the same way as is done in * OA_Dal_Maintenance_Priority::getPreviousAdDeliveryInfo() -- before * the total impressions requested value can be calculated. */ function _getTargetingStatisticsSpan($id, $type, $oStartDate, $oEndDate, $breakdown) { // Ensure the parameters are valid if (!$this->_testGetTargetingStatisticsSpanParameters($id, $type, $oStartDate, $oEndDate)) { return false; } // Ensure that, if a placement, the placement has advertisements if ($this->_testGetTargetingStatisticsSpanPlacement($id, $type) === false) { return false; } // Prepare the temporary results array $aResult = array(); // How many days in the span? $oStartDateCopy = new Date(); $oStartDateCopy->copy($oStartDate); $oStartDateCopy->setHour(0); $oStartDateCopy->setMinute(0); $oStartDateCopy->setSecond(0); $oEndDateCopy = new Date(); $oEndDateCopy->copy($oEndDate); $oEndDateCopy->setHour(0); $oEndDateCopy->setMinute(0); $oEndDateCopy->setSecond(0); $oSpan = new Date_Span(); $oSpan->setFromDateDiff($oStartDateCopy, $oEndDateCopy); $days = $oSpan->toDays(); // Re-set the start date from the original date, // as PHP5 may cause $oStartDateCopy to change if // the time zone is not correctly set $oStartDateCopy = new Date(); $oStartDateCopy->copy($oStartDate); $oStartDateCopy->setHour(0); $oStartDateCopy->setMinute(0); $oStartDateCopy->setSecond(0); for ($counter = 0; $counter <= $days; $counter++) { // Get this day's data $aTemp = $this->getDailyTargetingStatistics($id, $type, $oStartDateCopy); // Merge in the data appropriately $this->_mergeSpan($aResult, $aTemp, $type, $breakdown); $oStartDateCopy->addSeconds(SECONDS_PER_DAY); } return $aResult; }
/** * A method to return the number of days in the span, including the start and end days. * * @return integer The number of days in the span. */ function getDaysInSpan() { $oSpan = new Date_Span(); $oSpan->setFromDateDiff($this->oStartDate, $this->oEndDate); return (int) floor($oSpan->toDays()) + 1; }
/** * A private method to caclucate the number of days left until a * campaign expires based on the impression, click or conversion * delivery targets & the delivery rate of the campaign to date. * * @param array $aDeliveryData An array of two items. "delivered": * the number of impressions, clicks or * conversions delivered so far; and * "day_of_first": a string in YYYY-MM-DD * format representing the day that the * first impression, click or conversion * was delivered. * @param integer $target The total number of impressions, clicks * or conversions required to be delivered * by the campaign. * @return array An array of three items. "daysLeft": the estimated * number of days remaining until the campaign ends; * "date": the estimated date of expiration; and "date_f" */ function _calculateRemainingDays($aDeliveryData, $target) { global $date_format; $oNowDate = new Date(); $aExpiration = array(); // How many days since the first impression/click/conversion? if (!empty($aDeliveryData['day_of_first'])) { $oFirstDate = new Date($aDeliveryData['day_of_first']); $oSpan = new Date_Span(); $oSpan->setFromDateDiff($oFirstDate, $oNowDate); $daysSinceFirst = ceil($oSpan->toDays()); } else { $daysSinceFirst = 1; } // Have *any* impressions/clicks/conversions been delivered? if (!empty($aDeliveryData["delivered"]) && $aDeliveryData["delivered"] > 0) { $targetRemaining = $target - $aDeliveryData["delivered"]; $deliveryRate = $aDeliveryData["delivered"] / $daysSinceFirst; $daysLeft = (int) round($targetRemaining / $deliveryRate); $oSpan = new Date_Span(); $oSpan->setFromDays($daysLeft); $oEstimatedEndDate = new Date(); $oEstimatedEndDate->addSpan($oSpan); if ($oEstimatedEndDate->before($oNowDate)) { // Ooop! Wrapped into the past - get the biggest possible date $oEstimatedEndDate = new Date('1960-01-01 00:00:00'); $oEstimatedEndDate->subtractSeconds(1); } $estimatedEndDateFormat = $oEstimatedEndDate->format($date_format); $aExpiration = array('daysLeft' => $daysLeft, 'date_f' => $estimatedEndDateFormat, 'date' => $oEstimatedEndDate); } return $aExpiration; }
function testGetEcpm() { $revenue = 10; $clicks = 10; $conversions = 5; // Test each type with 0 impressions. $impressions = 0; // CPM $expected = 10; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_CPM, $revenue, $impressions); $this->assertEqual($expected, $result); // CPC // eCPM = default click ratio * revenue * 1000 $defaultClickRatio = null; $expected = 50; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_CPC, $revenue, $impressions, $clicks, $conversions, null, null, $defaultClickRatio); $this->assertEqual($expected, $result); $defaultClickRatio = 0.05; $expected = 500; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_CPC, $revenue, $impressions, $clicks, $conversions, null, null, $defaultClickRatio); $this->assertEqual($expected, $result); // CPA // eCPM = default conversion ratio * revenue * 1000 $defaultConversionRatio = null; $expected = 1; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_CPA, $revenue, $impressions, $clicks, $conversions, null, null, $defaultClickRatio, $defaultConversionRatio); $this->assertEqual($expected, $result); $defaultConversionRatio = 0.01; $expected = 100; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_CPA, $revenue, $impressions, $clicks, $conversions, null, null, $defaultClickRatio, $defaultConversionRatio); $this->assertEqual($expected, $result); // Tenancy // eCPM = 0. $expected = 0; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_MT, $revenue, $impressions, $clicks, $conversions, '2009-01-01', '2009-01-14', $defaultClickRatio, $defaultConversionRatio); $this->assertEqual($expected, $result); // Test each type with some impressions $impressions = 100000; // CPM // eCPM = CPM $expected = 10; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_CPM, $revenue, $impressions); $this->assertEqual($expected, $result); // CPC // eCPM = revenue * clicks / impressions * 1000 $expected = 1; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_CPC, $revenue, $impressions, $clicks); $this->assertEqual($expected, $result); // CPA // eCPM = revenue * conversions / impressions * 1000 $expected = 0.5; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_CPA, $revenue, $impressions, $clicks, $conversions); $this->assertEqual($expected, $result); // Tenancy // eCPM = (revenue / totalDaysInCampaign) * daysInCampaignSoFar / impressions * 1000 $now = new Date(); $startDate = new Date($now); $endDate = new Date($now); $endDate->addSeconds(60 * 60 * 24 * 10); // 10 days in the future. $span = new Date_Span(); $span->setFromDateDiff($startDate, $endDate); $this->assertEqual(10, $span->toDays()); // Total revenue for tenancy. $revenue = 10000; $revenuePerDay = $revenue / $span->toDays(); $this->assertEqual(1000, $revenuePerDay); // Beginning of campaign (no imps served) $impressions = 0; $expected = 0; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_MT, $revenue, $impressions, $clicks, $conversions, $startDate->getDate(DATE_FORMAT_ISO), $endDate->getDate(DATE_FORMAT_ISO)); $this->assertEqual($expected, $result); // Half way through campaign (40,000 imps served) $impressions = 40000; $startDate->subtractSeconds(60 * 60 * 24 * 5); // We are 5 days into the campaign. $endDate->subtractSeconds(60 * 60 * 24 * 5); $span->setFromDateDiff($startDate, $now); $this->assertEqual(5, $span->toDays()); $span->setFromDateDiff($endDate, $now); $this->assertEqual(5, $span->toDays()); // eCPM = (revenue / totalDaysInCampaign) * daysInCampaignSoFar / impressions * 1000 $expected = 125; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_MT, $revenue, $impressions, $clicks, $conversions, $startDate->getDate(DATE_FORMAT_ISO), $endDate->getDate(DATE_FORMAT_ISO)); $this->assertEqual($expected, $result); // End of campaign (70,000 imps served) $impressions = 70000; $startDate->subtractSeconds(60 * 60 * 24 * 5); // We are 10 days into the campaign. $endDate->subtractSeconds(60 * 60 * 24 * 5); $span->setFromDateDiff($startDate, $now); $this->assertEqual(10, $span->toDays()); $expected = 142.857142857; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_MT, $revenue, $impressions, $clicks, $conversions, $startDate->getDate(DATE_FORMAT_ISO), $endDate->getDate(DATE_FORMAT_ISO)); // Is this the correct margin? $this->assertWithinMargin($expected, $result, 0.0001, "Outside of margin"); // No dates given $expected = 0; $result = OX_Util_Utils::getEcpm(MAX_FINANCE_MT, $revenue, $impressions, $clicks, $conversions, null, null); $this->assertEqual($expected, $result); }
function getWeeks() { $span = new Date_Span(new Date($this->get('dato_start')), new Date($this->get('dato_slut'))); return round($span->toDays() / 7); }
/** * Calculates the effective CPM (eCPM) * * @param int $revenueType revenue type (CPM, CPA, etc) as defined in constants.php. * @param double $revenue revenue amount, eg 1.55. CPM, CPC, CPA: the rate. Tenancy: the total. * @param int $impressions the number of impressions. * @param int $clicks the number of clicks * @param int $conversions the number of conversions. * @param string $startDate start date of the campaign. Required for tenancy. * @param string $endDate end date of the campaign. Required for tenancy. * @param double defaultClickRatio click ratio to use when there are no impressions. * If null, uses the value in the config file. * @param double defaultConversionRatio conversion ratio to use when there are no impressions. * If null, uses the value in the config file. * * @return double the eCPM */ public static function getEcpm($revenueType, $revenue, $impressions = 0, $clicks = 0, $conversions = 0, $startDate = null, $endDate = null, $defaultClickRatio = null, $defaultConversionRatio = null) { $ecpm = 0.0; switch ($revenueType) { case MAX_FINANCE_CPM: // eCPM = CPM return $revenue; break; case MAX_FINANCE_CPC: if ($impressions != 0) { $ecpm = $revenue * $clicks / $impressions * 1000; } else { if (!$defaultClickRatio) { $defaultClickRatio = $GLOBALS['_MAX']['CONF']['priority']['defaultClickRatio']; } $ecpm = $defaultClickRatio * $revenue * 1000; } break; case MAX_FINANCE_CPA: if ($impressions != 0) { $ecpm = $revenue * $conversions / $impressions * 1000; } else { if (!$defaultConversionRatio) { $defaultConversionRatio = $GLOBALS['_MAX']['CONF']['priority']['defaultConversionRatio']; } $ecpm = $defaultConversionRatio * $revenue * 1000; } break; case MAX_FINANCE_MT: if ($impressions != 0) { if ($startDate && $endDate) { $daysInCampaign = new Date_Span(); $daysInCampaign->setFromDateDiff(new Date($startDate), new Date($endDate)); $daysSoFar = new Date_Span(); $daysSoFar->setFromDateDiff(new Date($startDate), new Date('Y-m-d')); $ecpm = $revenue / $daysInCampaign->toDays() * $daysSoFar->toDays() / $impressions * 1000; } else { // Not valid without start and end dates. $ecpm = 0.0; } } else { $ecpm = 0.0; } break; } return $ecpm; }