Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 310 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| UsersReportUsageExport | |
0.00% |
0 / 310 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| array | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
56 | |||
| headings | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
6 | |||
| getUserUsageData | |
0.00% |
0 / 227 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Exports; |
| 4 | |
| 5 | use Carbon\Carbon; |
| 6 | use Carbon\CarbonPeriod; |
| 7 | use App\Http\Models\Chart; |
| 8 | use MongoDB\BSON\UTCDateTime; |
| 9 | use App\Http\Models\Auth\User; |
| 10 | use Vitorccs\LaravelCsv\Concerns\FromArray; |
| 11 | use Vitorccs\LaravelCsv\Concerns\Exportable; |
| 12 | use Vitorccs\LaravelCsv\Concerns\WithHeadings; |
| 13 | use App\Traits\AccountCenter\Reporting\ChartTrait; |
| 14 | use MongoDB\BSON\ObjectId; |
| 15 | |
| 16 | class UsersReportUsageExport implements FromArray, WithHeadings |
| 17 | { |
| 18 | use Exportable; |
| 19 | use ChartTrait; |
| 20 | |
| 21 | public $company_id; |
| 22 | public $from_date; |
| 23 | public $to_date; |
| 24 | public $user_ids; |
| 25 | public $group_ids; |
| 26 | public $subgroup_ids; |
| 27 | |
| 28 | public function __construct($company_id, $from_date, $to_date, $user_ids, $group_ids, $subgroup_ids) |
| 29 | { |
| 30 | $this->company_id = $company_id; |
| 31 | $this->from_date = $from_date; |
| 32 | $this->to_date = $to_date; |
| 33 | $this->user_ids = $user_ids; |
| 34 | $this->group_ids = $group_ids; |
| 35 | $this->subgroup_ids = $subgroup_ids; |
| 36 | } |
| 37 | |
| 38 | public function array(): array |
| 39 | { |
| 40 | $users = User::select([ |
| 41 | 'id', |
| 42 | 'first_name', |
| 43 | 'last_name', |
| 44 | 'company_group_id', |
| 45 | 'company_id', |
| 46 | 'deleted_at', |
| 47 | "avatar" |
| 48 | ]) |
| 49 | ->where('company_id', $this->company_id) |
| 50 | ->get(); |
| 51 | |
| 52 | $ids = $users->pluck("id")->toArray(); |
| 53 | |
| 54 | $start_date = $this->from_date; |
| 55 | $end_date = $this->to_date; |
| 56 | |
| 57 | if ($start_date && $end_date) { |
| 58 | $start_date = Carbon::parse($start_date)->startOfDay(); |
| 59 | $end_date = Carbon::parse($end_date)->endOfDay(); |
| 60 | $start_date = $start_date->setTimezone("UTC")->startOfDay(); |
| 61 | $end_date = $end_date->setTimezone("UTC")->endOfDay(); |
| 62 | $start_date = new UTCDateTime($start_date->getTimestamp() * 1000); |
| 63 | $end_date = new UTCDateTime($end_date->getTimestamp() * 1000); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | $usageData = $this->getUserUsageData($ids, $start_date, $end_date); |
| 68 | |
| 69 | $period = CarbonPeriod::create($this->from_date, $this->to_date); |
| 70 | $data = []; |
| 71 | |
| 72 | foreach ($usageData as $item) { |
| 73 | $monthCollection = collect($item->monthly_usage); |
| 74 | $identifier = $item->subscription_data?->plan?->identifier ?? 'freemium'; |
| 75 | $row = [ |
| 76 | 'first_name' => $item->first_name ?? '', |
| 77 | 'last_name' => $item->last_name ?? '', |
| 78 | 'email' => $item->email ?? '', |
| 79 | 'license_type' => $this->parseSubscriptionPlanName($identifier), |
| 80 | 'flymsg_account_creation_date' => $item->account_creation_date ?? '', |
| 81 | 'extension_installed_on_any_browser' => ($item->flymsg_chrome_extension_installed == "Yes" || $item->flymsg_edge_extension_installed == "Yes") ? "Yes" : "No", |
| 82 | 'last_login_to_flymsg' => $item->last_login ?? '', |
| 83 | 'sentence_rewrite_ai_used' => round(collect($item->monthly_usage)->sum('sentence_rewrite_count') ?? 0, 2), |
| 84 | 'paragraph_rewrite_ai_used' => round(collect($item->monthly_usage)->sum('paragraph_rewrite_count') ?? 0, 2), |
| 85 | 'flyengage_ai_used' => round(collect($item->monthly_usage)->sum('flyengage_count') ?? 0, 2), |
| 86 | 'flyposts_ai_used' => round(collect($item->monthly_usage)->sum('flypost_count') ?? 0, 2), |
| 87 | 'no_of_flycuts_created' => $item->shortcuts_created ?? 0, |
| 88 | 'no_of_flyplates_added_to_flycuts' => $item->flyplates_created ?? 0, |
| 89 | 'characters_typed_by_flymsg' => round($monthCollection->sum('characters_typed_by_flymsg') ?? 0, 2), |
| 90 | 'time_saved_by_flymsg' => round($monthCollection->sum('time_saved_by_flymsg') ?? 0, 2), |
| 91 | 'cost_saved_by_flymsg' => round($monthCollection->sum('cost_saved_by_flymsg') ?? 0, 2), |
| 92 | ]; |
| 93 | foreach ($period as $date) { |
| 94 | $year_month = $date->format('Y-m'); |
| 95 | $month_year = $date->format('M Y'); |
| 96 | $month = $monthCollection->firstWhere('month_year', $year_month); |
| 97 | $row["characters_typed_by_flymsg___$month_year"] = round($month?->characters_typed_by_flymsg ?? 0, 2); |
| 98 | $row["time_saved_by_flymsg___$month_year"] = round($month?->time_saved_by_flymsg ?? 0, 2); |
| 99 | $row["cost_saved_by_flymsg___$month_year"] = round($month?->cost_saved_by_flymsg ?? 0, 2); |
| 100 | } |
| 101 | $data[] = $row; |
| 102 | } |
| 103 | |
| 104 | return $data; |
| 105 | } |
| 106 | |
| 107 | public function headings(): array |
| 108 | { |
| 109 | $headers = [ |
| 110 | 'Firstname', |
| 111 | 'Lastname', |
| 112 | 'Email', |
| 113 | 'License Type', |
| 114 | 'FlyMSG Account Creation Date', |
| 115 | 'Extension Installed on Any Browser', |
| 116 | 'Last Login to FlyMSG', |
| 117 | 'Total # of Times FlyEngage AI Used (Count)', |
| 118 | 'Total # of Times FlyPosts AI Used (Count)', |
| 119 | '# of FlyCuts Created (Count)', |
| 120 | '# of FlyPlates Added to FlyCuts (Count)', |
| 121 | 'Total # of Characters Typed by FlyMSG by User', |
| 122 | 'Total Time Saved by FlyMSG by User', |
| 123 | 'Total Cost Savings by FlyMSG by User', |
| 124 | ]; |
| 125 | |
| 126 | $period = CarbonPeriod::create($this->from_date, '1 month', $this->to_date); // Ensure monthly periods |
| 127 | |
| 128 | foreach ($period as $date) { |
| 129 | $month_year = $date->format('M Y'); |
| 130 | $headers[] = "Total # of Characters Typed by FlyMSG by User $month_year"; |
| 131 | $headers[] = "Total Time Saved by FlyMSG by User $month_year"; |
| 132 | $headers[] = "Total Cost Savings by FlyMSG by User $month_year"; |
| 133 | } |
| 134 | |
| 135 | return $headers; |
| 136 | } |
| 137 | |
| 138 | private function getUserUsageData($userIds, $startDate, $endDate) |
| 139 | { |
| 140 | $results = User::withoutGlobalScopes()->raw(function ($collection) use ($userIds, $startDate, $endDate) { |
| 141 | $matchExpression = [ |
| 142 | ['$eq' => ['$flymsg_id', '$$userId']] |
| 143 | ]; |
| 144 | |
| 145 | if ($startDate) { |
| 146 | $matchExpression[] = ['$gte' => ['$created_at', $startDate]]; |
| 147 | } |
| 148 | |
| 149 | if ($endDate) { |
| 150 | $matchExpression[] = ['$lte' => ['$created_at', $endDate]]; |
| 151 | } |
| 152 | |
| 153 | return $collection->aggregate([ |
| 154 | [ |
| 155 | '$match' => [ |
| 156 | '_id' => ['$in' => array_map(function ($userId) { |
| 157 | return new \MongoDB\BSON\ObjectId($userId); |
| 158 | }, $userIds)] |
| 159 | ] |
| 160 | ], |
| 161 | [ |
| 162 | '$lookup' => [ |
| 163 | 'from' => 'hubspot_properties', |
| 164 | 'let' => ['userId' => ['$toString' => '$_id']], |
| 165 | 'pipeline' => [ |
| 166 | [ |
| 167 | '$match' => [ |
| 168 | '$expr' => [ |
| 169 | '$and' => $matchExpression |
| 170 | ] |
| 171 | ] |
| 172 | ], |
| 173 | ['$sort' => ['last_login' => -1]], |
| 174 | ['$limit' => 1], |
| 175 | [ |
| 176 | '$project' => [ |
| 177 | 'subscription_type' => 1, |
| 178 | 'account_creation_date' => 1, |
| 179 | 'last_login' => 1, |
| 180 | 'flymsg_chrome_extension_installed' => 1, |
| 181 | 'flymsg_edge_extension_installed' => 1 |
| 182 | ] |
| 183 | ] |
| 184 | ], |
| 185 | 'as' => 'hubspot_data' |
| 186 | ] |
| 187 | ], |
| 188 | [ |
| 189 | '$lookup' => [ |
| 190 | 'from' => 'flycut_usage', |
| 191 | 'let' => ['userId' => ['$toString' => '$_id']], |
| 192 | 'pipeline' => [ |
| 193 | [ |
| 194 | '$match' => [ |
| 195 | '$expr' => [ |
| 196 | '$and' => [ |
| 197 | ['$eq' => ['$user_id', '$$userId']], |
| 198 | ['$gte' => ['$created_at', $startDate]], |
| 199 | ['$lte' => ['$created_at', $endDate]] |
| 200 | ] |
| 201 | ] |
| 202 | ] |
| 203 | ], |
| 204 | [ |
| 205 | '$group' => [ |
| 206 | '_id' => [ |
| 207 | 'month_year' => [ |
| 208 | '$dateToString' => [ |
| 209 | 'format' => '%Y-%m', |
| 210 | 'date' => '$created_at' |
| 211 | ] |
| 212 | ] |
| 213 | ], |
| 214 | 'characters_typed' => ['$sum' => '$characters_typed'], |
| 215 | 'time_saved' => ['$sum' => '$time_saved'], |
| 216 | 'cost_saved' => ['$sum' => '$cost_saved'], |
| 217 | 'flypost_count' => [ |
| 218 | '$sum' => [ |
| 219 | '$cond' => [['$eq' => ['$feature', 'flypost']], 1, 0] |
| 220 | ] |
| 221 | ], |
| 222 | 'flyengage_count' => [ |
| 223 | '$sum' => [ |
| 224 | '$cond' => [['$eq' => ['$feature', 'flyengage']], 1, 0] |
| 225 | ] |
| 226 | ], |
| 227 | 'sentence_rewrite_count' => [ |
| 228 | '$sum' => [ |
| 229 | '$cond' => [['$eq' => ['$feature', 'sentence_rewrite']], 1, 0] |
| 230 | ] |
| 231 | ], |
| 232 | 'paragraph_rewrite_count' => [ |
| 233 | '$sum' => [ |
| 234 | '$cond' => [['$eq' => ['$feature', 'paragraph_rewrite']], 1, 0] |
| 235 | ] |
| 236 | ] |
| 237 | ] |
| 238 | ], |
| 239 | ['$sort' => ['_id.month_year' => 1]] |
| 240 | ], |
| 241 | 'as' => 'monthly_usage_data' |
| 242 | ] |
| 243 | ], |
| 244 | [ |
| 245 | '$lookup' => [ |
| 246 | 'from' => 'shortcuts', |
| 247 | 'let' => ['userId' => ['$toString' => '$_id']], |
| 248 | 'pipeline' => [ |
| 249 | [ |
| 250 | '$match' => [ |
| 251 | '$expr' => [ |
| 252 | '$and' => [ |
| 253 | ['$eq' => ['$user_id', '$$userId']], |
| 254 | ['$gte' => ['$created_at', $startDate]], |
| 255 | ['$lte' => ['$created_at', $endDate]] |
| 256 | ] |
| 257 | ] |
| 258 | ] |
| 259 | ], |
| 260 | [ |
| 261 | '$group' => [ |
| 262 | '_id' => null, |
| 263 | 'shortcuts_created' => [ |
| 264 | '$sum' => ['$cond' => [['$eq' => ['$user_defined', true]], 1, 0]] |
| 265 | ], |
| 266 | 'flyplates_created' => [ |
| 267 | '$sum' => ['$cond' => [['$eq' => ['$user_defined', false]], 1, 0]] |
| 268 | ] |
| 269 | ] |
| 270 | ] |
| 271 | ], |
| 272 | 'as' => 'shortcuts_data' |
| 273 | ] |
| 274 | ], |
| 275 | [ |
| 276 | '$lookup' => [ |
| 277 | 'from' => 'subscriptions', |
| 278 | 'let' => ['userId' => ['$toString' => '$_id']], |
| 279 | 'pipeline' => [ |
| 280 | [ |
| 281 | '$match' => [ |
| 282 | '$expr' => [ |
| 283 | '$and' => [ |
| 284 | ['$eq' => ['$user_id', '$$userId']], |
| 285 | ['$gte' => ['$name', 'main']], |
| 286 | ['$lte' => ['$stripe_status', 'active']] |
| 287 | ] |
| 288 | ] |
| 289 | ] |
| 290 | ], |
| 291 | [ |
| 292 | '$sort' => [ |
| 293 | 'created_at' => -1, |
| 294 | ] |
| 295 | ], |
| 296 | [ |
| 297 | '$limit' => 1 |
| 298 | ], |
| 299 | [ |
| 300 | '$lookup' => [ |
| 301 | 'from' => 'plans', |
| 302 | 'localField' => 'stripe_plan', |
| 303 | 'foreignField' => 'stripe_id', |
| 304 | 'as' => 'plan' |
| 305 | ] |
| 306 | ], |
| 307 | [ |
| 308 | '$project' => [ |
| 309 | 'plan' => [ |
| 310 | '$arrayElemAt' => ['$plan', 0] |
| 311 | ], |
| 312 | 'subscription' => '$$ROOT' |
| 313 | ] |
| 314 | ] |
| 315 | ], |
| 316 | 'as' => 'subscription_data' |
| 317 | ] |
| 318 | ], |
| 319 | [ |
| 320 | '$unwind' => [ |
| 321 | 'path' => '$hubspot_data', |
| 322 | 'preserveNullAndEmptyArrays' => true |
| 323 | ] |
| 324 | ], |
| 325 | [ |
| 326 | '$unwind' => [ |
| 327 | 'path' => '$shortcuts_data', |
| 328 | 'preserveNullAndEmptyArrays' => true |
| 329 | ] |
| 330 | ], |
| 331 | [ |
| 332 | '$unwind' => [ |
| 333 | 'path' => '$subscription_data', |
| 334 | 'preserveNullAndEmptyArrays' => true |
| 335 | ] |
| 336 | ], |
| 337 | [ |
| 338 | '$project' => [ |
| 339 | 'user_id' => ['$toString' => '$_id'], |
| 340 | 'first_name' => 1, |
| 341 | 'last_name' => 1, |
| 342 | 'email' => 1, |
| 343 | |
| 344 | 'subscription_data' => 1, |
| 345 | |
| 346 | // Dados do Hubspot |
| 347 | 'subscription_type' => '$hubspot_data.subscription_type', |
| 348 | 'account_creation_date' => '$hubspot_data.account_creation_date', |
| 349 | 'last_login' => '$hubspot_data.last_login', |
| 350 | 'flymsg_chrome_extension_installed' => '$hubspot_data.flymsg_chrome_extension_installed', |
| 351 | 'flymsg_edge_extension_installed' => '$hubspot_data.flymsg_edge_extension_installed', |
| 352 | |
| 353 | // Dados de Flycut Usage Mensal |
| 354 | 'monthly_usage' => [ |
| 355 | '$map' => [ |
| 356 | 'input' => '$monthly_usage_data', |
| 357 | 'as' => 'usage', |
| 358 | 'in' => [ |
| 359 | 'month_year' => '$$usage._id.month_year', |
| 360 | 'characters_typed_by_flymsg' => '$$usage.characters_typed', |
| 361 | 'time_saved_by_flymsg' => '$$usage.time_saved', |
| 362 | 'cost_saved_by_flymsg' => '$$usage.cost_saved', |
| 363 | 'flypost_count' => '$$usage.flypost_count', |
| 364 | 'flyengage_count' => '$$usage.flyengage_count', |
| 365 | 'sentence_rewrite_count' => '$$usage.sentence_rewrite_count', |
| 366 | 'paragraph_rewrite_count' => '$$usage.paragraph_rewrite_count', |
| 367 | ] |
| 368 | ] |
| 369 | ], |
| 370 | |
| 371 | // Dados de Shortcuts |
| 372 | 'shortcuts_created' => '$shortcuts_data.shortcuts_created', |
| 373 | 'flyplates_created' => '$shortcuts_data.flyplates_created' |
| 374 | ] |
| 375 | ] |
| 376 | ]); |
| 377 | }); |
| 378 | |
| 379 | return $results; |
| 380 | } |
| 381 | } |