Visitor Statistics
This commit is contained in:
@@ -4,7 +4,7 @@ namespace Api {
|
||||
|
||||
use Driver\SQL\Condition\Compare;
|
||||
|
||||
class ApiKeyAPI extends Request {
|
||||
abstract class ApiKeyAPI extends Request {
|
||||
|
||||
protected function apiKeyExists($id) {
|
||||
$sql = $this->user->getSQL();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Api {
|
||||
class ContactAPI extends Request {
|
||||
abstract class ContactAPI extends Request {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Api {
|
||||
|
||||
use Driver\SQL\Condition\Compare;
|
||||
|
||||
class GroupsAPI extends Request {
|
||||
abstract class GroupsAPI extends Request {
|
||||
|
||||
protected function groupExists($name) {
|
||||
$sql = $this->user->getSQL();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Api {
|
||||
|
||||
class LanguageAPI extends Request {
|
||||
abstract class LanguageAPI extends Request {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Api {
|
||||
class MailAPI extends Request {
|
||||
abstract class MailAPI extends Request {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Api {
|
||||
class NotificationsAPI extends Request {
|
||||
abstract class NotificationsAPI extends Request {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Api {
|
||||
|
||||
class PermissionAPI extends Request {
|
||||
abstract class PermissionAPI extends Request {
|
||||
protected function checkStaticPermission() {
|
||||
if (!$this->user->isLoggedIn() || !$this->user->hasGroup(USER_GROUP_ADMIN)) {
|
||||
return $this->createError("Permission denied.");
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Api {
|
||||
|
||||
class SettingsAPI extends Request {
|
||||
abstract class SettingsAPI extends Request {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -34,39 +34,6 @@ class Stats extends Request {
|
||||
return ($this->success ? $res[0]["count"] : 0);
|
||||
}
|
||||
|
||||
private function getVisitorStatistics() {
|
||||
|
||||
$currentYear = getYear();
|
||||
$firstMonth = $currentYear * 100 + 01;
|
||||
$latsMonth = $currentYear * 100 + 12;
|
||||
|
||||
$sql = $this->user->getSQL();
|
||||
$res = $sql->select($sql->count(), "month")
|
||||
->from("Visitor")
|
||||
->where(new Compare("month", $firstMonth, ">="))
|
||||
->where(new Compare("month", $latsMonth, "<="))
|
||||
->where(new Compare("count", 1, ">"))
|
||||
->groupBy("month")
|
||||
->orderBy("month")
|
||||
->ascending()
|
||||
->execute();
|
||||
|
||||
$this->success = $this->success && ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
|
||||
$visitors = array();
|
||||
|
||||
if ($this->success) {
|
||||
foreach($res as $row) {
|
||||
$month = $row["month"];
|
||||
$count = $row["count"];
|
||||
$visitors[$month] = $count;
|
||||
}
|
||||
}
|
||||
|
||||
return $visitors;
|
||||
}
|
||||
|
||||
private function checkSettings() {
|
||||
$req = new \Api\Settings\Get($this->user);
|
||||
$this->success = $req->execute(array("key" => "^(mail_enabled|recaptcha_enabled)$"));
|
||||
@@ -88,11 +55,14 @@ class Stats extends Request {
|
||||
|
||||
$userCount = $this->getUserCount();
|
||||
$pageCount = $this->getPageCount();
|
||||
$visitorStatistics = $this->getVisitorStatistics();
|
||||
$req = new \Api\Visitors\Stats($this->user);
|
||||
$this->success = $req->execute(array("type"=>"monthly"));
|
||||
$this->lastError = $req->getLastError();
|
||||
if (!$this->success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$visitorStatistics = $req->getResult()["visitors"];
|
||||
$loadAvg = "Unknown";
|
||||
if (function_exists("sys_getloadavg")) {
|
||||
$loadAvg = sys_getloadavg();
|
||||
|
||||
88
core/Api/VisitorsAPI.class.php
Normal file
88
core/Api/VisitorsAPI.class.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Api {
|
||||
|
||||
abstract class VisitorsAPI extends Request {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace Api\Visitors {
|
||||
|
||||
use Api\Parameter\Parameter;
|
||||
use Api\Parameter\StringType;
|
||||
use Api\VisitorsAPI;
|
||||
use DateTime;
|
||||
use Driver\SQL\Condition\Compare;
|
||||
use Driver\SQL\Query\Select;
|
||||
use Objects\User;
|
||||
|
||||
class Stats extends VisitorsAPI {
|
||||
public function __construct(User $user, bool $externalCall = false) {
|
||||
parent::__construct($user, $externalCall, array(
|
||||
'type' => new StringType('type', 32),
|
||||
'date' => new Parameter('date', Parameter::TYPE_DATE, true, new DateTime())
|
||||
));
|
||||
}
|
||||
|
||||
private function setConditions(string $type, DateTime $date, Select $query) {
|
||||
if ($type === "yearly") {
|
||||
$yearStart = $date->format("Y0000");
|
||||
$yearEnd = $date->modify("+1 year")->format("Y0000");
|
||||
$query->where(new Compare("day", $yearStart, ">="));
|
||||
$query->where(new Compare("day", $yearEnd, "<"));
|
||||
} else if($type === "monthly") {
|
||||
$monthStart = $date->format("Ym00");
|
||||
$monthEnd = $date->modify("+1 month")->format("Ym00");
|
||||
$query->where(new Compare("day", $monthStart, ">="));
|
||||
$query->where(new Compare("day", $monthEnd, "<"));
|
||||
} else if($type === "weekly") {
|
||||
$weekStart = ($date->modify("monday this week"))->format("Ymd");
|
||||
$weekEnd = ($date->modify("sunday this week"))->format("Ymd");
|
||||
$query->where(new Compare("day", $weekStart, ">="));
|
||||
$query->where(new Compare("day", $weekEnd, "<="));
|
||||
} else {
|
||||
$this->createError("Invalid scope: $type");
|
||||
}
|
||||
}
|
||||
|
||||
public function execute($values = array()) {
|
||||
if (!parent::execute($values)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$date = $this->getParam("date");
|
||||
$type = $this->getParam("type");
|
||||
|
||||
$sql = $this->user->getSQL();
|
||||
$query = $sql->select($sql->count(), "day")
|
||||
->from("Visitor")
|
||||
->where(new Compare("count", 1, ">"))
|
||||
->groupBy("day")
|
||||
->orderBy("day")
|
||||
->ascending();
|
||||
|
||||
$this->setConditions($type, $date, $query);
|
||||
if (!$this->success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$res = $query->execute();
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
|
||||
if ($this->success) {
|
||||
$this->result["type"] = $type;
|
||||
$this->result["visitors"] = array();
|
||||
|
||||
foreach($res as $row) {
|
||||
$day = DateTime::createFromFormat("Ymd", $row["day"])->format("Y/m/d");
|
||||
$count = $row["count"];
|
||||
$this->result["visitors"][$day] = $count;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,10 +114,10 @@ class CreateDatabase {
|
||||
->foreignKey("user_id", "User", "uid");
|
||||
|
||||
$queries[] = $sql->createTable("Visitor")
|
||||
->addInt("month")
|
||||
->addInt("day")
|
||||
->addInt("count", false, 1)
|
||||
->addString("cookie", 26)
|
||||
->unique("month", "cookie");
|
||||
->unique("day", "cookie");
|
||||
|
||||
$queries[] = $sql->createTable("Route")
|
||||
->addSerial("uid")
|
||||
@@ -191,7 +191,8 @@ class CreateDatabase {
|
||||
->addRow("User/invite", array(USER_GROUP_ADMIN), "Allows users to create a new user and send them an invitation link")
|
||||
->addRow("User/edit", array(USER_GROUP_ADMIN), "Allows users to edit details and group memberships of any user")
|
||||
->addRow("User/delete", array(USER_GROUP_ADMIN), "Allows users to delete any other user")
|
||||
->addRow("Permission/fetch", array(USER_GROUP_ADMIN), "Allows users to list all API permissions");
|
||||
->addRow("Permission/fetch", array(USER_GROUP_ADMIN), "Allows users to list all API permissions")
|
||||
->addRow("Visitors/stats", array(USER_GROUP_ADMIN, USER_GROUP_SUPPORT), "Allowes users to see visitor statistics");
|
||||
|
||||
return $queries;
|
||||
}
|
||||
|
||||
@@ -260,10 +260,10 @@ class User extends ApiObject {
|
||||
}
|
||||
|
||||
$cookie = $_COOKIE["PHPSESSID"];
|
||||
$month = (new DateTime())->format("Ym");
|
||||
$day = (new DateTime())->format("Ymd");
|
||||
|
||||
$this->sql->insert("Visitor", array("cookie", "month"))
|
||||
->addRow($cookie, $month)
|
||||
$this->sql->insert("Visitor", array("cookie", "day"))
|
||||
->addRow($cookie, $day)
|
||||
->onDuplicateKeyStrategy(new UpdateStrategy(
|
||||
array("month", "cookie"),
|
||||
array("count" => new Add("Visitor.count", 1))))
|
||||
|
||||
Reference in New Issue
Block a user