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 | * @param ReportingExtensionUsageRequestDTO $filter |
| 17 | * @return array |
| 18 | */ |
| 19 | public function execute(ReportingExtensionUsageRequestDTO $filter) |
| 20 | { |
| 21 | |
| 22 | $users = User::select([ |
| 23 | "_id", |
| 24 | "first_name", |
| 25 | "last_name", |
| 26 | "company_group_id", |
| 27 | "company_id", |
| 28 | "created_at", |
| 29 | "avatar" |
| 30 | ]); |
| 31 | |
| 32 | $user_ids = explode(",", $filter->user_ids); |
| 33 | $user_ids = array_filter($user_ids, function ($value) { |
| 34 | return $value !== ""; |
| 35 | }); |
| 36 | if (count($user_ids) > 0) { |
| 37 | $users = $users->whereIn("_id", $user_ids); |
| 38 | } |
| 39 | |
| 40 | $group_ids = explode(",", $filter->group_ids); |
| 41 | $group_ids = array_filter($group_ids, function ($value) { |
| 42 | return $value !== ""; |
| 43 | }); |
| 44 | if (count($group_ids) > 0) { |
| 45 | $users = $users->whereIn("company_group_id", $group_ids); |
| 46 | } |
| 47 | |
| 48 | $subgroup_ids = explode(",", $filter->subgroup_ids); |
| 49 | $subgroup_ids = array_filter($subgroup_ids, function ($value) { |
| 50 | return $value !== ""; |
| 51 | }); |
| 52 | if (count($subgroup_ids) > 0) { |
| 53 | $users = $users->whereIn("company_group_id", $subgroup_ids); |
| 54 | } |
| 55 | |
| 56 | $hasFilter = count($user_ids) || count($group_ids) || count($subgroup_ids); |
| 57 | |
| 58 | $fromDate = $filter->fromDate ? Carbon::parse($filter->fromDate) : Carbon::now()->subMonths(12); |
| 59 | $toDate = $filter->toDate ? Carbon::parse($filter->toDate) : Carbon::now(); |
| 60 | |
| 61 | $query = UserInfo::whereBetween("user_created_at", [ |
| 62 | new UTCDateTime($fromDate->getTimestamp() * 1000), |
| 63 | new UTCDateTime($toDate->getTimestamp() * 1000), |
| 64 | ]); |
| 65 | |
| 66 | $ids = $users->pluck("id")->toArray(); |
| 67 | if ($hasFilter) { |
| 68 | $query = $query->whereIn('user_id', $ids); |
| 69 | } |
| 70 | |
| 71 | $userInfo = $query->get(); |
| 72 | |
| 73 | $months = $toDate->diffInMonths($fromDate); |
| 74 | $chart = $this->_make_line_chart_data_extension($userInfo, $months, $fromDate); |
| 75 | |
| 76 | return [ |
| 77 | "chart" => $chart, |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | private function _make_line_chart_data_extension($extensions_data, $months, $start_date) |
| 82 | { |
| 83 | $line_chart_data = []; |
| 84 | for ($i = 0; $i <= $months; $i++) { |
| 85 | $month_year = $start_date->copy()->addMonths($i)->format('M Y'); |
| 86 | $line_chart_data[$month_year] = [ |
| 87 | 'edge_installed' => 0, |
| 88 | 'chrome_installed' => 0, |
| 89 | 'uninstalls' => 0, |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | $aggregated_data = $extensions_data->groupBy(function ($data) { |
| 94 | return Carbon::parse($data->user_created_at)->format('M Y'); |
| 95 | }); |
| 96 | |
| 97 | foreach ($aggregated_data as $month_year => $usages) { |
| 98 | $edge_count = $usages->where('is_edge_extension_installed', true)->count(); |
| 99 | $chrome_count = $usages->where('is_chrome_extension_installed', true)->count(); |
| 100 | $uninstall_count = $usages->where('is_chrome_extension_uninstalled', true)->count() + $usages->where('is_edge_extension_uninstalled', true)->count(); |
| 101 | |
| 102 | if (isset($line_chart_data[$month_year])) { |
| 103 | $line_chart_data[$month_year]['edge_installed'] += $edge_count; |
| 104 | $line_chart_data[$month_year]['chrome_installed'] += $chrome_count; |
| 105 | $line_chart_data[$month_year]['uninstalls'] += $uninstall_count; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | uksort($line_chart_data, function ($a, $b) { |
| 110 | return strtotime($a) - strtotime($b); |
| 111 | }); |
| 112 | |
| 113 | return $line_chart_data; |
| 114 | } |
| 115 | } |