This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/adsl/classes/Service/Traffic/Adsl/Exetelhspa.php

220 lines
7.5 KiB
PHP
Raw Permalink Normal View History

2013-10-10 02:44:53 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is able to collect traffic information for Exetel hspa
*
* Traffic information is collected by each service, and cached,
* returning just the date in question.
*
* @package ADSL
* @category Service
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Service_Traffic_Adsl_Exetelhspa extends Service_Traffic_Adsl {
private $login_user_field = 'login_name';
private $login_pass_field = 'password';
private $date_field = 'date';
static $result = array();
/**
* Get the data for Exetel hspa services
*
* @return array
*/
protected function getdata($date) {
// Assume we have a good fetch, unless otherwise specified.
$this->fetchresult = TRUE;
// If we have already collected the date data, return it.
if (! empty(Service_Traffic_Adsl_Exetelhspa::$result[$date]))
return Service_Traffic_Adsl_Exetelhspa::$result[$date];
include_once 'includes/kohana/modules/simplehtmldom/classes/simple_html_dom.php';
// Find our services that need to be collected this way.
$update = array();
foreach ($this->aso->services() as $so) {
2013-10-10 02:44:53 +00:00
if (Minion_CLI::options('verbose'))
echo " = Service: ".$so->name()."\n";
if ($so->plugin()->service_stats_collect AND $so->plugin()->service_stats_lastupdate < $date) {
$debug = FALSE;
if (! $debug) {
// Start Session
$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)
->post('doLogin',1)
->post('submit','Login');
$request->client()->options($this->curlopts+array(
CURLOPT_COOKIEJAR=>sprintf('/tmp/usage.cookies.%s.txt',$so->plugin()->service_number),
));
$response = $request->execute();
$data = $response->body();
if (! $data) {
// Record some debugging if we are in verbose mode
if (Minion_CLI::options('verbose')) {
file_put_contents('/tmp/osb.response.txt',print_r($response,TRUE));
file_put_contents('/tmp/osb.request.txt',print_r($request,TRUE));
file_put_contents('/tmp/osb.data.txt',print_r($data,TRUE));
}
// @todo Log into a log file
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;
}
2013-10-10 02:44:53 +00:00
}
for ($servicedate=date('Y-m-d',strtotime($this->aso->stats_lastupdate.'+1 day'));
2013-10-10 02:44:53 +00:00
$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));
$lastday = date('Y-m-d',strtotime('-1 second',strtotime('+1 month',strtotime(date('Y-m',strtotime($servicedate)).'-01'))));
if (strtotime($lastday) > time())
$lastday = date('Y-m-d',strtotime('yesterday'));
if (Minion_CLI::options('verbose')) {
echo " - From Date: ".$servicedate."\n";
echo " - To Date: ".$lastday."\n";
}
2013-10-10 02:44:53 +00:00
$html = new simple_html_dom();
if (! $debug) {
$request = Request::factory($this->aso->stats_url.'usage_customize_query.php')
2013-10-10 02:44:53 +00:00
->method('POST')
->post('year_search_key',date('Y',strtotime($servicedate)))
->post('month_search_key',date('m',strtotime($servicedate)))
->post('start_day_search_key',date('d',strtotime($servicedate)))
->post('end_day_search_key',date('d',strtotime($lastday)))
->post('do_usage_search',1);
$request->client()->options($this->curlopts+array(
CURLOPT_COOKIEFILE=>sprintf('/tmp/usage.cookies.%s.txt',$so->plugin()->service_number),
));
$response = $request->execute();
$result = $response->body();
$html->load($result);
if (Minion_CLI::options('verbose'))
$html->save(sprintf('/tmp/osb.usage.%s.%s.html',$so->plugin()->service_number,$servicedate));
2013-10-10 02:44:53 +00:00
} else {
if (Minion_CLI::options('verbose'))
echo " ! From Debug File: \n";
$html->load_file(sprintf('/tmp/osb.usage.%s.%s.html',$so->plugin()->service_number,$servicedate));
2013-10-10 02:44:53 +00:00
}
$header = array();
$data = array();
foreach ($html->find('fieldset') as $index => $fieldset) {
if (! preg_match('/Usage Detail/',$fieldset->find('legend',0)->plaintext))
2013-10-10 02:44:53 +00:00
continue;
#if (Minion_CLI::options('verbose'))
# echo "X:";print_r((string)$fieldset->find('table',0)); echo "\n";
2013-10-10 02:44:53 +00:00
foreach ($fieldset->find('table',0)->find('tr') as $key => $values) {
foreach ($values->children() as $a => $b) {
#if (Minion_CLI::options('verbose'))
# print_r(array('a'=>(string)$a,'b'=>(string)$b));
2013-10-10 02:44:53 +00:00
# Header
if ($key == 0) {
switch ($b->plaintext) {
case 'Date': $header[$a] = 'date'; break;
case 'OffPeak Upload': $header[$a] = 'up_offpeak'; break;
case 'Peak Upload': $header[$a] = 'up_peak'; break;
case 'Upload': $header[$a] = 'up_peak'; break;
case 'OffPeak Download': $header[$a] = 'down_offpeak'; break;
case 'Peak Download': $header[$a] = 'down_peak'; break;
case 'Download': $header[$a] = 'down_peak'; break;
case 'Duration': break;
case 'Total': break;
default:
printf('Unkown header :%s',$b->plaintext);
$this->fetchresult = FALSE;
continue;
}
#echo "INDEX: $key:$a\n";
#echo "TAG: ".$b->tag."\n";d
2013-10-10 02:44:53 +00:00
#echo "HEADER: ".$b->plaintext."\n";
# Data
} else {
if (isset($header[$a]))
$data[$header[$a]] = $b->plaintext;
#echo "INDEX: $key:$a\n";
#echo "TAG: ".$b->tag."\n";
#echo "VALUES: ".$b->plaintext."\n";
}
}
#if (Minion_CLI::options('verbose'))
# print_r($data);
2013-10-10 02:44:53 +00:00
if (isset($data['date']) && preg_match('/^[0-9]{4}/',$data['date'])) {
$sdate = date('Y-m-d',strtotime($data['date']));
unset($data['date']);
if (isset($update[$so->plugin()->service_number][$sdate]))
foreach ($data as $key => $value)
$update[$so->plugin()->service_number][$sdate][$key] += $value;
else
$update[$so->plugin()->service_number][$sdate] = $data;
$update[$so->plugin()->service_number][$sdate]['service'] = $so->plugin()->service_number;
$update[$so->plugin()->service_number][$sdate]['date'] = $sdate;
}
}
}
#if (Minion_CLI::options('verbose'))
# print_r($update);
2013-10-10 02:44:53 +00:00
}
// If we got here and have data, we had a good fetch, update the stats date
$x = $so->plugin();
$x->service_stats_lastupdate = $lastday;
$x->save();
if (Minion_CLI::options('verbose'))
printf(" = Update Service %s to %s\n",$so->plugin()->service_number,$so->plugin()->service_stats_lastupdate);
2013-10-10 02:44:53 +00:00
}
}
// If there are no updates, return an empty array
if (! $update)
return array();
// Reformat the data into date order.
foreach ($update as $service => $sdata)
foreach ($sdata as $sdate => $details)
Service_Traffic_Adsl_Exetelhspa::$result[$sdate][$service] = $details;
// If the date we want is empty, return an array
if (empty(Service_Traffic_Adsl_Exetelhspa::$result[$date]))
return array();
// Return the date we asked for
return Service_Traffic_Adsl_Exetelhspa::$result[$date];
}
}
?>