Upsert on ACARS positions #572 (#573)

This commit is contained in:
Nabeel S 2020-02-22 16:03:01 -05:00 committed by GitHub
parent 6e87f7804c
commit 9ed07da9c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 28 deletions

View File

@ -154,7 +154,7 @@ class AcarsController extends Controller
$position['pirep_id'] = $id; $position['pirep_id'] = $id;
$position['type'] = AcarsType::FLIGHT_PATH; $position['type'] = AcarsType::FLIGHT_PATH;
if (array_key_exists('sim_time', $position)) { if (isset($position['sim_time'])) {
if ($position['sim_time'] instanceof \DateTime) { if ($position['sim_time'] instanceof \DateTime) {
$position['sim_time'] = Carbon::instance($position['sim_time']); $position['sim_time'] = Carbon::instance($position['sim_time']);
} else { } else {
@ -162,7 +162,7 @@ class AcarsController extends Controller
} }
} }
if (array_key_exists('created_at', $position)) { if (isset($position['created_at'])) {
if ($position['created_at'] instanceof \DateTime) { if ($position['created_at'] instanceof \DateTime) {
$position['created_at'] = Carbon::instance($position['created_at']); $position['created_at'] = Carbon::instance($position['created_at']);
} else { } else {
@ -171,8 +171,16 @@ class AcarsController extends Controller
} }
try { try {
$update = Acars::create($position); if (isset($position['id'])) {
$update->save(); Acars::updateOrInsert(
['id' => $position['id']],
$position
);
} else {
$update = Acars::create($position);
$update->save();
}
$count++; $count++;
} catch (QueryException $ex) { } catch (QueryException $ex) {
Log::info('Error on adding ACARS position: '.$ex->getMessage()); Log::info('Error on adding ACARS position: '.$ex->getMessage());
@ -215,17 +223,25 @@ class AcarsController extends Controller
$log['pirep_id'] = $id; $log['pirep_id'] = $id;
$log['type'] = AcarsType::LOG; $log['type'] = AcarsType::LOG;
if (array_key_exists('sim_time', $log)) { if (isset($log['sim_time'])) {
$log['sim_time'] = Carbon::createFromTimeString($log['sim_time']); $log['sim_time'] = Carbon::createFromTimeString($log['sim_time']);
} }
if (array_key_exists('created_at', $log)) { if (isset($log['created_at'])) {
$log['created_at'] = Carbon::createFromTimeString($log['created_at']); $log['created_at'] = Carbon::createFromTimeString($log['created_at']);
} }
try { try {
$acars = Acars::create($log); if (isset($log['id'])) {
$acars->save(); Acars::updateOrInsert(
['id' => $log['id']],
$log
);
} else {
$acars = Acars::create($log);
$acars->save();
}
$count++; $count++;
} catch (QueryException $ex) { } catch (QueryException $ex) {
Log::info('Error on adding ACARS position: '.$ex->getMessage()); Log::info('Error on adding ACARS position: '.$ex->getMessage());
@ -262,17 +278,25 @@ class AcarsController extends Controller
$log['type'] = AcarsType::LOG; $log['type'] = AcarsType::LOG;
$log['log'] = $log['event']; $log['log'] = $log['event'];
if (array_key_exists('sim_time', $log)) { if (isset($log['sim_time'])) {
$log['sim_time'] = Carbon::createFromTimeString($log['sim_time']); $log['sim_time'] = Carbon::createFromTimeString($log['sim_time']);
} }
if (array_key_exists('created_at', $log)) { if (isset($log['created_at'])) {
$log['created_at'] = Carbon::createFromTimeString($log['created_at']); $log['created_at'] = Carbon::createFromTimeString($log['created_at']);
} }
try { try {
$acars = Acars::create($log); if (isset($log['id'])) {
$acars->save(); Acars::updateOrInsert(
['id' => $log['id']],
$log
);
} else {
$acars = Acars::create($log);
$acars->save();
}
$count++; $count++;
} catch (QueryException $ex) { } catch (QueryException $ex) {
Log::info('Error on adding ACARS position: '.$ex->getMessage()); Log::info('Error on adding ACARS position: '.$ex->getMessage());

View File

@ -542,8 +542,12 @@ class PirepController extends Controller
$position['pirep_id'] = $id; $position['pirep_id'] = $id;
$position['type'] = AcarsType::ROUTE; $position['type'] = AcarsType::ROUTE;
$acars = Acars::create($position); if (isset($position['id'])) {
$acars->save(); Acars::updateOrInsert(['id' => $position['id']], $position);
} else {
$acars = Acars::create($position);
$acars->save();
}
$count++; $count++;
} }

View File

@ -2,8 +2,7 @@
namespace App\Models\Traits; namespace App\Models\Traits;
use App\Contracts\Model; use App\Support\Utils;
use Hashids\Hashids;
trait HashIdTrait trait HashIdTrait
{ {
@ -14,9 +13,7 @@ trait HashIdTrait
*/ */
final protected static function createNewHashId(): string final protected static function createNewHashId(): string
{ {
$hashids = new Hashids('', Model::ID_MAX_LENGTH); return Utils::generateNewId();
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
} }
/** /**

View File

@ -2,6 +2,8 @@
namespace App\Support; namespace App\Support;
use App\Contracts\Model;
use Hashids\Hashids;
use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Nwidart\Modules\Facades\Module; use Nwidart\Modules\Facades\Module;
@ -11,6 +13,24 @@ use Nwidart\Modules\Facades\Module;
*/ */
class Utils class Utils
{ {
/**
* Generate a new ID with a given length
*
* @param int [$length]
*
* @return string
*/
public static function generateNewId(int $length = null)
{
if (!$length) {
$length = Model::ID_MAX_LENGTH;
}
$hashids = new Hashids('', $length);
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
}
/** /**
* Returns a 40 character API key that a user can use * Returns a 40 character API key that a user can use
* *

View File

@ -4,7 +4,7 @@ return [
'themes_path' => resource_path('views/layouts'), // eg: base_path('resources/themes') 'themes_path' => resource_path('views/layouts'), // eg: base_path('resources/themes')
'asset_not_found' => 'LOG_ERROR', 'asset_not_found' => 'LOG_ERROR',
'default' => 'default', 'default' => 'default',
'cache' => true, 'cache' => false,
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@ -4,6 +4,7 @@ use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus; use App\Models\Enums\PirepStatus;
use App\Models\PirepFare; use App\Models\PirepFare;
use App\Repositories\SettingRepository; use App\Repositories\SettingRepository;
use App\Support\Utils;
/** /**
* Test API calls and authentication, etc * Test API calls and authentication, etc
@ -660,13 +661,22 @@ class AcarsTest extends TestCase
$uri = '/api/pireps/'.$pirep_id.'/acars/position'; $uri = '/api/pireps/'.$pirep_id.'/acars/position';
// Post an ACARS update // Post an ACARS update
$acars_count = \random_int(2, 10); $acars_count = \random_int(5, 10);
$acars = factory(App\Models\Acars::class, $acars_count)->make(['id' => ''])->toArray(); $acars = factory(App\Models\Acars::class, $acars_count)->make(['id' => ''])
->map(function ($point) {
$point['id'] = Utils::generateNewId();
return $point;
})
->toArray();
$update = ['positions' => $acars]; $update = ['positions' => $acars];
$response = $this->post($uri, $update); $response = $this->post($uri, $update);
$response->assertStatus(200)->assertJson(['count' => $acars_count]); $response->assertStatus(200)->assertJson(['count' => $acars_count]);
// Try posting again, should be ignored/not throw any sql errors
$response = $this->post($uri, $update);
$response->assertStatus(200)->assertJson(['count' => $acars_count]);
$response = $this->get($uri); $response = $this->get($uri);
$response->assertStatus(200)->assertJsonCount($acars_count, 'data'); $response->assertStatus(200)->assertJsonCount($acars_count, 'data');
} }
@ -700,11 +710,9 @@ class AcarsTest extends TestCase
$dt = date('c'); $dt = date('c');
$uri = '/api/pireps/'.$pirep_id.'/acars/position'; $uri = '/api/pireps/'.$pirep_id.'/acars/position';
$acars = factory(App\Models\Acars::class)->make( $acars = factory(App\Models\Acars::class)->make([
[ 'sim_time' => $dt,
'sim_time' => $dt, ])->toArray();
]
)->toArray();
$update = ['positions' => [$acars]]; $update = ['positions' => [$acars]];
$response = $this->post($uri, $update); $response = $this->post($uri, $update);
@ -729,7 +737,13 @@ class AcarsTest extends TestCase
$response->assertStatus(400); $response->assertStatus(400);
$post_route = [ $post_route = [
['order' => 1, 'name' => 'NAVPOINT', 'lat' => 'notanumber', 'lon' => 34.11], [
'id' => 'NAVPOINT',
'order' => 1,
'name' => 'NAVPOINT',
'lat' => 'notanumber',
'lon' => 34.11,
],
]; ];
$uri = '/api/pireps/'.$pirep_id.'/route'; $uri = '/api/pireps/'.$pirep_id.'/route';
@ -803,6 +817,10 @@ class AcarsTest extends TestCase
$response = $this->post($uri, ['route' => $post_route]); $response = $this->post($uri, ['route' => $post_route]);
$response->assertStatus(200)->assertJson(['count' => $route_count]); $response->assertStatus(200)->assertJson(['count' => $route_count]);
// Try double post to ignore SQL update
$response = $this->post($uri, ['route' => $post_route]);
$response->assertStatus(200)->assertJson(['count' => $route_count]);
/** /**
* Get * Get
*/ */