Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 158 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
AccountCenterReportingService | |
0.00% |
0 / 158 |
|
0.00% |
0 / 7 |
756 | |
0.00% |
0 / 1 |
getAllTop5ByMetric | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
2 | |||
getTop5ByProperty | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getTop5Users | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
findLicenseOverview | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
56 | |||
findUsersOverview | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
findShortcut | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
90 | |||
findFlyMsgAI | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace App\Http\Services\Admin\Reports; |
4 | |
5 | use App\DTO\AccountCenter\Reporting\ReportingRequestDTO; |
6 | use App\Exceptions\ExpectedException; |
7 | use App\Http\Models\Admin\CompanyLicenses; |
8 | use App\Http\Models\Auth\User; |
9 | use App\Http\Models\FlyMsgUserDailyUsage; |
10 | use App\Http\Models\UserInfo; |
11 | use App\Traits\HubspotPropertiesTrait; |
12 | use App\Traits\AccountCenter\Reporting\ShortcutTrait; |
13 | use App\Traits\AccountCenter\Reporting\FlyMsgAITrackingTrait; |
14 | use MongoDB\BSON\UTCDateTime; |
15 | |
16 | class AccountCenterReportingService extends SharedReportingService |
17 | { |
18 | use HubspotPropertiesTrait, ShortcutTrait, FlyMsgAITrackingTrait; |
19 | |
20 | protected function getAllTop5ByMetric($pipeline, string $property) |
21 | { |
22 | $pipeline[] = [ |
23 | '$group' => [ |
24 | '_id' => '$user_id', |
25 | $property => ['$sum' => "$$property"], |
26 | ] |
27 | ]; |
28 | $pipeline[] = [ |
29 | '$sort' => [$property => -1] |
30 | ]; |
31 | $pipeline[] = [ |
32 | '$limit' => 5 |
33 | ]; |
34 | $pipeline[] = [ |
35 | '$project' => [ |
36 | '_id' => 0, |
37 | 'user_id' => '$_id', |
38 | $property => 1, |
39 | ] |
40 | ]; |
41 | $pipeline[] = [ |
42 | '$match' => [ |
43 | $property => ['$gt' => 0] |
44 | ] |
45 | ]; |
46 | |
47 | return FlyMsgUserDailyUsage::raw(fn($collection) => $collection->aggregate($pipeline)); |
48 | } |
49 | |
50 | protected function getTop5ByProperty($query, $property) |
51 | { |
52 | return $query->groupBy('user_id')->get()->map(function ($top) use ($property) { |
53 | return [ |
54 | 'user_id' => $top->first()->user_id, |
55 | $property => $top->sum($property) |
56 | ]; |
57 | })->sortByDesc($property)->take(5); |
58 | } |
59 | |
60 | public function getTop5Users(ReportingRequestDTO $filter, string $property) |
61 | { |
62 | $baseQuery = $this->getFilteredQueryPipeline($filter); |
63 | return $this->getAllTop5ByMetric($baseQuery, $property); |
64 | } |
65 | |
66 | public function findLicenseOverview(FindUsersOverviewFilter $filter) |
67 | { |
68 | $users = $this->findUsers($filter); |
69 | $globalTotalLicensesAssigned = $this->findLicensesAssigned($filter, true, false); |
70 | |
71 | $totalLicensesAssigned = $this->findLicensesAssigned($filter, false, false); |
72 | $totalLicensesUsed = $this->findActiveLicensesUsage(false, $filter); |
73 | |
74 | $company_license = CompanyLicenses::where("company_id", $filter->companyId) |
75 | ->active() |
76 | ->first(); |
77 | |
78 | if (!$company_license) { |
79 | throw new ExpectedException("This company has no licenses"); |
80 | } |
81 | |
82 | $userIds = $users->whereStatus("Active")->pluck("id")->toArray(); |
83 | $percentage_of_assigned_licenses_have_been_activated = $totalLicensesUsed > 0 && $totalLicensesAssigned > 0 ? (($totalLicensesUsed / $totalLicensesAssigned) * 100) : 0; |
84 | $total_users = count($userIds); |
85 | $extensions_installed = $total_users > 0 ? $this->findExtensionInstalled($userIds, null, null) : 0; |
86 | $percentage_of_extension_installed_have_been_installed = $extensions_installed > 0 && $total_users > 0 ? (($extensions_installed / $total_users) * 100) : 0; |
87 | |
88 | return [ |
89 | "purchased" => $company_license->total_number_of_licenses_available, |
90 | "licenses_remaining_to_be_used" => $company_license->total_number_of_licenses_available - $globalTotalLicensesAssigned, |
91 | "assigned" => $totalLicensesAssigned, |
92 | "invited_licenses_pending_activation" => $totalLicensesAssigned - $totalLicensesUsed, |
93 | "activated" => $totalLicensesUsed, |
94 | "percentage_of_assigned_licenses_have_been_activated" => round($percentage_of_assigned_licenses_have_been_activated, 2), |
95 | "extensions_installed" => $extensions_installed, |
96 | "percentage_of_extension_installed_have_been_installed" => round($percentage_of_extension_installed_have_been_installed, 2), |
97 | ]; |
98 | } |
99 | |
100 | public function findUsersOverview(FindUsersOverviewFilter $filter) |
101 | { |
102 | $fromDate = $filter->fromDate; |
103 | $toDate = $filter->toDate; |
104 | |
105 | $users = $this->findUsers($filter); |
106 | $totalLicensesUsed = $this->findActiveLicensesUsage(false, $filter); |
107 | $userIds = $users->pluck("id")->toArray(); |
108 | |
109 | $extensions_installed = $this->findExtensionInstalled($userIds, $fromDate, $toDate); |
110 | |
111 | $extensions_uninstalled = $this->findExtensionsUninstalled($userIds, $fromDate, $toDate); |
112 | |
113 | $inactive_users = $this->findInactiveUsers($filter)->count(); |
114 | |
115 | return [ |
116 | 'active_users' => $totalLicensesUsed, |
117 | 'inactive_users' => $inactive_users, |
118 | 'with_extension_installed' => $extensions_installed, |
119 | 'with_extension_uninstalled' => $extensions_uninstalled, |
120 | ]; |
121 | } |
122 | |
123 | public function findShortcut(FindUsersOverviewFilter $filter) |
124 | { |
125 | ini_set('memory_limit', '3072M'); |
126 | |
127 | $filters = new ReportingRequestDTO( |
128 | fromDate: $filter->fromDate, |
129 | toDate: $filter->toDate, |
130 | companyId: $filter->companyId, |
131 | adminGroupIds: $filter->adminGroupIds, |
132 | userIds: !empty($filter->userIds) ? implode(',', $filter->userIds) : null, |
133 | groupIds: !empty($filter->groupIds) ? implode(',', $filter->groupIds) : null, |
134 | subgroupIds: !empty($filter->subgroupIds) ? implode(',', $filter->subgroupIds) : null, |
135 | role: $filter->currentRole, |
136 | companyIds: $filter->companyIds ?? null, |
137 | ); |
138 | |
139 | $shortcuts = $this->getFilteredQuery($filters); |
140 | |
141 | $property = "flycuts_created"; |
142 | |
143 | if (!$filter->userDefined) { |
144 | $property = "flyplates_added"; |
145 | } |
146 | |
147 | if ($filter->isTop5) { |
148 | $user_usage_counts = $shortcuts->groupBy('user_id')->get()->map(function ($userFlycuts) use ($property) { |
149 | return [ |
150 | 'user_id' => $userFlycuts->first()->user_id, |
151 | $property => $userFlycuts->sum($property) |
152 | ]; |
153 | })->sortByDesc($property)->take(5); |
154 | |
155 | $topUser = User::whereIn('_id', $user_usage_counts->pluck('user_id'))->get(); |
156 | |
157 | return $topUser->map(function ($user) use ($user_usage_counts, $property) { |
158 | return [ |
159 | 'name' => $user->first_name . ' ' . $user->last_name, |
160 | 'count' => collect($user_usage_counts)->where('user_id', $user->id)->first()[$property], |
161 | 'image' => $user->avatar |
162 | ]; |
163 | }); |
164 | } |
165 | |
166 | $total_users = (clone $shortcuts)->distinct('user_id')->count(); |
167 | $total_shortcut_created = (clone $shortcuts)->sum($property); |
168 | |
169 | $firstDate = $filter->toDate; |
170 | $lastDate = $filter->fromDate; |
171 | |
172 | if (!$firstDate) { |
173 | $firstDate = (clone $shortcuts)->min("created_at"); |
174 | } |
175 | |
176 | if (!$lastDate) { |
177 | $lastDate = (clone $shortcuts)->max("created_at"); |
178 | } |
179 | |
180 | $months = $lastDate->diffInMonths($firstDate); |
181 | |
182 | $average_shortcut_per_active_user = $total_users > 0 ? $total_shortcut_created / $total_users : 0; |
183 | |
184 | $chart = $this->buildLineChartDataForCounts($shortcuts->get(), $months, $property); |
185 | |
186 | return [ |
187 | "chart" => $chart, |
188 | "total_shortcut_created" => $total_shortcut_created, |
189 | "average_shortcut_per_active_user" => $average_shortcut_per_active_user, |
190 | ]; |
191 | } |
192 | |
193 | public function findFlyMsgAI(FindUsersOverviewFilter $filter) |
194 | { |
195 | ini_set('memory_limit', '3072M'); |
196 | |
197 | $users = $this->findUsers($filter)->get(); |
198 | // $usersActive = collect(clone $users)->where('status', "Active")->orWhere("status", null)->get(); |
199 | |
200 | $userIds = $users->pluck("id")->toArray(); |
201 | |
202 | $query = FlyMsgUserDailyUsage::whereIn('user_id', $userIds); |
203 | |
204 | if (!empty($filter->fromDate)) { |
205 | $query = $query->where('created_at', '>=', new UTCDateTime($filter->fromDate->getTimestamp() * 1000)); |
206 | } |
207 | |
208 | if (!empty($filter->toDate)) { |
209 | $query = $query->where('created_at', '<=', new UTCDateTime($filter->toDate->getTimestamp() * 1000)); |
210 | } |
211 | |
212 | $property = "{$filter->feature}_count"; |
213 | |
214 | if ($filter->isTop5) { |
215 | $user_usage_counts = $query->groupBy('user_id')->get()->map(function ($flyMsgAIs) use ($property) { |
216 | return [ |
217 | 'user_id' => $flyMsgAIs->first()->user_id, |
218 | $property => $flyMsgAIs->sum($property) |
219 | ]; |
220 | })->sortByDesc($property)->take(5); |
221 | $topUser = User::whereIn('_id', $user_usage_counts->pluck('user_id'))->get(); |
222 | return $topUser->map(function ($user) use ($user_usage_counts) { |
223 | return [ |
224 | 'name' => $user->first_name . ' ' . $user->last_name, |
225 | 'count' => collect($user_usage_counts)->where('user_id')->first()->flypost_count, |
226 | 'image' => $user->avatar |
227 | ]; |
228 | }); |
229 | } |
230 | |
231 | $total_users = $users->count(); |
232 | $total_flymsg_ai = (clone $query)->sum($property); |
233 | |
234 | $firstDate = $filter->toDate; |
235 | $lastDate = $filter->fromDate; |
236 | |
237 | if (!$firstDate) { |
238 | $firstDate = (clone $query)->min("created_at"); |
239 | } |
240 | |
241 | if (!$lastDate) { |
242 | $lastDate = (clone $query)->max("created_at"); |
243 | } |
244 | |
245 | $months = $lastDate->diffInMonths($firstDate); |
246 | $average_flymsgai_per_active_user = $total_users > 0 ? $total_flymsg_ai / $total_users : 0; |
247 | $chart = $this->buildLineChartDataForCounts($query->get(), $months, $property); |
248 | |
249 | return [ |
250 | "chart" => $chart, |
251 | "total_flymsg_ai" => $total_flymsg_ai, |
252 | "average_flymsgai_per_active_user" => $average_flymsgai_per_active_user, |
253 | ]; |
254 | } |
255 | } |