Added usage graph (ADSL), improved logging for usage collection (ADSL)
This commit is contained in:
22
app/Classes/External/Supplier.php
vendored
22
app/Classes/External/Supplier.php
vendored
@@ -11,6 +11,8 @@ use Illuminate\Support\Facades\Log;
|
||||
|
||||
abstract class Supplier
|
||||
{
|
||||
private const LOGKEY = 'AS-';
|
||||
|
||||
protected $o = NULL;
|
||||
protected $_columns = [];
|
||||
|
||||
@@ -23,16 +25,16 @@ abstract class Supplier
|
||||
/**
|
||||
* Connect and pull down traffic data
|
||||
*
|
||||
* @param array $args
|
||||
* @return mixed
|
||||
* @return Collection
|
||||
*/
|
||||
public function connect(array $args=[])
|
||||
public function fetch(): Collection
|
||||
{
|
||||
if ($x=$this->mustPause()) {
|
||||
Log::error('API Throttle, waiting .',['m'=>__METHOD__]);
|
||||
Log::notice(sprintf('%s:API Throttle, waiting [%s]...',self::LOGKEY,$x),['m'=>__METHOD__]);
|
||||
sleep($x);
|
||||
}
|
||||
|
||||
Log::debug(sprintf('%s:Supplier [%d], fetch data for [%s]...',self::LOGKEY,$this->o->id,$this->o->stats_lastupdate),['m'=>__METHOD__]);
|
||||
$result = Cache::remember('Supplier:'.$this->o->id.$this->o->stats_lastupdate,86400,function() {
|
||||
$client = $this->getClient();
|
||||
|
||||
@@ -53,14 +55,22 @@ abstract class Supplier
|
||||
$api_reset = Arr::get($result->getHeader('X-RateLimit-Reset'),0);
|
||||
|
||||
if ($api_remain === 0 AND $api_reset) {
|
||||
Log::error('API Throttle.',['m'=>__METHOD__]);
|
||||
Log::notice(sprintf('%s:API Throttle [%d].',self::LOGKEY,$api_reset),['m'=>__METHOD__]);
|
||||
Cache::put('api_throttle',$api_reset,now()->addSeconds($api_reset));
|
||||
}
|
||||
|
||||
return $result->getBody()->getContents();
|
||||
// Assume the supplier provides an ASCII output for text/html
|
||||
if (preg_match('#^text/html;#',$x=Arr::get($result->getHeader('Content-Type'),'0'))) {
|
||||
return collect(explode("\n",$result->getBody()->getContents()))->filter();
|
||||
|
||||
} else {
|
||||
Log::error(sprintf('%s:Havent handled header type [%s]',self::LOGKEY,$x),['m'=>__METHOD__]);
|
||||
throw new \Exception('Unhandled Content Type');
|
||||
}
|
||||
});
|
||||
|
||||
Log::debug(sprintf('%s:Supplier [%d], records returned [%d]...',self::LOGKEY,$this->o->id,$result->count()),['m'=>__METHOD__]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user