add simple stat rendering

This commit is contained in:
Dennis Eichhorn 2023-09-17 17:06:53 +00:00
parent a20fd274e4
commit 3b71f10926
11 changed files with 360 additions and 12 deletions

View File

@ -48,11 +48,26 @@
"pid": "/admin/monitoring",
"type": 3,
"subtype": 1,
"name": "Stats",
"uri": "{/base}/admin/monitoring/stats",
"target": "self",
"icon": null,
"order": 10,
"from": "Monitoring",
"permission": { "permission": 2, "category": null, "element": null },
"parent": 1000706001,
"children": []
},
{
"id": 1000706104,
"pid": "/admin/monitoring",
"type": 3,
"subtype": 1,
"name": "Security",
"uri": "{/base}/admin/monitoring/security/dashboard?{?}",
"target": "self",
"icon": null,
"order": 5,
"order": 15,
"from": "Monitoring",
"permission": { "permission": 2, "category": null, "element": null },
"parent": 1000706001,

View File

@ -87,11 +87,6 @@
"foreignTable": "country",
"foreignKey": "country_code2"
},
"monitoring_request_browser": {
"name": "monitoring_request_browser",
"type": "VARCHAR(255)",
"null": false
},
"monitoring_request_path": {
"name": "monitoring_request_path",
"type": "VARCHAR(255)",
@ -114,7 +109,7 @@
},
"monitoring_request_datetime": {
"name": "monitoring_request_datetime",
"type": "INT",
"type": "DATETIME",
"null": false
}
}

View File

@ -29,6 +29,17 @@ return [
],
],
],
'^.*/admin/monitoring/stats.*$' => [
[
'dest' => '\Modules\Monitoring\Controller\BackendController:viewStats',
'verb' => RouteVerb::GET,
'permission' => [
'module' => BackendController::NAME,
'type' => PermissionType::READ,
'state' => PermissionCategory::STATS,
],
],
],
'^.*/admin/monitoring/log/list.*$' => [
[
'dest' => '\Modules\Monitoring\Controller\BackendController:viewMonitoringLogList',

View File

@ -14,7 +14,10 @@ declare(strict_types=1);
namespace Modules\Monitoring\Controller;
use Modules\Monitoring\Models\ImpressionStatMapper;
use phpOMS\Asset\AssetType;
use phpOMS\Contract\RenderableInterface;
use phpOMS\DataStorage\Database\Query\Builder;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
use phpOMS\Views\View;
@ -53,6 +56,91 @@ final class BackendController extends Controller
return $view;
}
/**
* Routing end-point for application behaviour.
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param mixed $data Generic data
*
* @return RenderableInterface
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public function viewStats(RequestAbstract $request, ResponseAbstract $response, mixed $data = null) : RenderableInterface
{
$view = new View($this->app->l11nManager, $request, $response);
$view->setTemplate('/Modules/Monitoring/Theme/Backend/monitoring-stats');
$view->data['nav'] = $this->app->moduleManager->get('Navigation')->createNavigationMid(1000706001, $request, $response);
$head = $response->data['Content']->head;
$head->addAsset(AssetType::CSS, 'Resources/chartjs/Chartjs/chart.css');
$head->addAsset(AssetType::JSLATE, 'Resources/chartjs/Chartjs/chart.js');
$head->addAsset(AssetType::JSLATE, 'Modules/ItemManagement/Controller.js', ['type' => 'module']);
$view->data['stats'] = [];
$query = new Builder($this->app->dbPool->get());
$query->raw(
'SELECT DATE(monitoring_request_datetime) as date, COUNT(*)
FROM monitoring_request
GROUP BY date(monitoring_request_datetime)
ORDER BY date ASC;'
);
$view->data['stats']['impressions'] = $query->execute()->fetchAll(\PDO::FETCH_COLUMN|\PDO::FETCH_GROUP);
$query = new Builder($this->app->dbPool->get());
$query->raw(
'SELECT DATE(monitoring_request_datetime) as date, monitoring_request_country as country, COUNT(*) as count
FROM monitoring_request
GROUP BY date(monitoring_request_datetime), monitoring_request_country
ORDER BY date ASC;'
);
$view->data['stats']['country'] = [];
$temp = $query->execute()->fetchAll();
foreach ($temp as $t) {
if (!isset($view->data['stats']['country'][$t['country']])) {
$view->data['stats']['country'][$t['country']] = [];
}
$view->data['stats']['country'][$t['country']][$t['date']] = $t['count'];
}
$query = new Builder($this->app->dbPool->get());
$query->raw(
'SELECT monitoring_request_agent as agent, COUNT(*)
FROM monitoring_request
GROUP BY monitoring_request_agent;'
);
$view->data['stats']['browser'] = $query->execute()->fetchAll(\PDO::FETCH_COLUMN|\PDO::FETCH_GROUP);
$query = new Builder($this->app->dbPool->get());
$query->raw(
'SELECT DATE(monitoring_request_datetime) as date, monitoring_request_host as host, COUNT(*) as count
FROM monitoring_request
GROUP BY date(monitoring_request_datetime), monitoring_request_host
ORDER BY date ASC;'
);
$view->data['stats']['domain'] = [];
$temp = $query->execute()->fetchAll();
foreach ($temp as $t) {
if (!isset($view->data['stats']['domain'][$t['host']])) {
$view->data['stats']['domain'][$t['host']] = [];
}
$view->data['stats']['domain'][$t['host']][$t['date']] = $t['count'];
}
return $view;
}
/**
* Routing end-point for application behaviour.
*

View File

@ -14,9 +14,9 @@ declare(strict_types=1);
namespace Modules\Monitoring\Controller;
use Modules\Monitoring\Models\ImpressionStat;
use Modules\Monitoring\Models\ImpressionStatMapper;
use phpOMS\Message\Http\HttpRequest;
use phpOMS\Message\Statistic\ImpressionStat;
use phpOMS\Module\ModuleAbstract;
/**

30
Models/ImpressionStat.php Normal file
View File

@ -0,0 +1,30 @@
<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package Modules\Monitoring\Models
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\Monitoring\Models;
use phpOMS\Message\Http\ImpressionStat as Stat;
/**
* Impression stat class.
*
* @package Modules\Monitoring\Models
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
final class ImpressionStat extends Stat
{
public int $id = 0;
}

View File

@ -15,7 +15,6 @@ declare(strict_types=1);
namespace Modules\Monitoring\Models;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
use phpOMS\Message\Statistic\ImpressionStat;
/**
* Item mapper class.
@ -41,12 +40,11 @@ final class ImpressionStatMapper extends DataMapperFactory
'monitoring_request_host' => ['name' => 'monitoring_request_host', 'type' => 'string', 'internal' => 'host',],
'monitoring_request_language' => ['name' => 'monitoring_request_language', 'type' => 'string', 'internal' => 'language',],
'monitoring_request_country' => ['name' => 'monitoring_request_country', 'type' => 'string', 'internal' => 'country',],
'monitoring_request_browser' => ['name' => 'monitoring_request_browser', 'type' => 'string', 'internal' => 'browser',],
'monitoring_request_path' => ['name' => 'monitoring_request_path', 'type' => 'string', 'internal' => 'path',],
'monitoring_request_uri' => ['name' => 'monitoring_request_uri', 'type' => 'string', 'internal' => 'uri',],
'monitoring_request_referer' => ['name' => 'monitoring_request_referer', 'type' => 'string', 'internal' => 'referer',],
'monitoring_request_agent' => ['name' => 'monitoring_request_agent', 'type' => 'string', 'internal' => 'agent',],
'monitoring_request_datetime' => ['name' => 'monitoring_request_datetime', 'type' => 'int', 'internal' => 'datetime',],
'monitoring_request_agent' => ['name' => 'monitoring_request_agent', 'type' => 'string', 'internal' => 'userAgent',],
'monitoring_request_datetime' => ['name' => 'monitoring_request_datetime', 'type' => 'DateTime', 'internal' => 'datetime',],
];
/**

View File

@ -31,4 +31,6 @@ abstract class PermissionCategory extends Enum
public const LOG = 2;
public const SECURITY = 3;
public const STATS = 3;
}

View File

@ -16,4 +16,5 @@ return ['Navigation' => [
'Logs' => 'Protokoll',
'Monitoring' => 'Überwachung',
'Security' => 'Sicherheit',
'Stats' => 'Stats',
]];

View File

@ -16,4 +16,5 @@ return ['Navigation' => [
'Logs' => 'Logs',
'Monitoring' => 'Monitoring',
'Security' => 'Security',
'Stats' => 'Stats',
]];

View File

@ -0,0 +1,207 @@
<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package Modules\Monitoring
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
echo $this->data['nav']->render(); ?>
<div class="row">
<div class="col-xs-12 col-md-6" >
<section class="portlet">
<div class="portlet-head"><?= $this->getHtml('Daily'); ?></div>
<div class="portlet-body">
<canvas class="chart" id="impressions-daily"
data-chart='{
"type": "line",
"data": {
"labels": [
<?php $data = []; foreach ($this->data['stats']['impressions'] as $key => $value) { $data[] = '"' . $key . '"'; } echo \implode(',', $data); ?>
],
"datasets": [
{
"label": "<?= $this->getHtml('Daily'); ?>",
"type": "line",
"data": [
<?php $data = []; foreach ($this->data['stats']['impressions'] as $value) { $data[] = $value[0]; } echo \implode(',', $data); ?>
],
"fill": false,
"borderColor": "rgb(255, 99, 132)",
"backgroundColor": "rgb(255, 99, 132)",
"tension": 0.0
}
]
},
"options": {
"title": {
"display": false,
"text": "<?= $this->getHtml('Daily'); ?>"
},
"scales": {
"x": {
"id": "axis-1",
"display": true
},
"y": {
"id": "axis-2",
"display": true,
"position": "left",
"beginAtZero": true,
"ticks": {
"stepSize": 1
}
}
}
}
}'></canvas>
</div>
</section>
</div>
<div class="col-xs-12 col-md-6">
<section class="portlet">
<div class="portlet-head"><?= $this->getHtml('Country'); ?></div>
<div class="portlet-body">
<canvas class="chart" id="country-daily"
data-chart='{
"type": "bar",
"data": {
"labels": [
<?php $data = []; foreach ($this->data['stats']['impressions'] as $key => $value) { $data[] = '"' . $key . '"'; } echo \implode(',', $data); ?>
],
"datasets": [
<?php $c = 0; foreach ($this->data['stats']['country'] as $country => $values) : ++$c; ?>
<?= $c > 1 ? ',' : ''; ?>
{
"label": "<?= $this->printHtml($country); ?>",
"type": "bar",
"data": [
<?php $data = []; foreach ($this->data['stats']['impressions'] as $key => $value) { $data[] = $values[$key] ?? 0; } echo \implode(',', $data); ?>
]
}
<?php endforeach; ?>
]
},
"options": {
"responsive": true,
"title": {
"display": false,
"text": "<?= $this->getHtml('Country'); ?>"
},
"scales": {
"x": {
"id": "axis-1",
"display": true,
"stacked": true
},
"y": {
"id": "axis-2",
"display": true,
"position": "left",
"beginAtZero": true,
"ticks": {
"stepSize": 1
},
"stacked": true
}
}
}
}'></canvas>
</div>
</section>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-6" >
<section class="portlet">
<div class="portlet-head"><?= $this->getHtml('Browser'); ?></div>
<div class="portlet-body">
<canvas class="chart" id="browser-pie"
data-chart='{
"type": "pie",
"data": {
"labels": [
<?php $data = []; foreach ($this->data['stats']['browser'] as $key => $value) { $data[] = '"' . $key . '"'; } echo \implode(',', $data); ?>
],
"datasets": [
{
"label": "<?= $this->getHtml('Browser'); ?>",
"type": "pie",
"data": [
<?php $data = []; foreach ($this->data['stats']['browser'] as $value) { $data[] = $value[0]; } echo \implode(',', $data); ?>
]
}
]
},
"options": {
"responsive": true,
"title": {
"display": false,
"text": "<?= $this->getHtml('Browser'); ?>"
}
}
}'></canvas>
</div>
</section>
</div>
<div class="col-xs-12 col-md-6">
<section class="portlet">
<div class="portlet-head"><?= $this->getHtml('Domain'); ?></div>
<div class="portlet-body">
<canvas class="chart" id="domain-daily"
data-chart='{
"type": "bar",
"data": {
"labels": [
<?php $data = []; foreach ($this->data['stats']['impressions'] as $key => $value) { $data[] = '"' . $key . '"'; } echo \implode(',', $data); ?>
],
"datasets": [
<?php $c = 0; foreach ($this->data['stats']['domain'] as $host => $values) : ++$c; ?>
<?= $c > 1 ? ',' : ''; ?>
{
"label": "<?= $this->printHtml($host); ?>",
"type": "bar",
"data": [
<?php $data = []; foreach ($this->data['stats']['impressions'] as $key => $value) { $data[] = $values[$key] ?? 0; } echo \implode(',', $data); ?>
]
}
<?php endforeach; ?>
]
},
"options": {
"responsive": true,
"title": {
"display": false,
"text": "<?= $this->getHtml('Domain'); ?>"
},
"scales": {
"x": {
"id": "axis-1",
"display": true
},
"y": {
"id": "axis-2",
"display": true,
"position": "left",
"beginAtZero": true,
"ticks": {
"stepSize": 1
}
}
}
}
}'></canvas>
</div>
</section>
</div>
</div>