Overview stats
This commit is contained in:
80
core/Api/Stats.class.php
Normal file
80
core/Api/Stats.class.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Api;
|
||||
|
||||
use Driver\SQL\Condition\Compare;
|
||||
|
||||
class Stats extends Request {
|
||||
|
||||
public function __construct($user, $externalCall = false) {
|
||||
parent::__construct($user, $externalCall, array());
|
||||
$this->csrfTokenRequired = true;
|
||||
$this->loginRequired = true;
|
||||
$this->requiredGroup = USER_GROUP_ADMIN;
|
||||
}
|
||||
|
||||
private function getUserCount() {
|
||||
$sql = $this->user->getSQL();
|
||||
$res = $sql->select($sql->count())->from("User")->execute();
|
||||
$this->success = ($res !== FALSE);
|
||||
$this->lastError = $sql->getLastError();
|
||||
|
||||
return ($this->success ? $res[0]["count"] : 0);
|
||||
}
|
||||
|
||||
private function getPageCount() {
|
||||
return 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 = ($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;
|
||||
}
|
||||
|
||||
public function execute($values = array()) {
|
||||
if(!parent::execute($values)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$userCount = $this->getUserCount();
|
||||
$pageCount = $this->getPageCount();
|
||||
$visitorStatistics = $this->getVisitorStatistics();
|
||||
|
||||
if ($this->success) {
|
||||
$this->result["userCount"] = $userCount;
|
||||
$this->result["pageCount"] = $pageCount;
|
||||
$this->result["visitors"] = $visitorStatistics;
|
||||
}
|
||||
|
||||
return $this->success;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -113,6 +113,12 @@ class CreateDatabase {
|
||||
->primaryKey("uid")
|
||||
->foreignKey("user_id", "User", "uid");
|
||||
|
||||
$queries[] = $sql->createTable("Visitor")
|
||||
->addInt("month")
|
||||
->addInt("count", false, 1)
|
||||
->addString("cookie", 26)
|
||||
->unique("month", "cookie");
|
||||
|
||||
return $queries;
|
||||
}
|
||||
}
|
||||
|
||||
13
core/Driver/SQL/Expression/Add.class.php
Normal file
13
core/Driver/SQL/Expression/Add.class.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Expression;
|
||||
|
||||
use Driver\SQL\Condition\Compare;
|
||||
|
||||
class Add extends Compare {
|
||||
|
||||
public function __construct($col, $val) {
|
||||
parent::__construct($col, $val, "+");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use \Driver\SQL\Column\DateTimeColumn;
|
||||
use Driver\SQL\Column\BoolColumn;
|
||||
use Driver\SQL\Column\JsonColumn;
|
||||
|
||||
use Driver\SQL\Expression\Add;
|
||||
use Driver\SQL\Strategy\Strategy;
|
||||
use \Driver\SQL\Strategy\UpdateStrategy;
|
||||
|
||||
@@ -172,8 +173,13 @@ class MySQL extends SQL {
|
||||
if ($value instanceof Column) {
|
||||
$columnName = $this->columnName($value->getName());
|
||||
$updateValues[] = "$leftColumn=$columnName";
|
||||
} else if($value instanceof Add) {
|
||||
$columnName = $this->columnName($value->getColumn());
|
||||
$operator = $value->getOperator();
|
||||
$value = $value->getValue();
|
||||
$updateValues[] = "$leftColumn=$columnName$operator" . $this->addValue($value, $params);
|
||||
} else {
|
||||
$updateValues[] = "`$leftColumn=" . $this->addValue($value, $params);
|
||||
$updateValues[] = "$leftColumn=" . $this->addValue($value, $params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -292,6 +292,12 @@ abstract class SQL {
|
||||
}
|
||||
}
|
||||
|
||||
public function sum($col) {
|
||||
$sumCol = strtolower(str_replace(".","_", $col)) . "_sum";
|
||||
$col = $this->columnName($col);
|
||||
return new Keyword("SUM($col) AS $sumCol");
|
||||
}
|
||||
|
||||
public function distinct($col) {
|
||||
$col = $this->columnName($col);
|
||||
return new Keyword("DISTINCT($col)");
|
||||
|
||||
@@ -4,6 +4,9 @@ namespace Objects;
|
||||
|
||||
use Api\SetLanguage;
|
||||
use Configuration\Configuration;
|
||||
use DateTime;
|
||||
use Driver\SQL\Expression\Add;
|
||||
use Driver\SQL\Strategy\UpdateStrategy;
|
||||
use Exception;
|
||||
use External\JWT;
|
||||
use Driver\SQL\SQL;
|
||||
@@ -118,6 +121,7 @@ class User extends ApiObject {
|
||||
}
|
||||
|
||||
$this->language->sendCookie();
|
||||
session_write_close();
|
||||
}
|
||||
|
||||
public function readData($userId, $sessionId, $sessionUpdate = true) {
|
||||
@@ -232,4 +236,16 @@ class User extends ApiObject {
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
public function processVisit() {
|
||||
if ($this->sql && isset($_COOKIE["PHPSESSID"]) && !empty($_COOKIE["PHPSESSID"])) {
|
||||
$cookie = $_COOKIE["PHPSESSID"];
|
||||
$month = (new DateTime())->format("Ym");
|
||||
|
||||
$this->sql->insert("Visitor", array("cookie", "month"))
|
||||
->addRow($cookie, $month)
|
||||
->onDuplicateKeyStrategy(new UpdateStrategy(array("count" => new Add("count", 1))))
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user