More updates to ADSL module

This commit is contained in:
Deon George
2013-11-15 16:25:45 +11:00
parent f80fcdc4c1
commit c66f2f14c4
12 changed files with 203 additions and 193 deletions

View File

@@ -115,7 +115,7 @@ class Model_Product_Plugin_Adsl extends Model_Product_Plugin {
*
* @param array Traffic Used in each metric.
* @param bool Whether the output should be a string
* @param bool Display the over alloance numbers
* @param bool Display the over allowance numbers
* @param int Divide the numbers
*/
public function allowance(array $data=array(),$format=FALSE,$over=FALSE,$divide=0) {

View File

@@ -103,6 +103,19 @@ $(document).ready(function() {
return $format ? Config::date($x) : $x;
}
/**
* This function will take an array of numbers and change it into a cumulative array
*/
private function cumulative($array) {
$result = array();
$s = 0;
foreach ($array as $k => $v)
$result[$k] = ($s += $v);
return $result;
}
/**
* This function will return the months that have traffic data.
* This array can be used in a select list to display the traffic for that month
@@ -229,6 +242,80 @@ $(document).ready(function() {
throw new Kohana_Exception('This function hasnt been written yet.');
}
/**
* Return the template variables, used mainly in emailing
*
* @param array Variables that need to be expanded
* @param array Data that was previously calculated
*/
public function template_variables(array $array,array $data=array()) {
$result = array();
$friendly = array(
'base_down_peak'=>'Peak',
'base_down_offpeak'=>'OffPeak',
'cumulative_base_down_peak'=>'Total Peak',
'cumulative_base_down_offpeak'=>'Total OffPeak',
);
if (! isset($data['allow']))
$data['allow'] = $this->plan()->allowance(array(),FALSE,TRUE);
if (! isset($data['last']))
$data['last'] = $this->traffic->find_last()->date();
if (! isset($data['used']))
$data['used'] = $this->traffic_month($data['last']);
if (! isset($data['day']))
$data['day'] = date('d',strtotime('yesterday'));
$daysleft = date('d',strtotime('last day of',$data['last']))-$data['day'];
$traffic_type = $this->get_traffic_typedaily($data['last']);
$google = GoogleChart::factory('Legacy')
->type('vertical_bar')
->title(sprintf('DSL traffic usage as at %s',Config::date($data['last'])));
foreach ($traffic_type as $k => $details)
$google->sdata(array('yl'=>($x=$this->traffic->friendly($k))),array($x=>$details));
// Work out comulative numbers
foreach ($traffic_type as $k => $details)
$google->sdata(array('yr'=>($x='Cumulative '.$this->traffic->friendly($k))),array($x=>$this->cumulative($traffic_type[$k])));
foreach ($array as $item) {
switch ($item) {
case 'MONTH_GRAPH': $value = (string)$google; break;
case 'MONTH_TABLE': $value = $google->table(FALSE,array(
'table'=>'style="border: 1px solid #bebcb7; padding: 5px 5px; background: none repeat scroll 0% 0% #f8f7f5; font-size: 70%;"',
)); break;
case 'OFFPEAK_ALLOWANCE': $value = isset($data['allow']['base_down_offpeak']) ? $data['allow']['base_down_offpeak'].' MB' : '-'; break;
case 'OFFPEAK_USAGE': $value = isset($data['used']['base_down_offpeak']) ? $data['used']['base_down_offpeak'].' MB' : '-'; break;
case 'PEAK_ALLOWANCE': $value = isset($data['allow']['base_down_peak']) ? $data['allow']['base_down_peak'].' MB' : '-'; break;
case 'PEAK_USAGE': $value = isset($data['used']['base_down_peak']) ? $data['used']['base_down_peak'].' MB' : '-'; break;
case 'OFFPEAK_AVERAGE': $value = isset($data['used']['base_down_offpeak']) ? round($data['used']['base_down_offpeak']/$data['day'],2).' MB' : '-'; break;
case 'OFFPEAK_AVERAGE_REMAIN': $value = ((isset($data['used']['base_down_offpeak']) AND ($data['allow']['base_down_offpeak'] > $data['used']['base_down_offpeak']) AND $daysleft) ? round(($data['allow']['base_down_offpeak']-$data['used']['base_down_offpeak'])/$daysleft,2).' MB' : '-'); break;
case 'PEAK_AVERAGE': $value = isset($data['used']['base_down_peak']) ? round($data['used']['base_down_peak']/$data['day'],2).' MB' : '-'; break;
case 'PEAK_AVERAGE_REMAIN': $value = ((isset($data['used']['base_down_peak']) AND ($data['allow']['base_down_peak'] > $data['used']['base_down_peak']) AND $daysleft) ? round(($data['allow']['base_down_peak']-$data['used']['base_down_peak'])/$daysleft,2).' MB' : '-'); break;
case 'SERVICE_NUMBER': $value = $this->service_number; break;
case 'USAGE_DATE': $value = Config::date($data['last']); break;
case 'USER_NAME': $value = $this->service->account->name(); break;
default:
$value = '';
}
$result[$item] = $value;
}
return $result;
}
/**
* Calculate the Excess Traffic Charges
*/
@@ -298,6 +385,48 @@ $(document).ready(function() {
return $x ? array_pop($x) : 0;
}
/**
* Determine if we alert traffic
*
* We alert traffic if:
* + 80% of usage every 3 days
* + average daily usage > allowance every 5 days
* + last day of the period
*
* @return bool
*/
public function traffic_report() {
$result = array();
$result['day'] = date('d',strtotime('yesterday'));
$result['last'] = $this->traffic->find_last()->date();
$lastday = date('d',strtotime('last day of',$result['last']));
$result['allow'] = $this->plan()->allowance(array(),FALSE,TRUE);
$result['used'] = $this->traffic_month($result['last']);
if (! $result['used'])
return array();
$result['day'] = 3;
// If we are the last day of the period, and we had traffic
if ($result['day'] == $lastday)
return $result;
foreach ($result['used'] as $k => $v) {
// If we are at 80% usage
if ($v/($result['allow'][$k] > 0 ? $result['allow'][$k] : 1) >= .8 AND $result['day']%3 == 0)
return $result;
// If our average is greater than our allowance
if ($result['day']%5 == 0 AND ($v/$result['day'] > $result['allow'][$k]/$result['day']))
return $result;
}
// If we get here, then we dont need to report usage.
return array();
}
/**
* Render a table of traffic
*/
@@ -323,10 +452,6 @@ $(document).ready(function() {
* Search for services matching a term
*/
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=NULL) {
// We only show service numbers.
if (! is_numeric($term))
return array();
$ao = Auth::instance()->get_user();
$options['key'] = 'id';
@@ -335,130 +460,14 @@ $(document).ready(function() {
->join('service')
->on('service.id','=',$this->_table_name.'.service_id')
->where('service.account_id','IN',$ao->RTM->customers($ao->RTM))
->and_where($this->_table_name.'.service_number','like','%'.$term.'%');
->where_open()
->and_where($this->_table_name.'.service_number','like','%'.$term.'%')
->or_where($this->_table_name.'.service_address','like','%'.$term.'%')
->where_close();
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
}
public function template_variables($array) {
$friendly = array(
'base_down_peak'=>'Peak',
'base_down_offpeak'=>'OffPeak',
'cumulative_base_down_peak'=>'Total Peak',
'cumulative_base_down_offpeak'=>'Total OffPeak',
);
$result = array();
if ($this->service->product->prod_plugin_file != 'ADSL')
throw new Kohana_Exception('Huh? How did this get called, for a non ADSL product (:ppf)',array(':ppf'=>$this->service_id));
$allowance = $this->service->product->plugin()->allowance(FALSE);
$period = strtotime('yesterday');
$traffic_data = $this->get_traffic_data_daily($period,FALSE);
$traffic = $this->get_traffic_data_month($period);
$traffic_type = $this->get_traffic_data_daily($period,TRUE);
$day = count($traffic_type) ? max(array_keys($traffic_type)) : 1;
$date = mktime(0,0,0,date('n',$period),$day,date('Y',$period));
$daysleft = date('d',strtotime('last day of',$date))-$day;
$google = GoogleChart::factory('Legacy')
->type('vertical_bar')
->title(sprintf('DSL traffic usage as at %s',Config::date($date)));
foreach ($traffic_data as $k => $details)
$google->sdata(array('yl'=>($x=isset($friendly[$k]) ? $friendly[$k] : $k)),array($x=>$traffic_data[$k]));
// Work out comulative numbers
foreach ($traffic_data as $k => $details)
$google->sdata(array('yr'=>($x=isset($friendly['cumulative'.$k]) ? $friendly['cumulative'.$k] : 'cumulative'.$k)),array($x=>$this->cumulative($traffic_data[$k])));
foreach ($array as $item) {
switch ($item) {
case 'MONTH_GRAPH': $value = (string)$google; break;
case 'MONTH_TABLE': $value = $google->table(FALSE,array(
'table'=>'style="border: 1px solid #bebcb7; padding: 5px 5px; background: none repeat scroll 0% 0% #f8f7f5; font-size: 70%;"',
)); break;
case 'OFFPEAK_ALLOWANCE': $value = isset($allowance['base_down_offpeak']) ? $allowance['base_down_offpeak'].' MB' : '-'; break;
case 'OFFPEAK_USAGE': $value = isset($traffic['base_down_offpeak']) ? $traffic['base_down_offpeak'].' MB' : '-'; break;
case 'PEAK_ALLOWANCE': $value = isset($allowance['base_down_peak']) ? $allowance['base_down_peak'].' MB' : '-'; break;
case 'PEAK_USAGE': $value = isset($traffic['base_down_peak']) ? $traffic['base_down_peak'].' MB' : '-'; break;
case 'OFFPEAK_AVERAGE': $value = isset($traffic['base_down_offpeak']) ? round($traffic['base_down_offpeak']/$day,2).' MB' : '-'; break;
case 'OFFPEAK_AVERAGE_REMAIN': $value = ((isset($traffic['base_down_offpeak']) AND ($allowance['base_down_offpeak'] > $traffic['base_down_offpeak']) AND $daysleft) ? round(($allowance['base_down_offpeak']-$traffic['base_down_offpeak'])/$daysleft,2).' MB' : '-'); break;
case 'PEAK_AVERAGE': $value = isset($traffic['base_down_peak']) ? round($traffic['base_down_peak']/$day,2).' MB' : '-'; break;
case 'PEAK_AVERAGE_REMAIN': $value = ((isset($traffic['base_down_peak']) AND ($allowance['base_down_peak'] > $traffic['base_down_peak']) AND $daysleft) ? round(($allowance['base_down_peak']-$traffic['base_down_peak'])/$daysleft,2).' MB' : '-'); break;
case 'SERVICE_NUMBER': $value = $this->service_number; break;
case 'USAGE_DATE': $value = Config::date($date); break;
case 'USER_NAME': $value = $this->service->account->name(); break;
default:
$value = '';
}
$result[$item] = $value;
}
return $result;
}
/**
* This function will take an array of numbers and change it into a cumulative array
*/
private function cumulative($array) {
$result = array();
$s = 0;
foreach ($array as $k => $v)
$result[$k] = ($s += $v);
return $result;
}
/**
* Determine if we alert traffic
*
* We alert traffic if:
* + 80% of usage every 3 days
* + average daily usage > allowance every 5 days
* + last day of the period
*/
public function report_traffic() {
if ($this->service->product->prod_plugin_file != 'ADSL')
throw new Kohana_Exception('Huh? How did this get called, for a non ADSL product (:ppf)',array(':ppf'=>$this->service_id));
$allowance = $this->service->product->plugin()->allowance(FALSE);
$period = strtotime('yesterday');
$traffic_data = $this->get_traffic_data_daily($period,FALSE);
$traffic = $this->get_traffic_data_month($period);
$traffic_type = $this->get_traffic_data_daily($period,TRUE);
// @todo If no data comes in, then this can be stuck reporting traffic for an old date.
$day = count($traffic_type) ? max(array_keys($traffic_type)) : 1;
$lastday = date('d',strtotime('last day of',$period));
foreach ($traffic as $k => $v) {
// If we are the last day of the period
if ($day == $lastday AND $v)
return TRUE;
// If we are at 80% usage
if ($v/($allowance[$k] > 0 ? $allowance[$k] : 1) >= .8 AND $day%3 == 0)
return TRUE;
// If our average is greater than our allowance
if ($day%5 == 0 AND ($v/$day > $allowance[$k]/$day))
return TRUE;
}
// If we get here, then we dont need to report usage.
return FALSE;
}
/**
* Get specific service details for use in other modules
* For Example: Invoice
@@ -478,47 +487,5 @@ $(document).ready(function() {
return parent::$_details($type);
}
}
/**
* Return an array of the data used in a month by day
*/
private function get_traffic_data_daily($period=NULL,$bydate=FALSE) {
$result = array();
if (is_null($period))
$period = strtotime('yesterday');
$t = ORM::factory('Service_Plugin_Adsl_Traffic')
->where('service','=',$this->service_username)
->and_where('date','>=',date('Y-m-d',mktime(0,0,0,date('m',$period),1,date('Y',$period))));
foreach ($t->find_all() as $to) {
$day = (string)date('d',strtotime($to->date));
if ($bydate)
$result[$day] = $to->traffic($this->service->product->plugin());
else
foreach ($to->traffic($this->service->product->plugin()) as $k => $v)
$result[$k][$day] = (int)$v;
}
return $result;
}
/**
* Calculate the total traffic used in a month
*/
private function get_traffic_data_month($period=NULL,$cache=0) {
$result = array();
foreach ($this->get_traffic_data_daily($period,TRUE,$cache) as $tdata)
foreach ($tdata as $k => $v)
if (isset($result[$k]))
$result[$k] += $v;
else
$result[$k] = $v;
return $result;
}
}
?>

View File

@@ -48,6 +48,10 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
return $result;
}
public function date() {
return strtotime($this->date);
}
public function friendly($name) {
return isset($this->_friendly[$name]) ? $this->_friendly[$name] : $name;
}