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 86afba378c
commit 2ac7389223
10 changed files with 115 additions and 126 deletions

View File

@ -11,7 +11,6 @@
*/
class Model_ADSL_Plan extends ORM_OSB {
// Relationships
// @todo This model should probably be joined with product_plugin_adsl
protected $_belongs_to = array(
'adsl_supplier_plan'=>array('model'=>'ADSL_Supplier_Plan'),
);
@ -34,5 +33,16 @@ class Model_ADSL_Plan extends ORM_OSB {
array('Currency::display',array(':value')),
),
);
public function products($active) {
$x = ORM::factory('Product')
->where('prod_plugin_file','=','ADSL')
->and_where('prod_plugin_data','=',$this);
if ($active)
$x->where_active();
return $x;
}
}
?>

View File

@ -10,27 +10,23 @@
* @license http://dev.osbill.net/license.html
*/
class Model_ADSL_Supplier extends ORM_OSB {
protected $_updated_column = FALSE;
// Relationships
protected $_has_many = array(
'adsl_supplier_plan'=>array('model'=>'ADSL_Supplier_Plan','foreign_key'=>'supplier_id','far_key'=>'id'),
);
protected $_updated_column = FALSE;
/**
* Return a list of plans that this supplier makes available
*/
public function plans($active=TRUE) {
$a = $this->adsl_supplier_plan;
if ($active)
$a->where_active();
return $a;
return $active ? $this->adsl_supplier_plan->where_active() : $this->adsl_supplier_plan;
}
/**
* Return a list of plans that we provide by this supplier
* @deprecated
*/
public function adsl_plans($active=TRUE) {
$result = array();
@ -48,25 +44,19 @@ class Model_ADSL_Supplier extends ORM_OSB {
* @param boolean $active TRUE List only active Services|False List all services
*/
public function services($active=TRUE) {
$services = array();
$result = array();
// Get a list of plans made for this supplier
$plans = array_keys($this->adsl_plans(FALSE));
foreach ($this->plans(FALSE)->find_all() as $aspo) {
foreach ($aspo->adsl_plan->find_all() as $apo) {
foreach ($apo->products(FALSE)->find_all() as $po) {
foreach ($po->services($active)->find_all() as $so) {
array_push($result,$so);
}
}
}
}
// Start with all our ADSL Plans
foreach (ORM::factory('Service')->list_bylistgroup('ADSL') as $so)
if (! $active OR $so->status)
if (in_array($so->product->prod_plugin_data,$plans) OR in_array($so->plugin()->provided_adsl_plan_id,$plans))
array_push($services,$so);
// @todo poor cludge, we should find them without using list_bylistgroup()
if (! $services)
foreach (ORM::factory('Service')->list_bylistgroup('HSPA') as $so)
if (! $active OR $so->status)
if (in_array($so->product->prod_plugin_data,$plans) OR in_array($so->plugin()->provided_adsl_plan_id,$plans))
array_push($services,$so);
return $services;
return $result;
}
}
?>

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(

View File

@ -0,0 +1,28 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Get ADSL Traffic from Suppliers
*
* @package ADSL
* @category Tasks
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Adsl_Trafficget extends Minion_Task {
private function _traffic_suppliers($active=FALSE) {
$suppliers = ORM::factory('ADSL_Supplier');
return $active ? $suppliers->list_active() : $suppliers->find_all();
}
protected function _execute(array $params) {
foreach ($this->_traffic_suppliers(TRUE) as $aso) {
if (Minion_CLI::options('verbose'))
echo $aso->name."\n";
Service_Traffic_Adsl::instance($aso->name)->update_traffic();
}
}
}
?>

View File

@ -63,7 +63,7 @@ class Controller_Admin_Product extends Controller_Product {
$pco = ORM::factory('Product_Category',$this->request->param('id'));
if (! $pco->loaded())
HTTP::redirect('welcome/index');
HTTP::redirect(URL::link('admin','product/list'));
if ($_POST)
$pco->values($_POST)->save();

View File

@ -13,7 +13,6 @@
* + price_type: 0=One Time, 1=Recurring, 2=Trial
*/
class Model_Product extends ORM_OSB {
// @todo this doesnt have our site_id when getting the translation
protected $_has_many = array(
'product_translate'=>array('far_key'=>'id'),
'service'=>array('far_key'=>'id'),
@ -42,7 +41,6 @@ class Model_Product extends ORM_OSB {
// Our attributes that are arrays, we'll convert/unconvert them
protected $_serialize_column = array(
'avail_category',
'price_group',
);
@ -55,6 +53,34 @@ class Model_Product extends ORM_OSB {
return $x->loaded() ? $x->display($full ? 'description_full' : 'description_short') : 'No Description';
}
/**
* This will render the product feature summary information
*/
public function feature_summary() {
return (is_null($plugin = $this->plugin())) ? HTML::nbsp('') : $plugin->feature_summary();
}
/**
* Is price shown for a specific period
*
* @param $p int recurring schedule period
*/
public function is_price_shown($p) {
$x = $this->keyget('price_group',$p);
return (isset($x['show']) AND $x['show']) ? TRUE : FALSE;
}
/**
* Test if the product is a TRIAL product
* (price_type == 2)
*
* @return boolean
*/
public function is_trial() {
return ($this->price_type == 2) ? TRUE : FALSE;
}
/**
* Return the object of the product plugin
*/
@ -114,18 +140,8 @@ class Model_Product extends ORM_OSB {
return $x->loaded() ? $x->display('name') : 'No Title';
}
/**
* Which categories is this product available in
*/
public function categories() {
return $this->avail_category;
}
/**
* This will render the product feature summary information
*/
public function feature_summary() {
return (is_null($plugin = $this->plugin())) ? HTML::nbsp('') : $plugin->feature_summary();
public function list_type($type) {
return $this->where('prod_plugin_file','=',$type)->find_all();
}
/**
@ -133,9 +149,6 @@ class Model_Product extends ORM_OSB {
* @todo This needs to be tested with more than one price group enabled
*/
public function get_price_array() {
if (! $this->loaded())
throw new Kohana_Exception('Call to :method where no object loaded?',array(':method'=>__METHOD__));
// Figure out our eligable groups
// @todo Need to work out our default groups elsewhere, not in product
// All users are members of the all user group "0"
@ -164,34 +177,6 @@ class Model_Product extends ORM_OSB {
return $price ? $price : array('0'=>array('price_base'=>0,'price_setup'=>0));
}
/**
* Test if the product is a TRIAL product
* (price_type == 2)
*
* @return boolean
*/
public function is_trial() {
if ($this->price_type == 2)
return TRUE;
else
return FALSE;
}
public function show_thumb() {
$mediapath = Route::get('default/media');
$thumbfile = sprintf('prod_thmb_%s',$this->id);
// @todo This needs to be optimised. By nice if find_files could return the HTML path too.
if (Kohana::find_file('media/img/thumbnails',$thumbfile,'png')) {
$thumb = $mediapath->uri(array('file'=>'img/thumbnails/'.$thumbfile.'.png'));
// @todo Change the ALT to the product name.
echo HTML::image($thumb,array('alt'=>_('Thumb Nail')));
} else
echo '';
}
/**
* Enable the plugin to store data
*/
@ -202,15 +187,6 @@ class Model_Product extends ORM_OSB {
return $plugin->admin_update();
}
/**
* Is price shown for a specific period
*/
public function isPriceShown($p) {
$x = $this->keyget('price_group',$p);
return (isset($x['show']) AND $x['show']) ? TRUE : FALSE;
}
/**
* Return the configured price groups for this product
*/

View File

@ -16,18 +16,6 @@ class Controller_Task_Service extends Controller_Task {
return $active ? $suppliers->list_active() : $suppliers->find_all();
}
/**
* List all services by their default checkout method
*/
public function action_gettraffic() {
foreach ($this->_traffic_suppliers(TRUE) as $aso) {
if (Minion_CLI::options('verbose'))
echo $aso->name."\n";
$traffic = Service_Traffic_Adsl::instance($aso->name)->update_traffic();
}
}
/**
* Charges for excess traffic usage
*/