Initial user stats recalculation #254

This commit is contained in:
Nabeel Shahzad 2018-08-20 09:42:54 -05:00
parent 5ee053cace
commit 89baf34824
5 changed files with 104 additions and 24 deletions

View File

@ -0,0 +1,49 @@
<?php
namespace App\Cron\Nightly;
use App\Events\CronNightly;
use App\Interfaces\Listener;
use App\Models\Enums\UserState;
use App\Models\Journal;
use App\Repositories\UserRepository;
use App\Services\UserService;
use Log;
/**
* This recalculates the balances on all of the journals
* @package App\Listeners\Cron
*/
class RecalculateStats extends Listener
{
private $userRepo,
$userSvc;
public function __construct(UserRepository $userRepo, UserService $userService)
{
$this->userRepo = $userRepo;
$this->userSvc = $userService;
}
/**
* Recalculate the stats for active users
* @param CronNightly $event
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function handle(CronNightly $event): void
{
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);
}
Log::info('Done recalculating stats');
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Providers;
use App\Cron\Nightly\RecalculateStats;
use App\Cron\Nightly\SetActiveFlights;
use App\Events\CronMonthly;
use App\Events\CronNightly;
use App\Events\CronWeekly;
use App\Cron\Nightly\ApplyExpenses;
use App\Cron\Nightly\PilotLeave;
use App\Cron\Nightly\RecalculateBalances;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class CronServiceProvider extends ServiceProvider
{
protected $listen = [
# Cron hooks
CronNightly::class => [
ApplyExpenses::class,
RecalculateBalances::class,
PilotLeave::class,
SetActiveFlights::class,
RecalculateStats::class,
],
CronWeekly::class => [
],
CronMonthly::class => [
\App\Cron\Monthly\ApplyExpenses::class
],
];
protected $subscribe = [];
}

View File

@ -2,16 +2,9 @@
namespace App\Providers;
use App\Cron\Nightly\SetActiveFlights;
use App\Events\CronMonthly;
use App\Events\CronNightly;
use App\Events\CronWeekly;
use App\Events\Expenses;
use App\Events\UserStatsChanged;
use App\Listeners\AwardListener;
use App\Cron\Nightly\ApplyExpenses;
use App\Cron\Nightly\PilotLeave;
use App\Cron\Nightly\RecalculateBalances;
use App\Listeners\ExpenseListener;
use App\Listeners\FinanceEvents;
use App\Listeners\NotificationEvents;
@ -20,26 +13,10 @@ use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvi
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
Expenses::class => [
Expenses::class => [
ExpenseListener::class
],
# Cron hooks
CronNightly::class => [
ApplyExpenses::class,
RecalculateBalances::class,
PilotLeave::class,
SetActiveFlights::class,
],
CronWeekly::class => [
],
CronMonthly::class => [
\App\Cron\Monthly\ApplyExpenses::class
],
UserStatsChanged::class => [
AwardListener::class,
],

View File

@ -6,7 +6,9 @@ use App\Events\UserRegistered;
use App\Events\UserStateChanged;
use App\Events\UserStatsChanged;
use App\Interfaces\Service;
use App\Models\Enums\PirepState;
use App\Models\Enums\UserState;
use App\Models\Pirep;
use App\Models\Rank;
use App\Models\Role;
use App\Models\User;
@ -236,6 +238,21 @@ class UserService extends Service
*/
public function recalculateStats(User $user): User
{
# Recalc their hours
$w = [
'user_id' => $user->id,
'state' => PirepState::ACCEPTED,
];
$flight_time = Pirep::where($w)->sum('flight_time');
$user->flight_time = $flight_time;
# Recalc the rank
$this->calculatePilotRank($user);
Log::info('User '.$user->ident.' updated; rank='.$user->rank->name.'; flight_time='.$user->flight_time.' minutes');
$user->save();
return $user;
}
}

View File

@ -84,6 +84,7 @@ return [
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\CronServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,