public function getFlightNumberForSpacecraftAttribute()
 {
     $self = $this;
     return SpacecraftFlight::whereHas('spacecraft', function ($q) use($self) {
         $q->where('spacecraft_id', $self->spacecraft->id);
     })->whereHas('mission', function ($q) use($self) {
         $q->before($self->mission->launch_order_id);
     })->count() + 1;
 }
 public static function dragon($substatistic, $dynamicString)
 {
     if ($substatistic === 'Missions') {
         if ($dynamicString === 'missionCount') {
             return Cache::remember('stats:description:dragon:missions:missionCount', 60, function () {
                 return SpacecraftFlight::whereHas('mission', function ($q) {
                     $q->whereComplete();
                 })->count();
             });
         }
     }
     if ($substatistic === 'ISS Resupplies') {
         if ($dynamicString === 'issResupplyCount') {
             return Cache::remember('stats:description:dragon:issResupplies:issResupplyCount', 60, function () {
                 return SpacecraftFlight::whereNotNull('iss_berth')->whereHas('mission', function ($q) {
                     $q->whereComplete();
                 })->count();
             });
         }
     }
 }
 public function run()
 {
     // COTS-1
     SpacecraftFlight::create(['mission_id' => 7, 'spacecraft_id' => 1, 'flight_name' => 'COTS Demo 1', 'return_method' => 'Splashdown', 'end_of_mission' => Carbon::create(2010, 12, 8, 19, 2, 52)]);
     // COTS-2+
     SpacecraftFlight::create(['mission_id' => 8, 'spacecraft_id' => 2, 'flight_name' => 'COTS Demo 2+', 'return_method' => 'Splashdown', 'upmass' => 520, 'downmass' => 660, 'end_of_mission' => Carbon::create(2012, 5, 31, 15, 42, 0), 'iss_berth' => Carbon::create(2012, 5, 25, 16, 2, 0), 'iss_unberth' => Carbon::create(2012, 5, 31, 9, 49, 0)]);
     // CRS-1
     SpacecraftFlight::create(['mission_id' => 9, 'spacecraft_id' => 3, 'flight_name' => 'SpaceX CRS-1', 'return_method' => 'Splashdown', 'upmass' => 905, 'downmass' => 905, 'end_of_mission' => Carbon::create(2012, 10, 28, 19, 22, 0), 'iss_berth' => Carbon::create(2012, 10, 10, 13, 3, 0), 'iss_unberth' => Carbon::create(2012, 10, 28, 13, 29, 0)]);
     // CRS-2
     SpacecraftFlight::create(['mission_id' => 10, 'spacecraft_id' => 4, 'flight_name' => 'SpaceX CRS-2', 'return_method' => 'Splashdown', 'upmass' => 677, 'downmass' => 1370, 'end_of_mission' => Carbon::create(2013, 3, 26, 16, 34, 0), 'iss_berth' => Carbon::create(2013, 3, 3, 13, 56, 0), 'iss_unberth' => Carbon::create(2013, 3, 26, 8, 10, 0)]);
     // CRS-3
     SpacecraftFlight::create(['mission_id' => 14, 'spacecraft_id' => 5, 'flight_name' => 'SpaceX CRS-3', 'return_method' => 'Splashdown', 'upmass' => 2117, 'downmass' => 1563, 'end_of_mission' => Carbon::create(2014, 5, 18, 19, 5, 0), 'iss_berth' => Carbon::create(2014, 4, 20, 14, 6, 0), 'iss_unberth' => Carbon::create(2014, 5, 18, 11, 55, 0)]);
     // CRS-4
     SpacecraftFlight::create(['mission_id' => 18, 'spacecraft_id' => 6, 'flight_name' => 'SpaceX CRS-4', 'return_method' => 'Splashdown', 'upmass' => 2216, 'downmass' => 1486, 'end_of_mission' => Carbon::create(2014, 10, 25, 19, 38, 0), 'iss_berth' => Carbon::create(2014, 9, 23, 13, 21, 0), 'iss_unberth' => Carbon::create(2014, 10, 25, 13, 57, 0)]);
     // CRS-5
     SpacecraftFlight::create(['mission_id' => 19, 'spacecraft_id' => 7, 'flight_name' => 'SpaceX CRS-5', 'return_method' => 'Splashdown', 'upmass' => 2395, 'downmass' => 1662, 'end_of_mission' => Carbon::create(2015, 2, 11, 0, 44, 0), 'iss_berth' => Carbon::create(2015, 1, 12, 13, 54, 0), 'iss_unberth' => Carbon::create(2015, 2, 10, 17, 11, 0)]);
     // CRS-6
     SpacecraftFlight::create(['mission_id' => 22, 'spacecraft_id' => 8, 'flight_name' => 'SpaceX CRS-6', 'return_method' => 'Splashdown', 'upmass' => 2015, 'downmass' => 1370, 'end_of_mission' => Carbon::create(2015, 5, 21, 16, 42, 0), 'iss_berth' => Carbon::create(2015, 4, 17, 13, 29, 0), 'iss_unberth' => Carbon::create(2015, 5, 21, 9, 29, 0)]);
     // CRS-7
     SpacecraftFlight::create(['mission_id' => 24, 'spacecraft_id' => 9, 'flight_name' => 'SpaceX CRS-7', 'return_method' => 'Did not return', 'upmass' => 1952, 'downmass' => 675, 'end_of_mission' => Carbon::create(2015, 6, 28, 14, 23, 30)]);
 }
 /**
  * @param $substatistic
  * @return mixed
  */
 public static function dragon($substatistic)
 {
     if ($substatistic === 'Missions') {
         return Cache::remember('stats:dragon:missions', 60, function () {
             return SpacecraftFlight::whereHas('mission', function ($q) {
                 $q->whereComplete();
             })->count();
         });
     }
     if ($substatistic === 'ISS Resupplies') {
         return Cache::remember('stats:dragon:ISSResupplies', 60, function () {
             return SpacecraftFlight::whereNotNull('iss_berth')->whereHas('mission', function ($q) {
                 $q->whereComplete();
             })->count();
         });
     }
     if ($substatistic === 'Total Flight Time') {
         return Cache::remember('stats:dragon:totalflighttime', 60, function () {
             return DB::table('spacecraft_flights_pivot')->selectRaw('SUM(TIMESTAMPDIFF(SECOND,missions.launch_exact,spacecraft_flights_pivot.end_of_mission)) AS duration')->where('missions.status', 'Complete')->join('missions', 'missions.mission_id', '=', 'spacecraft_flights_pivot.mission_id')->first()->duration;
         });
     }
     if ($substatistic === 'Flight Time') {
         return Cache::remember('stats:dragon:flighttime', 60, function () {
             return ['values' => SpacecraftFlight::selectRaw('TIMESTAMPDIFF(SECOND,missions.launch_exact,spacecraft_flights_pivot.end_of_mission) / 86400 AS duration, missions.name AS mission')->where('missions.status', 'Complete')->join('missions', 'missions.mission_id', '=', 'spacecraft_flights_pivot.mission_id')->get(), 'extrapolation' => false, 'xAxis' => ['type' => 'ordinal', 'key' => 'mission', 'title' => 'Mission'], 'yAxis' => ['type' => 'linear', 'key' => 'duration', 'title' => 'Duration (Days)', 'zeroing' => true]];
         });
     }
     if ($substatistic === 'Cargo') {
         $query = Cache::remember('stats:dragon:cargo', 60, function () {
             return SpacecraftFlight::select(DB::raw('SUM(upmass) AS upmass, SUM(downmass) AS downmass'))->whereHas('mission', function ($q) {
                 $q->whereComplete();
             })->first();
         });
         $stat[0] = number_format($query->upmass);
         $stat[1] = number_format($query->downmass);
         return $stat;
     }
     if ($substatistic === 'Reflights') {
         return Cache::remember('stats:dragon:reflights', 60, function () {
             return DB::select(DB::raw("SELECT COALESCE(SUM(reflights), 0) as total_flights FROM (SELECT COUNT(*)-1 as reflights FROM spacecraft JOIN spacecraft_flights_pivot ON spacecraft.spacecraft_id = spacecraft_flights_pivot.spacecraft_id WHERE spacecraft.spacecraft_id=spacecraft_flights_pivot.spacecraft_id GROUP BY spacecraft_flights_pivot.spacecraft_id HAVING reflights > 0) reflights"))[0]->total_flights;
         });
     }
 }