From 507ce5f24f7a74348e7946fbe771373e07c170d3 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 3 Jan 2018 21:40:05 -0600 Subject: [PATCH] Add flights to the importer, added a few missing columns with output --- app/Console/Services/Importer.php | 49 +++++++++++++++---- ...2017_06_17_214650_create_flight_tables.php | 2 + app/Models/Flight.php | 10 ++-- .../views/admin/flights/fields.blade.php | 14 +++++- .../layouts/default/flights/table.blade.php | 35 ++++++------- 5 files changed, 76 insertions(+), 34 deletions(-) diff --git a/app/Console/Services/Importer.php b/app/Console/Services/Importer.php index ae9ab356..cbb8d74b 100644 --- a/app/Console/Services/Importer.php +++ b/app/Console/Services/Importer.php @@ -2,23 +2,22 @@ namespace App\Console\Services; -use Illuminate\Support\Str; use PDO; -use Hashids\Hashids; use Doctrine\DBAL\Driver\PDOException; +use Illuminate\Support\Str; use Illuminate\Database\QueryException; +use Illuminate\Support\Facades\Hash; use Symfony\Component\Console\Output\ConsoleOutput; use App\Models\Aircraft; use App\Models\Airline; use App\Models\Airport; +use App\Models\Flight; use App\Models\Rank; use App\Models\Subfleet; use App\Models\User; use App\Models\Enums\UserState; use App\Facades\Utils; -use Illuminate\Support\Facades\Mail; -use Illuminate\Support\Facades\Hash; /** * Class Importer @@ -53,7 +52,7 @@ class Importer * CONSTANTS */ - const BATCH_READ_ROWS = 500; + const BATCH_READ_ROWS = 300; const SUBFLEET_NAME = 'Imported Aircraft'; /** @@ -390,8 +389,8 @@ class Importer foreach ($this->readRows('airports') as $row) { $attrs = [ - 'id' => $row->icao, - 'icao' => $row->icao, + 'id' => trim($row->icao), + 'icao' => trim($row->icao), 'name' => $row->name, 'country' => $row->country, 'lat' => $row->lat, @@ -412,15 +411,41 @@ class Importer */ protected function importFlights() { - /*$this->comment('--- FLIGHT SCHEDULE IMPORT ---'); + $this->comment('--- FLIGHT SCHEDULE IMPORT ---'); $count = 0; foreach ($this->readRows('schedules') as $row) { + $airline_id = $this->getMapping('airlines', $row->code); + $attrs = [ + 'dpt_airport_id' => $row->depicao, + 'arr_airport_id' => $row->arricao, + 'route' => $row->route ?: '', + 'distance' => round($row->distance ?: 0, 2), + 'level' => $row->flightlevel ?: 0, + 'dpt_time' => $row->deptime ?: '', + 'arr_time' => $row->arrtime ?: '', + 'flight_time' => $row->flighttime ?: '', + 'notes' => $row->notes ?: '', + 'active' => $row->enabled ?: true, + ]; + + $flight = Flight::firstOrCreate( + ['airline_id' => $airline_id, 'flight_number' => $row->flightnum], + $attrs + ); + + $this->addMapping('flights', $row->id, $flight->id); + + // TODO: deserialize route_details into ACARS table + + if($flight->wasRecentlyCreated) { + ++$count; + } } - $this->info('Imported ' . $count . ' flights');*/ + $this->info('Imported ' . $count . ' flights'); } /** @@ -473,6 +498,8 @@ class Importer $attrs ); + $this->addMapping('users', $row->pilotid, $user->id); + if ($user->wasRecentlyCreated) { ++$count; } @@ -498,6 +525,8 @@ class Importer { // TODO: This state might differ between simpilot and classic version + $state = (int) $state; + // Declare array of classic states $phpvms_classic_states = [ 'ACTIVE' => 0, @@ -516,6 +545,8 @@ class Importer return UserState::SUSPENDED; } elseif ($state === $phpvms_classic_states['ON_LEAVE']) { return UserState::ON_LEAVE; + } else { + $this->error('Unknown status: '. $state); } } } diff --git a/app/Database/migrations/2017_06_17_214650_create_flight_tables.php b/app/Database/migrations/2017_06_17_214650_create_flight_tables.php index 129ac182..237a46a8 100644 --- a/app/Database/migrations/2017_06_17_214650_create_flight_tables.php +++ b/app/Database/migrations/2017_06_17_214650_create_flight_tables.php @@ -24,6 +24,8 @@ class CreateFlightTables extends Migration $table->string('alt_airport_id', 5)->nullable(); $table->string('dpt_time', 10)->nullable(); $table->string('arr_time', 10)->nullable(); + $table->unsignedInteger('level')->default(0); + $table->unsignedDecimal('distance', 19)->default(0.0); $table->unsignedDecimal('flight_time', 19)->nullable(); $table->text('route')->nullable(); $table->text('notes')->nullable(); diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 2b5089c3..f19ca70e 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -24,6 +24,8 @@ class Flight extends BaseModel 'route', 'dpt_time', 'arr_time', + 'level', + 'distance', 'notes', 'has_bid', 'active', @@ -31,12 +33,8 @@ class Flight extends BaseModel protected $casts = [ 'flight_number' => 'integer', - 'route_code' => 'string', - 'route_leg' => 'string', - 'route' => 'string', - 'dpt_time' => 'string', - 'arr_time' => 'string', - 'notes' => 'string', + 'level' => 'integer', + 'distance' => 'float', 'has_bid' => 'boolean', 'active' => 'boolean', ]; diff --git a/resources/views/admin/flights/fields.blade.php b/resources/views/admin/flights/fields.blade.php index f7f5f7f5..c730a6ba 100644 --- a/resources/views/admin/flights/fields.blade.php +++ b/resources/views/admin/flights/fields.blade.php @@ -55,16 +55,26 @@ SAME ROW
-
+
{!! Form::label('dpt_time', 'Departure Time:') !!} {!! Form::text('dpt_time', null, ['class' => 'form-control']) !!}
-
+
{!! Form::label('arr_time', 'Arrival Time:') !!} {!! Form::text('arr_time', null, ['class' => 'form-control']) !!}
+ +
+ {!! Form::label('distance', 'Distance:') !!} + {!! Form::text('distance', null, ['class' => 'form-control']) !!} +
+ +
+ {!! Form::label('level', 'Flight Level:') !!} + {!! Form::text('level', null, ['class' => 'form-control']) !!} +
diff --git a/resources/views/layouts/default/flights/table.blade.php b/resources/views/layouts/default/flights/table.blade.php index 8491a616..46c69e02 100644 --- a/resources/views/layouts/default/flights/table.blade.php +++ b/resources/views/layouts/default/flights/table.blade.php @@ -25,26 +25,27 @@
-
-
-
- DEP  - {!! $flight->dpt_airport->icao !!}  - - ARR  - {!! $flight->arr_airport->icao !!}  -
-
- - {!! $flight->dpt_time !!} - {!! $flight->arr_time !!} -
-
+
+ DEP  + {!! $flight->dpt_airport->icao !!}@if($flight->dpt_time), {!! $flight->dpt_time !!}@endif +
+ ARR  + {!! $flight->arr_airport->icao !!}@if($flight->arr_time), {!! $flight->arr_time !!}@endif +
+ @if($flight->distance) + DISTANCE  + {!! $flight->distance !!} {!! setting('general.distance_unit') !!} + @endif +
+ @if($flight->level) + LEVEL  + {!! $flight->level !!} {!! setting('general.altitude_unit') !!} + @endif
-
+
- ROUTE  + ROUTE  {!! $flight->route !!}