2013-10-10 02:44:53 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports Services Traffic for ADSL
|
|
|
|
*
|
|
|
|
* @package ADSL
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
2016-08-15 11:23:18 +00:00
|
|
|
class Model_Service_Plugin_Adsl_Traffic extends ORM {
|
2013-10-10 02:44:53 +00:00
|
|
|
protected $_table_name = 'service__adsl_traffic';
|
|
|
|
protected $_primary_key = 'service';
|
|
|
|
protected $_disable_wild_select = TRUE;
|
|
|
|
|
|
|
|
protected $_created_column = FALSE;
|
|
|
|
protected $_updated_column = FALSE;
|
|
|
|
|
2013-10-11 04:02:56 +00:00
|
|
|
protected $_has_one = array(
|
|
|
|
'plan'=>array('model'=>'Service_Plugin_Adsl','foreign_key'=>'service_username','far_key'=>'service'),
|
|
|
|
);
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
public static $metrics = array(
|
|
|
|
'down_peak'=>'base_down_peak',
|
|
|
|
'down_offpeak'=>'base_down_offpeak',
|
|
|
|
'up_peak'=>'base_up_peak',
|
|
|
|
'up_offpeak'=>'base_up_offpeak',
|
|
|
|
'peer'=>'base_down_peak',
|
|
|
|
'internal'=>'base_down_offpeak',
|
|
|
|
);
|
|
|
|
|
|
|
|
private $_friendly = array(
|
|
|
|
'base_up_peak'=>'UP Peak',
|
|
|
|
'base_up_offpeak'=>'UP Offpeak',
|
|
|
|
'base_down_peak'=>'DOWN Peak',
|
|
|
|
'base_down_offpeak'=>'DOWN Offpeak',
|
|
|
|
'base_peer'=>'Peer',
|
|
|
|
'base_internal'=>'Internal',
|
2013-10-14 00:26:08 +00:00
|
|
|
);
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
public function rules() {
|
|
|
|
$result = parent::rules();
|
|
|
|
|
|
|
|
// We don use the "ID" field.
|
|
|
|
unset($result['id']);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-11-15 05:25:45 +00:00
|
|
|
public function date() {
|
|
|
|
return strtotime($this->date);
|
|
|
|
}
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
public function friendly($name) {
|
|
|
|
return isset($this->_friendly[$name]) ? $this->_friendly[$name] : $name;
|
|
|
|
}
|
|
|
|
|
2013-10-14 00:26:08 +00:00
|
|
|
public function save(Validation $validation = NULL) {
|
|
|
|
// If all our values are zero/NULL, we'll ignore if our previous one was.
|
|
|
|
if ($this->total() !== 0)
|
|
|
|
return parent::save($validation);
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
$l = ORM::factory($this->_object_name)->where('service','=',$this->service)->where('supplier_id','=',$this->supplier_id)->find_last();
|
2013-10-14 00:26:08 +00:00
|
|
|
|
|
|
|
if ($l->total() !== 0)
|
|
|
|
return parent::save($validation);
|
|
|
|
|
|
|
|
// Fake our save, since the previous value is 0.
|
|
|
|
$this->_loaded = $this->_saved = TRUE;
|
|
|
|
$this->_changed = array();
|
|
|
|
$this->_original_values = $this->_object;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
/**
|
|
|
|
* Generate the select statements to SUM up the traffic metrics that we use
|
|
|
|
*/
|
|
|
|
public function selectsummetric() {
|
|
|
|
foreach (Model_Service_Plugin_Adsl_Traffic::$metrics as $metric=>$v)
|
|
|
|
$this->select(array("sum(".$metric.")",$metric));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Total up the traffic into the metrics we use
|
|
|
|
*/
|
2013-10-14 00:26:08 +00:00
|
|
|
public function total() {
|
|
|
|
$result = 0;
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
foreach (array_keys(Model_Service_Plugin_Adsl_Traffic::$metrics) as $metric) {
|
2013-10-14 00:26:08 +00:00
|
|
|
if (is_null($this->$metric))
|
|
|
|
$this->$metric = 0;
|
|
|
|
|
|
|
|
$result += $this->$metric;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
/**
|
|
|
|
* Collapse our traffic data into an array as per $this->_metric
|
|
|
|
*/
|
|
|
|
public function traffic_data() {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach (Model_Service_Plugin_Adsl_Traffic::$metrics as $metric=>$v) {
|
|
|
|
if (! isset($result[$v]))
|
|
|
|
$result[$v] = 0;
|
|
|
|
|
|
|
|
$result[$v] += $this->{$metric};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
2013-10-14 00:26:08 +00:00
|
|
|
|
2013-11-14 11:50:35 +00:00
|
|
|
/**
|
|
|
|
* Find the last traffic record
|
|
|
|
*/
|
|
|
|
public function find_last() {
|
2013-10-14 00:26:08 +00:00
|
|
|
return $this->order_by('date','DESC')->order_by('time','DESC')->find();
|
|
|
|
}
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
?>
|