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