SAAS-1229, SAAS-1230 - initial implementation of station suspend when over bandwidth limit

This commit is contained in:
Duncan Sommerville 2015-11-24 13:36:54 -05:00
parent 04167c103b
commit 82544f34d9
6 changed files with 64 additions and 0 deletions

View File

@ -53,6 +53,32 @@ class Application_Common_HTTPHelper
return $stationUrl;
}
/**
* Execute a cURL POST
*
* @param string $url the URL to POST to
* @param string[] $userPwd array of user args of the form ['user', 'pwd']
* @param array $formData array of form data kwargs
*
* @return mixed the cURL result
*/
public static function doPost($url, $userPwd, $formData) {
$params = "";
foreach($formData as $key=>$value) {
$params .= $key.'='.$value.'&';
}
rtrim($params, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_USERPWD, implode(':', $userPwd));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
class ZendActionHttpException extends Exception {

View File

@ -12,6 +12,7 @@ class ProvisioningHelper
private $instanceId;
private $stationName, $description;
private $defaultIcecastPassword;
private $bandwidthLimit;
public function __construct($apikey)
{
@ -126,6 +127,9 @@ class ProvisioningHelper
if (isset($_POST['icecast_pass'])) {
$this->defaultIcecastPassword = $_POST['icecast_pass'];
}
if (isset($_POST['bandwidth_limit'])) {
$this->bandwidthLimit = $_POST['bandwidth_limit'];
}
}
/**
@ -219,6 +223,9 @@ class ProvisioningHelper
if (isset($this->defaultIcecastPassword)) {
Application_Model_Preference::setDefaultIcecastPassword($this->defaultIcecastPassword);
}
if (isset($this->bandwidthLimit)) {
Application_Model_Preference::setBandwidthLimit($this->bandwidthLimit);
}
}
}

View File

@ -12,6 +12,8 @@ define('COMPANY_SITE' , 'Sourcefabric.org');
define('COMPANY_SITE_URL' , 'http://sourcefabric.org/');
define('SUPPORT_EMAIL_ADDRESS', "help@sourcefabric.org");
define("AIRTIMEPRO_API_URL", "https://account.sourcefabric.com/api/");
define('HELP_URL' , 'http://help.sourcefabric.org/');
define('FAQ_URL' , 'https://sourcefabricberlin.zendesk.com/hc/en-us/sections/200994309-Airtime-FAQ');
define('WHOS_USING_URL' , 'http://sourcefabric.org/en/airtime/whosusing');

View File

@ -156,6 +156,19 @@ class ApiController extends Zend_Controller_Action
}
Application_Model_Preference::incrementBandwidthLimitCounter($usageBytes);
Application_Model_Preference::setBandwidthLimitUpdateTimer();
$usage = Application_Model_Preference::getBandwidthLimitCounter();
if ($usage > Application_Model_Preference::getBandwidthLimit()) {
$CC_CONFIG = Config::getConfig();
$user = array('', $CC_CONFIG['apiKey'][0]);
$data = array('reason' => "Bandwidth limit exceeded");
try {
$result = Application_Common_HTTPHelper::doPost(AIRTIMEPRO_API_URL, $user, $data);
Logging::info($result);
} catch (Exception $e) {
throw $e;
}
}
}
//Used by the SaaS monitoring

View File

@ -44,6 +44,9 @@ class ProvisioningController extends Zend_Controller_Action
if (isset($_POST['icecast_pass'])) {
Application_Model_Preference::setDefaultIcecastPassword($_POST['icecast_pass']);
}
if (isset($_POST['bandwidth_limit'])) {
Application_Model_Preference::setBandwidthLimit($_POST['bandwidth_limit']);
}
} catch (Exception $e) {
$this->getResponse()
->setHttpResponseCode(400)

View File

@ -1585,6 +1585,19 @@ class Application_Model_Preference
self::setValue("station_podcast_privacy", $value);
}
/**
* Accessors for station bandwidth limit.
*/
public static function getBandwidthLimit() {
$val = self::getValue("bandwidth_limit");
return empty($val) ? 0 : $val;
}
public static function setBandwidthLimit($value) {
self::setValue("bandwidth_limit", $value);
}
/**
* Accessors for bandwidth limit counter.
* Tracks bandwidth usage.