add analytic chart and table
This commit is contained in:
parent
23ee09b1b5
commit
e67966df45
133
core/Analytic/RangeAnalytic.php
Normal file
133
core/Analytic/RangeAnalytic.php
Normal file
|
@ -0,0 +1,133 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core\Analytic;
|
||||
|
||||
use App\Core\Repository\Analytic\RefererRepositoryQuery;
|
||||
use App\Core\Repository\Analytic\ViewRepositoryQuery;
|
||||
use App\Core\Entity\Site\Node;
|
||||
|
||||
/**
|
||||
* class RangeAnalytic.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class RangeAnalytic
|
||||
{
|
||||
protected ViewRepositoryQuery $viewQuery;
|
||||
protected RefererRepositoryQuery $refererQuery;
|
||||
|
||||
public function __construct(ViewRepositoryQuery $viewQuery, RefererRepositoryQuery $refererQuery)
|
||||
{
|
||||
$this->viewQuery = $viewQuery;
|
||||
$this->refererQuery = $refererQuery;
|
||||
}
|
||||
|
||||
public function getViews(\DateTime $from, \DateTime $to, Node $node): array
|
||||
{
|
||||
$entities = $this->viewQuery->create()
|
||||
->andWhere('.date >= :from')
|
||||
->andWhere('.date <= :to')
|
||||
->andWhere('.node = :node')
|
||||
->orderBy('.date')
|
||||
->setParameters([
|
||||
':from' => $from,
|
||||
':to' => $to,
|
||||
':node' => $node->getId(),
|
||||
])
|
||||
->find()
|
||||
;
|
||||
|
||||
$diff = $from->diff($to);
|
||||
|
||||
if ($diff->days >= 365) {
|
||||
$format = 'Y-m';
|
||||
} else {
|
||||
$format = 'Y-m-d';
|
||||
}
|
||||
|
||||
$datas = [];
|
||||
|
||||
foreach ($entities as $entity) {
|
||||
$index = $entity->getDate()->format($format);
|
||||
|
||||
if (!isset($datas[$index])) {
|
||||
$datas[$index] = 0;
|
||||
}
|
||||
|
||||
$datas[$index] += $entity->getViews();
|
||||
}
|
||||
|
||||
return $datas;
|
||||
}
|
||||
|
||||
public function getPathViews(\DateTime $from, \DateTime $to, Node $node): array
|
||||
{
|
||||
$entities = $this->viewQuery->create()
|
||||
->andWhere('.date >= :from')
|
||||
->andWhere('.date <= :to')
|
||||
->andWhere('.node = :node')
|
||||
->orderBy('.date')
|
||||
->setParameters([
|
||||
':from' => $from,
|
||||
':to' => $to,
|
||||
':node' => $node->getId(),
|
||||
])
|
||||
->find()
|
||||
;
|
||||
|
||||
$datas = [];
|
||||
|
||||
foreach ($entities as $entity) {
|
||||
$index = $entity->getPath();
|
||||
|
||||
if (!isset($datas[$index])) {
|
||||
$datas[$index] = 0;
|
||||
}
|
||||
|
||||
$datas[$index] += $entity->getViews();
|
||||
}
|
||||
|
||||
return $datas;
|
||||
}
|
||||
|
||||
public function getReferers(\DateTime $from, \DateTime $to, Node $node): array
|
||||
{
|
||||
$entities = $this->refererQuery->create()
|
||||
->andWhere('.date >= :from')
|
||||
->andWhere('.date <= :to')
|
||||
->andWhere('.node = :node')
|
||||
->orderBy('.date')
|
||||
->setParameters([
|
||||
':from' => $from,
|
||||
':to' => $to,
|
||||
':node' => $node->getId(),
|
||||
])
|
||||
->find()
|
||||
;
|
||||
|
||||
$datas = [];
|
||||
|
||||
foreach ($entities as $entity) {
|
||||
$index = parse_url($entity->getUri(), PHP_URL_HOST);
|
||||
|
||||
if (!isset($datas[$index])) {
|
||||
$datas[$index] = [
|
||||
'views' => 0,
|
||||
'uris' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$datas[$index]['views'] += $entity->getViews();
|
||||
|
||||
$path = parse_url($entity->getUri(), PHP_URL_PATH);
|
||||
|
||||
if (!isset($datas[$index]['uris'][$path])) {
|
||||
$datas[$index]['uris'][$path] = 0;
|
||||
}
|
||||
|
||||
$datas[$index]['uris'][$path] += $entity->getViews();
|
||||
}
|
||||
|
||||
return $datas;
|
||||
}
|
||||
}
|
37
core/Controller/Analytic/AnalyticController.php
Normal file
37
core/Controller/Analytic/AnalyticController.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core\Controller\Analytic;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use App\Core\Entity\Site\Node;
|
||||
use App\Core\Analytic\RangeAnalytic;
|
||||
|
||||
/**
|
||||
* @Route("/admin/analytic")
|
||||
*/
|
||||
class AnalyticController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Route("/stats/{node}/{range}", name="admin_analytic_stats")
|
||||
*/
|
||||
public function stats(Node $node, RangeAnalytic $rangeAnalytic, string $range = '7days'): Response
|
||||
{
|
||||
if (!in_array($range, ['7days', '30days', '90days', '1year'])) {
|
||||
throw $this->createNotFoundException();
|
||||
}
|
||||
|
||||
$views = $rangeAnalytic->getViews(new \DateTime('now - '.$range), new \DateTime(), $node);
|
||||
$pathViews = $rangeAnalytic->getPathViews(new \DateTime('now - '.$range), new \DateTime(), $node);
|
||||
$referers = $rangeAnalytic->getReferers(new \DateTime('now - '.$range), new \DateTime(), $node);
|
||||
|
||||
return $this->render('@Core/analytic/stats.html.twig', [
|
||||
'range' => $range,
|
||||
'views' => $views,
|
||||
'pathViews' => $pathViews,
|
||||
'referers' => $referers,
|
||||
'node' => $node,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -18,28 +18,28 @@ class Referer implements EntityInterface
|
|||
* @ORM\GeneratedValue
|
||||
* @ORM\Column(type="integer")
|
||||
*/
|
||||
private $id;
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity=Node::class, inversedBy="nodeViews")
|
||||
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
|
||||
*/
|
||||
private $node;
|
||||
protected $node;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="string", length=255)
|
||||
*/
|
||||
private $uri;
|
||||
protected $uri;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="integer", options={"default"=0})
|
||||
*/
|
||||
private $views = 0;
|
||||
protected $views = 0;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="date")
|
||||
*/
|
||||
private $date;
|
||||
protected $date;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
|
|
|
@ -18,28 +18,28 @@ class View implements EntityInterface
|
|||
* @ORM\GeneratedValue
|
||||
* @ORM\Column(type="integer")
|
||||
*/
|
||||
private $id;
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity=Node::class, inversedBy="nodeViews")
|
||||
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
|
||||
*/
|
||||
private $node;
|
||||
protected $node;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="string", length=255)
|
||||
*/
|
||||
private $path;
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="integer", options={"default"=0})
|
||||
*/
|
||||
private $views = 0;
|
||||
protected $views = 0;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="date")
|
||||
*/
|
||||
private $date;
|
||||
protected $date;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
|
|
|
@ -145,7 +145,7 @@ class Node implements EntityInterface
|
|||
/**
|
||||
* @ORM\Column(type="boolean", options={"default"=0})
|
||||
*/
|
||||
protected $enableAnalytic = false;
|
||||
protected $enableAnalytics = false;
|
||||
|
||||
/**
|
||||
* @ORM\OneToMany(targetEntity=View::class, mappedBy="node")
|
||||
|
@ -569,14 +569,14 @@ class Node implements EntityInterface
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function getEnableAnalytic(): ?bool
|
||||
public function getEnableAnalytics(): ?bool
|
||||
{
|
||||
return $this->enableAnalytic;
|
||||
return $this->enableAnalytics;
|
||||
}
|
||||
|
||||
public function setEnableAnalytic(bool $enableAnalytic): self
|
||||
public function setEnableAnalytics(bool $enableAnalytics): self
|
||||
{
|
||||
$this->enableAnalytic = $enableAnalytic;
|
||||
$this->enableAnalytics = $enableAnalytics;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ class AnalyticListener
|
|||
|
||||
$node = $this->nodeRepository->findOneBy([
|
||||
'id' => $request->attributes->get('_node'),
|
||||
'enableViewCounter' => true,
|
||||
'enableAnalytics' => true,
|
||||
]);
|
||||
|
||||
if (!$node) {
|
||||
|
|
|
@ -61,10 +61,10 @@ class NodeType extends AbstractType
|
|||
);
|
||||
|
||||
$builder->add(
|
||||
'enableAnalytic',
|
||||
'enableAnalytics',
|
||||
CheckboxType::class,
|
||||
[
|
||||
'label' => 'Enable analytic',
|
||||
'label' => 'Enable analytics',
|
||||
'required' => false,
|
||||
'attr' => [
|
||||
],
|
||||
|
|
|
@ -39,7 +39,8 @@
|
|||
"Never": "Jamais"
|
||||
"URL": "URL"
|
||||
"Disable URL": "Désactiver l'URL"
|
||||
"Enable analytic": "Activer l'analyse"
|
||||
"Enable analytics": "Activer les analyses"
|
||||
"Analytics": "Analyses"
|
||||
"Controller": "Contrôleur"
|
||||
"Leave blank for automatic generation": "Laisser vide pour une génération automatique"
|
||||
"Leave blank to use the default one. Example: App\\Controller\\FooController::barAction": "Laisser vide pour utiliser celui par défaut. Exemple : App\\Controller\\FooController::barAction"
|
||||
|
|
111
core/Resources/views/analytic/stats.html.twig
Normal file
111
core/Resources/views/analytic/stats.html.twig
Normal file
|
@ -0,0 +1,111 @@
|
|||
<div class="modal-dialog modal-dialog-large">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">
|
||||
{{ 'Analytics'|trans }}
|
||||
</h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<ul class="nav nav-pills" role="tablist">
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link {% if range == '7days' %}active{% endif %}" data-modal="{{ path('admin_analytic_stats', {node: node.id, range: '7days'}) }}" href="#">
|
||||
{{ 'Last 7 days'|trans }}
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link {% if range == '30days' %}active{% endif %}" data-modal="{{ path('admin_analytic_stats', {node: node.id, range: '30days'}) }}" href="#">
|
||||
{{ 'Last 30 days'|trans }}
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link {% if range == '90days' %}active{% endif %}" data-modal="{{ path('admin_analytic_stats', {node: node.id, range: '90days'}) }}" href="#">
|
||||
{{ 'Last 90 days'|trans }}
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link {% if range == '1year' %}active{% endif %}" data-modal="{{ path('admin_analytic_stats', {node: node.id, range: '1year'}) }}" href="#">
|
||||
{{ 'Last year'|trans }}
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
{% set values = [] %}
|
||||
{% for item in views %}{% set values = values|merge([item]) %}{% endfor %}
|
||||
|
||||
<div class="tab-content pt-4">
|
||||
<div id="analytic-chart-container">
|
||||
<canvas id="analytic-chart" data-label="{{ 'Views'|trans }}" data-labels="{{ views|keys|json_encode }}" data-values="{{ values|json_encode }}"></canvas>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="pl-md-3">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ 'Path'|trans }}</th>
|
||||
<th width="100" class="text-right">{{ 'Views'|trans }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for path, views in pathViews %}
|
||||
<tr>
|
||||
<td>{{ path }}</td>
|
||||
<td class="text-right">{{ views }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<div class="pl-md-3">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ 'Referers'|trans }}</th>
|
||||
<th width="100" class="text-right">{{ 'Views'|trans }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for referer, info in referers %}
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<details>
|
||||
<summary>
|
||||
<div class="float-right">
|
||||
{{ info.views }}
|
||||
</div>
|
||||
|
||||
{{ referer }}
|
||||
</summary>
|
||||
|
||||
{% for path, views in info.uris %}
|
||||
<div class="pl-3">
|
||||
<div class="float-right">
|
||||
{{ info.views }}
|
||||
</div>
|
||||
|
||||
{{ path }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</details>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Close'|trans }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -72,6 +72,7 @@
|
|||
{% set move = path('admin_site_node_move', {entity: node.id}) %}
|
||||
{% set edit = path('admin_site_node_edit', {entity: node.id}) %}
|
||||
{% set new = path('admin_site_node_new', {node: node.id}) %}
|
||||
{% set analytics = path('admin_analytic_stats', {node: node.id}) %}
|
||||
|
||||
<div class="list-group-item" id="node-{{ node.id }}">
|
||||
<div class="row">
|
||||
|
@ -147,6 +148,10 @@
|
|||
{% endif %}
|
||||
</button>
|
||||
|
||||
<button data-modal="{{ analytics }}" class="btn btn-sm btn-light border border-dark mr-1 mb-1">
|
||||
<span data-modal="{{ analytics }}" class="fa fa-chart-bar"></span>
|
||||
</button>
|
||||
|
||||
<button data-modal="{{ move }}" class="btn btn-sm btn-dark mr-1 mb-1">
|
||||
<span data-modal="{{ move }}" class="fa fa-arrows-alt"></span>
|
||||
</button>
|
||||
|
|
Loading…
Reference in a new issue