|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Service\StatsService; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
8
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
9
|
|
|
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface; |
|
10
|
|
|
use Symfony\UX\Chartjs\Model\Chart; |
|
11
|
|
|
|
|
12
|
|
|
class StatsController extends AbstractController |
|
13
|
|
|
{ |
|
14
|
|
|
const WANDER_NUMBER_COLOUR = '#2491B3'; |
|
15
|
|
|
const WANDER_DISTANCE_COLOUR = '#ffb266'; |
|
16
|
|
|
const IMAGES_NUMBER_COLOUR = '#BF5439'; |
|
17
|
|
|
/** |
|
18
|
|
|
* @Route("/stats", name="stats_index") |
|
19
|
|
|
*/ |
|
20
|
|
|
public function index( |
|
21
|
|
|
StatsService $statsService, |
|
22
|
|
|
ChartBuilderInterface $chartBuilder |
|
23
|
|
|
): Response { |
|
24
|
|
|
$wanderStats = $statsService->getWanderStats(); |
|
25
|
|
|
$imageStats = $statsService->getImageStats(); |
|
26
|
|
|
|
|
27
|
|
|
$wanderDataSeries = |
|
28
|
|
|
[ |
|
29
|
|
|
[ |
|
30
|
|
|
'label' => 'Number of Wanders', |
|
31
|
|
|
'colour' => self::WANDER_NUMBER_COLOUR, |
|
32
|
|
|
'extractFunction' => fn($dp): int => $dp['numberOfWanders'], |
|
33
|
|
|
], |
|
34
|
|
|
[ |
|
35
|
|
|
'label' => 'Distance Walked (km)', |
|
36
|
|
|
'colour' => self::WANDER_DISTANCE_COLOUR, |
|
37
|
|
|
'extractFunction' => fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2) |
|
38
|
|
|
] |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
$monthlyWanderChart = $chartBuilder |
|
42
|
|
|
->createChart(Chart::TYPE_BAR) |
|
43
|
|
|
->setData($this->generatePeriodicChartData($wanderStats['monthlyStats'], $wanderDataSeries)); |
|
44
|
|
|
|
|
45
|
|
|
$yearlyWanderChart = $chartBuilder |
|
46
|
|
|
->createChart(Chart::TYPE_BAR) |
|
47
|
|
|
->setData($this->generatePeriodicChartData($wanderStats['yearlyStats'], $wanderDataSeries)); |
|
48
|
|
|
|
|
49
|
|
|
$imageDataSeries = |
|
50
|
|
|
[ |
|
51
|
|
|
[ |
|
52
|
|
|
'label' => 'Photos Taken', |
|
53
|
|
|
'colour' => self::IMAGES_NUMBER_COLOUR, |
|
54
|
|
|
'extractFunction' => fn($dp): int => $dp['numberOfImages'], |
|
55
|
|
|
] |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
$monthlyImagesChart = $chartBuilder |
|
59
|
|
|
->createChart(Chart::TYPE_BAR) |
|
60
|
|
|
->setData($this->generatePeriodicChartData($wanderStats['monthlyStats'], $imageDataSeries)); |
|
61
|
|
|
|
|
62
|
|
|
$yearlyImagesChart = $chartBuilder |
|
63
|
|
|
->createChart(Chart::TYPE_BAR) |
|
64
|
|
|
->setData($this->generatePeriodicChartData($wanderStats['yearlyStats'], $imageDataSeries)); |
|
65
|
|
|
|
|
66
|
|
|
return $this->render('stats/index.html.twig', [ |
|
67
|
|
|
'controller_name' => 'StatsController', // TODO: Remove this boilerplate |
|
68
|
|
|
'imageStats' => $imageStats, |
|
69
|
|
|
'wanderStats' => $wanderStats, |
|
70
|
|
|
'monthlyWanderChart' => $monthlyWanderChart, |
|
71
|
|
|
'yearlyWanderChart' => $yearlyWanderChart, |
|
72
|
|
|
'monthlyImagesChart' => $monthlyImagesChart, |
|
73
|
|
|
'yearlyImagesChart' => $yearlyImagesChart |
|
74
|
|
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array<string, mixed> $sourceStats |
|
79
|
|
|
* @param array<int, array<string, mixed>> $seriesDefinitions |
|
80
|
|
|
* @return array<string, mixed> |
|
81
|
|
|
*/ |
|
82
|
|
|
private function generatePeriodicChartData( |
|
83
|
|
|
array $sourceStats, |
|
84
|
|
|
array $seriesDefinitions |
|
85
|
|
|
): array { |
|
86
|
|
|
$data = [ |
|
87
|
|
|
'labels' => array_map(fn($dp): string => $dp['periodLabel'], $sourceStats) |
|
88
|
|
|
]; |
|
89
|
|
|
foreach ($seriesDefinitions as $series) { |
|
90
|
|
|
$data['datasets'][] = [ |
|
91
|
|
|
'label' => $series['label'], |
|
92
|
|
|
'backgroundColor' => $series['colour'], |
|
93
|
|
|
'borderColor' => 'black', |
|
94
|
|
|
'borderWidth' => 1, |
|
95
|
|
|
'borderRadius' => 5, |
|
96
|
|
|
'data' => array_map($series['extractFunction'], $sourceStats) |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
return $data; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|