Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.92% |
10 / 13 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| PlanAnalyticsService | |
76.92% |
10 / 13 |
|
66.67% |
2 / 3 |
3.11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| usersByPlan | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| usageByPlan | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services\Admin\SystemDashboard; |
| 4 | |
| 5 | use App\Http\Repositories\Admin\SystemDashboard\PlanAnalyticsRepository; |
| 6 | use Carbon\Carbon; |
| 7 | use Illuminate\Support\Facades\Cache; |
| 8 | |
| 9 | class PlanAnalyticsService |
| 10 | { |
| 11 | public function __construct( |
| 12 | private PlanAnalyticsRepository $repository |
| 13 | ) {} |
| 14 | |
| 15 | /** |
| 16 | * Return active user counts grouped by plan identifier. |
| 17 | * |
| 18 | * Cached for 300 s to reduce aggregation load. |
| 19 | * |
| 20 | * @return array<int, array{identifier: string, title: string, user_count: int}> |
| 21 | */ |
| 22 | public function usersByPlan(): array |
| 23 | { |
| 24 | return Cache::remember('system_dashboard:users_by_plan', 300, function () { |
| 25 | return $this->repository->usersByPlan(); |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Return average usage metrics per plan, bucketed by period. |
| 31 | * |
| 32 | * Cached for 600 s per unique (period, from, to) triplet. |
| 33 | * |
| 34 | * @param string $period daily|weekly|monthly |
| 35 | * @param Carbon $from |
| 36 | * @param Carbon $to |
| 37 | * @return array<int, array<string, mixed>> |
| 38 | */ |
| 39 | public function usageByPlan(string $period, Carbon $from, Carbon $to): array |
| 40 | { |
| 41 | $cacheKey = sprintf( |
| 42 | 'system_dashboard:usage_by_plan:%s:%d:%d', |
| 43 | $period, |
| 44 | $from->startOfDay()->timestamp, |
| 45 | $to->endOfDay()->timestamp |
| 46 | ); |
| 47 | |
| 48 | return Cache::remember($cacheKey, 600, function () use ($period, $from, $to) { |
| 49 | return $this->repository->usageByPlan($period, $from, $to); |
| 50 | }); |
| 51 | } |
| 52 | } |