Overview stats

This commit is contained in:
Roman Hergenreder 2020-06-17 23:50:08 +02:00
parent 01b994601b
commit 94eb70c24e
12 changed files with 188 additions and 19 deletions

6
admin/dist/main.js vendored

File diff suppressed because one or more lines are too long

@ -53,4 +53,8 @@ export default class API {
async createUser(username, email, password, confirmPassword) {
return this.apiCall("user/create", { username: username, email: email, password: password, confirmPassword: confirmPassword });
}
async getStats() {
return this.apiCall("stats");
}
};

@ -81,7 +81,7 @@ class AdminDashboard extends React.Component {
<Route path={"/admin/dashboard"}><Overview {...this.controlObj} notifications={this.state.notifications} /></Route>
<Route exact={true} path={"/admin/users"}><UserOverview {...this.controlObj} /></Route>
<Route exact={true} path={"/admin/users/adduser"}><CreateUser {...this.controlObj} /></Route>
<Route path={"/admin/logs"}><Logs {...this.controlObj} notifications={this.state.notifications} /></Route>
<Route path={"/admin/logs"}><Logs {...this.controlObj} /></Route>
<Route path={"*"}><View404 /></Route>
</Switch>
<Dialog {...this.state.dialog}/>

@ -4,50 +4,86 @@ import Icon from "../elements/icon";
import { Bar } from 'react-chartjs-2';
import {Collapse} from 'react-collapse';
import moment from 'moment';
import Alert from "../elements/alert";
export default class Overview extends React.Component {
constructor(props) {
super(props);
this.parent = {
showDialog: props.showDialog,
notifications: props.notification,
api: props.api,
};
this.state = {
chartVisible : true,
userCount: 0,
notificationCount: 0,
visitors: { },
errors: []
}
}
fetchStats() {
removeError(i) {
if (i >= 0 && i < this.state.errors.length) {
let errors = this.state.errors.slice();
errors.splice(i, 1);
this.setState({...this.state, errors: errors});
}
}
componentDidMount() {
this.parent.api.getStats().then((res) => {
if(!res.success) {
let errors = this.state.errors.slice();
errors.push({ message: res.msg, title: "Error fetching Stats" });
this.setState({ ...this.state, errors: errors });
} else {
this.setState({
...this.state,
userCount: res.userCount,
pageCount: res.pageCount,
visitors: res.visitors,
});
}
});
}
render() {
let userCount = 0;
let notificationCount = 0;
let pageCount = 0;
let visitorCount = 0;
const colors = [
'#ff4444', '#ffbb33', '#00C851', '#33b5e5',
'#ff4444', '#ffbb33', '#00C851', '#33b5e5',
'#ff4444', '#ffbb33', '#00C851', '#33b5e5'
];
let data = new Array(12).fill(0);
let visitorCount = 0;
for (let date in this.state.visitors) {
let month = parseInt(date) % 100 - 1;
if (month >= 0 && month < 12) {
data[month] = this.state.visitors[date];
visitorCount += this.state.visitors[date];
}
}
let chartOptions = {};
let chartData = {
labels: moment.monthsShort(),
datasets: [{
label: 'Unique Visitors ' + moment().year(),
borderWidth: 1,
data: [ 10, 20, 30, 0, 15, 5, 40, 100, 6, 3, 10, 20 ],
data: data,
backgroundColor: colors,
}]
};
let errors = [];
for (let i = 0; i < this.state.errors.length; i++) {
errors.push(<Alert key={"error-" + i} onClose={() => this.removeError(i)} {...this.state.errors[i]}/>)
}
return <>
<div className={"content-header"}>
<div className={"container-fluid"}>
@ -66,11 +102,12 @@ export default class Overview extends React.Component {
</div>
<section className={"content"}>
<div className={"container-fluid"}>
{errors}
<div className={"row"}>
<div className={"col-lg-3 col-6"}>
<div className="small-box bg-info">
<div className={"inner"}>
<h3>{userCount}</h3>
<h3>{this.state.userCount}</h3>
<p>Users registered</p>
</div>
<div className="icon">
@ -82,7 +119,7 @@ export default class Overview extends React.Component {
<div className={"col-lg-3 col-6"}>
<div className={"small-box bg-success"}>
<div className={"inner"}>
<h3>{pageCount}</h3>
<h3>{this.state.pageCount}</h3>
<p>Routes & Pages</p>
</div>
<div className="icon">
@ -94,7 +131,7 @@ export default class Overview extends React.Component {
<div className={"col-lg-3 col-6"}>
<div className={"small-box bg-warning"}>
<div className={"inner"}>
<h3>{notificationCount}</h3>
<h3>{this.props.notifications.length}</h3>
<p>new Notifications</p>
</div>
<div className={"icon"}>

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;
}
}

@ -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();
}
}
}

@ -79,6 +79,7 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) {
$document = new $class($user);
}
$user->processVisit();
$response = $document->getCode();
}
}

6
js/admin.min.js vendored

File diff suppressed because one or more lines are too long