Update and fix broadband traffic

This commit is contained in:
Deon George
2022-04-20 16:24:58 +10:00
parent 621a132e35
commit 16b7e0b493
13 changed files with 161 additions and 112 deletions

View File

@@ -9,14 +9,16 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use App\Classes\External\Supplier as ExternalSupplier;
use App\Mail\TrafficMismatch;
use App\Models\Service\Adsl;
use App\Models\Service\AdslTraffic;
use App\Models\AdslSupplier;
use App\Models\Supplier;
use App\Models\Service\Broadband as ServiceBroadband;
use App\Models\Usage\Broadband as UsageBroadband;
/**
* Class BroadbandTraffic
@@ -33,7 +35,9 @@ final class BroadbandTraffic implements ShouldQueue
protected Model $o; // The supplier we are updating from
private const class_prefix = 'App\Classes\External\Supplier\\';
public function __construct(AdslSupplier $o)
private const traffic = 'broadband';
public function __construct(Supplier $o)
{
$this->o = $o;
}
@@ -43,12 +47,14 @@ final class BroadbandTraffic implements ShouldQueue
*
* @return void
* @throws \Exception
* @todo The column stats_lastupdate is actually the "next" date that stats should be retrieved. Rename it.
*/
public function handle()
{
Log::info(sprintf('%s:Importing Broadband Traffic from [%s]',self::LOGKEY,$this->o->name));
if ((! $connection=$this->o->detail->connections->get('broadband')) || (count(array_intersect(array_keys($connection),ExternalSupplier::traffic_connection_keys)) !== 3))
throw new \Exception('No or missing connection details for:'.self::traffic);
$u = 0;
// Load our class for this supplier
@@ -57,21 +63,24 @@ final class BroadbandTraffic implements ShouldQueue
$o = new $class($this->o);
} else {
Log::error(sprintf('%s: Class doesnt exist: %d',get_class($this),$class));
Log::error(sprintf('%s: Class doesnt exist: %s',self::LOGKEY,$class));
exit(1);
}
$last_update = Carbon::create(Arr::get($connection,'last'))->addDay();
Arr::set($connection,'last',$last_update->format('Y-m-d'));
// Repeat pull traffic data until yesterday
while ($this->o->stats_lastupdate < Carbon::now()->subDay()) {
Log::notice(sprintf('%s:Next update is [%s]',self::LOGKEY,$this->o->stats_lastupdate->format('Y-m-d')));
while ($last_update < Carbon::now()->subDay()) {
Log::notice(sprintf('%s:Next update is [%s]',self::LOGKEY,$last_update->format('Y-m-d')));
// Delete traffic, since we'll refresh it.
AdslTraffic::where('supplier_id',$this->o->id)
->where('date',$this->o->stats_lastupdate)
UsageBroadband::where('supplier_id',$this->o->id)
->where('date',$last_update->format('Y-m-d'))
->delete();
$c = 0;
foreach ($o->fetch() as $line) {
foreach ($o->fetch($connection,self::traffic) as $line) {
// The first row is our header
if (! $c++) {
$fields = $o->getColumns(preg_replace('/,\s+/',',',$line),collect($o->header()));
@@ -79,28 +88,26 @@ final class BroadbandTraffic implements ShouldQueue
}
if (! $fields->count())
abort(500,'? No fields in data exportupda');
abort(500,'? No fields in data export');
$row = str_getcsv(trim($line));
try {
// @todo Put the date format in the DB.
$date = Carbon::createFromFormat('Y-m-d',$row[$o->getColumnKey('Date')]);
// Find the right service dependant on the dates we supplied the service
$oo = Adsl::where('service_username',$row[$o->getColumnKey('Login')])
->select(DB::raw('ab_service__adsl.*'))
// Find the right service dependent on the dates we supplied the service
$oo = ServiceBroadband::where('service_username',$row[$o->getColumnKey('Login')])
->select(DB::raw('service_broadband.*'))
->join('services','services.id','=','service_id')
->where('services.start_at','<=',$date)
->where(function($query) use ($date) {
$query->whereNULL('services.stop_at')
->orWhere('services.stop_at','<=',$date);
})
->get();
->single();
$to = new AdslTraffic;
$to->site_id = 1; // @todo TO ADDRESS
$to->date = $this->o->stats_lastupdate;
$to = new UsageBroadband;
$to->site_id = $oo->site_id;
$to->date = $last_update;
$to->supplier_id = $this->o->id;
$to->up_peak = $row[$o->getColumnKey('Peak upload')];
$to->up_offpeak = $row[$o->getColumnKey('Off peak upload')];
@@ -111,30 +118,35 @@ final class BroadbandTraffic implements ShouldQueue
$to->time = '24:00'; // @todo
// If we have no records
if ($oo->count() != 1) {
if (! $oo) {
Log::error(sprintf('%s:Too many services return for [%s]',self::LOGKEY,$row[$o->getColumnKey('Login')]),['date'=>$date,'count'=>$oo->count()]);
$to->service = $row[$o->getColumnKey('Login')];
$to->save();
} else {
$oo->first()->traffic()->save($to);
$to->service_item_id = $oo->id;
}
$u++;
if ($to->save())
$u++;
} catch (\Exception $e) {
dump($to);
Log::error(sprintf('%s:Exception occurred when storing traffic record for [%s].',self::LOGKEY,$row[$o->getColumnKey('Login')]),['row'=>$row,'line'=>$line]);
throw new \Exception('Error while storing traffic date');
}
}
Log::info(sprintf('%s: Records Imported [%d] for [%s]',self::LOGKEY,$u,$last_update->format('Y-m-d')));
Log::info(sprintf('%s: Records Imported [%d] for [%s]',self::LOGKEY,$u,$this->o->stats_lastupdate->format('Y-m-d')));
// Save our current progress.
$this->o->detail->connections = $this->o->detail->connections->put(self::traffic,array_merge($connection,['last'=>$last_update->format('Y-m-d')]));
$this->o->detail->save();
// Update our details for the next iteration.
$last_update = $last_update->addDay();
Arr::set($connection,'last',$last_update->format('Y-m-d'));
if ($u) {
$this->o->stats_lastupdate = $this->o->stats_lastupdate->addDay();
$this->o->save();
if ($this->o->trafficMismatch($date)->count())
Mail::to('deon@graytech.net.au') // @todo To change
->send(new TrafficMismatch($this->o,$date));