Fixed ADSL traffic collection, and migrated Task to Minion

This commit is contained in:
Deon George
2013-06-01 23:43:31 +10:00
parent d4168146ee
commit 1a40f95b30
10 changed files with 115 additions and 126 deletions

View File

@@ -9,8 +9,8 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Service_Traffic_Adsl {
protected $so;
abstract class Service_Traffic_Adsl {
protected $aso;
protected $today;
protected $fetchresult = NULL;
protected $curlopts = array(
@@ -33,14 +33,14 @@ class Service_Traffic_Adsl {
// Our DB record must be the suffix of this class name
$supplier = preg_replace('/^'.get_parent_class($this).'_/','',get_class($this));
$so = ORM::factory('ADSL_Supplier')
$aso = ORM::factory('ADSL_Supplier')
->where('name','=',$supplier)
->find();
if (! $so->loaded())
if (! $aso->loaded())
throw new Kohana_Exception('Supplier :supplier not defined in the database',array(':supplier'=>$supplier));
$this->so = $so;
$this->aso = $aso;
$this->today = date('Y-m-d',strtotime('yesterday'));
}
@@ -56,22 +56,14 @@ class Service_Traffic_Adsl {
return new $sc;
}
/**
* Get the last date we obtained the stats.
*/
private function last_update() {
return $this->so->stats_lastupdate;
}
/**
* Traffic data from supplier
*/
public function update_traffic() {
if (Minion_CLI::options('verbose'))
echo ' - Last: '.date('Y-m-d',strtotime($this->last_update().'+1 day'))."\n";
echo ' - Last: '.date('Y-m-d',strtotime($this->aso->stats_lastupdate.'+1 day'))."\n";
$alreadyrun = FALSE;
for ($querydate=date('Y-m-d',strtotime($this->last_update().'+1 day'));
for ($querydate=date('Y-m-d',strtotime($this->aso->stats_lastupdate.'+1 day'));
$querydate<=$this->today;
$querydate=date('Y-m-d',strtotime($querydate.'+1 day'))) {
@@ -96,7 +88,7 @@ class Service_Traffic_Adsl {
$traffic = ORM::factory('Service_Plugin_Adsl_Traffic');
foreach ($data as $item) {
$traffic->values($item,array_keys($item));
$traffic->supplier_id = $this->so->id;
$traffic->supplier_id = $this->aso->id;
if ($traffic->check())
$traffic->save();
@@ -108,15 +100,17 @@ class Service_Traffic_Adsl {
}
}
$this->so->stats_lastupdate = $this->today;
$this->so->save();
if ($this->fetchresult) {
$this->aso->stats_lastupdate = $this->today;
$this->aso->save();
}
}
public function charge_excess_traffic() {
$date = strtotime('last month');
// @todo need a way to find out services that have traffic charges dynamically.
foreach ($this->so->services() as $so) {
foreach ($this->aso->services() as $so) {
if ($charge = $so->plugin()->traffic_lastmonth_exceed(FALSE,$date)) {
foreach ($charge as $metric => $details) {
$co = ORM::factory('Charge');
@@ -149,7 +143,7 @@ class Service_Traffic_Adsl {
public function alert_traffic() {
$et = Email_Template::instance('adsl_traffic_notice');
foreach ($this->so->services() as $so) {
foreach ($this->aso->services() as $so) {
if (! $so->plugin()->report_traffic())
continue;

View File

@@ -37,13 +37,13 @@ class Service_Traffic_Adsl_Exetelhspa extends Service_Traffic_Adsl {
// Find our services that need to be collected this way.
$update = array();
foreach ($this->so->services() as $so) {
foreach ($this->aso->services() as $so) {
if (Minion_CLI::options('verbose'))
echo " = Service: ".$so->name()."\n";
if ($so->plugin()->service_stats_collect AND $so->plugin()->service_stats_lastupdate < $date) {
// Start Session
$request = Request::factory($this->so->stats_url)
$request = Request::factory($this->aso->stats_url)
->method('POST')
->post($this->login_user_field,$so->plugin()->service_username)
->post($this->login_pass_field,$so->plugin()->service_password)
@@ -59,14 +59,14 @@ class Service_Traffic_Adsl_Exetelhspa extends Service_Traffic_Adsl {
if (! $data) {
// @todo Log into a log file
printf('Bad fetch for %s [%s]',$so->plugin()->service_number,$this->so->stats_lastupdate);
printf('Bad fetch for %s [%s]',$so->plugin()->service_number,$this->aso->stats_lastupdate);
#$html = new simple_html_dom();
#$html->load($data);
#$html->save(sprintf('/afs/local/tmp/usage.%s.%s.login.html',$so->plugin()->service_number,'login'));
continue;
}
for ($servicedate=date('Y-m-d',strtotime($this->so->stats_lastupdate.'+1 day'));
for ($servicedate=date('Y-m-d',strtotime($this->aso->stats_lastupdate.'+1 day'));
$servicedate <= $this->today;
$servicedate=date('Y-m-d',strtotime('+1 month',strtotime(date('Y-m',strtotime($servicedate)).'-01')))) {
#print_r(array('sn'=>$so->plugin()->service_number,'sd'=>$servicedate));
@@ -78,7 +78,7 @@ class Service_Traffic_Adsl_Exetelhspa extends Service_Traffic_Adsl {
$html = new simple_html_dom();
$notdebug = TRUE;
if ($notdebug) {
$request = Request::factory($this->so->stats_url.'usage_customize_query.php')
$request = Request::factory($this->aso->stats_url.'usage_customize_query.php')
->method('POST')
->post('year_search_key',date('Y',strtotime($servicedate)))
->post('month_search_key',date('m',strtotime($servicedate)))

View File

@@ -37,10 +37,13 @@ class Service_Traffic_Adsl_Exetelpe extends Service_Traffic_Adsl {
// Find our services that need to be collected this way.
$update = array();
foreach ($this->so->services() as $so) {
foreach ($this->aso->services() as $so) {
if (Minion_CLI::options('verbose'))
echo " = Service: ".$so->name()."\n";
if ($so->plugin()->service_stats_collect AND $so->plugin()->service_stats_lastupdate < $date) {
// Start Session
$request = Request::factory($this->so->stats_url)
$request = Request::factory($this->aso->stats_url)
->method('POST')
->post($this->login_user_field,$so->plugin()->service_username == NULL ? '' : $so->plugin()->service_username)
->post($this->login_pass_field,$so->plugin()->service_password == NULL ? '' : $so->plugin()->service_password)
@@ -56,14 +59,14 @@ class Service_Traffic_Adsl_Exetelpe extends Service_Traffic_Adsl {
if (! $data) {
// @todo Log into a log file
printf('Bad fetch for %s [%s]',$so->plugin()->service_number,$this->so->stats_lastupdate);
printf('Bad fetch for %s [%s]',$so->plugin()->service_number,$this->aso->stats_lastupdate);
#$html = new simple_html_dom();
#$html->load($data);
#$html->save(sprintf('/afs/local/tmp/usage.%s.%s.login.html',$so->plugin()->service_number,'login'));
continue;
}
for ($servicedate=date('Y-m-d',strtotime($this->so->stats_lastupdate.'+1 day'));
for ($servicedate=date('Y-m-d',strtotime($this->aso->stats_lastupdate.'+1 day'));
$servicedate <= $this->today;
$servicedate=date('Y-m-d',strtotime('+1 month',strtotime(date('Y-m',strtotime($servicedate)).'-01')))) {
@@ -74,7 +77,7 @@ class Service_Traffic_Adsl_Exetelpe extends Service_Traffic_Adsl {
$html = new simple_html_dom();
$notdebug = TRUE;
if ($notdebug) {
$request = Request::factory($this->so->stats_url.'usage_customize_query.php')
$request = Request::factory($this->aso->stats_url.'usage_customize_query.php')
->method('POST')
->post('year_search_key',date('Y',strtotime($servicedate)))
->post('month_search_key',date('m',strtotime($servicedate)))

View File

@@ -20,10 +20,10 @@ class Service_Traffic_Adsl_Exetelvisp extends Service_Traffic_Adsl {
// Assume we have a bad fetch, unless otherwise specified.
$this->fetchresult = FALSE;
$request = Request::factory($this->so->stats_url)
$request = Request::factory($this->aso->stats_url)
->method('POST')
->post($this->login_user_field,$this->so->stats_username)
->post($this->login_pass_field,$this->so->stats_password)
->post($this->login_user_field,$this->aso->stats_username)
->post($this->login_pass_field,$this->aso->stats_password)
->post($this->date_field,$date);
$request->client()->options($this->curlopts+array(