-
Notifications
You must be signed in to change notification settings - Fork 16
/
db_helpers_tech.php
94 lines (80 loc) · 3.22 KB
/
db_helpers_tech.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
use App\Models\Enums\PirepState;
use Illuminate\Support\Facades\DB;
use Modules\DisposableBasic\Models\DB_Runway;
use Modules\DisposableBasic\Models\DB_Spec;
// Get Average Taxi Time for given airport
// Return numeric string
if (!function_exists('DB_AvgTaxiTime')) {
function DB_AvgTaxiTime($icao, $type = 'out', $default = 10)
{
if ($type === 'in') {
$dep_arr = 'arr_airport_id';
$out_in = 'taxi-in-time';
} else {
$dep_arr = 'dpt_airport_id';
$out_in = 'taxi-out-time';
}
$pireps = DB::table('pireps')->whereNull('deleted_at')->select('id')->where([$dep_arr => $icao, 'state' => PirepState::ACCEPTED])->pluck('id')->all();
$field_values = DB::table('pirep_field_values')->select('value')->whereIn('pirep_id', $pireps)->where('slug', $out_in)->orderby('created_at', 'desc')->take(100)->pluck('value')->all();
$taxi_times = collect();
foreach ($field_values as $fv) {
$duration = substr($fv, 0, stripos($fv, 'm'));
if (is_numeric($duration) && $duration > 0) {
$taxi_times->push($duration);
}
}
$avg_time = $taxi_times->avg();
if ($avg_time > 0) {
$result = ceil($avg_time);
}
return isset($result) ? $result : $default;
}
}
// Get runways of an airport (by icao code)
if (!function_exists('DB_GetRunways')) {
function DB_GetRunways($icao)
{
$runways = DB_Runway::where('airport_id', $icao)->orderby('runway_ident', 'asc')->get();
return $runways;
}
}
// Get detailed addon specifications for an Aircraft
// Start from Aircraft level, then check Subfleet and ICAO Type (with $deep_check = true)
if (!function_exists('DB_GetSpecs')) {
function DB_GetSpecs($aircraft, $deep_check = false, $with_icao = false)
{
$specs = DB_Spec::where(['aircraft_id' => $aircraft->id, 'active' => true])->orderby('saircraft')->get();
if ($with_icao && filled($specs)) {
// Get ICAO Specs and combine with any aircraft specs provided
$specs_icao = DB_GetSpecs_ICAO($aircraft->icao);
$specs = $specs->merge($specs_icao);
}
if ($deep_check && blank($specs) && filled($aircraft->subfleet)) {
$specs = DB_GetSpecs_SF($aircraft->subfleet);
}
if ($deep_check && blank($specs)) {
$specs = DB_GetSpecs_ICAO($aircraft->icao);
}
return filled($specs) ? $specs : [];
}
}
// Get detailed addon specificatiions for a Subfleet
if (!function_exists('DB_GetSpecs_SF')) {
function DB_GetSpecs_SF($subfleet, $deep_check = false)
{
$specs = DB_Spec::where(['subfleet_id' => $subfleet->id, 'active' => true])->orderby('saircraft')->get();
if ($deep_check && blank($specs)) {
$specs = DB_GetSpecs_ICAO(substr($subfleet->type, 0, 4));
}
return filled($specs) ? $specs : null;
}
}
// Get detailed addon specifications for an ICAO Type
if (!function_exists('DB_GetSpecs_ICAO')) {
function DB_GetSpecs_ICAO($icao)
{
$specs = DB_Spec::where(['icao_id' => $icao, 'active' => true])->orderby('saircraft')->get();
return filled($specs) ? $specs : null;
}
}