Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
1.41% |
1 / 71 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
SubgroupCommand | |
1.41% |
1 / 71 |
|
50.00% |
1 / 2 |
86.63 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
handle | |
0.00% |
0 / 70 |
|
0.00% |
0 / 1 |
72 |
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 SubgroupCommand 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:subgroup {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('fix_subgroup', 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 | if (!$user->user_info_id) { |
68 | $user_info = $this->userInfoService->processUser($user); |
69 | $user->user_info_id = $user_info->id; |
70 | $user->save(); |
71 | } |
72 | $this->flyMsgUserDailyUsageService->processUserUsage($user, false); |
73 | $this->flyMsgUserDailyUsageService->processUserSummaryUsage($user); |
74 | // $this->userInfoService->pushItToHubspot($user->id, false); |
75 | $user->last_update_usage_at = new UTCDateTime(now()->getTimestamp() * 1000); |
76 | |
77 | UserInfo::where('user_id', $user->id) |
78 | ->update([ |
79 | 'last_update_usage_at' => $user->last_update_usage_at, |
80 | '$unset' => [ |
81 | 'fix_subgroup' => 1, |
82 | ] |
83 | ]); |
84 | |
85 | $added_users_count++; |
86 | } catch (\Exception $e) { |
87 | $this->error("Failed to process user: " . $userInfo->email . " - Error: " . $e->getMessage()); |
88 | $failed_users[] = [ |
89 | 'user_id' => $user?->id, |
90 | 'email' => $userInfo->email, |
91 | 'error' => $e->getMessage(), |
92 | ]; |
93 | $failed_users_count++; |
94 | } |
95 | |
96 | $duration = now()->diffInMilliseconds($processingStartAt); |
97 | |
98 | $totalProcessingTime += $duration; |
99 | |
100 | $totalUsers--; |
101 | $executedUsers = count($users) - $totalUsers; |
102 | $averageProcessingTime = round($totalProcessingTime / $executedUsers); |
103 | $estimatedTimeLeftSeconds = round(($totalUsers * $averageProcessingTime) / 1000, 2); |
104 | $estimatedTimeLeftMinutes = round($estimatedTimeLeftSeconds / 60, 2); |
105 | $timeLeftMessage = $estimatedTimeLeftMinutes > 1 ? "Estimated time left: {$estimatedTimeLeftMinutes} minutes." : "Estimated time left: {$estimatedTimeLeftSeconds} seconds."; |
106 | $this->output->write("\rAverage processing time $averageProcessingTime milliseconds | Percentage: " . round(($totalUsers / count($users)) * 100, 2) . "% | Remaining users: " . $totalUsers . " | {$timeLeftMessage} ", false); |
107 | } |
108 | |
109 | if ($userInfoDispatcher) { |
110 | UserInfo::setEventDispatcher($userInfoDispatcher); |
111 | } |
112 | if ($userDispatcher) { |
113 | User::setEventDispatcher($userDispatcher); |
114 | } |
115 | if ($dailyDispatcher) { |
116 | FlyMsgUserDailyUsage::setEventDispatcher($dailyDispatcher); |
117 | } |
118 | |
119 | $totalDurationInMinutes = now()->diffInMinutes($totalTimeStartAt); |
120 | $totalDurationInSeconds = now()->diffInSeconds($totalTimeStartAt); |
121 | |
122 | $this->info("UpdateUsageCommand completed"); |
123 | $this->info("Total duration in minutes: {$totalDurationInMinutes}"); |
124 | $this->info("Total duration in seconds: {$totalDurationInSeconds}"); |
125 | $this->info("Completed users: {$added_users_count}"); |
126 | $this->info("Failed users: {$failed_users_count}"); |
127 | } |
128 | } |