Major refactoring for PIREP statuses and states to accomodate ACARS/route data

This commit is contained in:
Nabeel Shahzad
2017-12-19 20:19:36 -06:00
parent ac4958a2be
commit 0375bb420f
38 changed files with 352 additions and 170 deletions

View File

@@ -8,7 +8,7 @@ use Faker\Generator as Faker;
$factory->define(App\Models\Airport::class, function (Faker $faker) {
return [
'id' => strtoupper($faker->unique()->text(5)),
'icao' => function(array $apt) { return $apt['id']; },
'icao' => function(array $apt) { return substr($apt['id'],0, 4); },
'iata' => function (array $apt) { return $apt['id']; },
'name' => $faker->sentence(3),
'country' => $faker->country,

View File

@@ -0,0 +1,15 @@
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\Navpoint::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 100000),
'name' => $faker->unique()->text(10),
'title' => $faker->unique()->text(25),
'airway' => $faker->unique()->text(7),
'lat' => $faker->latitude,
'lon' => $faker->longitude,
'freq' => $faker->randomFloat(2, 100, 1000),
];
});

View File

@@ -1,5 +1,7 @@
<?php
use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState;
use Faker\Generator as Faker;
# Match the list available in tests/data/*.yml
@@ -40,8 +42,8 @@ $factory->define(App\Models\Pirep::class, function (Faker $faker) use ($airlines
'flight_time' => $faker->randomFloat(2),
'route' => $faker->text(200),
'notes' => $faker->text(200),
'source' => $faker->randomElement([0, 1]), # MANUAL/ACARS
'status' => config('enums.pirep_status.PENDING'), //$faker->randomElement([-1, 0, 1]), # REJECTED/PENDING/ACCEPTED
'source' => $faker->randomElement([PirepSource::MANUAL, PirepSource::ACARS]),
'state' => PirepState::PENDING, //$faker->randomElement([-1, 0, 1]), # REJECTED/PENDING/ACCEPTED
'raw_data' => $raw_data ?: $raw_data = json_encode(['key' => 'value']),
'created_at' => $faker->dateTimeBetween('-1 week', 'now'),
'updated_at' => function(array $pirep) {

View File

@@ -29,6 +29,7 @@ class CreateUsersTable extends Migration
$table->decimal('balance', 19)->nullable();
$table->string('timezone', 64)->nullable();
$table->unsignedTinyInteger('status')->default(0);
$table->unsignedTinyInteger('state')->default(0);
$table->boolean('active')->nullable();
$table->rememberToken();
$table->timestamps();

View File

@@ -10,7 +10,7 @@ class CreateAirportsTable extends Migration
Schema::create('airports', function (Blueprint $table) {
$table->string('id', 5)->primary();
$table->string('iata', 5)->nullable();
$table->string('icao', 5);
$table->string('icao', 4);
$table->string('name', 100);
$table->string('location', 100)->nullable();
$table->string('country', 64)->nullable();

View File

@@ -30,7 +30,8 @@ class CreatePirepTables extends Migration
$table->string('route', 250)->nullable();
$table->string('notes', 250)->nullable();
$table->unsignedTinyInteger('source')->default(0);
$table->tinyInteger('status')->default(0); # -1 rejected, 0 pending, 1 accepted
$table->tinyInteger('state')->default(0); # -1 rejected, 0 pending, 1 accepted
#$table->tinyInteger('status')->default(0); # -1 rejected, 0 pending, 1 accepted
$table->longText('raw_data')->nullable();
$table->timestamps();
$table->softDeletes();

View File

@@ -0,0 +1,47 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNavdataTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
/**
* See for defs, modify/update based on this
* https://github.com/skiselkov/openfmc/blob/master/airac.h
*/
Schema::create('navpoints', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 10);
$table->string('title', 25);
$table->string('airway', 7)->nullable();
$table->string('airway_type', 1)->nullable();
$table->bigInteger('seq')->nullable();
$table->string('loc', 4)->nullable();
$table->float('lat', 7, 4)->default(0.0);
$table->float('lon', 7, 4)->default(0.0);
$table->string('freq', 7);
$table->integer('type');
$table->index('name');
$table->index('airway');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('navpoints');
}
}

View File

@@ -0,0 +1,50 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAcarsTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
/**
* See for defs, modify/update based on this
* https://github.com/skiselkov/openfmc/blob/master/airac.h
*/
Schema::create('acars', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('acars_id', 12);
$table->string('name', 10)->nullable();
$table->float('lat', 7, 4)->default(0.0);
$table->float('lon', 7, 4)->default(0.0);
# TODO: More columns here for what might be required
# polymorphic relation columns.
# parent_type can be flight, pirep or acars
# once
$table->unsignedBigInteger('parent_id');
$table->string('parent_type');
$table->timestamps();
$table->index('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('acars');
}
}

View File

@@ -336,7 +336,7 @@ pireps:
dpt_airport_id: KAUS
arr_airport_id: KJFK
flight_time: 180 # 6 hours
status: 0
state: 0
route: PLMMR2 SPA Q22 BEARI FAK PHLBO3
notes: just a pilot report
- id: pirepid_2
@@ -347,7 +347,7 @@ pireps:
dpt_airport_id: KJFK
arr_airport_id: KAUS
flight_time: 180 # 6 hours
status: 0
state: 0
route: PLMMR2 SPA Q22 BEARI FAK PHLBO3
notes: just a pilot report
- id: pirepid_3
@@ -358,7 +358,7 @@ pireps:
dpt_airport_id: KJFK
arr_airport_id: KAUS
flight_time: 180 # 6 hours
status: 0
state: 0
route: PLMMR2 SPA Q22 BEARI FAK PHLBO3
notes: just a pilot report

View File

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreatePirepRequest;
use App\Http\Requests\UpdatePirepRequest;
use App\Models\Enums\PirepState;
use App\Repositories\AircraftRepository;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
@@ -81,7 +82,7 @@ class PirepController extends BaseController
$this->pirepRepo->pushCriteria($criterea);
$pireps = $this->pirepRepo
->findWhere(['status' => config('enums.pirep_status.PENDING')])
->findWhere(['status' => PirepState::PENDING])
->orderBy('created_at', 'desc')
->paginate();
@@ -211,12 +212,12 @@ class PirepController extends BaseController
*/
public function status(Request $request)
{
Log::info('PIREP status update call', [$request->toArray()]);
Log::info('PIREP state update call', [$request->toArray()]);
$pirep = $this->pirepRepo->findWithoutFail($request->id);
if($request->isMethod('post')) {
$new_status = (int) $request->new_status;
$pirep = $this->pirepSvc->changeStatus($pirep, $new_status);
$pirep = $this->pirepSvc->changeState($pirep, $new_status);
}
$pirep->refresh();

View File

@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Frontend;
use App\Facades\Utils;
use App\Models\Enums\PirepSource;
use App\Repositories\Criteria\WhereCriteria;
use App\Services\PIREPService;
use Illuminate\Support\Facades\Auth;
@@ -95,7 +96,7 @@ class PirepController extends Controller
$custom_fields[] = [
'name' => $cfield->name,
'value' => $field_val,
'source' => config('enums.sources.MANUAL')
'source' => PirepSource::MANUAL
];
}

10
app/Models/Acars.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Acars extends Model
{
public $table = 'acars';
}

28
app/Models/Enums/Days.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
/**
*
*/
namespace App\Models\Enums;
class Days extends EnumBase {
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 4;
const THURSDAY = 8;
const FRIDAY = 16;
const SATURDAY = 32;
const SUNDAY = 64;
protected static $labels = [
Days::MONDAY => 'Monday',
Days::TUESDAY => 'Tuesday',
Days::WEDNESDAY => 'Wednesday',
Days::THURSDAY => 'Thursday',
Days::FRIDAY => 'Friday',
Days::SATURDAY => 'Saturday',
Days::SUNDAY => 'Sunday',
];
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: nshahzad
* Date: 12/19/17
* Time: 8:04 PM
*/
namespace App\Models\Enums;
/**
* Class EnumBase
* @package App\Models\Enums
* TODO: Implement lang translations for enum labels
*/
class EnumBase
{
protected static $labels = [];
/**
* @param $value
* @return mixed
*/
public static function label($value) {
return self::$labels[$value];
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: nshahzad
* Date: 12/19/17
* Time: 8:13 PM
*/
namespace App\Models\Enums;
class PirepSource extends EnumBase
{
const MANUAL = 0;
const ACARS = 1;
protected static $labels = [
PirepSource::MANUAL => 'Manual',
PirepSource::ACARS => 'ACARS',
];
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Models\Enums;
use Illuminate\Support\Facades\Facade;
class PirepState extends EnumBase {
const REJECTED = -1;
const PENDING = 0;
const ACCEPTED = 1;
protected static $labels = [
PirepState::REJECTED => 'Rejected',
PirepState::PENDING => 'Pending',
PirepState::ACCEPTED => 'Accepted',
];
}

View File

@@ -0,0 +1,27 @@
<?php
/**
* Enums for PIREP statuses
*/
namespace App\Models\Enums;
/**
* Tied to the ACARS statuses/states
* Class PirepStatus
* @package App\Models\Enums
*/
class PirepStatus extends EnumBase
{
const PREFILE = 0;
const SCHEDULED = 0;
const ENROUTE = 1;
const ARRIVED = 2;
protected static $labels = [
PirepStatus::PREFILE => 'Prefiled',
PirepStatus::SCHEDULED => 'Scheduled',
PirepStatus::ENROUTE => 'Enroute',
PirepStatus::ARRIVED => 'Arrived',
];
}

View File

@@ -59,7 +59,11 @@ class Flight extends Model
'arr_airport_id' => 'required',
];
public function getFlightIdIataAttribute($value)
/**
* Get the flight ident, e.,g JBU1900
* @param $value
*/
public function getIdentAttribute()
{
}

10
app/Models/Navpoint.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Navpoint extends Model
{
public $table = 'navpoints';
}

View File

@@ -37,6 +37,7 @@ class Pirep extends Model
'level',
'route',
'notes',
'state',
'status',
'raw_data',
];
@@ -52,6 +53,7 @@ class Pirep extends Model
'level' => 'integer',
'fuel_used' => 'integer',
'source' => 'integer',
'state' => 'integer',
'status' => 'integer',
];
@@ -60,19 +62,19 @@ class Pirep extends Model
*
* @var array
*/
public static $rules
= [
'dpt_airport_id' => 'required',
'arr_airport_id' => 'required',
];
public static $rules = [
'dpt_airport_id' => 'required',
'arr_airport_id' => 'required',
];
/**
* Get the flight ident, e.,g JBU1900
* @return string
*/
public function getFlightId()
public function getIdentAttribute()
{
$flight_id = $this->airline->code;
if($this->flight_id) {
if ($this->flight_id) {
$flight_id .= $this->flight->flight_number;
} else {
$flight_id .= $this->flight_number;

View File

@@ -5,7 +5,9 @@ namespace App\Providers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use App\Repositories\SettingRepository;
use Illuminate\Database\Eloquent\Relations\Relation;
use App\Models\Flight;
use App\Models\Pirep;
class AppServiceProvider extends ServiceProvider
{
@@ -16,6 +18,11 @@ class AppServiceProvider extends ServiceProvider
{
Schema::defaultStringLength(191);
Relation::morphMap([
'flights' => Flight::class,
'pireps' => Pirep::class,
]);
$this->app->bind('setting', SettingRepository::class);
//\VaCentral\VaCentral::setVaCentralUrl(config('phpvms.vacentral_api_url'));

View File

@@ -2,6 +2,8 @@
namespace App\Services;
use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState;
use App\Models\Pirep;
use App\Models\PirepFieldValues;
@@ -46,10 +48,10 @@ class PIREPService extends BaseService
# Figure out what default state should be. Look at the default
# behavior from the rank that the pilot is assigned to
if($pirep->source === config('enums.sources.ACARS')) {
$default_status = $pirep->pilot->rank->auto_approve_acars;
if($pirep->source === PirepSource::ACARS) {
$default_state = $pirep->pilot->rank->auto_approve_acars;
} else {
$default_status = $pirep->pilot->rank->auto_approve_manual;
$default_state = $pirep->pilot->rank->auto_approve_manual;
}
$pirep->save();
@@ -68,12 +70,12 @@ class PIREPService extends BaseService
event(new PirepFiled($pirep));
if ($default_status === config('enums.pirep_status.ACCEPTED')) {
if ($default_state === PirepState::ACCEPTED) {
$pirep = $this->accept($pirep);
}
# only update the pilot last state if they are accepted
if ($default_status === config('enums.pirep_status.ACCEPTED')) {
if ($default_state === PirepState::ACCEPTED) {
$this->setPilotState($pirep);
}
@@ -82,24 +84,24 @@ class PIREPService extends BaseService
/**
* @param Pirep $pirep
* @param int $new_status
* @param int $new_state
* @return Pirep
*/
public function changeStatus(Pirep $pirep, int $new_status): Pirep
public function changeState(Pirep $pirep, int $new_state)
{
Log::info('PIREP ' . $pirep->id . ' status change from '.$pirep->status.' to ' . $new_status);
Log::info('PIREP ' . $pirep->id . ' state change from '.$pirep->state.' to ' . $new_state);
if ($pirep->status === $new_status) {
if ($pirep->state === $new_state) {
return $pirep;
}
/**
* Move from a PENDING status into either ACCEPTED or REJECTED
*/
if ($pirep->status === config('enums.pirep_status.PENDING')) {
if ($new_status === config('enums.pirep_status.ACCEPTED')) {
if ($pirep->state === PirepState::PENDING) {
if ($new_state === PirepState::ACCEPTED) {
return $this->accept($pirep);
} elseif ($new_status === config('enums.pirep_status.REJECTED')) {
} elseif ($new_state === PirepState::REJECTED) {
return $this->reject($pirep);
} else {
return $pirep;
@@ -109,7 +111,7 @@ class PIREPService extends BaseService
/*
* Move from a ACCEPTED to REJECTED status
*/
elseif ($pirep->status === config('enums.pirep_status.ACCEPTED')) {
elseif ($pirep->state === PirepState::ACCEPTED) {
$pirep = $this->reject($pirep);
return $pirep;
}
@@ -117,10 +119,12 @@ class PIREPService extends BaseService
/**
* Move from REJECTED to ACCEPTED
*/
elseif ($pirep->status === config('enums.pirep_status.REJECTED')) {
elseif ($pirep->state === PirepState::REJECTED) {
$pirep = $this->accept($pirep);
return $pirep;
}
return $pirep->refresh();
}
/**
@@ -130,7 +134,7 @@ class PIREPService extends BaseService
public function accept(Pirep $pirep): Pirep
{
# moving from a REJECTED state to ACCEPTED, reconcile statuses
if ($pirep->status === config('enums.pirep_status.ACCEPTED')) {
if ($pirep->state === PirepState::ACCEPTED) {
return $pirep;
}
@@ -143,13 +147,13 @@ class PIREPService extends BaseService
$pirep->pilot->refresh();
# Change the status
$pirep->status = config('enums.pirep_status.ACCEPTED');
$pirep->state = PirepState::ACCEPTED;
$pirep->save();
$pirep->refresh();
$this->setPilotState($pirep);
Log::info('PIREP '.$pirep->id.' status change to ACCEPTED');
Log::info('PIREP '.$pirep->id.' state change to ACCEPTED');
event(new PirepAccepted($pirep));
@@ -164,7 +168,7 @@ class PIREPService extends BaseService
{
# If this was previously ACCEPTED, then reconcile the flight hours
# that have already been counted, etc
if ($pirep->status === config('enums.pirep_status.ACCEPTED')) {
if ($pirep->state === PirepState::ACCEPTED) {
$pilot = $pirep->pilot;
$ft = $pirep->flight_time * -1;
@@ -175,11 +179,11 @@ class PIREPService extends BaseService
}
# Change the status
$pirep->status = config('enums.pirep_status.REJECTED');
$pirep->state = PirepState::REJECTED;
$pirep->save();
$pirep->refresh();
Log::info('PIREP ' . $pirep->id . ' status change to REJECTED');
Log::info('PIREP ' . $pirep->id . ' state change to REJECTED');
event(new PirepRejected($pirep));