Compare commits

..

2 Commits

Author SHA1 Message Date
1c4cb6f38c Fix usage_broadband, since our usage data is a float
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 31s
Create Docker Image / Final Docker Image Manifest (push) Successful in 9s
2024-07-26 14:46:06 +10:00
667109150d Added API stub configuration 2024-07-26 14:46:06 +10:00
6 changed files with 74 additions and 25 deletions

View File

@ -3,6 +3,7 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Jobs\BroadbandTraffic as Job;
use App\Models\Supplier;
@ -15,7 +16,7 @@ class BroadbandTraffic extends Command
* @var string
*/
protected $signature = 'broadband:traffic:import'.
' {--s|supplier= : Supplier Name}';
' {supplier? : Supplier Name}';
/**
* The console command description.
@ -31,14 +32,26 @@ class BroadbandTraffic extends Command
*/
public function handle()
{
if ($this->option('supplier')) {
$o = Supplier::where('name','ilike',$this->option('supplier'))->singleOrFail();
if ($this->argument('supplier')) {
try {
$o = Supplier::active()
->where('name','ilike',$this->argument('supplier'))
->sole();
Job::dispatchSync($o->id);
return;
} catch (ModelNotFoundException $e) {
$this->error(sprintf('Supplier [%s] not found',$this->argument('supplier')));
return self::FAILURE;
}
Job::dispatchSync($o->name);
return self::SUCCESS;
}
foreach (Supplier::active()->get() as $o)
Job::dispatchSync($o->id);
Job::dispatchSync($o->name);
return self::SUCCESS;
}
}

View File

@ -5,24 +5,22 @@ namespace App\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
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\Supplier;
use App\Models\{Rtm,Supplier};
use App\Models\Service\Broadband as ServiceBroadband;
use App\Models\Usage\Broadband as UsageBroadband;
/**
* Class BroadbandTraffic
* Read and update the traffic for an Broadband supplier
* Read and update the traffic for a Broadband supplier
*
* @package App\Jobs
*/
@ -32,15 +30,11 @@ final class BroadbandTraffic implements ShouldQueue
private const LOGKEY = 'JBT';
protected int $sid; // The supplier we are updating from
private const class_prefix = 'App\Classes\External\Supplier\\';
private const traffic = 'broadband';
public function __construct(int $sid)
{
$this->sid = $sid;
}
public function __construct(private string $supplier) {}
/**
* Execute the job.
@ -50,12 +44,19 @@ final class BroadbandTraffic implements ShouldQueue
*/
public function handle()
{
$so = Supplier::findOrFail($this->sid);
$so = Supplier::active()
->where('name','ilike',$this->supplier)
->sole();
// Wholesaler email
$ro = Rtm::where('parent_id',NULL)->sole();
Log::info(sprintf('%s:Importing Broadband Traffic from [%s]',self::LOGKEY,$so->name));
if ((! $connection=$so->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);
// Count of updated records
$u = 0;
// Load our class for this supplier
@ -95,15 +96,16 @@ final class BroadbandTraffic implements ShouldQueue
try {
$date = Carbon::createFromFormat('Y-m-d',$row[$o->getColumnKey('Date')]);
// 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')
->select('service_broadband.*')
->join('services',['service_broadband.service_id'=>'services.id'])
->where('services.start_at','<=',$date)
->where(function($query) use ($date) {
->where(fn($query)=>
$query->whereNULL('services.stop_at')
->orWhere('services.stop_at','<=',$date);
})
->orWhere('services.stop_at','>=',$date)
)
->single();
$to = new UsageBroadband;
@ -149,7 +151,7 @@ final class BroadbandTraffic implements ShouldQueue
if ($u) {
if ($so->trafficMismatch($date)->count())
Mail::to('deon@graytech.net.au') // @todo To change
Mail::to($ro->owner->email)
->send(new TrafficMismatch($so,$date));
}
}

View File

@ -25,6 +25,13 @@ class Rtm extends Model
return $this->hasMany(self::class,'parent_id');
}
public function owner()
{
return $this->hasOneThrough(User::class,Account::class,'id','id','account_id','user_id');
}
/* METHODS */
/**
* Return all the children RTM records that this record is parent of
*

View File

@ -10,7 +10,7 @@ use App\Http\Middleware\{Role,SetSite};
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
//api: __DIR__.'/../routes/api.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)

View File

@ -0,0 +1,27 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::update('ALTER TABLE usage_broadband ALTER COLUMN up_peak TYPE REAL');
DB::update('ALTER TABLE usage_broadband ALTER COLUMN down_peak TYPE REAL');
DB::update('ALTER TABLE usage_broadband ALTER COLUMN up_offpeak TYPE REAL');
DB::update('ALTER TABLE usage_broadband ALTER COLUMN down_offpeak TYPE REAL');
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// NOOP
}
};

View File

@ -4,6 +4,6 @@ use Illuminate\Support\Facades\Schedule;
use App\Jobs\BroadbandTraffic;
Schedule::job(new BroadbandTraffic(1))
Schedule::job(new BroadbandTraffic('exetelvisp'))
->timezone('Australia/Melbourne')
->dailyAt('10:00');