Skip to content

Commit d42a2e6

Browse files
committed
Add endpoints for all stats
1 parent 36ddb00 commit d42a2e6

File tree

4 files changed

+156
-68
lines changed

4 files changed

+156
-68
lines changed

routes/api.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Shika\Controllers\Api\SiteApiController;
4+
use Shika\Controllers\Api\StatsApiController;
45
use Shika\Controllers\Api\UserApiController;
56
use Shika\Middleware\ApiMiddleware;
67
use Slim\App;
@@ -13,8 +14,13 @@
1314

1415
$group->get("/sites", [SiteApiController::class, "sites"]);
1516
$group->get("/sites/{id}", [SiteApiController::class, "site"]);
16-
$group->get("/sites/{id}/referrers", [SiteApiController::class, "referrers"]);
17-
$group->get("/sites/{id}/pages", [SiteApiController::class, "pages"]);
17+
18+
$group->get("/sites/{id}/referrers", [StatsApiController::class, "referrers"]);
19+
$group->get("/sites/{id}/pages", [StatsApiController::class, "pages"]);
20+
$group->get("/sites/{id}/browsers", [StatsApiController::class, "browsers"]);
21+
$group->get("/sites/{id}/operating-systems", [StatsApiController::class, "systems"]);
22+
$group->get("/sites/{id}/device-types", [StatsApiController::class, "devices"]);
23+
$group->get("/sites/{id}/countries", [StatsApiController::class, "countries"]);
1824
})
1925
->add(ApiMiddleware::class);
2026
};

src/Controllers/Api/SiteApiController.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -47,45 +47,4 @@ public function site(Request $request, Response $response, array $args)
4747

4848
return $this->json($response, $site);
4949
}
50-
51-
public function referrers(Request $request, Response $response, array $args)
52-
{
53-
$site = $this->sites->findById($args["id"]);
54-
55-
if (!$site)
56-
{
57-
throw new HttpNotFoundException($request);
58-
}
59-
60-
$from = $this->getFromTime($request);
61-
62-
return $this->json($response, $this->visits->getReferrers($from, $site->id));
63-
}
64-
65-
public function pages(Request $request, Response $response, array $args)
66-
{
67-
$site = $this->sites->findById($args["id"]);
68-
69-
if (!$site)
70-
{
71-
throw new HttpNotFoundException($request);
72-
}
73-
74-
$from = $this->getFromTime($request);
75-
76-
return $this->json($response, $this->visits->getPages($from, $site->id));
77-
}
78-
79-
private function getFromTime(Request $request)
80-
{
81-
$params = $request->getQueryParams();
82-
83-
if (!isset($params["from"]) || intval($params["from"]) == 0)
84-
{
85-
// default to last 7 days
86-
return time() - 604800;
87-
}
88-
89-
return intval($params["from"]);
90-
}
9150
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

src/Repositories/VisitRepository.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function getTotalVisits(int $site)
2323
return $this->database->getColumn("SELECT COUNT(*) FROM `visits` WHERE `site_id` = ?", $site);
2424
}
2525

26-
public function getReferrers(int $from = 0, int $site = 0)
26+
public function groupBy(string $field, string $name, int $from = 0, int $site = 0)
2727
{
28-
$query = "SELECT `referrer_host` AS `referrer`, count(*) as `count` FROM `visits` WHERE `referrer_host` IS NOT NULL";
28+
$query = "SELECT `$field` AS `$name`, count(*) as `count` FROM `visits` WHERE `$field` IS NOT NULL";
2929
$params = [];
3030

3131
if ($site > 0)
@@ -40,29 +40,7 @@ public function getReferrers(int $from = 0, int $site = 0)
4040
array_push($params, gmdate("Y-m-d H:i:s", $from));
4141
}
4242

43-
$query .= " GROUP BY `referrer_host` ORDER BY `count` DESC";
44-
45-
return $this->database->getAll($query, ...$params);
46-
}
47-
48-
public function getPages(int $from = 0, int $site = 0)
49-
{
50-
$query = "SELECT `visit_path` AS `path`, count(*) as `count` FROM `visits` WHERE `visit_path` IS NOT NULL";
51-
$params = [];
52-
53-
if ($site > 0)
54-
{
55-
$query .= " AND `site_id` = ?";
56-
array_push($params, $site);
57-
}
58-
59-
if ($from > 0)
60-
{
61-
$query .= " AND `visit_at` > ?";
62-
array_push($params, gmdate("Y-m-d H:i:s", $from));
63-
}
64-
65-
$query .= " GROUP BY `visit_path` ORDER BY `count` DESC";
43+
$query .= " GROUP BY `$field` ORDER BY `count` DESC";
6644

6745
return $this->database->getAll($query, ...$params);
6846
}

0 commit comments

Comments
 (0)