web-base/core/Api/Stats.class.php

95 lines
2.6 KiB
PHP
Raw Normal View History

2020-06-17 23:50:08 +02:00
<?php
namespace Api;
use Driver\SQL\Condition\Compare;
2020-06-19 13:13:13 +02:00
use Driver\SQL\Condition\CondBool;
2020-06-17 23:50:08 +02:00
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();
2020-06-19 13:13:13 +02:00
$this->success = $this->success && ($res !== FALSE);
2020-06-17 23:50:08 +02:00
$this->lastError = $sql->getLastError();
return ($this->success ? $res[0]["count"] : 0);
}
private function getPageCount() {
2020-06-19 13:13:13 +02:00
$sql = $this->user->getSQL();
$res = $sql->select($sql->count())->from("Route")
->where(new CondBool("active"))
->execute();
$this->success = $this->success && ($res !== FALSE);
$this->lastError = $sql->getLastError();
return ($this->success ? $res[0]["count"] : 0);
2020-06-17 23:50:08 +02:00
}
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();
2020-06-19 13:13:13 +02:00
$this->success = $this->success && ($res !== FALSE);
2020-06-17 23:50:08 +02:00
$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;
2020-06-19 13:13:13 +02:00
$this->result["server"] = array(
"server" => $_SERVER["SERVER_SOFTWARE"] ?? "Unknown",
"memory_usage" => memory_get_usage(),
"load_avg" => sys_getloadavg(),
"database" => $this->user->getSQL()->getStatus(),
"mail" => $this->user->getConfiguration()->getMail() !== NULL
);
2020-06-17 23:50:08 +02:00
}
return $this->success;
}
}