Additional logging for the stats recalculation (#358)

* Additional logging for the stats recalculation

* style fix
This commit is contained in:
Nabeel S
2019-08-10 20:42:35 -04:00
committed by GitHub
parent c1103afe8f
commit dc51897314
19 changed files with 99 additions and 43 deletions
+4 -7
View File
@@ -16,15 +16,12 @@ abstract class Command extends \Illuminate\Console\Command
abstract public function handle(); abstract public function handle();
/** /**
* Splice the logger and replace the active handlers with * Splice the logger and replace the active handlers with the handlers from the
* the handlers from the "cron" stack in config/logging.php * a stack in config/logging.php
* *
* Close out any of the existing handlers so we don't leave * @param string $channel_name Channel name from config/logging.php
* file descriptors leaking around
*
* @param string $channel_name Channel name to grab the handlers from
*/ */
public function redirectLoggingToStdout($channel_name): void public function redirectLoggingToFile($channel_name): void
{ {
$logger = app(\Illuminate\Log\Logger::class); $logger = app(\Illuminate\Log\Logger::class);
+2
View File
@@ -47,6 +47,8 @@ class AcarsReplay extends Command
{ {
parent::__construct(); parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->httpClient = new Client([ $this->httpClient = new Client([
'base_uri' => config('app.url'), 'base_uri' => config('app.url'),
'headers' => [ 'headers' => [
+7
View File
@@ -13,6 +13,13 @@ class ComposerCommand extends Command
protected $signature = 'phpvms:composer {cmd}'; protected $signature = 'phpvms:composer {cmd}';
protected $description = 'Composer related tasks'; protected $description = 'Composer related tasks';
public function __construct()
{
parent::__construct();
$this->redirectLoggingToFile('stdout');
}
/** /**
* Run composer update related commands * Run composer update related commands
*/ */
+4 -1
View File
@@ -4,6 +4,7 @@ namespace App\Console\Commands;
use App\Console\Command; use App\Console\Command;
use Log; use Log;
use Tivie\OS\Detector;
/** /**
* Class CreateDatabase * Class CreateDatabase
@@ -20,7 +21,9 @@ class CreateDatabase extends Command
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->os = new \Tivie\OS\Detector();
$this->redirectLoggingToFile('stdout');
$this->os = new Detector();
} }
/** /**
+23 -10
View File
@@ -11,6 +11,7 @@ use App\Repositories\AcarsRepository;
use App\Services\AirportService; use App\Services\AirportService;
use App\Services\AwardService; use App\Services\AwardService;
use App\Services\DatabaseService; use App\Services\DatabaseService;
use App\Services\UserService;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@@ -35,6 +36,8 @@ class DevCommands extends Command
public function __construct(DatabaseService $dbSvc) public function __construct(DatabaseService $dbSvc)
{ {
parent::__construct(); parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->dbSvc = $dbSvc; $this->dbSvc = $dbSvc;
} }
@@ -51,16 +54,17 @@ class DevCommands extends Command
} }
$commands = [ $commands = [
'clear-acars' => 'clearAcars', 'clear-acars' => 'clearAcars',
'clear-users' => 'clearUsers', 'clear-users' => 'clearUsers',
'compile-assets' => 'compileAssets', 'compile-assets' => 'compileAssets',
'db-attrs' => 'dbAttrs', 'db-attrs' => 'dbAttrs',
'list-awards' => 'listAwardClasses', 'list-awards' => 'listAwardClasses',
'live-flights' => 'liveFlights', 'live-flights' => 'liveFlights',
'manual-insert' => 'manualInsert', 'manual-insert' => 'manualInsert',
'metar' => 'getMetar', 'metar' => 'getMetar',
'reset-install' => 'resetInstall', 'recalculate-stats' => 'recalculateStats',
'xml-to-yaml' => 'xmlToYaml', 'reset-install' => 'resetInstall',
'xml-to-yaml' => 'xmlToYaml',
]; ];
if (!array_key_exists($command, $commands)) { 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 * Delete all of the tables, etc from the database, for a clean install
*/ */
+2
View File
@@ -26,6 +26,8 @@ class DevInstall extends Command
public function __construct(\DatabaseSeeder $databaseSeeder) public function __construct(\DatabaseSeeder $databaseSeeder)
{ {
parent::__construct(); parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->databaseSeeder = $databaseSeeder; $this->databaseSeeder = $databaseSeeder;
} }
+2
View File
@@ -23,6 +23,8 @@ class ImportCsv extends Command
public function __construct(ImportService $importer) public function __construct(ImportService $importer)
{ {
parent::__construct(); parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->importer = $importer; $this->importer = $importer;
} }
+7
View File
@@ -14,6 +14,13 @@ class NavdataImport extends Command
protected $signature = 'phpvms:navdata'; protected $signature = 'phpvms:navdata';
protected $description = ''; protected $description = '';
public function __construct()
{
parent::__construct();
$this->redirectLoggingToFile('stdout');
}
/** /**
* @throws \League\Geotools\Exception\InvalidArgumentException * @throws \League\Geotools\Exception\InvalidArgumentException
* *
+6
View File
@@ -13,6 +13,12 @@ class TestApi extends Command
protected $signature = 'phpvms:test-api {apikey} {url}'; protected $signature = 'phpvms:test-api {apikey} {url}';
protected $httpClient; protected $httpClient;
public function __construct()
{
parent::__construct();
$this->redirectLoggingToFile('stdout');
}
/** /**
* Run dev related commands * Run dev related commands
*/ */
+2
View File
@@ -18,6 +18,8 @@ class Version extends Command
public function __construct(VersionService $versionSvc) public function __construct(VersionService $versionSvc)
{ {
parent::__construct(); parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->versionSvc = $versionSvc; $this->versionSvc = $versionSvc;
} }
+2
View File
@@ -17,6 +17,8 @@ class YamlExport extends Command
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->redirectLoggingToFile('stdout');
} }
/** /**
+2
View File
@@ -22,7 +22,9 @@ class YamlImport extends Command
public function __construct(DatabaseService $dbSvc) public function __construct(DatabaseService $dbSvc)
{ {
parent::__construct(); parent::__construct();
$this->dbSvc = $dbSvc; $this->dbSvc = $dbSvc;
$this->redirectLoggingToFile('stdout');
} }
/** /**
+1 -1
View File
@@ -17,7 +17,7 @@ class Hourly extends Command
public function handle(): void public function handle(): void
{ {
$this->redirectLoggingToStdout('cron'); $this->redirectLoggingToFile('cron');
event(new CronHourly()); event(new CronHourly());
} }
} }
+1 -1
View File
@@ -19,7 +19,7 @@ class Monthly extends Command
public function handle(): void public function handle(): void
{ {
$this->redirectLoggingToStdout('cron'); $this->redirectLoggingToFile('cron');
event(new CronMonthly()); event(new CronMonthly());
} }
} }
+1 -1
View File
@@ -19,7 +19,7 @@ class Nightly extends Command
public function handle(): void public function handle(): void
{ {
$this->redirectLoggingToStdout('cron'); $this->redirectLoggingToFile('cron');
event(new CronNightly()); event(new CronNightly());
} }
} }
+1 -1
View File
@@ -19,7 +19,7 @@ class Weekly extends Command
public function handle(): void public function handle(): void
{ {
$this->redirectLoggingToStdout('cron'); $this->redirectLoggingToFile('cron');
event(new CronWeekly()); event(new CronWeekly());
} }
} }
+2 -13
View File
@@ -4,8 +4,6 @@ namespace App\Cron\Nightly;
use App\Contracts\Listener; use App\Contracts\Listener;
use App\Events\CronNightly; use App\Events\CronNightly;
use App\Models\Enums\UserState;
use App\Repositories\UserRepository;
use App\Services\UserService; use App\Services\UserService;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -14,12 +12,10 @@ use Illuminate\Support\Facades\Log;
*/ */
class RecalculateStats extends Listener class RecalculateStats extends Listener
{ {
private $userRepo;
private $userSvc; private $userSvc;
public function __construct(UserRepository $userRepo, UserService $userService) public function __construct(UserService $userService)
{ {
$this->userRepo = $userRepo;
$this->userSvc = $userService; $this->userSvc = $userService;
} }
@@ -35,14 +31,7 @@ class RecalculateStats extends Listener
{ {
Log::info('Recalculating balances'); Log::info('Recalculating balances');
$w = [ $this->userSvc->recalculateAllUserStats();
['state', '!=', UserState::REJECTED],
];
$users = $this->userRepo->findWhere($w, ['id', 'name', 'airline_id']);
foreach ($users as $user) {
$this->userSvc->recalculateStats($user);
}
Log::info('Done recalculating stats'); Log::info('Done recalculating stats');
} }
+2 -5
View File
@@ -2,11 +2,8 @@
namespace App\Exceptions; namespace App\Exceptions;
use Symfony\Component\HttpKernel\Exception\HttpException; use Exception;
/** class SettingNotFound extends Exception
* Class SettingNotFound
*/
class SettingNotFound extends HttpException
{ {
} }
+28 -3
View File
@@ -15,6 +15,7 @@ use App\Models\Role;
use App\Models\User; use App\Models\User;
use App\Repositories\AircraftRepository; use App\Repositories\AircraftRepository;
use App\Repositories\SubfleetRepository; use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Support\Units\Time; use App\Support\Units\Time;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -27,19 +28,23 @@ class UserService extends Service
{ {
private $aircraftRepo; private $aircraftRepo;
private $subfleetRepo; private $subfleetRepo;
private $userRepo;
/** /**
* UserService constructor. * UserService constructor.
* *
* @param AircraftRepository $aircraftRepo * @param AircraftRepository $aircraftRepo
* @param SubfleetRepository $subfleetRepo * @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo
*/ */
public function __construct( public function __construct(
AircraftRepository $aircraftRepo, AircraftRepository $aircraftRepo,
SubfleetRepository $subfleetRepo SubfleetRepository $subfleetRepo,
UserRepository $userRepo
) { ) {
$this->aircraftRepo = $aircraftRepo; $this->aircraftRepo = $aircraftRepo;
$this->subfleetRepo = $subfleetRepo; $this->subfleetRepo = $subfleetRepo;
$this->userRepo = $userRepo;
} }
/** /**
@@ -259,7 +264,7 @@ class UserService extends Service
} }
// If we should count their transfer hours? // 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); $pilot_hours = new Time($user->flight_time + $user->transfer_time);
} else { } else {
$pilot_hours = new Time($user->flight_time); $pilot_hours = new Time($user->flight_time);
@@ -315,6 +320,22 @@ class UserService extends Service
return $user; 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 * 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'); $flight_time = Pirep::where($w)->sum('flight_time');
$user->flight_time = $flight_time; $user->flight_time = $flight_time;
$user->save();
// Recalc the rank // Recalc the rank
$this->calculatePilotRank($user); $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(); $user->save();
return $user; return $user;