Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 65 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| StatisticsService | |
0.00% |
0 / 65 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| getDashboardStatistics | |
0.00% |
0 / 65 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services; |
| 4 | |
| 5 | use App\Http\Models\UserInfo; |
| 6 | use Illuminate\Support\Facades\DB; |
| 7 | |
| 8 | class StatisticsService |
| 9 | { |
| 10 | public function getDashboardStatistics() |
| 11 | { |
| 12 | $stats = UserInfo::where('deleted_at', null) |
| 13 | ->where('is_invite', false) |
| 14 | ->raw(function ($collection) { |
| 15 | return $collection->aggregate([ |
| 16 | [ |
| 17 | '$group' => [ |
| 18 | '_id' => null, |
| 19 | 'total_users' => ['$sum' => 1], |
| 20 | 'total_company_users' => [ |
| 21 | '$sum' => [ |
| 22 | '$cond' => [['$ifNull' => ['$company_id', false]], 1, 0], |
| 23 | ], |
| 24 | ], |
| 25 | 'total_flycuts_created' => ['$sum' => '$number_of_flycuts_created_count'], |
| 26 | 'total_flyplates_added' => ['$sum' => '$number_of_flyplates_in_flycuts_count'], |
| 27 | 'total_characters_typed' => ['$sum' => '$total___of_characters_typed_by_flymsg_by_user'], |
| 28 | 'total_cost_saved' => ['$sum' => '$total_cost_savings_by_flymsg_by_user'], |
| 29 | 'total_time_saved' => ['$sum' => '$total_time_saved_by_flymsg_by_user'], |
| 30 | 'total_flycuts_deployed' => ['$sum' => '$total___of_times_flycut_used__count_'], |
| 31 | 'total_flyengage_used' => ['$sum' => '$total___of_times_flyengage_used__count_'], |
| 32 | 'total_flyposts_used' => ['$sum' => '$total___of_times_flyposts_used__count_'], |
| 33 | 'total_paragraph_used' => ['$sum' => '$total___of_times_paragraph_rewrite_used__count_'], |
| 34 | 'total_flygrammar_used' => ['$sum' => '$total___of_times_flygrammar_is_used_count'], |
| 35 | ], |
| 36 | ], |
| 37 | ]); |
| 38 | })->first(); |
| 39 | |
| 40 | $usersByPlan = UserInfo::where('deleted_at', null) |
| 41 | ->where('is_invite', false) |
| 42 | ->whereNotNull('plan_id') |
| 43 | ->raw(function ($collection) { |
| 44 | return $collection->aggregate([ |
| 45 | [ |
| 46 | '$match' => [ |
| 47 | 'plan_id' => ['$ne' => null] |
| 48 | ] |
| 49 | ], |
| 50 | [ |
| 51 | '$group' => [ |
| 52 | '_id' => '$plan_name', |
| 53 | 'count' => ['$sum' => 1], |
| 54 | ], |
| 55 | ], |
| 56 | [ |
| 57 | '$project' => [ |
| 58 | 'plan_name' => '$_id', |
| 59 | 'count' => 1, |
| 60 | '_id' => 0, |
| 61 | ], |
| 62 | ], |
| 63 | [ |
| 64 | '$sort' => [ |
| 65 | 'count' => -1, |
| 66 | ], |
| 67 | ] |
| 68 | ]); |
| 69 | }); |
| 70 | |
| 71 | $totalUsersWithPlan = $usersByPlan->sum('count'); |
| 72 | |
| 73 | $usersByPlan->each(function ($plan) use ($totalUsersWithPlan) { |
| 74 | $plan->percentage = $totalUsersWithPlan > 0 ? ($plan->count / $totalUsersWithPlan) * 100 : 0; |
| 75 | }); |
| 76 | |
| 77 | return [ |
| 78 | 'stats' => (object) ($stats ? $stats->getAttributes() : []), |
| 79 | 'usersByPlan' => $usersByPlan, |
| 80 | ]; |
| 81 | } |
| 82 | } |