Replace importer with AJAX powered; better error handling #443 (#447)

* Replace importer with AJAX powered; better error handling #443

* Formatting

* Fix command line importer
This commit is contained in:
Nabeel S
2019-12-02 09:57:35 -05:00
committed by GitHub
parent 50dc79bc8d
commit 68eff40753
31 changed files with 544 additions and 354 deletions
@@ -5,21 +5,17 @@ namespace Modules\Installer\Services\Importer\Importers;
use App\Models\Aircraft;
use App\Models\Airline;
use App\Models\Subfleet;
use Modules\Installer\Exceptions\ImporterNoMoreRecords;
use Modules\Installer\Services\Importer\BaseImporter;
class AircraftImporter extends BaseImporter
{
public $table = 'aircraft';
/**
* CONSTANTS
*/
public const SUBFLEET_NAME = 'Imported Aircraft';
/**
* @param int $start
*
* @throws \Modules\Installer\Exceptions\ImporterNoMoreRecords
*/
public function run($start = 0)
{
$this->comment('--- AIRCRAFT IMPORT ---');
@@ -29,7 +25,7 @@ class AircraftImporter extends BaseImporter
$this->info('Subfleet ID is '.$subfleet->id);
$count = 0;
foreach ($this->db->readRows('aircraft') as $row) {
foreach ($this->db->readRows($this->table, $start) as $row) {
$where = [
'name' => $row->fullname,
'registration' => $row->registration,
@@ -49,8 +45,6 @@ class AircraftImporter extends BaseImporter
}
$this->info('Imported '.$count.' aircraft');
throw new ImporterNoMoreRecords();
}
/**
@@ -4,27 +4,31 @@ namespace Modules\Installer\Services\Importer\Importers;
use App\Models\Airline;
use Illuminate\Support\Facades\Log;
use Modules\Installer\Exceptions\ImporterNoMoreRecords;
use Modules\Installer\Services\Importer\BaseImporter;
class AirlineImporter extends BaseImporter
{
public $table = 'airlines';
/**
* @param int $start
*
* @throws \Modules\Installer\Exceptions\ImporterNoMoreRecords
*/
public function run($start = 0)
{
$this->comment('--- AIRLINE IMPORT ---');
$count = 0;
foreach ($this->db->readRows('airlines', $start) as $row) {
$airline = Airline::firstOrCreate(['icao' => $row->code], [
'iata' => $row->code,
'name' => $row->name,
'active' => $row->enabled,
]);
foreach ($this->db->readRows($this->table, $start) as $row) {
$attrs = [
'iata' => $row->code,
'name' => $row->name,
'active' => $row->enabled,
];
$w = ['icao' => $row->code];
//$airline = Airline::firstOrCreate($w, $attrs);
$airline = Airline::create(array_merge($w, $attrs));
$this->idMapper->addMapping('airlines', $row->id, $airline->id);
$this->idMapper->addMapping('airlines', $row->code, $airline->id);
@@ -37,7 +41,5 @@ class AirlineImporter extends BaseImporter
}
$this->info('Imported '.$count.' airlines');
throw new ImporterNoMoreRecords();
}
}
@@ -3,22 +3,29 @@
namespace Modules\Installer\Services\Importer\Importers;
use App\Models\Airport;
use Modules\Installer\Exceptions\ImporterNoMoreRecords;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
use Modules\Installer\Services\Importer\BaseImporter;
class AirportImporter extends BaseImporter
{
/**
* @param int $start
*
* @throws \Modules\Installer\Exceptions\ImporterNoMoreRecords
*/
protected $table = 'airports';
public function run($start = 0)
{
$this->comment('--- AIRPORT IMPORT ---');
$fields = [
'icao',
'name',
'country',
'lat',
'lng',
'hub',
];
$count = 0;
foreach ($this->db->readRows('airports', $start) as $row) {
foreach ($this->db->readRows($this->table, $start, $fields) as $row) {
$attrs = [
'id' => trim($row->icao),
'icao' => trim($row->icao),
@@ -29,7 +36,21 @@ class AirportImporter extends BaseImporter
'hub' => $row->hub,
];
$airport = Airport::updateOrCreate(['id' => $attrs['id']], $attrs);
$w = ['id' => $attrs['id']];
//$airport = Airport::updateOrCreate($w, $attrs);
try {
$airport = Airport::create(array_merge($w, $attrs));
} catch (QueryException $e) {
$sqlState = $e->errorInfo[0];
$errorCode = $e->errorInfo[1];
if ($sqlState === '23000' && $errorCode === 1062) {
Log::info('Found duplicate for '.$row->icao.', ignoring');
return true;
}
return false;
}
if ($airport->wasRecentlyCreated) {
$count++;
@@ -37,7 +58,5 @@ class AirportImporter extends BaseImporter
}
$this->info('Imported '.$count.' airports');
throw new ImporterNoMoreRecords();
}
}
@@ -0,0 +1,88 @@
<?php
namespace Modules\Installer\Services\Importer\Importers;
use App\Models\Acars;
use App\Models\Airline;
use App\Models\Airport;
use App\Models\Bid;
use App\Models\Expense;
use App\Models\File;
use App\Models\Flight;
use App\Models\FlightField;
use App\Models\FlightFieldValue;
use App\Models\Journal;
use App\Models\JournalTransaction;
use App\Models\News;
use App\Models\Pirep;
use App\Models\Subfleet;
use App\Models\User;
use App\Models\UserAward;
use Illuminate\Support\Facades\DB;
use Modules\Installer\Services\Importer\BaseImporter;
class ClearDatabase extends BaseImporter
{
/**
* Returns a default manifest just so this step gets run
*/
public function getManifest(): array
{
return [
[
'importer' => get_class($this),
'start' => 0,
'end' => 1,
'message' => 'Clearing database',
],
];
}
public function run($start = 0)
{
$this->cleanupDb();
}
/**
* Cleanup the local database of any users and other data that might conflict
* before running the importer
*/
protected function cleanupDb()
{
$this->info('Running database cleanup/empty before starting');
DB::statement('SET FOREIGN_KEY_CHECKS=0');
Bid::truncate();
File::truncate();
News::truncate();
Expense::truncate();
JournalTransaction::truncate();
Journal::truncate();
// Clear flights
DB::table('flight_fare')->truncate();
DB::table('flight_subfleet')->truncate();
FlightField::truncate();
FlightFieldValue::truncate();
Flight::truncate();
Subfleet::truncate();
// Clear permissions
// DB::table('permission_role')->truncate();
// DB::table('permission_user')->truncate();
// DB::table('role_user')->truncate();
// Role::truncate();
Airline::truncate();
Airport::truncate();
Acars::truncate();
Pirep::truncate();
UserAward::truncate();
User::truncate();
DB::statement('SET FOREIGN_KEY_CHECKS=1');
}
}
@@ -0,0 +1,58 @@
<?php
namespace Modules\Installer\Services\Importer\Importers;
use App\Models\User;
use App\Services\UserService;
use Modules\Installer\Services\Importer\BaseImporter;
class FinalizeImporter extends BaseImporter
{
/**
* Returns a default manifest just so this step gets run
*/
public function getManifest(): array
{
return [
[
'importer' => get_class($this),
'start' => 0,
'end' => 1,
'message' => 'Finalizing import',
],
];
}
/**
* The start method. Takes the offset to start from
*
* @param int $start
*
* @return mixed
*/
public function run($start = 0)
{
$this->findLastPireps();
$this->recalculateUserStats();
}
/**
* Go through and set the last PIREP ID for the users
*/
protected function findLastPireps()
{
}
/**
* Recalculate all of the user stats
*/
protected function recalculateUserStats()
{
$this->comment('--- RECALCULATING USER STATS ---');
$userSvc = app(UserService::class);
User::all()->each(function ($user) use ($userSvc) {
$userSvc->recalculateStats($user);
});
}
}
@@ -7,12 +7,30 @@ use Modules\Installer\Services\Importer\BaseImporter;
class FlightImporter extends BaseImporter
{
protected $table = 'schedules';
public function run($start = 0)
{
$this->comment('--- FLIGHT SCHEDULE IMPORT ---');
$fields = [
'id',
'code',
'flightnum',
'depicao',
'arricao',
'route',
'distance',
'flightlevel',
'deptime',
'arrtime',
'flightttime',
'notes',
'enabled',
];
$count = 0;
foreach ($this->db->readRows('schedules', $start) as $row) {
foreach ($this->db->readRows($this->table, $start, $fields) as $row) {
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
$flight_num = trim($row->flightnum);
@@ -32,7 +50,8 @@ class FlightImporter extends BaseImporter
try {
$w = ['airline_id' => $airline_id, 'flight_number' => $flight_num];
$flight = Flight::updateOrCreate($w, $attrs);
// $flight = Flight::updateOrCreate($w, $attrs);
$flight = Flight::create(array_merge($w, $attrs));
} catch (\Exception $e) {
//$this->error($e);
}
@@ -11,6 +11,8 @@ use Modules\Installer\Services\Importer\BaseImporter;
*/
class GroupImporter extends BaseImporter
{
protected $table = 'groups';
/**
* Permissions in the legacy system, mapping them to the current system
*/
@@ -66,7 +68,7 @@ class GroupImporter extends BaseImporter
$roleSvc = app(RoleService::class);
$count = 0;
foreach ($this->db->readRows('groups') as $row) {
foreach ($this->db->readRows($this->table, $start) as $row) {
$name = str_slug($row->name);
$role = Role::firstOrCreate(
['name' => $name],
@@ -10,12 +10,33 @@ use Modules\Installer\Services\Importer\BaseImporter;
class PirepImporter extends BaseImporter
{
protected $table = 'pireps';
public function run($start = 0)
{
$this->comment('--- PIREP IMPORT ---');
$fields = [
'pirepid',
'pilotid',
'code',
'aircraft',
'flightnum',
'depicao',
'arricao',
'fuelused',
'route',
'source',
'accepted',
'submitdate',
'distance',
'flighttime_stamp',
'flighttype',
'flightlevel',
];
$count = 0;
foreach ($this->db->readRows('pireps', $start) as $row) {
foreach ($this->db->readRows($this->table, $start, $fields) as $row) {
$pirep_id = $row->pirepid;
$user_id = $this->idMapper->getMapping('users', $row->pilotid);
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
@@ -70,7 +91,10 @@ class PirepImporter extends BaseImporter
$attrs['level'] = 0;
}
$pirep = Pirep::updateOrCreate(['id' => $pirep_id], $attrs);
$w = ['id' => $pirep_id];
$pirep = Pirep::updateOrCreate($w, $attrs);
//$pirep = Pirep::create(array_merge($w, $attrs));
//Log::debug('pirep oldid='.$pirep_id.', olduserid='.$row->pilotid
// .'; new id='.$pirep->id.', user id='.$user_id);
@@ -7,12 +7,14 @@ use Modules\Installer\Services\Importer\BaseImporter;
class RankImport extends BaseImporter
{
protected $table = 'ranks';
public function run($start = 0)
{
$this->comment('--- RANK IMPORT ---');
$count = 0;
foreach ($this->db->readRows('ranks') as $row) {
foreach ($this->db->readRows($this->table, $start) as $row) {
$rank = Rank::firstOrCreate(['name' => $row->rank], [
'image_url' => $row->rankimage,
'hours' => $row->minhours,
@@ -13,6 +13,8 @@ use Modules\Installer\Services\Importer\BaseImporter;
class UserImport extends BaseImporter
{
protected $table = 'pilots';
/**
* @var UserService
*/
@@ -26,7 +28,7 @@ class UserImport extends BaseImporter
$count = 0;
$first_row = true;
foreach ($this->db->readRows('pilots', $start) as $row) {
foreach ($this->db->readRows($this->table, $start) as $row) {
$pilot_id = $row->pilotid; // This isn't their actual ID
$name = $row->firstname.' '.$row->lastname;