public static function doMonthlyPartnerUsage(Partner $partner)
 {
     KalturaLog::debug("Validating partner [" . $partner->getId() . "]");
     $packages = new PartnerPackages();
     $partnerPackage = $packages->getPackageDetails($partner->getPartnerPackage());
     if ($partnerPackage[PartnerPackages::PACKAGE_CYCLE_FEE] != 0) {
         KalturaLog::debug("Partner has paid package, skipping validation [" . $partner->getId() . "]");
         return;
     }
     $block_notification_grace = time() - dateUtils::DAY * self::BLOCKING_DAYS_GRACE;
     $delete_grace = time() - dateUtils::DAY * 30;
     $report_date = dateUtils::todayOffset(-1);
     list($monthlyStorage, $monthlyTraffic, $monthlyTranscoding) = myPartnerUtils::collectPartnerMonthlyStatisticsFromDWH($partner, $report_date);
     $email_link_hash = 'pid=' . $partner->getId() . '&h=' . self::getEmailLinkHash($partner->getId(), $partner->getSecret());
     self::validatePartnerMonthlyUsagePerType($partner, $partnerPackage, $monthlyStorage, PartnerPackages::PACKAGE_STORAGE_USAGE, $report_date, $block_notification_grace, $delete_grace, $email_link_hash);
     self::validatePartnerMonthlyUsagePerType($partner, $partnerPackage, $monthlyTraffic, PartnerPackages::PACKAGE_TRAFFIC_USAGE, $report_date, $block_notification_grace, $delete_grace, $email_link_hash);
     self::validatePartnerMonthlyUsagePerType($partner, $partnerPackage, $monthlyTranscoding, PartnerPackages::PACKAGE_TRANSCODING_USAGE, $report_date, $block_notification_grace, $delete_grace, $email_link_hash);
     $partner->save();
 }
 public static function doPartnerUsage(Partner $partner)
 {
     if ($partner->getExtendedFreeTrail()) {
         if ($partner->getExtendedFreeTrailExpiryDate() < time()) {
             //ExtendedFreeTrail ended
             $partner->setExtendedFreeTrail(null);
             $partner->setExtendedFreeTrailExpiryDate(null);
             $partner->setExtendedFreeTrailExpiryReason('');
         } else {
             //ExtendedFreeTrail
             if ($partner->getExtendedFreeTrailExpiryDate() < time() + dateUtils::DAY * 7 && !$partner->getExtendedFreeTrailEndsWarning()) {
                 $partner->setExtendedFreeTrailEndsWarning(true);
                 $partner->save();
                 $email_link_hash = 'pid=' . $partner->getId() . '&h=' . self::getEmailLinkHash($partner->getId(), $partner->getSecret());
                 $mail_parmas = array($partner->getAdminName(), $email_link_hash);
                 myPartnerUtils::notifiyPartner(myPartnerUtils::KALTURA_EXTENED_FREE_TRAIL_ENDS_WARNING, $partner, $mail_parmas);
             }
             return;
         }
     }
     $should_block_delete_partner = true;
     $blocking_days_grace = 7;
     $block_notification_grace = time() - dateUtils::DAY * $blocking_days_grace;
     $delete_grace = time() - dateUtils::DAY * 30;
     $packages = new PartnerPackages();
     $partnerPackage = $packages->getPackageDetails($partner->getPartnerPackage());
     $report_date = date('Y-m') . '-01';
     // We are now working with the DWH and a stored-procedure, and not with record type 6 on partner_activity.
     $report_date = dateUtils::todayOffset(-1);
     list($totalStorage, $totalUsage, $totalTraffic) = myPartnerUtils::collectPartnerStatisticsFromDWH($partner, $partnerPackage, $report_date);
     $totalUsageGB = $totalUsage / 1024 / 1024;
     // from KB to GB
     $percent = round($totalUsageGB / $partnerPackage['cycle_bw'] * 100, 2);
     KalturaLog::debug("percent (" . $partner->getId() . ") is: {$percent}");
     $email_link_hash = 'pid=' . $partner->getId() . '&h=' . self::getEmailLinkHash($partner->getId(), $partner->getSecret());
     $email_link_hash_adOpt = $email_link_hash . '&type=adOptIn';
     /* mindtouch partners - extra mail parameter */
     $mindtouch_notice = ' ';
     if ($partner->getType() == 103) {
         $mindtouch_notice = '<BR><BR>Note: You must be a MindTouch paying customer to upgrade your video service. If you are not a paying MindTouch customer, contact MindTouch: http://www.mindtouch.com/about_mindtouch/contact_mindtouch to get a quote.<BR><BR>';
     }
     if ($percent >= 80 && $percent < 100 && !$partner->getEightyPercentWarning()) {
         KalturaLog::debug("partner " . $partner->getId() . " reached 80% - setting first warning");
         /* prepare mail job, and set EightyPercentWarning() to true/date */
         $partner->setEightyPercentWarning(time());
         $partner->setUsageLimitWarning(0);
         $body_params = array($partner->getAdminName(), $partnerPackage['cycle_bw'], $mindtouch_notice, round($totalUsageGB, 2), $email_link_hash);
         myPartnerUtils::notifiyPartner(myPartnerUtils::KALTURA_PACKAGE_EIGHTY_PERCENT_WARNING, $partner, $body_params);
     } elseif ($percent >= 80 && $percent < 100 && $partner->getEightyPercentWarning() && !$partner->getUsageLimitWarning()) {
         KalturaLog::debug("passed the 80%, assume notification sent, nothing to do.");
     } elseif ($percent < 80 && $partner->getEightyPercentWarning()) {
         KalturaLog::debug("partner " . $partner->getId() . " was 80%, now not. clearing warnings");
         /* clear getEightyPercentWarning */
         $partner->setEightyPercentWarning(0);
         $partner->setUsageLimitWarning(0);
     } elseif ($percent >= 100 && !$partner->getUsageLimitWarning()) {
         KalturaLog::debug("partner " . $partner->getId() . " reached 100% - setting second warning");
         /* prepare mail job, and set getUsageLimitWarning() date */
         $partner->setUsageLimitWarning(time());
         $body_params = array($partner->getAdminName(), $mindtouch_notice, round($totalUsageGB, 2), $email_link_hash);
         myPartnerUtils::notifiyPartner(myPartnerUtils::KALTURA_PACKAGE_LIMIT_WARNING_1, $partner, $body_params);
     } elseif ($percent >= 100 && $partnerPackage['cycle_fee'] == 0 && $partner->getUsageLimitWarning() > 0 && $partner->getUsageLimitWarning() <= $block_notification_grace && $partner->getUsageLimitWarning() > $delete_grace && $partner->getStatus() != Partner::PARTNER_STATUS_CONTENT_BLOCK) {
         KalturaLog::debug("partner " . $partner->getId() . " reached 100% {$blocking_days_grace} days ago - sending block email and blocking partner");
         /* send block email and block partner */
         $body_params = array($partner->getAdminName(), $mindtouch_notice, round($totalUsageGB, 2), $email_link_hash);
         myPartnerUtils::notifiyPartner(myPartnerUtils::KALTURA_PACKAGE_LIMIT_WARNING_2, $partner, $body_params);
         if ($should_block_delete_partner) {
             $partner->setStatus(Partner::PARTNER_STATUS_CONTENT_BLOCK);
         }
     } elseif ($percent >= 120 && $partnerPackage['cycle_fee'] != 0 && $partner->getUsageLimitWarning() <= $block_notification_grace) {
         $body_params = array($partner->getAdminName(), round($totalUsageGB, 2));
         myPartnerUtils::notifiyPartner(myPartnerUtils::KALTURA_PAID_PACKAGE_SUGGEST_UPGRADE, $partner, $body_params);
     } elseif ($percent >= 100 && $partnerPackage['cycle_fee'] == 0 && $partner->getUsageLimitWarning() > 0 && $partner->getUsageLimitWarning() <= $delete_grace && $partner->getStatus() == Partner::PARTNER_STATUS_CONTENT_BLOCK) {
         KalturaLog::debug("partner " . $partner->getId() . " reached 100% a month ago - deleting partner");
         /* delete partner */
         $body_params = array($partner->getAdminName());
         myPartnerUtils::notifiyPartner(myPartnerUtils::KALTURA_DELETE_ACCOUNT, $partner, $body_params);
         if ($should_block_delete_partner) {
             $partner->setStatus(Partner::PARTNER_STATUS_DELETED);
         }
     } elseif ($percent < 80 && ($partner->getUsageLimitWarning() || $partner->getEightyPercentWarning())) {
         KalturaLog::debug("partner " . $partner->getId() . " OK");
         // PARTNER OK
         // resetting status and warnings should only be done manually
         //$partner->setStatus(1);
         $partner->setEightyPercentWarning(0);
         $partner->setUsageLimitWarning(0);
     }
     $partner->save();
 }
Beispiel #3
0
 /**
  * Get usage statistics for a partner
  * Calculation is done according to partner's package
  *
  * Additional data returned is a graph points of streaming usage in a timeframe
  * The resolution can be "days" or "months"
  *
  * @link http://docs.kaltura.org/api/partner/usage
  * @action getUsage
  * @param int $year
  * @param int $month
  * @param string $resolution accepted values are "days" or "months"
  * @return KalturaPartnerUsage
  * 
  * @throws APIErrors::UNKNOWN_PARTNER_ID
  */
 function getUsageAction($year = '', $month = 1, $resolution = "days")
 {
     $dbPartner = PartnerPeer::retrieveByPK($this->getPartnerId());
     if (!$dbPartner) {
         throw new KalturaAPIException(APIErrors::UNKNOWN_PARTNER_ID, $this->getPartnerId());
     }
     $packages = new PartnerPackages();
     $partnerUsage = new KalturaPartnerUsage();
     $partnerPackage = $packages->getPackageDetails($dbPartner->getPartnerPackage());
     $report_date = dateUtils::todayOffset(-3);
     list($totalStorage, $totalUsage, $totalTraffic) = myPartnerUtils::collectPartnerUsageFromDWH($dbPartner, $partnerPackage, $report_date);
     $partnerUsage->hostingGB = round($totalStorage / 1024, 2);
     // from MB to GB
     $totalUsageGB = round($totalUsage / 1024 / 1024, 2);
     // from KB to GB
     if ($partnerPackage) {
         $partnerUsage->Percent = round($totalUsageGB / $partnerPackage['cycle_bw'] * 100, 2);
         $partnerUsage->packageBW = $partnerPackage['cycle_bw'];
     }
     $partnerUsage->usageGB = $totalUsageGB;
     $partnerUsage->reachedLimitDate = $dbPartner->getUsageLimitWarning();
     if ($year != '' && is_int($year)) {
         $graph_lines = myPartnerUtils::getPartnerUsageGraph($year, $month, $dbPartner, $resolution);
         // currently we provide only one line, output as a string.
         // in the future this could be extended to something like KalturaGraphLines object
         $partnerUsage->usageGraph = $graph_lines['line'];
     }
     return $partnerUsage;
 }
 /**
  * Get usage statistics for a partner
  * Calculation is done according to partner's package
  *
  * @action getStatistics
  * @return KalturaPartnerStatistics
  * 
  * @throws APIErrors::UNKNOWN_PARTNER_ID
  */
 public function getStatisticsAction()
 {
     $dbPartner = PartnerPeer::retrieveByPK($this->getPartnerId());
     if (!$dbPartner) {
         throw new KalturaAPIException(APIErrors::UNKNOWN_PARTNER_ID, $this->getPartnerId());
     }
     $packages = new PartnerPackages();
     $partnerUsage = new KalturaPartnerStatistics();
     $partnerPackage = $packages->getPackageDetails($dbPartner->getPartnerPackage());
     $report_date = date("Y-m-d", time());
     list($totalStorage, $totalUsage, $totalTraffic) = myPartnerUtils::collectPartnerUsageFromDWH($dbPartner, $partnerPackage, $report_date);
     $partnerUsage->hosting = round($totalStorage / 1024, 2);
     // from MB to GB
     $totalUsageGB = round($totalUsage / 1024 / 1024, 2);
     // from KB to GB
     if ($partnerPackage) {
         $partnerUsage->usagePercent = round($totalUsageGB / $partnerPackage['cycle_bw'] * 100, 2);
         $partnerUsage->packageBandwidthAndStorage = $partnerPackage['cycle_bw'];
     }
     if ($totalTraffic) {
         $partnerUsage->bandwidth = round($totalTraffic / 1024 / 1024, 2);
         // from KB to GB
     }
     $partnerUsage->usage = $totalUsageGB;
     $partnerUsage->reachedLimitDate = $dbPartner->getUsageLimitWarning();
     return $partnerUsage;
 }
 public function executeImpl($partner_id, $subp_id, $puser_id, $partner_prefix, $puser_kuser)
 {
     // make sure the secret fits the one in the partner's table
     $partner = PartnerPeer::retrieveByPK($partner_id);
     if (!$partner) {
         // CANNOT be because we are already in the service. it would have fallen before...
         $this->addException(APIErrors::UNKNOWN_PARTNER_ID);
     }
     $packages = new PartnerPackages();
     $partnerPackage = $packages->getPackageDetails($partner->getPartnerPackage());
     $report_date = dateUtils::todayOffset(-3);
     list($totalStorage, $totalUsage, $totalTraffic) = myPartnerUtils::collectPartnerUsageFromDWH($partner, $partnerPackage, $report_date);
     $return['hostingGB'] = round($totalStorage / 1024, 2);
     // from MB to GB
     $totalUsageGB = round($totalUsage / 1024 / 1024, 2);
     // from KB to GB
     $return['Percent'] = round($totalUsageGB / $partnerPackage['cycle_bw'] * 100, 2);
     $return['package_bw'] = $partnerPackage['cycle_bw'];
     $return['GB'] = $totalUsageGB;
     $return['reached_limit_date'] = $partner->getUsageLimitWarning();
     /* total usage status */
     /*  --- ended total usage status --- */
     /* usage graph */
     $year = $this->getPM("year");
     $month = $this->getP("month");
     $resolution = $this->getP("resolution");
     $graph_points = myPartnerUtils::getPartnerUsageGraph($year, $month, $partner, $resolution);
     /* --- ended usage graph --- */
     $this->addMsg("usage", $return);
     if (isset($debug_data)) {
         $this->addDebug('debug_data', $debug_data);
     }
     $this->addMsg("graph", $graph_points);
     /*
     * ADD ATTRIBUTES TO GRAPH
     
     <graph caption=""
     bgcolor="0xffffff" 
     border="false" 
     bordercolor="0xffffa7" 
     customxaxis="true" 
     minimumx="0"  
     maximumx="10" 
     intervalx="1" 
     xaxisname="Time"
     xtype="Category" 
     dataunits="hours"
     customyaxis="false" 
     minimumy="0" 
     maximumy="100" 
     intervaly="10" 
     yaxisname=""
     showdatatips="true" 
     mousesensitivity="50" 
     datatipmode="multiple"
     gridlinesdirection="both"
     customgridlines="true" 
     lineshadow="false"
     horizontalstrokecolor="0xeeeeee" 
     horizontalstrokesize=".3" 
     horizontalfillcolor="0xffffff" 
     horizontalfillsize=".3" 
     horizontalstrokealpha="0.1"
     horizontalalternatefillcolor="0xffffff" 
     horizontalalternatefillsize=".3"
     verticalstrokecolor="0xcccccc" 
     verticalstrokesize=".5"  
     verticalstrokealpha="0.5"
     verticalfillcolor="0xffffff" 
     verticalfillsize=".3" 
     verticalalternatefillcolor="0xffffff"
     verticalalternatefillsize=".3">
     
     * 
     */
 }