ADSL Traffic import
This commit is contained in:
parent
86861a6daf
commit
7d41fb857a
11
app/Classes/External/Supplier.php
vendored
11
app/Classes/External/Supplier.php
vendored
@ -26,7 +26,7 @@ abstract class Supplier
|
||||
* @param array $args
|
||||
* @return mixed
|
||||
*/
|
||||
protected function connect(array $args=[])
|
||||
public function connect(array $args=[])
|
||||
{
|
||||
if ($x=$this->mustPause()) {
|
||||
Log::error('API Throttle, waiting .',['m'=>__METHOD__]);
|
||||
@ -80,7 +80,7 @@ abstract class Supplier
|
||||
* @param Collection $expect
|
||||
* @return Collection
|
||||
*/
|
||||
protected function getColumns(string $line,Collection $expect): Collection
|
||||
public function getColumns(string $line,Collection $expect): Collection
|
||||
{
|
||||
$fields = collect(explode(',',$line))->filter();
|
||||
$this->_columns = $expect;
|
||||
@ -98,11 +98,16 @@ abstract class Supplier
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getColumnKey(string $key)
|
||||
public function getColumnKey(string $key)
|
||||
{
|
||||
return $this->_columns->search($key);
|
||||
}
|
||||
|
||||
public function header(): array
|
||||
{
|
||||
return static::$header;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the supplier has API throttling...
|
||||
*
|
||||
|
31
app/Classes/External/Supplier/ExetelVisp.php
vendored
Normal file
31
app/Classes/External/Supplier/ExetelVisp.php
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\External\Supplier;
|
||||
|
||||
use App\Classes\External\Supplier;
|
||||
|
||||
/**
|
||||
* Class ExetelVisp
|
||||
* Handles interacting with Exetel VISP
|
||||
*
|
||||
* @package App\Classes\External\Payments
|
||||
*/
|
||||
class ExetelVisp extends Supplier
|
||||
{
|
||||
protected $login_user_field = 'username';
|
||||
protected $login_pass_field = 'password';
|
||||
protected $date_field = 'date';
|
||||
|
||||
protected static $header = [
|
||||
'Login',
|
||||
'Date',
|
||||
'Peak upload',
|
||||
'Peak download',
|
||||
'Off peak upload',
|
||||
'Off peak download',
|
||||
];
|
||||
|
||||
// Exetel provides data in CSV format, eg:
|
||||
// Login, Date, Peak upload, Peak download, Off peak upload, Off peak download\n
|
||||
// 0301408987@graytech.net.au,2020-05-02,41.35,41.04,49.22,27.69\n
|
||||
}
|
142
app/Jobs/BroadbandTraffic.php
Normal file
142
app/Jobs/BroadbandTraffic.php
Normal file
@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Mail\TrafficMismatch;
|
||||
use App\Models\Service\Adsl;
|
||||
use App\Models\Service\AdslTraffic;
|
||||
use App\Models\AdslSupplier;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
/**
|
||||
* Class BroadbandTraffic
|
||||
* Read and update the traffic for an Broadband supplier
|
||||
*
|
||||
* @package App\Jobs
|
||||
*/
|
||||
class BroadbandTraffic implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $aso = NULL;
|
||||
private $class_prefix = 'App\Classes\External\Supplier\\';
|
||||
|
||||
public function __construct(AdslSupplier $o)
|
||||
{
|
||||
$this->aso = $o;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$u = 0;
|
||||
|
||||
$class = $this->class_prefix.$this->aso->name;
|
||||
|
||||
if (class_exists($class)) {
|
||||
$o = new $class($this->aso);
|
||||
|
||||
} else {
|
||||
Log::error(sprintf('%s: Class doesnt exist: %d',get_class($this),$class));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
AdslTraffic::where('supplier_id',$this->aso->id)
|
||||
->where('date',$this->aso->stats_lastupdate)
|
||||
->delete();
|
||||
|
||||
// @todo Need to trap errors from getting data
|
||||
$c = 0;
|
||||
foreach (explode("\n",$o->connect()) as $line) {
|
||||
if (! trim($line))
|
||||
continue;
|
||||
|
||||
// The first row is our header
|
||||
if (! $c++) {
|
||||
$fields = $o->getColumns(preg_replace('/,\s+/',',',$line),collect($o->header()));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $fields->count())
|
||||
abort(500,'? No fields in data exportupda');
|
||||
|
||||
$row = str_getcsv(trim($line));
|
||||
|
||||
try {
|
||||
$date = Carbon::createFromFormat('Y-m-d',$row[$o->getColumnKey('Date')]);
|
||||
|
||||
$oo = Adsl::where('service_username',$row[$o->getColumnKey('Login')])
|
||||
->select(DB::raw('ab_service__adsl.*'))
|
||||
->join('ab_service','ab_service.id','=','service_id')
|
||||
->where('ab_service.date_start','<=',$date->format('U'))
|
||||
->where(function($query) use ($date) {
|
||||
$query->whereNULL('ab_service.date_end')
|
||||
->orWhere('ab_service.date_end','<=',$date->format('U'));
|
||||
})
|
||||
->get();
|
||||
|
||||
// If we have no records
|
||||
if ($oo->count() != 1) {
|
||||
Log::error(sprintf('! Records Errors for:%s (%s) [%s]',$row[$o->getColumnKey('Login')],$date,$oo->count()));
|
||||
|
||||
$to = new AdslTraffic;
|
||||
$to->site_id = 1; // @todo TO ADDRESS
|
||||
$to->service = $row[$o->getColumnKey('Login')];
|
||||
$to->date = $this->aso->stats_lastupdate;
|
||||
$to->supplier_id = $this->aso->id;
|
||||
$to->up_peak = $row[$o->getColumnKey('Peak upload')];
|
||||
$to->up_offpeak = $row[$o->getColumnKey('Off peak upload')];
|
||||
$to->down_peak = $row[$o->getColumnKey('Peak download')];
|
||||
$to->down_offpeak = $row[$o->getColumnKey('Off peak download')];
|
||||
// $to->peer
|
||||
// $to->internal
|
||||
$to->time = '24:00'; // @todo
|
||||
$to->save();
|
||||
$u++;
|
||||
|
||||
} else {
|
||||
$to = new AdslTraffic;
|
||||
$to->site_id = 1; // @todo TO ADDRESS
|
||||
$to->date = $this->aso->stats_lastupdate;
|
||||
$to->supplier_id = $this->aso->id;
|
||||
$to->up_peak = $row[$o->getColumnKey('Peak upload')];
|
||||
$to->up_offpeak = $row[$o->getColumnKey('Off peak upload')];
|
||||
$to->down_peak = $row[$o->getColumnKey('Peak download')];
|
||||
$to->down_offpeak = $row[$o->getColumnKey('Off peak download')];
|
||||
// $to->peer
|
||||
// $to->internal
|
||||
$to->time = '24:00'; // @todo
|
||||
$oo->first()->traffic()->save($to);
|
||||
$u++;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
dd(['row'=>$row,'line'=>$line]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($u) {
|
||||
$this->aso->stats_lastupdate = $this->aso->stats_lastupdate->addDay();
|
||||
$this->aso->save();
|
||||
|
||||
if ($this->aso->traffic_mismatch($date)->count())
|
||||
Mail::to('deon@graytech.net.au') // @todo To change
|
||||
->send(new TrafficMismatch($this->aso,$date));
|
||||
}
|
||||
|
||||
Log::info(sprintf('%s: Records Imported: %d',get_class($this),$u));
|
||||
}
|
||||
}
|
45
app/Mail/TrafficMismatch.php
Normal file
45
app/Mail/TrafficMismatch.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Models\Site;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
use App\Models\AdslSupplier;
|
||||
|
||||
class TrafficMismatch extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(AdslSupplier $o,Carbon $date)
|
||||
{
|
||||
$this->aso = $o;
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the message.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return $this
|
||||
->markdown('email.system.broadband_traffic_mismatch')
|
||||
->subject('Traffic Mismatch for '.$this->date)
|
||||
->with([
|
||||
'site'=>Site::find(1), // @todo To auto determine
|
||||
'date'=>$this->date,
|
||||
'aso'=>$this->aso,
|
||||
]);
|
||||
}
|
||||
}
|
@ -2,9 +2,38 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Service\AdslTraffic;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class AdslSupplier extends Model
|
||||
{
|
||||
protected $table = 'ab_adsl_supplier';
|
||||
|
||||
protected $dates = [
|
||||
'stats_lastupdate',
|
||||
];
|
||||
|
||||
public $timestamps = FALSE;
|
||||
|
||||
/** SCOPES */
|
||||
|
||||
/**
|
||||
* Only query active categories
|
||||
*/
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where('active',TRUE);
|
||||
}
|
||||
|
||||
/** METHODS **/
|
||||
|
||||
public function traffic_mismatch(Carbon $date): Collection
|
||||
{
|
||||
return AdslTraffic::where('date',$date->format('Y-m-d'))
|
||||
->where('supplier_id',$this->id)
|
||||
->whereNULL('ab_service_adsl_id')
|
||||
->get();
|
||||
}
|
||||
}
|
@ -21,6 +21,18 @@ class Adsl extends ServiceType implements ServiceItem
|
||||
];
|
||||
protected $table = 'ab_service__adsl';
|
||||
|
||||
/** RELATIONSHIPS **/
|
||||
|
||||
/**
|
||||
* The accounts that this user manages
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function traffic()
|
||||
{
|
||||
return $this->hasMany(AdslTraffic::class,'ab_service_adsl_id');
|
||||
}
|
||||
|
||||
/** SCOPES */
|
||||
|
||||
/**
|
||||
|
16
app/Models/Service/AdslTraffic.php
Normal file
16
app/Models/Service/AdslTraffic.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Service;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class AdslTraffic extends Model
|
||||
{
|
||||
protected $table = 'ab_service__adsl_traffic';
|
||||
public $timestamps = FALSE;
|
||||
|
||||
public function broadband()
|
||||
{
|
||||
return $this->belongsTo(Adsl::class);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class EnhanceAdslTraffic extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('ab_service__adsl_traffic', function (Blueprint $table) {
|
||||
$table->bigInteger('ab_service_adsl_id')->nullable();
|
||||
$table->foreign('ab_service_adsl_id')->references('id')->on('ab_service__adsl');
|
||||
|
||||
$table->unique(['ab_service_adsl_id','date','time']);
|
||||
});
|
||||
|
||||
DB::statement('ALTER TABLE ab_service__adsl_traffic CHANGE COLUMN service service varchar(128) DEFAULT NULL');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
DB::statement('ALTER TABLE ab_service__adsl_traffic CHANGE COLUMN service service varchar(128) NOT NULL');
|
||||
|
||||
Schema::table('ab_service__adsl_traffic', function (Blueprint $table) {
|
||||
$table->dropForeign(['ab_service_adsl_id']);
|
||||
$table->dropUnique(['ab_service_adsl_id','date','time']);
|
||||
$table->dropColumn('ab_service_adsl_id');
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
@component('mail::message',['site'=>$site,'heading'=>'Traffic Mismatch: '.$date])
|
||||
Hi {{ isset($user) ? $user->name.',' : '' }}
|
||||
|
||||
The traffic import today, had mismatching records. A request to have those login removed has been generated.
|
||||
|
||||
@component('mail::table')
|
||||
| ID |
|
||||
| - |
|
||||
@foreach ($aso->traffic_mismatch($date) as $o)
|
||||
| {{ $o->service }}
|
||||
@endforeach
|
||||
|
||||
@endcomponent
|
||||
|
||||
Thanks,<br>
|
||||
{{ config('mail.from.name') }}
|
||||
@endcomponent
|
Loading…
Reference in New Issue
Block a user