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