Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
ReportingFlypostAIRequest | |
0.00% |
0 / 50 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
execute | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
20 | |||
_make_line_chart_data_for_counts | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace App\Actions; |
4 | |
5 | use App\DTO\ReportingFlypostAIRequestDTO; |
6 | use App\Http\Models\Auth\User; |
7 | use App\Http\Models\FlyMsgAI\FlyMsgAITracking; |
8 | use Carbon\Carbon; |
9 | use MongoDB\BSON\UTCDateTime; |
10 | |
11 | class ReportingFlypostAIRequest |
12 | { |
13 | /** |
14 | * Execute the action to get Flypost AI data. |
15 | * |
16 | * @param ReportingFlypostAIRequestDTO $filter |
17 | * @return array |
18 | */ |
19 | public function execute(ReportingFlypostAIRequestDTO $filter) |
20 | { |
21 | $start_date = $filter->fromDate; |
22 | $end_date = $filter->toDate; |
23 | |
24 | // Parse dates or set defaults |
25 | if ($start_date && $end_date) { |
26 | $start_date = Carbon::parse($start_date); |
27 | $end_date = Carbon::parse($end_date); |
28 | } else { |
29 | $start_date = Carbon::now()->subMonths(12)->startOfMonth(); |
30 | $end_date = Carbon::now()->endOfMonth(); |
31 | } |
32 | |
33 | // Convert start_date and end_date to UTCDateTime once to reuse |
34 | $start = new UTCDateTime($start_date->getTimestamp() * 1000); |
35 | $end = new UTCDateTime($end_date->getTimestamp() * 1000); |
36 | |
37 | // Fetch all users |
38 | $users = User::select([ |
39 | "id", |
40 | "first_name", |
41 | "last_name", |
42 | "company_group_id", |
43 | "company_id", |
44 | "created_at", |
45 | "avatar" |
46 | ])->get(); |
47 | |
48 | $ids = $users->pluck("id")->toArray(); |
49 | |
50 | // Fetch FlyMsgAITracking records within the date range using the previously fetched IDs |
51 | $flypost_ai = FlyMsgAITracking::whereIn("user_id", $ids) |
52 | ->whereBetween("created_at", [$start, $end]) |
53 | ->where("feature", "=", "flypost") |
54 | ->get(); |
55 | |
56 | $total_users = $users->count(); |
57 | $months = $end_date->diffInMonths($start_date); |
58 | $chart = $this->_make_line_chart_data_for_counts($flypost_ai, $months); |
59 | $total_flypost_ai_created = $flypost_ai->count(); |
60 | $average_flypost_ai_per_active_user = $total_users > 0 ? $total_flypost_ai_created / $total_users : 0; |
61 | |
62 | return [ |
63 | "chart" => $chart, |
64 | "total_flypost_ai_created" => number_format($total_flypost_ai_created, 0, ".", ","), |
65 | "average_flypost_ai_per_active_user" => number_format(round($average_flypost_ai_per_active_user, 2), 2, ".", ","), |
66 | ]; |
67 | } |
68 | |
69 | |
70 | private function _make_line_chart_data_for_counts($flycuts, $months) |
71 | { |
72 | // Initialize an array with 12 months set to 0 |
73 | $line_chart_data = []; |
74 | $current_date = Carbon::now(); |
75 | for ($i = 0; $i < $months; $i++) { |
76 | $month_year = $current_date->subMonth()->format('M Y'); |
77 | $line_chart_data[$month_year] = ['count' => 0]; |
78 | } |
79 | |
80 | // Reset the current date to now |
81 | $current_date = Carbon::now(); |
82 | |
83 | // Aggregate the data by month |
84 | $aggregated_data = $flycuts->groupBy(function ($date) { |
85 | return Carbon::parse($date->created_at)->format('Y-m'); |
86 | }); |
87 | |
88 | // Process the aggregated data and populate the line chart data array |
89 | foreach ($aggregated_data as $month_year => $usages) { |
90 | $month = Carbon::parse($usages->first()->created_at)->format('M Y'); |
91 | $flycuts_used = $usages->count(); |
92 | $line_chart_data[$month] = ['count' => $flycuts_used]; |
93 | } |
94 | |
95 | // Ensure the data is sorted by date keys |
96 | uksort($line_chart_data, function ($a, $b) { |
97 | return strtotime($b) - strtotime($a); |
98 | }); |
99 | |
100 | return $line_chart_data; |
101 | } |
102 | } |