1+ <?php
2+
3+ namespace Shika \Controllers \Api ;
4+
5+ use Psr \Http \Message \ResponseInterface as Response ;
6+ use Psr \Http \Message \ServerRequestInterface as Request ;
7+ use Shika \Helpers \JsonResponse ;
8+ use Shika \Repositories \SiteRepository ;
9+ use Shika \Repositories \VisitRepository ;
10+ use Slim \Exception \HttpNotFoundException ;
11+
12+ class StatsApiController
13+ {
14+ use JsonResponse;
15+
16+ private SiteRepository $ sites ;
17+ private VisitRepository $ visits ;
18+
19+ public function __construct (SiteRepository $ sites , VisitRepository $ visits )
20+ {
21+ $ this ->sites = $ sites ;
22+ $ this ->visits = $ visits ;
23+ }
24+
25+ /**
26+ * Returns the top referrers on /sites/{id}/referrers
27+ */
28+ public function referrers (Request $ request , Response $ response , array $ args )
29+ {
30+ $ site = $ this ->sites ->findById ($ args ["id " ]);
31+
32+ if (!$ site )
33+ {
34+ throw new HttpNotFoundException ($ request );
35+ }
36+
37+ $ from = $ this ->getFromTime ($ request );
38+ $ referrers = $ this ->visits ->groupBy ("referrer_host " , "referrer " , $ from , $ site ->id );
39+
40+ return $ this ->json ($ response , $ referrers );
41+ }
42+
43+ /**
44+ * Returns the top referrers on /sites/{id}/pages
45+ */
46+ public function pages (Request $ request , Response $ response , array $ args )
47+ {
48+ $ site = $ this ->sites ->findById ($ args ["id " ]);
49+
50+ if (!$ site )
51+ {
52+ throw new HttpNotFoundException ($ request );
53+ }
54+
55+ $ from = $ this ->getFromTime ($ request );
56+ $ pages = $ this ->visits ->groupBy ("visit_path " , "path " , $ from , $ site ->id );
57+
58+ return $ this ->json ($ response , $ pages );
59+ }
60+
61+ /**
62+ * Returns the top browsers on /sites/{id}/browsers
63+ */
64+ public function browsers (Request $ request , Response $ response , array $ args )
65+ {
66+ $ site = $ this ->sites ->findById ($ args ["id " ]);
67+
68+ if (!$ site )
69+ {
70+ throw new HttpNotFoundException ($ request );
71+ }
72+
73+ $ from = $ this ->getFromTime ($ request );
74+ $ browsers = $ this ->visits ->groupBy ("browser " , "browser " , $ from , $ site ->id );
75+
76+ return $ this ->json ($ response , $ browsers );
77+ }
78+
79+ /**
80+ * Returns the top operating systems on /sites/{id}/operating-systems
81+ */
82+ public function systems (Request $ request , Response $ response , array $ args )
83+ {
84+ $ site = $ this ->sites ->findById ($ args ["id " ]);
85+
86+ if (!$ site )
87+ {
88+ throw new HttpNotFoundException ($ request );
89+ }
90+
91+ $ from = $ this ->getFromTime ($ request );
92+ $ systems = $ this ->visits ->groupBy ("operating_system " , "operating_system " , $ from , $ site ->id );
93+
94+ return $ this ->json ($ response , $ systems );
95+ }
96+
97+ /**
98+ * Returns the top referrers on /sites/{id}/device-types
99+ */
100+ public function devices (Request $ request , Response $ response , array $ args )
101+ {
102+ $ site = $ this ->sites ->findById ($ args ["id " ]);
103+
104+ if (!$ site )
105+ {
106+ throw new HttpNotFoundException ($ request );
107+ }
108+
109+ $ from = $ this ->getFromTime ($ request );
110+ $ devices = $ this ->visits ->groupBy ("device_type " , "device_type " , $ from , $ site ->id );
111+
112+ return $ this ->json ($ response , $ devices );
113+ }
114+
115+ /**
116+ * Returns the top referrers on /sites/{id}/countries
117+ */
118+ public function countries (Request $ request , Response $ response , array $ args )
119+ {
120+ $ site = $ this ->sites ->findById ($ args ["id " ]);
121+
122+ if (!$ site )
123+ {
124+ throw new HttpNotFoundException ($ request );
125+ }
126+
127+ $ from = $ this ->getFromTime ($ request );
128+ $ countries = $ this ->visits ->groupBy ("country_code " , "country " , $ from , $ site ->id );
129+
130+ return $ this ->json ($ response , $ countries );
131+ }
132+
133+ private function getFromTime (Request $ request )
134+ {
135+ $ params = $ request ->getQueryParams ();
136+
137+ if (!isset ($ params ["from " ]) || intval ($ params ["from " ]) == 0 )
138+ {
139+ // default to last 7 days
140+ return time () - 604800 ;
141+ }
142+
143+ return intval ($ params ["from " ]);
144+ }
145+ }
0 commit comments