2020-05-28 05:08:13 +00:00
< ? php
namespace App\Jobs ;
use Carbon\Carbon ;
use Illuminate\Bus\Queueable ;
use Illuminate\Contracts\Queue\ShouldQueue ;
2021-07-29 03:11:14 +00:00
use Illuminate\Database\Eloquent\Model ;
2020-05-28 05:08:13 +00:00
use Illuminate\Foundation\Bus\Dispatchable ;
use Illuminate\Queue\InteractsWithQueue ;
use Illuminate\Queue\SerializesModels ;
2022-04-20 06:24:58 +00:00
use Illuminate\Support\Arr ;
2020-05-28 05:08:13 +00:00
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Log ;
2021-07-29 03:11:14 +00:00
use Illuminate\Support\Facades\Mail ;
2020-05-28 05:08:13 +00:00
2022-04-20 06:24:58 +00:00
use App\Classes\External\Supplier as ExternalSupplier ;
2020-05-28 05:08:13 +00:00
use App\Mail\TrafficMismatch ;
2022-04-20 06:24:58 +00:00
use App\Models\Supplier ;
use App\Models\Service\Broadband as ServiceBroadband ;
use App\Models\Usage\Broadband as UsageBroadband ;
2020-05-28 05:08:13 +00:00
/**
* Class BroadbandTraffic
* Read and update the traffic for an Broadband supplier
*
* @ package App\Jobs
*/
2021-07-29 03:11:14 +00:00
final class BroadbandTraffic implements ShouldQueue
2020-05-28 05:08:13 +00:00
{
use Dispatchable , InteractsWithQueue , Queueable , SerializesModels ;
2021-02-17 13:22:50 +00:00
private const LOGKEY = 'JBT' ;
2024-07-06 09:56:56 +00:00
protected int $sid ; // The supplier we are updating from
2021-07-29 03:11:14 +00:00
private const class_prefix = 'App\Classes\External\Supplier\\' ;
2020-05-28 05:08:13 +00:00
2022-04-20 06:24:58 +00:00
private const traffic = 'broadband' ;
2024-07-06 09:56:56 +00:00
public function __construct ( int $sid )
2020-05-28 05:08:13 +00:00
{
2024-07-06 09:56:56 +00:00
$this -> sid = $sid ;
2020-05-28 05:08:13 +00:00
}
/**
* Execute the job .
*
* @ return void
2021-02-17 13:22:50 +00:00
* @ throws \Exception
2020-05-28 05:08:13 +00:00
*/
public function handle ()
{
2024-07-06 09:56:56 +00:00
$so = Supplier :: findOrFail ( $this -> sid );
Log :: info ( sprintf ( '%s:Importing Broadband Traffic from [%s]' , self :: LOGKEY , $so -> name ));
2021-02-17 13:22:50 +00:00
2024-07-06 09:56:56 +00:00
if (( ! $connection = $so -> detail -> connections -> get ( 'broadband' )) || ( count ( array_intersect ( array_keys ( $connection ), ExternalSupplier :: traffic_connection_keys )) !== 3 ))
2022-04-20 06:24:58 +00:00
throw new \Exception ( 'No or missing connection details for:' . self :: traffic );
2020-05-28 05:08:13 +00:00
$u = 0 ;
2021-02-17 13:22:50 +00:00
// Load our class for this supplier
2024-07-06 09:56:56 +00:00
$class = self :: class_prefix . $so -> name ;
2020-05-28 05:08:13 +00:00
if ( class_exists ( $class )) {
2024-07-06 09:56:56 +00:00
$o = new $class ( $so );
2020-05-28 05:08:13 +00:00
} else {
2022-04-20 06:24:58 +00:00
Log :: error ( sprintf ( '%s: Class doesnt exist: %s' , self :: LOGKEY , $class ));
2020-05-28 05:08:13 +00:00
exit ( 1 );
}
2022-04-20 06:24:58 +00:00
$last_update = Carbon :: create ( Arr :: get ( $connection , 'last' )) -> addDay ();
Arr :: set ( $connection , 'last' , $last_update -> format ( 'Y-m-d' ));
2021-02-17 13:22:50 +00:00
// Repeat pull traffic data until yesterday
2022-04-20 06:24:58 +00:00
while ( $last_update < Carbon :: now () -> subDay ()) {
Log :: notice ( sprintf ( '%s:Next update is [%s]' , self :: LOGKEY , $last_update -> format ( 'Y-m-d' )));
2021-02-17 13:22:50 +00:00
// Delete traffic, since we'll refresh it.
2024-07-06 09:56:56 +00:00
UsageBroadband :: where ( 'supplier_id' , $so -> id )
2022-04-20 06:24:58 +00:00
-> where ( 'date' , $last_update -> format ( 'Y-m-d' ))
2021-02-17 13:22:50 +00:00
-> delete ();
$c = 0 ;
2022-04-20 06:24:58 +00:00
foreach ( $o -> fetch ( $connection , self :: traffic ) as $line ) {
2021-02-17 13:22:50 +00:00
// The first row is our header
if ( ! $c ++ ) {
$fields = $o -> getColumns ( preg_replace ( '/,\s+/' , ',' , $line ), collect ( $o -> header ()));
continue ;
}
2020-05-28 05:08:13 +00:00
2021-02-17 13:22:50 +00:00
if ( ! $fields -> count ())
2022-04-20 06:24:58 +00:00
abort ( 500 , '? No fields in data export' );
2020-05-28 05:08:13 +00:00
2021-02-17 13:22:50 +00:00
$row = str_getcsv ( trim ( $line ));
2020-05-28 05:08:13 +00:00
2021-02-17 13:22:50 +00:00
try {
$date = Carbon :: createFromFormat ( 'Y-m-d' , $row [ $o -> getColumnKey ( 'Date' )]);
2022-04-20 06:24:58 +00:00
// 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.*' ))
2022-04-19 07:07:39 +00:00
-> join ( 'services' , 'services.id' , '=' , 'service_id' )
-> where ( 'services.start_at' , '<=' , $date )
2021-02-17 13:22:50 +00:00
-> where ( function ( $query ) use ( $date ) {
2022-04-19 07:07:39 +00:00
$query -> whereNULL ( 'services.stop_at' )
-> orWhere ( 'services.stop_at' , '<=' , $date );
2021-02-17 13:22:50 +00:00
})
2022-04-20 06:24:58 +00:00
-> single ();
2020-05-28 05:08:13 +00:00
2022-04-20 06:24:58 +00:00
$to = new UsageBroadband ;
$to -> date = $last_update ;
2024-07-06 09:56:56 +00:00
$to -> supplier_id = $so -> id ;
2020-05-28 05:08:13 +00:00
$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
2021-02-17 13:22:50 +00:00
// If we have no records
2022-04-20 06:24:58 +00:00
if ( ! $oo ) {
2022-09-29 02:16:08 +00:00
Log :: error ( sprintf ( '%s:None or too many services return for [%s]' , self :: LOGKEY , $row [ $o -> getColumnKey ( 'Login' )]),[ 'date' => $date ]);
2021-02-17 13:22:50 +00:00
$to -> service = $row [ $o -> getColumnKey ( 'Login' )];
2022-09-29 01:22:16 +00:00
$to -> site_id = 1 ; // @todo This needs to be worked out a better way
2021-02-17 13:22:50 +00:00
} else {
2022-09-29 01:22:16 +00:00
$to -> site_id = $oo -> site_id ;
2022-04-20 06:24:58 +00:00
$to -> service_item_id = $oo -> id ;
2021-02-17 13:22:50 +00:00
}
2022-04-20 06:24:58 +00:00
if ( $to -> save ())
$u ++ ;
2021-02-17 13:22:50 +00:00
} catch ( \Exception $e ) {
2021-07-29 03:11:14 +00:00
Log :: error ( sprintf ( '%s:Exception occurred when storing traffic record for [%s].' , self :: LOGKEY , $row [ $o -> getColumnKey ( 'Login' )]),[ 'row' => $row , 'line' => $line ]);
2022-09-29 01:22:16 +00:00
throw new \Exception ( 'Error while storing traffic data: ' . $e -> getMessage ());
2020-05-28 05:08:13 +00:00
}
}
2022-04-20 06:24:58 +00:00
Log :: info ( sprintf ( '%s: Records Imported [%d] for [%s]' , self :: LOGKEY , $u , $last_update -> format ( 'Y-m-d' )));
2020-05-28 05:08:13 +00:00
2022-04-20 06:24:58 +00:00
// Save our current progress.
2024-07-06 09:56:56 +00:00
$so -> detail -> connections = $so -> detail -> connections -> put ( self :: traffic , array_merge ( $connection ,[ 'last' => $last_update -> format ( 'Y-m-d' )]));
$so -> detail -> save ();
2020-05-28 05:08:13 +00:00
2022-04-20 06:24:58 +00:00
// Update our details for the next iteration.
$last_update = $last_update -> addDay ();
Arr :: set ( $connection , 'last' , $last_update -> format ( 'Y-m-d' ));
2021-02-17 13:22:50 +00:00
2022-04-20 06:24:58 +00:00
if ( $u ) {
2024-07-06 09:56:56 +00:00
if ( $so -> trafficMismatch ( $date ) -> count ())
2021-02-17 13:22:50 +00:00
Mail :: to ( 'deon@graytech.net.au' ) // @todo To change
2024-07-06 09:56:56 +00:00
-> send ( new TrafficMismatch ( $so , $date ));
2021-02-17 13:22:50 +00:00
}
}
2020-05-28 05:08:13 +00:00
}
}