|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LoginCidadao\CoreBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use LoginCidadao\OAuthBundle\Entity\ClientRepository; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
7
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use LoginCidadao\BadgesControlBundle\Model\Badge; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
12
|
|
|
use JMS\Serializer\SerializationContext; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @Route("/statistics") |
|
16
|
|
|
*/ |
|
17
|
|
|
class StatisticsController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @Route("/", name="lc_statistics") |
|
22
|
|
|
* @Template() |
|
23
|
|
|
*/ |
|
24
|
|
|
public function indexAction(Request $request) |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @Route("/users/badges", name="lc_statistics_user_badge") |
|
31
|
|
|
* @Template() |
|
32
|
|
|
*/ |
|
33
|
|
|
public function usersByBadgesAction() |
|
34
|
|
|
{ |
|
35
|
|
|
$badgesHandler = $this->get('badges.handler'); |
|
36
|
|
|
|
|
37
|
|
|
$badges = $badgesHandler->getAvailableBadges(); |
|
38
|
|
|
foreach ($badges as $client => $badge) { |
|
39
|
|
|
foreach ($badge as $name => $desc) { |
|
40
|
|
|
$filterBadge = new Badge($client, $name); |
|
41
|
|
|
$count = $badgesHandler->countBearers($filterBadge); |
|
42
|
|
|
$b = array_shift($count); |
|
43
|
|
|
$data[$client][] = $b; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
48
|
|
|
$repo = $em->getRepository('LoginCidadaoCoreBundle:Person'); |
|
49
|
|
|
$totalUsers = $repo->getCountAll(); |
|
50
|
|
|
|
|
51
|
|
|
return array("data" => $data, "totalUsers" => $totalUsers['qty']); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @Route("/users/region/{type}", name="lc_statistics_user_region") |
|
56
|
|
|
* @Template() |
|
57
|
|
|
*/ |
|
58
|
|
|
public function usersByRegionAction($type) |
|
59
|
|
|
{ |
|
60
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
61
|
|
|
$repo = $em->getRepository('LoginCidadaoCoreBundle:Person'); |
|
62
|
|
|
if ($type == "country") { |
|
63
|
|
|
$data = $repo->getCountByCountry(); |
|
64
|
|
|
} else { |
|
65
|
|
|
$data = $repo->getCountByState(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$totalUsers = $repo->getCountAll(); |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return $this->render('LoginCidadaoCoreBundle:Statistics:usersByRegion.html.twig', |
|
72
|
|
|
array('data' => $data, 'totalUsers' => $totalUsers)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @Route("/users/city/{stateId}", name="lc_statistics_user_city") |
|
77
|
|
|
* @Template() |
|
78
|
|
|
*/ |
|
79
|
|
|
public function usersByCityAction($stateId) |
|
80
|
|
|
{ |
|
81
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
82
|
|
|
$repo = $em->getRepository('LoginCidadaoCoreBundle:Person'); |
|
83
|
|
|
$data = $repo->getCountByCity($stateId); |
|
84
|
|
|
|
|
85
|
|
|
return $this->render('LoginCidadaoCoreBundle:Statistics:usersByCity.html.twig', |
|
86
|
|
|
array('data' => $data)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @Route("/users/services", name="lc_statistics_user_services") |
|
91
|
|
|
* @Template() |
|
92
|
|
|
*/ |
|
93
|
|
|
public function usersByServicesAction(Request $request) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
/** @var ClientRepository $repo */ |
|
96
|
|
|
$repo = $this->getDoctrine() |
|
97
|
|
|
->getRepository('LoginCidadaoOAuthBundle:Client'); |
|
98
|
|
|
$totals = $repo->getCountPerson($this->getUser()); |
|
99
|
|
|
|
|
100
|
|
|
$keys = array(); |
|
101
|
|
|
foreach ($totals as $total) { |
|
102
|
|
|
$client = $total['client']; |
|
103
|
|
|
$keys[] = $client->getId(); |
|
104
|
|
|
} |
|
105
|
|
|
$evoData = $this->getStatsHandler()->getIndexed('agg.client.users', $keys, 30); |
|
106
|
|
|
|
|
107
|
|
|
$context = SerializationContext::create()->setGroups('date'); |
|
108
|
|
|
$serializer = $this->get('jms_serializer'); |
|
109
|
|
|
$evo = $serializer->serialize($evoData, 'json', $context); |
|
110
|
|
|
|
|
111
|
|
|
return compact('totals', 'evo'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @Route("/services/users-by-day.{_format}", |
|
116
|
|
|
* name="lc_statistics_service_users_day", |
|
117
|
|
|
* defaults={"_format": "html", "clientId": null} |
|
118
|
|
|
* ) |
|
119
|
|
|
* @Template() |
|
120
|
|
|
*/ |
|
121
|
|
|
public function usersByServiceByDayAction(Request $request, $clientId = null) |
|
122
|
|
|
{ |
|
123
|
|
|
$data = $this->getNewUsersByService(30, $clientId, |
|
124
|
|
|
$request->getRequestFormat()); |
|
125
|
|
|
|
|
126
|
|
|
if ($request->getRequestFormat() === 'json') { |
|
127
|
|
|
return new JsonResponse($data); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return compact('data'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @Route("/services/users-by-day-week.{_format}", |
|
135
|
|
|
* name="lc_statistics_service_users_day_week", |
|
136
|
|
|
* defaults={"_format": "html", "clientId": null} |
|
137
|
|
|
* ) |
|
138
|
|
|
* @Template() |
|
139
|
|
|
*/ |
|
140
|
|
|
public function usersByServiceByDayOfWeekAction(Request $request) |
|
141
|
|
|
{ |
|
142
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
143
|
|
|
$repo = $em->getRepository('LoginCidadaoCoreBundle:Authorization'); |
|
144
|
|
|
$rawData = $repo->statsUsersByServiceByDayOfWeek(); |
|
145
|
|
|
|
|
146
|
|
|
if ($request->getRequestFormat() === 'json') { |
|
147
|
|
|
return new JsonResponse($rawData); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$data = $rawData; |
|
151
|
|
|
|
|
152
|
|
|
return compact('data'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
private function getNewUsersByService($days, $clientId = null, |
|
156
|
|
|
$format = 'html') |
|
157
|
|
|
{ |
|
158
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
159
|
|
|
$repo = $em->getRepository('LoginCidadaoOAuthBundle:Client'); |
|
160
|
|
|
$rawData = $repo->statsUsersByServiceByDay($days, $clientId, |
|
161
|
|
|
$this->getUser()); |
|
162
|
|
|
$rawTotals = $repo->getCountPerson($this->getUser(), $clientId); |
|
163
|
|
|
|
|
164
|
|
|
if ($format === 'json') { |
|
|
|
|
|
|
165
|
|
|
//return $rawData; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$totals = array(); |
|
169
|
|
|
foreach ($rawTotals as $entry) { |
|
170
|
|
|
$client = $entry['client']; |
|
171
|
|
|
|
|
172
|
|
|
$totals[$client->getId()] = array('client' => $client, 'total' => $entry['qty']); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$data = array(); |
|
176
|
|
|
$total = array(); |
|
|
|
|
|
|
177
|
|
|
foreach ($rawData as $stat) { |
|
178
|
|
|
$id = $stat['client']; |
|
179
|
|
|
$client = $totals[$id]['client']; |
|
180
|
|
|
$total = $totals[$id]['total']; |
|
181
|
|
|
$count = $stat['users']; |
|
182
|
|
|
$day = $stat['day']; |
|
183
|
|
|
@$totalPeriod[$id] += $count; |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
$data[$id]['client'] = $client; |
|
186
|
|
|
$data[$id]['total_period'] = $totalPeriod[$id]; |
|
187
|
|
|
$data[$id]['total'] = $total; |
|
188
|
|
|
$data[$id]['days'] = $days; |
|
189
|
|
|
$data[$id]['data'][] = array($day, $count); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $data; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return \LoginCidadao\StatsBundle\Handler\StatsHandler |
|
197
|
|
|
*/ |
|
198
|
|
|
private function getStatsHandler() |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->get('statistics.handler'); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.