Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
1.35% |
1 / 74 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| StatusDateCommand | |
1.35% |
1 / 74 |
|
50.00% |
1 / 2 |
69.44 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
0.00% |
0 / 73 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Http\Models\Admin\AdminUserInvitation; |
| 6 | use App\Http\Models\Auth\User; |
| 7 | use App\Http\Models\FlyMsgUserDailyUsage; |
| 8 | use App\Http\Models\UserInfo; |
| 9 | use App\Services\UserInfo\FlyMsgUserDailyUsageService; |
| 10 | use App\Services\UserInfo\UserInfoService; |
| 11 | use Carbon\Carbon; |
| 12 | use Illuminate\Console\Command; |
| 13 | use Illuminate\Support\Facades\Log; |
| 14 | use MongoDB\BSON\UTCDateTime; |
| 15 | |
| 16 | class StatusDateCommand extends Command |
| 17 | { |
| 18 | public function __construct( |
| 19 | public UserInfoService $userInfoService, |
| 20 | public FlyMsgUserDailyUsageService $flyMsgUserDailyUsageService |
| 21 | ) { |
| 22 | parent::__construct(); |
| 23 | } |
| 24 | |
| 25 | protected $signature = 'cac:status {perPage} {page}'; |
| 26 | |
| 27 | protected $description = 'Update Usage To create daily usage for all users deploy'; |
| 28 | |
| 29 | public function handle(): void |
| 30 | { |
| 31 | ini_set('memory_limit', '-1'); |
| 32 | $perPage = $this->argument('perPage'); |
| 33 | $page = $this->argument('page'); |
| 34 | |
| 35 | $this->info("UpdateUsageCommand started"); |
| 36 | $totalTimeStartAt = now(); |
| 37 | |
| 38 | ini_set('memory_limit', '-1'); |
| 39 | |
| 40 | $failed_users = []; |
| 41 | $failed_users_count = 0; |
| 42 | $added_users_count = 0; |
| 43 | |
| 44 | $users = UserInfo::where('status_date_problem', 1) |
| 45 | ->skip(($page - 1) * $perPage) |
| 46 | ->take($perPage) |
| 47 | ->get(); |
| 48 | |
| 49 | $totalUsers = count($users); |
| 50 | $this->info("Processing " . $totalUsers . " users"); |
| 51 | |
| 52 | $userInfoDispatcher = UserInfo::getEventDispatcher(); |
| 53 | UserInfo::unsetEventDispatcher(); |
| 54 | $userDispatcher = User::getEventDispatcher(); |
| 55 | User::unsetEventDispatcher(); |
| 56 | $dailyDispatcher = FlyMsgUserDailyUsage::getEventDispatcher(); |
| 57 | FlyMsgUserDailyUsage::unsetEventDispatcher(); |
| 58 | |
| 59 | $totalProcessingTime = 0; |
| 60 | |
| 61 | foreach ($users as $userInfo) { |
| 62 | $processingStartAt = now(); |
| 63 | try { |
| 64 | |
| 65 | $user = User::where('email', $userInfo->email)->first(); |
| 66 | |
| 67 | $account_status_date = match (true) { |
| 68 | !is_null($user?->deactivated_at) => $user->deactivated_at, |
| 69 | !is_null($user?->activation_date) => Carbon::parse($user->activation_date), |
| 70 | !is_null($user?->created_at) => $user->created_at, |
| 71 | $user?->status == 'Invited' => AdminUserInvitation::where("email", $user->email)->first()?->updated_at, |
| 72 | empty($user) => $userInfo->updated_at, |
| 73 | default => null, |
| 74 | }; |
| 75 | |
| 76 | $userInfo->status_date = new UTCDateTime($account_status_date->timestamp * 1000); |
| 77 | |
| 78 | UserInfo::where('user_id', $userInfo->user_id) |
| 79 | ->update([ |
| 80 | 'last_update_usage_at' => new UTCDateTime(now()->getTimestamp() * 1000), |
| 81 | 'status_date' => $userInfo->status_date, |
| 82 | '$unset' => [ |
| 83 | 'status_date_problem' => 1, |
| 84 | ] |
| 85 | ]); |
| 86 | $added_users_count++; |
| 87 | } catch (\Exception $e) { |
| 88 | $this->error("Failed to process user: " . $userInfo->email . " - Error: " . $e->getMessage()); |
| 89 | $failed_users[] = [ |
| 90 | 'user_id' => $user?->id, |
| 91 | 'email' => $userInfo->email, |
| 92 | 'error' => $e->getMessage(), |
| 93 | ]; |
| 94 | $failed_users_count++; |
| 95 | } |
| 96 | |
| 97 | $duration = now()->diffInMilliseconds($processingStartAt); |
| 98 | |
| 99 | $totalProcessingTime += $duration; |
| 100 | |
| 101 | $totalUsers--; |
| 102 | $executedUsers = count($users) - $totalUsers; |
| 103 | $averageProcessingTime = round($totalProcessingTime / $executedUsers); |
| 104 | $estimatedTimeLeftSeconds = round(($totalUsers * $averageProcessingTime) / 1000, 2); |
| 105 | $estimatedTimeLeftMinutes = round($estimatedTimeLeftSeconds / 60, 2); |
| 106 | $timeLeftMessage = $estimatedTimeLeftMinutes > 1 ? "Estimated time left: {$estimatedTimeLeftMinutes} minutes." : "Estimated time left: {$estimatedTimeLeftSeconds} seconds."; |
| 107 | $this->output->write("\rAverage processing time $averageProcessingTime milliseconds | Percentage: " . round(($totalUsers / count($users)) * 100, 2) . "% | Remaining users: " . $totalUsers . " | {$timeLeftMessage} ", false); |
| 108 | } |
| 109 | |
| 110 | if ($userInfoDispatcher) { |
| 111 | UserInfo::setEventDispatcher($userInfoDispatcher); |
| 112 | } |
| 113 | if ($userDispatcher) { |
| 114 | User::setEventDispatcher($userDispatcher); |
| 115 | } |
| 116 | if ($dailyDispatcher) { |
| 117 | FlyMsgUserDailyUsage::setEventDispatcher($dailyDispatcher); |
| 118 | } |
| 119 | |
| 120 | $totalDurationInMinutes = now()->diffInMinutes($totalTimeStartAt); |
| 121 | $totalDurationInSeconds = now()->diffInSeconds($totalTimeStartAt); |
| 122 | |
| 123 | $this->info("UpdateUsageCommand completed"); |
| 124 | $this->info("Total duration in minutes: {$totalDurationInMinutes}"); |
| 125 | $this->info("Total duration in seconds: {$totalDurationInSeconds}"); |
| 126 | $this->info("Completed users: {$added_users_count}"); |
| 127 | $this->info("Failed users: {$failed_users_count}"); |
| 128 | } |
| 129 | } |