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

View File

@@ -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);

View File

@@ -47,6 +47,8 @@ class AcarsReplay extends Command
{
parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->httpClient = new Client([
'base_uri' => config('app.url'),
'headers' => [

View File

@@ -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
*/

View File

@@ -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();
}
/**

View File

@@ -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
*/

View File

@@ -26,6 +26,8 @@ class DevInstall extends Command
public function __construct(\DatabaseSeeder $databaseSeeder)
{
parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->databaseSeeder = $databaseSeeder;
}

View File

@@ -23,6 +23,8 @@ class ImportCsv extends Command
public function __construct(ImportService $importer)
{
parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->importer = $importer;
}

View File

@@ -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
*

View File

@@ -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
*/

View File

@@ -18,6 +18,8 @@ class Version extends Command
public function __construct(VersionService $versionSvc)
{
parent::__construct();
$this->redirectLoggingToFile('stdout');
$this->versionSvc = $versionSvc;
}

View File

@@ -17,6 +17,8 @@ class YamlExport extends Command
public function __construct()
{
parent::__construct();
$this->redirectLoggingToFile('stdout');
}
/**

View File

@@ -22,7 +22,9 @@ class YamlImport extends Command
public function __construct(DatabaseService $dbSvc)
{
parent::__construct();
$this->dbSvc = $dbSvc;
$this->redirectLoggingToFile('stdout');
}
/**

View File

@@ -17,7 +17,7 @@ class Hourly extends Command
public function handle(): void
{
$this->redirectLoggingToStdout('cron');
$this->redirectLoggingToFile('cron');
event(new CronHourly());
}
}

View File

@@ -19,7 +19,7 @@ class Monthly extends Command
public function handle(): void
{
$this->redirectLoggingToStdout('cron');
$this->redirectLoggingToFile('cron');
event(new CronMonthly());
}
}

View File

@@ -19,7 +19,7 @@ class Nightly extends Command
public function handle(): void
{
$this->redirectLoggingToStdout('cron');
$this->redirectLoggingToFile('cron');
event(new CronNightly());
}
}

View File

@@ -19,7 +19,7 @@ class Weekly extends Command
public function handle(): void
{
$this->redirectLoggingToStdout('cron');
$this->redirectLoggingToFile('cron');
event(new CronWeekly());
}
}

View File

@@ -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');
}

View File

@@ -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
{
}

View File

@@ -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;