Improvements to traffic collection, zero record ignore, some fixes to order_by

This commit is contained in:
Deon George
2013-10-14 11:26:08 +11:00
parent 335e8e65bd
commit c347032497
6 changed files with 94 additions and 4 deletions

View File

@@ -21,6 +21,15 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
'plan'=>array('model'=>'Service_Plugin_Adsl','foreign_key'=>'service_username','far_key'=>'service'),
);
private $metrics = array(
'up_peak',
'up_offpeak',
'down_peak',
'down_offpeak',
'peer',
'internal',
);
public function rules() {
$result = parent::rules();
@@ -30,6 +39,37 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
return $result;
}
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);
$l = ORM::factory($this->_object_name)->where('service','=',$this->service)->where('supplier_id','=',$this->supplier_id)->list_last();
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;
}
public function total() {
$result = 0;
foreach ($this->metrics as $metric) {
if (is_null($this->$metric))
$this->$metric = 0;
$result += $this->$metric;
}
return $result;
}
public function traffic(Model_Product_Plugin_Adsl $plan) {
// Roll up the charges according to the product plan configuration
return ADSL::allowance(array(
@@ -43,5 +83,9 @@ class Model_Service_Plugin_Adsl_Traffic extends ORM_OSB {
'extra_up_offpeak'=>$plan->extra_up_offpeak,
));
}
public function list_last() {
return $this->order_by('date','DESC')->order_by('time','DESC')->find();
}
}
?>