Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 66 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ReportingExtensionUsageRequest | |
0.00% |
0 / 66 |
|
0.00% |
0 / 2 |
182 | |
0.00% |
0 / 1 |
| execute | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
90 | |||
| _make_line_chart_data_extension | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Actions; |
| 4 | |
| 5 | use App\DTO\ReportingExtensionUsageRequestDTO; |
| 6 | use App\Http\Models\Auth\User; |
| 7 | use App\Http\Models\UserInfo; |
| 8 | use Illuminate\Support\Carbon; |
| 9 | use MongoDB\BSON\UTCDateTime; |
| 10 | |
| 11 | class ReportingExtensionUsageRequest |
| 12 | { |
| 13 | /** |
| 14 | * Execute the action. |
| 15 | * |
| 16 | * @return array |
| 17 | */ |
| 18 | public function execute(ReportingExtensionUsageRequestDTO $filter) |
| 19 | { |
| 20 | |
| 21 | $users = User::select([ |
| 22 | '_id', |
| 23 | 'first_name', |
| 24 | 'last_name', |
| 25 | 'company_group_id', |
| 26 | 'company_id', |
| 27 | 'created_at', |
| 28 | 'avatar', |
| 29 | ]); |
| 30 | |
| 31 | $user_ids = explode(',', $filter->user_ids); |
| 32 | $user_ids = array_filter($user_ids, function ($value) { |
| 33 | return $value !== ''; |
| 34 | }); |
| 35 | if (count($user_ids) > 0) { |
| 36 | $users = $users->whereIn('_id', $user_ids); |
| 37 | } |
| 38 | |
| 39 | $group_ids = explode(',', $filter->group_ids); |
| 40 | $group_ids = array_filter($group_ids, function ($value) { |
| 41 | return $value !== ''; |
| 42 | }); |
| 43 | if (count($group_ids) > 0) { |
| 44 | $users = $users->whereIn('company_group_id', $group_ids); |
| 45 | } |
| 46 | |
| 47 | $subgroup_ids = explode(',', $filter->subgroup_ids); |
| 48 | $subgroup_ids = array_filter($subgroup_ids, function ($value) { |
| 49 | return $value !== ''; |
| 50 | }); |
| 51 | if (count($subgroup_ids) > 0) { |
| 52 | $users = $users->whereIn('company_group_id', $subgroup_ids); |
| 53 | } |
| 54 | |
| 55 | $hasFilter = count($user_ids) || count($group_ids) || count($subgroup_ids); |
| 56 | |
| 57 | $fromDate = $filter->fromDate ? Carbon::parse($filter->fromDate) : Carbon::now()->subMonths(12); |
| 58 | $toDate = $filter->toDate ? Carbon::parse($filter->toDate) : Carbon::now(); |
| 59 | |
| 60 | $query = UserInfo::whereBetween('user_created_at', [ |
| 61 | new UTCDateTime($fromDate->getTimestamp() * 1000), |
| 62 | new UTCDateTime($toDate->getTimestamp() * 1000), |
| 63 | ]); |
| 64 | |
| 65 | $ids = $users->pluck('id')->toArray(); |
| 66 | if ($hasFilter) { |
| 67 | $query = $query->whereIn('user_id', $ids); |
| 68 | } |
| 69 | |
| 70 | $userInfo = $query->get(); |
| 71 | |
| 72 | $months = $toDate->diffInMonths($fromDate); |
| 73 | $chart = $this->_make_line_chart_data_extension($userInfo, $months, $fromDate); |
| 74 | |
| 75 | return [ |
| 76 | 'chart' => $chart, |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | private function _make_line_chart_data_extension($extensions_data, $months, $start_date) |
| 81 | { |
| 82 | $line_chart_data = []; |
| 83 | for ($i = 0; $i <= $months; $i++) { |
| 84 | $month_year = $start_date->copy()->addMonths($i)->format('M Y'); |
| 85 | $line_chart_data[$month_year] = [ |
| 86 | 'edge_installed' => 0, |
| 87 | 'chrome_installed' => 0, |
| 88 | 'uninstalls' => 0, |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | $aggregated_data = $extensions_data->groupBy(function ($data) { |
| 93 | return Carbon::parse($data->user_created_at)->format('M Y'); |
| 94 | }); |
| 95 | |
| 96 | foreach ($aggregated_data as $month_year => $usages) { |
| 97 | $edge_count = $usages->where('is_edge_extension_installed', true)->count(); |
| 98 | $chrome_count = $usages->where('is_chrome_extension_installed', true)->count(); |
| 99 | $uninstall_count = $usages->where('is_chrome_extension_uninstalled', true)->count() + $usages->where('is_edge_extension_uninstalled', true)->count(); |
| 100 | |
| 101 | if (isset($line_chart_data[$month_year])) { |
| 102 | $line_chart_data[$month_year]['edge_installed'] += $edge_count; |
| 103 | $line_chart_data[$month_year]['chrome_installed'] += $chrome_count; |
| 104 | $line_chart_data[$month_year]['uninstalls'] += $uninstall_count; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | uksort($line_chart_data, function ($a, $b) { |
| 109 | return strtotime($a) - strtotime($b); |
| 110 | }); |
| 111 | |
| 112 | return $line_chart_data; |
| 113 | } |
| 114 | } |