2018-01-03 11:00:46 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Services;
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Facades\Utils;
|
2018-01-03 11:00:46 +08:00
|
|
|
use App\Models\Aircraft;
|
|
|
|
use App\Models\Airline;
|
|
|
|
use App\Models\Airport;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\Enums\FlightType;
|
|
|
|
use App\Models\Enums\PirepSource;
|
|
|
|
use App\Models\Enums\UserState;
|
2018-01-04 11:40:05 +08:00
|
|
|
use App\Models\Flight;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\Pirep;
|
2018-01-03 11:00:46 +08:00
|
|
|
use App\Models\Rank;
|
|
|
|
use App\Models\Subfleet;
|
2018-01-04 05:41:21 +08:00
|
|
|
use App\Models\User;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use PDO;
|
2018-08-27 00:40:04 +08:00
|
|
|
use PDOException;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput;
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Importer
|
|
|
|
* TODO: Batch import
|
|
|
|
*/
|
|
|
|
class Importer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Hold references
|
|
|
|
*/
|
2018-01-04 08:36:08 +08:00
|
|
|
private $mappedEntities = [];
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hold the PDO connection to the old database
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:00:46 +08:00
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
private $conn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
private $creds;
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hold the instance of the console logger
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:00:46 +08:00
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
private $log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CONSTANTS
|
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
public const BATCH_READ_ROWS = 300;
|
2018-08-27 02:51:47 +08:00
|
|
|
public const SUBFLEET_NAME = 'Imported Aircraft';
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-01-03 11:16:35 +08:00
|
|
|
/**
|
|
|
|
* Importer constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:16:35 +08:00
|
|
|
* @param $db_creds
|
|
|
|
*/
|
2018-01-03 11:00:46 +08:00
|
|
|
public function __construct($db_creds)
|
|
|
|
{
|
|
|
|
// Setup the logger
|
|
|
|
$this->log = new ConsoleOutput();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// The db credentials
|
2018-01-03 11:00:46 +08:00
|
|
|
$this->creds = array_merge([
|
2018-03-20 09:50:40 +08:00
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'port' => 3306,
|
|
|
|
'name' => '',
|
|
|
|
'user' => '',
|
|
|
|
'pass' => '',
|
2018-08-27 00:40:04 +08:00
|
|
|
'table_prefix' => 'phpvms_',
|
2018-01-03 11:00:46 +08:00
|
|
|
], $db_creds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int|void
|
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->reconnect();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Import all the different parts
|
2018-01-03 11:00:46 +08:00
|
|
|
$this->importRanks();
|
|
|
|
$this->importAirlines();
|
|
|
|
$this->importAircraft();
|
|
|
|
$this->importAirports();
|
|
|
|
|
|
|
|
$this->importUsers();
|
|
|
|
$this->importFlights();
|
|
|
|
$this->importPireps();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Finish up
|
2018-01-30 08:14:55 +08:00
|
|
|
$this->findLastPireps();
|
2018-01-03 11:00:46 +08:00
|
|
|
$this->recalculateRanks();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconnect to the old phpVMS DB using PDO
|
|
|
|
*/
|
|
|
|
protected function reconnect()
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
$dsn = 'mysql:'.implode(';', [
|
|
|
|
'host='.$this->creds['host'],
|
|
|
|
'port='.$this->creds['port'],
|
2018-08-27 00:40:04 +08:00
|
|
|
'dbname='.$this->creds['name'],
|
2018-01-03 11:00:46 +08:00
|
|
|
]);
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Connection string: '.$dsn);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->conn = new PDO($dsn, $this->creds['user'], $this->creds['pass']);
|
|
|
|
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
$this->error($e);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $message
|
|
|
|
*/
|
|
|
|
protected function comment($message)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->log->writeln('<comment>'.$message.'</comment>');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $message
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
protected function error($message)
|
|
|
|
{
|
|
|
|
$this->log->writeln('<error>'.$message.'</error>');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-27 02:50:08 +08:00
|
|
|
* @param string|array $message
|
2018-01-03 11:00:46 +08:00
|
|
|
*/
|
|
|
|
protected function info($message)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (\is_array($message)) {
|
2018-08-27 02:51:47 +08:00
|
|
|
/* @noinspection ForgottenDebugOutputInspection */
|
2018-01-03 11:00:46 +08:00
|
|
|
print_r($message);
|
2018-03-20 09:50:40 +08:00
|
|
|
} else {
|
2018-01-03 11:00:46 +08:00
|
|
|
$this->log->writeln('<info>'.$message.'</info>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the table name with the prefix
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:00:46 +08:00
|
|
|
* @param $table
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:00:46 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function tableName($table)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($this->creds['table_prefix'] !== false) {
|
2018-01-04 05:41:21 +08:00
|
|
|
return $this->creds['table_prefix'].$table;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $table;
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:00:46 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function saveModel($model)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$model->save();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-03 11:00:46 +08:00
|
|
|
return true;
|
|
|
|
} catch (QueryException $e) {
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($e->getCode() !== '23000') {
|
2018-01-04 08:36:08 +08:00
|
|
|
$this->error($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 08:36:08 +08:00
|
|
|
/**
|
|
|
|
* Create a new mapping between an old ID and the new one
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-04 08:36:08 +08:00
|
|
|
* @param $entity
|
|
|
|
* @param $old_id
|
|
|
|
* @param $new_id
|
|
|
|
*/
|
|
|
|
protected function addMapping($entity, $old_id, $new_id)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!array_key_exists($entity, $this->mappedEntities)) {
|
2018-01-04 08:36:08 +08:00
|
|
|
$this->mappedEntities[$entity] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mappedEntities[$entity][$old_id] = $new_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the ID for a mapping
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-04 08:36:08 +08:00
|
|
|
* @param $entity
|
|
|
|
* @param $old_id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-04 08:36:08 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function getMapping($entity, $old_id)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!array_key_exists($entity, $this->mappedEntities)) {
|
2018-01-04 08:36:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity = $this->mappedEntities[$entity];
|
2018-03-20 09:50:40 +08:00
|
|
|
if (array_key_exists($old_id, $entity)) {
|
2018-01-04 08:36:08 +08:00
|
|
|
return $entity[$old_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:14:55 +08:00
|
|
|
/**
|
|
|
|
* @param $date
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-30 08:14:55 +08:00
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
protected function parseDate($date)
|
|
|
|
{
|
|
|
|
$carbon = Carbon::parse($date);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-30 08:14:55 +08:00
|
|
|
return $carbon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a decimal duration and convert it to minutes
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-30 08:14:55 +08:00
|
|
|
* @param $duration
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-30 08:14:55 +08:00
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
protected function convertDuration($duration)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (strpos($duration, '.') !== false) {
|
2018-01-30 08:14:55 +08:00
|
|
|
$delim = '.';
|
2018-03-20 09:50:40 +08:00
|
|
|
} elseif (strpos($duration, ':')) {
|
2018-01-30 08:14:55 +08:00
|
|
|
$delim = ':';
|
|
|
|
} else {
|
2018-08-27 00:40:04 +08:00
|
|
|
// no delimiter, assume it's just a straight hour
|
2018-01-30 08:14:55 +08:00
|
|
|
return (int) $duration * 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hm = explode($delim, $duration);
|
|
|
|
$hours = (int) $hm[0] * 60;
|
|
|
|
$mins = (int) $hm[1];
|
|
|
|
|
|
|
|
return $hours + $mins;
|
|
|
|
}
|
|
|
|
|
2018-01-03 11:00:46 +08:00
|
|
|
/**
|
|
|
|
* @param $table
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:00:46 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getTotalRows($table)
|
|
|
|
{
|
|
|
|
$table = $this->tableName($table);
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$sql = 'SELECT COUNT(*) FROM '.$table;
|
2018-01-03 11:00:46 +08:00
|
|
|
$rows = $this->conn->query($sql)->fetchColumn();
|
|
|
|
|
|
|
|
$this->info('Found '.$rows.' rows in '.$table);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-04 05:41:21 +08:00
|
|
|
return (int) $rows;
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all the rows in a table, but read them in a batched manner
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param string $table The name of the table
|
|
|
|
* @param null $read_rows Number of rows to read
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:00:46 +08:00
|
|
|
* @return \Generator
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
protected function readRows($table, $read_rows = null)
|
2018-01-03 11:00:46 +08:00
|
|
|
{
|
2018-01-04 05:41:21 +08:00
|
|
|
// Set the table prefix if it has been entered
|
|
|
|
$this->tableName($table);
|
|
|
|
|
2018-01-03 11:00:46 +08:00
|
|
|
$offset = 0;
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($read_rows === null) {
|
2018-01-30 08:14:55 +08:00
|
|
|
$read_rows = self::BATCH_READ_ROWS;
|
|
|
|
}
|
|
|
|
|
2018-01-03 11:00:46 +08:00
|
|
|
$total_rows = $this->getTotalRows($table);
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
while ($offset < $total_rows) {
|
2018-01-30 08:14:55 +08:00
|
|
|
$rows_to_read = $offset + $read_rows;
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($rows_to_read > $total_rows) {
|
2018-01-03 11:00:46 +08:00
|
|
|
$rows_to_read = $total_rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Reading '.$offset.' to '.$rows_to_read.' of '.$total_rows);
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$sql = 'SELECT * FROM '.$this->tableName($table)
|
|
|
|
.' LIMIT '.self::BATCH_READ_ROWS.' OFFSET '.$offset;
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
foreach ($this->conn->query($sql) as $row) {
|
|
|
|
yield $row;
|
|
|
|
}
|
2018-03-20 09:50:40 +08:00
|
|
|
} catch (PDOException $e) {
|
2018-01-03 11:00:46 +08:00
|
|
|
// Without incrementing the offset, it should re-run the same query
|
|
|
|
$this->error($e);
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (strpos($e->getMessage(), 'server has gone away') !== false) {
|
2018-01-03 11:00:46 +08:00
|
|
|
$this->reconnect();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset += self::BATCH_READ_ROWS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 11:16:35 +08:00
|
|
|
/**
|
|
|
|
* Return the subfleet
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-03 11:16:35 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getSubfleet()
|
|
|
|
{
|
2018-01-04 08:36:08 +08:00
|
|
|
$airline = Airline::first();
|
|
|
|
$subfleet = Subfleet::firstOrCreate(
|
|
|
|
['airline_id' => $airline->id, 'name' => self::SUBFLEET_NAME],
|
|
|
|
['type' => 'PHPVMS']
|
|
|
|
);
|
2018-01-03 11:16:35 +08:00
|
|
|
|
|
|
|
return $subfleet;
|
|
|
|
}
|
|
|
|
|
2018-01-03 11:00:46 +08:00
|
|
|
/**
|
|
|
|
* All the individual importers, done on a per-table basis
|
|
|
|
* Some tables get saved locally for tables that use FK refs
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import all of the ranks
|
|
|
|
*/
|
|
|
|
protected function importRanks()
|
|
|
|
{
|
|
|
|
$this->comment('--- RANK IMPORT ---');
|
|
|
|
|
|
|
|
$count = 0;
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($this->readRows('ranks') as $row) {
|
2018-01-04 08:36:08 +08:00
|
|
|
$rank = Rank::firstOrCreate(
|
|
|
|
['name' => $row->rank],
|
2018-03-20 09:50:40 +08:00
|
|
|
['image_url' => $row->rankimage, 'hours' => $row->minhours]
|
2018-01-04 08:36:08 +08:00
|
|
|
);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-01-04 08:36:08 +08:00
|
|
|
$this->addMapping('ranks', $row->rankid, $rank->id);
|
|
|
|
$this->addMapping('ranks', $row->rank, $rank->id);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($rank->wasRecentlyCreated) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Imported '.$count.' ranks');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import all of the airlines. Save them all in the private var $airlines
|
|
|
|
* They're used to lookup from other reference tables
|
|
|
|
*/
|
|
|
|
protected function importAirlines()
|
|
|
|
{
|
|
|
|
$this->comment('--- AIRLINE IMPORT ---');
|
|
|
|
|
|
|
|
$count = 0;
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($this->readRows('airlines') as $row) {
|
2018-01-04 08:36:08 +08:00
|
|
|
$airline = Airline::firstOrCreate(
|
|
|
|
['icao' => $row->code],
|
|
|
|
['iata' => $row->code, 'name' => $row->name, 'active' => $row->enabled]
|
|
|
|
);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-01-04 08:36:08 +08:00
|
|
|
$this->addMapping('airlines', $row->id, $airline->id);
|
|
|
|
$this->addMapping('airlines', $row->code, $airline->id);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-01-04 08:36:08 +08:00
|
|
|
if ($airline->wasRecentlyCreated) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Imported '.$count.' airlines');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imported the aircraft
|
|
|
|
*/
|
|
|
|
protected function importAircraft()
|
|
|
|
{
|
|
|
|
$this->comment('--- AIRCRAFT IMPORT ---');
|
|
|
|
|
2018-01-03 11:16:35 +08:00
|
|
|
$subfleet = $this->getSubfleet();
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
$this->info('Subfleet ID is '.$subfleet->id);
|
|
|
|
|
|
|
|
$count = 0;
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($this->readRows('aircraft') as $row) {
|
2018-01-04 08:36:08 +08:00
|
|
|
$aircraft = Aircraft::firstOrCreate(
|
|
|
|
['name' => $row->fullname, 'registration' => $row->registration],
|
2018-03-20 09:50:40 +08:00
|
|
|
['icao' => $row->icao,
|
2018-01-04 08:36:08 +08:00
|
|
|
'subfleet_id' => $subfleet->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'active' => $row->enabled,
|
2019-05-13 01:08:31 +08:00
|
|
|
]
|
|
|
|
);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-01-04 08:36:08 +08:00
|
|
|
$this->addMapping('aircraft', $row->id, $aircraft->id);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($aircraft->wasRecentlyCreated) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Imported '.$count.' aircraft');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import all of the airports
|
|
|
|
*/
|
|
|
|
protected function importAirports()
|
|
|
|
{
|
|
|
|
$this->comment('--- AIRPORT IMPORT ---');
|
|
|
|
|
|
|
|
$count = 0;
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($this->readRows('airports') as $row) {
|
2018-01-03 11:00:46 +08:00
|
|
|
$attrs = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'id' => trim($row->icao),
|
|
|
|
'icao' => trim($row->icao),
|
|
|
|
'name' => $row->name,
|
2018-01-03 11:00:46 +08:00
|
|
|
'country' => $row->country,
|
2018-03-20 09:50:40 +08:00
|
|
|
'lat' => $row->lat,
|
|
|
|
'lon' => $row->lng,
|
|
|
|
'hub' => $row->hub,
|
2018-01-03 11:00:46 +08:00
|
|
|
];
|
|
|
|
|
2018-01-30 08:14:55 +08:00
|
|
|
$airport = Airport::updateOrCreate(
|
|
|
|
['id' => $attrs['id']],
|
|
|
|
$attrs
|
|
|
|
);
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($airport->wasRecentlyCreated) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Imported '.$count.' airports');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import the flights and schedules
|
|
|
|
*/
|
|
|
|
protected function importFlights()
|
|
|
|
{
|
2018-01-04 11:40:05 +08:00
|
|
|
$this->comment('--- FLIGHT SCHEDULE IMPORT ---');
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
$count = 0;
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($this->readRows('schedules') as $row) {
|
2018-01-04 11:40:05 +08:00
|
|
|
$airline_id = $this->getMapping('airlines', $row->code);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-01-30 08:14:55 +08:00
|
|
|
$flight_num = trim($row->flightnum);
|
|
|
|
|
2018-01-04 11:40:05 +08:00
|
|
|
$attrs = [
|
|
|
|
'dpt_airport_id' => $row->depicao,
|
|
|
|
'arr_airport_id' => $row->arricao,
|
2018-03-20 09:50:40 +08:00
|
|
|
'route' => $row->route ?: '',
|
|
|
|
'distance' => round($row->distance ?: 0, 2),
|
|
|
|
'level' => $row->flightlevel ?: 0,
|
|
|
|
'dpt_time' => $row->deptime ?: '',
|
|
|
|
'arr_time' => $row->arrtime ?: '',
|
|
|
|
'flight_time' => $this->convertDuration($row->flighttime) ?: '',
|
|
|
|
'notes' => $row->notes ?: '',
|
|
|
|
'active' => $row->enabled ?: true,
|
2018-01-04 11:40:05 +08:00
|
|
|
];
|
|
|
|
|
2018-01-30 08:14:55 +08:00
|
|
|
try {
|
|
|
|
$flight = Flight::updateOrCreate(
|
|
|
|
['airline_id' => $airline_id, 'flight_number' => $flight_num],
|
|
|
|
$attrs
|
|
|
|
);
|
|
|
|
} catch (\Exception $e) {
|
2018-08-27 00:40:04 +08:00
|
|
|
//$this->error($e);
|
2018-01-30 08:14:55 +08:00
|
|
|
}
|
2018-01-04 11:40:05 +08:00
|
|
|
|
|
|
|
$this->addMapping('flights', $row->id, $flight->id);
|
|
|
|
|
|
|
|
// TODO: deserialize route_details into ACARS table
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($flight->wasRecentlyCreated) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-01-04 11:40:05 +08:00
|
|
|
}
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Imported '.$count.' flights');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import all of the PIREPs
|
|
|
|
*/
|
|
|
|
protected function importPireps()
|
|
|
|
{
|
2018-01-30 08:14:55 +08:00
|
|
|
$this->comment('--- PIREP IMPORT ---');
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
$count = 0;
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($this->readRows('pireps') as $row) {
|
2018-01-30 08:14:55 +08:00
|
|
|
$pirep_id = $row->pirepid;
|
|
|
|
$user_id = $this->getMapping('users', $row->pilotid);
|
|
|
|
$airline_id = $this->getMapping('airlines', $row->code);
|
|
|
|
$aircraft_id = $this->getMapping('aircraft', $row->aircraft);
|
|
|
|
|
|
|
|
$attrs = [
|
2018-08-27 00:40:04 +08:00
|
|
|
//'id' => $pirep_id,
|
2018-03-20 09:50:40 +08:00
|
|
|
'user_id' => $user_id,
|
|
|
|
'airline_id' => $airline_id,
|
|
|
|
'aircraft_id' => $aircraft_id,
|
|
|
|
'flight_number' => $row->flightnum ?: '',
|
2018-01-30 08:14:55 +08:00
|
|
|
'dpt_airport_id' => $row->depicao,
|
|
|
|
'arr_airport_id' => $row->arricao,
|
2018-03-20 09:50:40 +08:00
|
|
|
'block_fuel' => $row->fuelused,
|
|
|
|
'route' => $row->route ?: '',
|
|
|
|
'source_name' => $row->source,
|
|
|
|
'created_at' => $this->parseDate($row->submitdate),
|
|
|
|
'updated_at' => $this->parseDate($row->modifieddate),
|
2018-01-30 08:14:55 +08:00
|
|
|
];
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Set the distance
|
2018-01-30 08:14:55 +08:00
|
|
|
$distance = round($row->distance ?: 0, 2);
|
|
|
|
$attrs['distance'] = $distance;
|
|
|
|
$attrs['planned_distance'] = $distance;
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Set the flight time properly
|
2018-01-30 08:14:55 +08:00
|
|
|
$duration = $this->convertDuration($row->flighttime_stamp);
|
|
|
|
$attrs['flight_time'] = $duration;
|
|
|
|
$attrs['planned_flight_time'] = $duration;
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Set how it was filed
|
2018-03-20 09:50:40 +08:00
|
|
|
if (strtoupper($row->source) === 'MANUAL') {
|
2018-01-30 08:14:55 +08:00
|
|
|
$attrs['source'] = PirepSource::MANUAL;
|
|
|
|
} else {
|
|
|
|
$attrs['source'] = PirepSource::ACARS;
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Set the flight type
|
2018-01-30 08:14:55 +08:00
|
|
|
$row->flighttype = strtoupper($row->flighttype);
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($row->flighttype === 'P') {
|
2018-03-30 00:34:36 +08:00
|
|
|
$attrs['flight_type'] = FlightType::SCHED_PAX;
|
2018-03-20 09:50:40 +08:00
|
|
|
} elseif ($row->flighttype === 'C') {
|
2018-03-30 00:34:36 +08:00
|
|
|
$attrs['flight_type'] = FlightType::SCHED_CARGO;
|
2018-01-30 08:14:55 +08:00
|
|
|
} else {
|
2018-03-30 00:34:36 +08:00
|
|
|
$attrs['flight_type'] = FlightType::CHARTER_PAX_ONLY;
|
2018-01-30 08:14:55 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Set the flight level of the PIREP is set
|
2018-03-20 09:50:40 +08:00
|
|
|
if (property_exists($row, 'flightlevel')) {
|
2018-01-30 08:14:55 +08:00
|
|
|
$attrs['level'] = $row->flightlevel;
|
|
|
|
} else {
|
|
|
|
$attrs['level'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pirep = Pirep::updateOrCreate(
|
|
|
|
['id' => $pirep_id],
|
|
|
|
$attrs
|
|
|
|
);
|
|
|
|
|
|
|
|
$source = strtoupper($row->source);
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($source === 'SMARTCARS') {
|
2018-08-27 00:40:04 +08:00
|
|
|
// TODO: Parse smartcars log into the acars table
|
2018-03-20 09:50:40 +08:00
|
|
|
} elseif ($source === 'KACARS') {
|
2018-08-27 00:40:04 +08:00
|
|
|
// TODO: Parse kACARS log into acars table
|
2018-03-20 09:50:40 +08:00
|
|
|
} elseif ($source === 'XACARS') {
|
2018-08-27 00:40:04 +08:00
|
|
|
// TODO: Parse XACARS log into acars table
|
2018-01-30 08:14:55 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// TODO: Add extra fields in as PIREP fields
|
2018-01-30 08:14:55 +08:00
|
|
|
$this->addMapping('pireps', $row->pirepid, $pirep->id);
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-01-30 08:14:55 +08:00
|
|
|
if ($pirep->wasRecentlyCreated) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-01-30 08:14:55 +08:00
|
|
|
}
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Imported '.$count.' pireps');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function importUsers()
|
|
|
|
{
|
2018-01-04 05:41:21 +08:00
|
|
|
$this->comment('--- USER IMPORT ---');
|
2018-01-03 11:00:46 +08:00
|
|
|
|
|
|
|
$count = 0;
|
2018-01-30 08:14:55 +08:00
|
|
|
foreach ($this->readRows('pilots', 50) as $row) {
|
2018-08-27 00:40:04 +08:00
|
|
|
// TODO: What to do about pilot ids
|
2018-01-04 05:41:21 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$name = $row->firstname.' '.$row->lastname;
|
2018-01-04 08:36:08 +08:00
|
|
|
|
|
|
|
$airline_id = $this->getMapping('airlines', $row->code);
|
|
|
|
$rank_id = $this->getMapping('ranks', $row->rank);
|
2018-01-04 05:41:21 +08:00
|
|
|
$state = $this->getUserState($row->retired);
|
|
|
|
|
2018-01-04 08:36:08 +08:00
|
|
|
$new_password = Str::random(60);
|
|
|
|
|
2018-01-04 05:41:21 +08:00
|
|
|
$attrs = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'name' => $name,
|
|
|
|
'password' => Hash::make($new_password),
|
|
|
|
'api_key' => Utils::generateApiKey(),
|
|
|
|
'airline_id' => $airline_id,
|
|
|
|
'rank_id' => $rank_id,
|
2018-01-04 05:41:21 +08:00
|
|
|
'home_airport_id' => $row->hub,
|
|
|
|
'curr_airport_id' => $row->hub,
|
2018-03-20 09:50:40 +08:00
|
|
|
'flights' => (int) $row->totalflights,
|
|
|
|
'flight_time' => Utils::hoursToMinutes($row->totalhours),
|
|
|
|
'state' => $state,
|
|
|
|
'created_at' => $this->parseDate($row->joindate),
|
2018-01-04 05:41:21 +08:00
|
|
|
];
|
2018-01-03 11:00:46 +08:00
|
|
|
|
2018-01-30 08:14:55 +08:00
|
|
|
$user = User::updateOrCreate(
|
2018-01-04 08:36:08 +08:00
|
|
|
['email' => $row->email],
|
|
|
|
$attrs
|
|
|
|
);
|
|
|
|
|
2018-01-04 11:40:05 +08:00
|
|
|
$this->addMapping('users', $row->pilotid, $user->id);
|
|
|
|
|
2018-01-04 08:36:08 +08:00
|
|
|
if ($user->wasRecentlyCreated) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-01-04 05:41:21 +08:00
|
|
|
}
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->info('Imported '.$count.' users');
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 08:14:55 +08:00
|
|
|
/**
|
|
|
|
* Go through and set the last PIREP ID for the users
|
|
|
|
*/
|
|
|
|
protected function findLastPireps()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-03 11:00:46 +08:00
|
|
|
/**
|
|
|
|
* Recalculate all of the user ranks
|
|
|
|
*/
|
|
|
|
protected function recalculateRanks()
|
|
|
|
{
|
2018-01-03 11:16:35 +08:00
|
|
|
/*$this->comment('--- RECALCULATING RANKS ---');*/
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|
2018-01-04 05:41:21 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user's new state from their original state
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-04 08:36:08 +08:00
|
|
|
* @param $state
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-04 08:36:08 +08:00
|
|
|
* @return int
|
2018-01-04 05:41:21 +08:00
|
|
|
*/
|
|
|
|
protected function getUserState($state)
|
|
|
|
{
|
2018-01-04 08:36:08 +08:00
|
|
|
// TODO: This state might differ between simpilot and classic version
|
|
|
|
|
2018-01-04 11:40:05 +08:00
|
|
|
$state = (int) $state;
|
|
|
|
|
2018-01-04 05:41:21 +08:00
|
|
|
// Declare array of classic states
|
|
|
|
$phpvms_classic_states = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'ACTIVE' => 0,
|
2018-01-04 05:41:21 +08:00
|
|
|
'INACTIVE' => 1,
|
2018-03-20 09:50:40 +08:00
|
|
|
'BANNED' => 2,
|
2018-08-27 00:40:04 +08:00
|
|
|
'ON_LEAVE' => 3,
|
2018-01-04 05:41:21 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
// Decide which state they will be in accordance with v7
|
2018-01-04 08:36:08 +08:00
|
|
|
if ($state === $phpvms_classic_states['ACTIVE']) {
|
2018-01-04 05:41:21 +08:00
|
|
|
return UserState::ACTIVE;
|
2019-05-13 07:05:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($state === $phpvms_classic_states['INACTIVE']) {
|
2018-08-27 00:40:04 +08:00
|
|
|
// TODO: Make an inactive state?
|
2018-01-04 05:41:21 +08:00
|
|
|
return UserState::REJECTED;
|
2019-05-13 07:05:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($state === $phpvms_classic_states['BANNED']) {
|
2018-01-04 05:41:21 +08:00
|
|
|
return UserState::SUSPENDED;
|
2019-05-13 07:05:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($state === $phpvms_classic_states['ON_LEAVE']) {
|
2018-01-04 05:41:21 +08:00
|
|
|
return UserState::ON_LEAVE;
|
|
|
|
}
|
2019-05-13 07:05:36 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$this->error('Unknown status: '.$state);
|
2019-05-13 07:05:36 +08:00
|
|
|
return UserState::ACTIVE;
|
2018-01-04 05:41:21 +08:00
|
|
|
}
|
2018-01-03 11:00:46 +08:00
|
|
|
}
|