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

91 lines
2.5 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 {
2020-06-27 22:47:12 +02:00
private bool $mailConfigured;
private bool $recaptchaConfigured;
2020-06-17 23:50:08 +02:00
public function __construct($user, $externalCall = false) {
parent::__construct($user, $externalCall, array());
}
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
}
2020-06-27 22:47:12 +02:00
private function checkSettings() {
2020-06-25 16:54:58 +02:00
$req = new \Api\Settings\Get($this->user);
2020-06-27 22:47:12 +02:00
$this->success = $req->execute(array("key" => "^(mail_enabled|recaptcha_enabled)$"));
$this->lastError = $req->getLastError();
2020-06-25 16:54:58 +02:00
if ($this->success) {
2020-06-27 22:47:12 +02:00
$settings = $req->getResult()["settings"];
$this->mailConfigured = ($settings["mail_enabled"] ?? "0") === "1";
$this->recaptchaConfigured = ($settings["recaptcha_enabled"] ?? "0") === "1";
2020-06-25 16:54:58 +02:00
}
return $this->success;
}
2020-06-17 23:50:08 +02:00
public function execute($values = array()) {
if(!parent::execute($values)) {
return false;
}
$userCount = $this->getUserCount();
$pageCount = $this->getPageCount();
2020-07-01 21:10:25 +02:00
$req = new \Api\Visitors\Stats($this->user);
$this->success = $req->execute(array("type"=>"monthly"));
$this->lastError = $req->getLastError();
2020-06-25 16:54:58 +02:00
if (!$this->success) {
return false;
}
2020-06-17 23:50:08 +02:00
2020-07-01 21:10:25 +02:00
$visitorStatistics = $req->getResult()["visitors"];
2020-06-24 16:13:54 +02:00
$loadAvg = "Unknown";
if (function_exists("sys_getloadavg")) {
$loadAvg = sys_getloadavg();
}
2020-06-27 22:47:12 +02:00
if (!$this->checkSettings()) {
2020-06-25 16:54:58 +02:00
return false;
2020-06-17 23:50:08 +02:00
}
2020-06-25 16:54:58 +02:00
$this->result["userCount"] = $userCount;
$this->result["pageCount"] = $pageCount;
$this->result["visitors"] = $visitorStatistics;
$this->result["server"] = array(
"version" => WEBBASE_VERSION,
"server" => $_SERVER["SERVER_SOFTWARE"] ?? "Unknown",
"memory_usage" => memory_get_usage(),
"load_avg" => $loadAvg,
"database" => $this->user->getSQL()->getStatus(),
2020-06-27 22:47:12 +02:00
"mail" => $this->mailConfigured,
"reCaptcha" => $this->recaptchaConfigured
2020-06-25 16:54:58 +02:00
);
2020-06-17 23:50:08 +02:00
return $this->success;
}
}