diff --git a/app/Console/Command.php b/app/Console/Command.php index b073440d..ae164883 100644 --- a/app/Console/Command.php +++ b/app/Console/Command.php @@ -16,15 +16,12 @@ abstract class Command extends \Illuminate\Console\Command abstract public function handle(); /** - * Splice the logger and replace the active handlers with - * the handlers from the "cron" stack in config/logging.php + * Splice the logger and replace the active handlers with the handlers from the + * a stack in config/logging.php * - * Close out any of the existing handlers so we don't leave - * file descriptors leaking around - * - * @param string $channel_name Channel name to grab the handlers from + * @param string $channel_name Channel name from config/logging.php */ - public function redirectLoggingToStdout($channel_name): void + public function redirectLoggingToFile($channel_name): void { $logger = app(\Illuminate\Log\Logger::class); diff --git a/app/Console/Commands/AcarsReplay.php b/app/Console/Commands/AcarsReplay.php index 3898f3f9..4dcb5010 100644 --- a/app/Console/Commands/AcarsReplay.php +++ b/app/Console/Commands/AcarsReplay.php @@ -47,6 +47,8 @@ class AcarsReplay extends Command { parent::__construct(); + $this->redirectLoggingToFile('stdout'); + $this->httpClient = new Client([ 'base_uri' => config('app.url'), 'headers' => [ diff --git a/app/Console/Commands/ComposerCommand.php b/app/Console/Commands/ComposerCommand.php index c64c8c35..bea3d7af 100644 --- a/app/Console/Commands/ComposerCommand.php +++ b/app/Console/Commands/ComposerCommand.php @@ -13,6 +13,13 @@ class ComposerCommand extends Command protected $signature = 'phpvms:composer {cmd}'; protected $description = 'Composer related tasks'; + public function __construct() + { + parent::__construct(); + + $this->redirectLoggingToFile('stdout'); + } + /** * Run composer update related commands */ diff --git a/app/Console/Commands/CreateDatabase.php b/app/Console/Commands/CreateDatabase.php index ea821cbb..4fcb001e 100644 --- a/app/Console/Commands/CreateDatabase.php +++ b/app/Console/Commands/CreateDatabase.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use App\Console\Command; use Log; +use Tivie\OS\Detector; /** * Class CreateDatabase @@ -20,7 +21,9 @@ class CreateDatabase extends Command public function __construct() { parent::__construct(); - $this->os = new \Tivie\OS\Detector(); + + $this->redirectLoggingToFile('stdout'); + $this->os = new Detector(); } /** diff --git a/app/Console/Commands/DevCommands.php b/app/Console/Commands/DevCommands.php index fe2cf699..3dbbcdb4 100644 --- a/app/Console/Commands/DevCommands.php +++ b/app/Console/Commands/DevCommands.php @@ -11,6 +11,7 @@ use App\Repositories\AcarsRepository; use App\Services\AirportService; use App\Services\AwardService; use App\Services\DatabaseService; +use App\Services\UserService; use Illuminate\Database\QueryException; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; @@ -35,6 +36,8 @@ class DevCommands extends Command public function __construct(DatabaseService $dbSvc) { parent::__construct(); + + $this->redirectLoggingToFile('stdout'); $this->dbSvc = $dbSvc; } @@ -51,16 +54,17 @@ class DevCommands extends Command } $commands = [ - 'clear-acars' => 'clearAcars', - 'clear-users' => 'clearUsers', - 'compile-assets' => 'compileAssets', - 'db-attrs' => 'dbAttrs', - 'list-awards' => 'listAwardClasses', - 'live-flights' => 'liveFlights', - 'manual-insert' => 'manualInsert', - 'metar' => 'getMetar', - 'reset-install' => 'resetInstall', - 'xml-to-yaml' => 'xmlToYaml', + 'clear-acars' => 'clearAcars', + 'clear-users' => 'clearUsers', + 'compile-assets' => 'compileAssets', + 'db-attrs' => 'dbAttrs', + 'list-awards' => 'listAwardClasses', + 'live-flights' => 'liveFlights', + 'manual-insert' => 'manualInsert', + 'metar' => 'getMetar', + 'recalculate-stats' => 'recalculateStats', + 'reset-install' => 'resetInstall', + 'xml-to-yaml' => 'xmlToYaml', ]; if (!array_key_exists($command, $commands)) { @@ -234,6 +238,15 @@ class DevCommands extends Command } } + /** + * Recalculate the stats for all users + */ + protected function recalculateStats(): void + { + $userSvc = app(UserService::class); + $userSvc->recalculateAllUserStats(); + } + /** * Delete all of the tables, etc from the database, for a clean install */ diff --git a/app/Console/Commands/DevInstall.php b/app/Console/Commands/DevInstall.php index 8129d1af..795c316e 100644 --- a/app/Console/Commands/DevInstall.php +++ b/app/Console/Commands/DevInstall.php @@ -26,6 +26,8 @@ class DevInstall extends Command public function __construct(\DatabaseSeeder $databaseSeeder) { parent::__construct(); + + $this->redirectLoggingToFile('stdout'); $this->databaseSeeder = $databaseSeeder; } diff --git a/app/Console/Commands/ImportCsv.php b/app/Console/Commands/ImportCsv.php index 59fc8a53..3dff3361 100644 --- a/app/Console/Commands/ImportCsv.php +++ b/app/Console/Commands/ImportCsv.php @@ -23,6 +23,8 @@ class ImportCsv extends Command public function __construct(ImportService $importer) { parent::__construct(); + + $this->redirectLoggingToFile('stdout'); $this->importer = $importer; } diff --git a/app/Console/Commands/NavdataImport.php b/app/Console/Commands/NavdataImport.php index 93550d76..040a514e 100644 --- a/app/Console/Commands/NavdataImport.php +++ b/app/Console/Commands/NavdataImport.php @@ -14,6 +14,13 @@ class NavdataImport extends Command protected $signature = 'phpvms:navdata'; protected $description = ''; + public function __construct() + { + parent::__construct(); + + $this->redirectLoggingToFile('stdout'); + } + /** * @throws \League\Geotools\Exception\InvalidArgumentException * diff --git a/app/Console/Commands/TestApi.php b/app/Console/Commands/TestApi.php index d65441cf..85483e59 100644 --- a/app/Console/Commands/TestApi.php +++ b/app/Console/Commands/TestApi.php @@ -13,6 +13,12 @@ class TestApi extends Command protected $signature = 'phpvms:test-api {apikey} {url}'; protected $httpClient; + public function __construct() + { + parent::__construct(); + $this->redirectLoggingToFile('stdout'); + } + /** * Run dev related commands */ diff --git a/app/Console/Commands/Version.php b/app/Console/Commands/Version.php index 9902478b..f0976474 100644 --- a/app/Console/Commands/Version.php +++ b/app/Console/Commands/Version.php @@ -18,6 +18,8 @@ class Version extends Command public function __construct(VersionService $versionSvc) { parent::__construct(); + + $this->redirectLoggingToFile('stdout'); $this->versionSvc = $versionSvc; } diff --git a/app/Console/Commands/YamlExport.php b/app/Console/Commands/YamlExport.php index 27f9fffa..bb97e90e 100644 --- a/app/Console/Commands/YamlExport.php +++ b/app/Console/Commands/YamlExport.php @@ -17,6 +17,8 @@ class YamlExport extends Command public function __construct() { parent::__construct(); + + $this->redirectLoggingToFile('stdout'); } /** diff --git a/app/Console/Commands/YamlImport.php b/app/Console/Commands/YamlImport.php index 8eb137a9..555acdba 100644 --- a/app/Console/Commands/YamlImport.php +++ b/app/Console/Commands/YamlImport.php @@ -22,7 +22,9 @@ class YamlImport extends Command public function __construct(DatabaseService $dbSvc) { parent::__construct(); + $this->dbSvc = $dbSvc; + $this->redirectLoggingToFile('stdout'); } /** diff --git a/app/Console/Cron/Hourly.php b/app/Console/Cron/Hourly.php index e278b7fd..f1f5bc45 100644 --- a/app/Console/Cron/Hourly.php +++ b/app/Console/Cron/Hourly.php @@ -17,7 +17,7 @@ class Hourly extends Command public function handle(): void { - $this->redirectLoggingToStdout('cron'); + $this->redirectLoggingToFile('cron'); event(new CronHourly()); } } diff --git a/app/Console/Cron/Monthly.php b/app/Console/Cron/Monthly.php index efa3e445..ddf2fa81 100644 --- a/app/Console/Cron/Monthly.php +++ b/app/Console/Cron/Monthly.php @@ -19,7 +19,7 @@ class Monthly extends Command public function handle(): void { - $this->redirectLoggingToStdout('cron'); + $this->redirectLoggingToFile('cron'); event(new CronMonthly()); } } diff --git a/app/Console/Cron/Nightly.php b/app/Console/Cron/Nightly.php index 9f0131dd..c6e88161 100644 --- a/app/Console/Cron/Nightly.php +++ b/app/Console/Cron/Nightly.php @@ -19,7 +19,7 @@ class Nightly extends Command public function handle(): void { - $this->redirectLoggingToStdout('cron'); + $this->redirectLoggingToFile('cron'); event(new CronNightly()); } } diff --git a/app/Console/Cron/Weekly.php b/app/Console/Cron/Weekly.php index 4109d127..e3a998a9 100644 --- a/app/Console/Cron/Weekly.php +++ b/app/Console/Cron/Weekly.php @@ -19,7 +19,7 @@ class Weekly extends Command public function handle(): void { - $this->redirectLoggingToStdout('cron'); + $this->redirectLoggingToFile('cron'); event(new CronWeekly()); } } diff --git a/app/Cron/Nightly/RecalculateStats.php b/app/Cron/Nightly/RecalculateStats.php index fe939598..cb761372 100644 --- a/app/Cron/Nightly/RecalculateStats.php +++ b/app/Cron/Nightly/RecalculateStats.php @@ -4,8 +4,6 @@ namespace App\Cron\Nightly; use App\Contracts\Listener; use App\Events\CronNightly; -use App\Models\Enums\UserState; -use App\Repositories\UserRepository; use App\Services\UserService; use Illuminate\Support\Facades\Log; @@ -14,12 +12,10 @@ use Illuminate\Support\Facades\Log; */ class RecalculateStats extends Listener { - private $userRepo; private $userSvc; - public function __construct(UserRepository $userRepo, UserService $userService) + public function __construct(UserService $userService) { - $this->userRepo = $userRepo; $this->userSvc = $userService; } @@ -35,14 +31,7 @@ class RecalculateStats extends Listener { Log::info('Recalculating balances'); - $w = [ - ['state', '!=', UserState::REJECTED], - ]; - - $users = $this->userRepo->findWhere($w, ['id', 'name', 'airline_id']); - foreach ($users as $user) { - $this->userSvc->recalculateStats($user); - } + $this->userSvc->recalculateAllUserStats(); Log::info('Done recalculating stats'); } diff --git a/app/Exceptions/SettingNotFound.php b/app/Exceptions/SettingNotFound.php index 589f1cd4..b4c0b35d 100644 --- a/app/Exceptions/SettingNotFound.php +++ b/app/Exceptions/SettingNotFound.php @@ -2,11 +2,8 @@ namespace App\Exceptions; -use Symfony\Component\HttpKernel\Exception\HttpException; +use Exception; -/** - * Class SettingNotFound - */ -class SettingNotFound extends HttpException +class SettingNotFound extends Exception { } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index a7dc5e76..79a82049 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -15,6 +15,7 @@ use App\Models\Role; use App\Models\User; use App\Repositories\AircraftRepository; use App\Repositories\SubfleetRepository; +use App\Repositories\UserRepository; use App\Support\Units\Time; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; @@ -27,19 +28,23 @@ class UserService extends Service { private $aircraftRepo; private $subfleetRepo; + private $userRepo; /** * UserService constructor. * * @param AircraftRepository $aircraftRepo * @param SubfleetRepository $subfleetRepo + * @param UserRepository $userRepo */ public function __construct( AircraftRepository $aircraftRepo, - SubfleetRepository $subfleetRepo + SubfleetRepository $subfleetRepo, + UserRepository $userRepo ) { $this->aircraftRepo = $aircraftRepo; $this->subfleetRepo = $subfleetRepo; + $this->userRepo = $userRepo; } /** @@ -259,7 +264,7 @@ class UserService extends Service } // If we should count their transfer hours? - if (setting('pilots.count_transfer_hours') === true) { + if (setting('pilots.count_transfer_hours', false) === true) { $pilot_hours = new Time($user->flight_time + $user->transfer_time); } else { $pilot_hours = new Time($user->flight_time); @@ -315,6 +320,22 @@ class UserService extends Service return $user; } + /** + * Recalculate the stats for all active users + */ + public function recalculateAllUserStats(): void + { + $w = [ + ['state', '!=', UserState::REJECTED], + ]; + + $this->userRepo + ->findWhere($w, ['id', 'name', 'airline_id']) + ->each(function ($user, $_) { + return $this->recalculateStats($user); + }); + } + /** * Recount/update all of the stats for a user * @@ -336,10 +357,14 @@ class UserService extends Service $flight_time = Pirep::where($w)->sum('flight_time'); $user->flight_time = $flight_time; + $user->save(); + // Recalc the rank $this->calculatePilotRank($user); - Log::info('User '.$user->ident.' updated; rank='.$user->rank->name.'; flight_time='.$user->flight_time.' minutes'); + Log::info('User '.$user->ident.' updated; flight count='.$flight_count + .', rank='.$user->rank->name + .', flight_time='.$user->flight_time.' minutes'); $user->save(); return $user;