Skip to content

Commit

Permalink
add stats
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed May 17, 2019
1 parent 0fd0cbb commit 467fad3
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 7 deletions.
22 changes: 22 additions & 0 deletions bin/dump-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if [ -z ${1} ]; then
echo "host must be provided"
exit 1
fi

if [ -z ${2} ]; then
echo "username must be provided"
exit 1
fi

if [ -z ${3} ]; then
echo "password must be provided"
exit 1
fi


PROJECT_NAME=${PWD##*/}

mkdir -p dump

docker run --rm mongo mongodump --host $1 --username $2 --password $3 --archive --gzip | \
cat > dump/dump_${PROJECT_NAME}_$(date '+%d-%m-%Y_%H-%M-%S').gz
44 changes: 44 additions & 0 deletions bin/restore-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

set -e
set -x

if [[ ! "$1" ]]
then
echo Host is required!
exit
fi

HOST=$1

if [[ ! "$2" ]]
then
LATEST_DUMP=`ls -rt dump | tail -n 1`

if [ ! -z "$LATEST_DUMP" ]
then
read -p "Would you like restire the latest dump ${LATEST_DUMP}? [Y/n]" USE_LATEST_DUMP
if [[ $USE_LATEST_DUMP == "n" ]]
then
exit
fi

FILEPATH="${PWD}/dump/${LATEST_DUMP}"
fi
else
FILEPATH=$2
fi

if [[ ! -f "$FILEPATH" ]]
then
echo "Such file doesn't exists!"
exit
fi

read -p "The database will be DROPPED. Proceed? [Y/n]" DROPDB
if [[ $DROPDB == "n" ]]
then
exit
fi

cat $FILEPATH | docker run --rm -i mongo mongorestore --host $HOST --archive --gzip --drop
5 changes: 5 additions & 0 deletions bin/restore-local-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

HOST=`docker network inspect bridge --format='{{range $p, $conf := .IPAM.Config}} {{(index $conf).Gateway}} {{end}}'`

./bin/restore-db.sh $HOST
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ security:
always_remember_me: true

access_control:
- { path: ^/admin/, roles: ROLE_ADMIN }
- { path: ^/telegram/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
2 changes: 1 addition & 1 deletion src/Controller/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class LogController
{
/**
* @Route("/log", name="logs")
* @Route("/admin/log", name="logs")
*/
public function logs(
Request $request,
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function ordersStuck(
}

/**
* @Route("/orders/in-processing", name="orders-in-processing")
* @Route("/admin/orders/in-processing", name="orders-in-processing")
*/
public function ordersInProcessing(
Request $request,
Expand Down
8 changes: 4 additions & 4 deletions src/Controller/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class ProductController
{
/**
* @Route("products", name="products")
* @Route("/admin/products", name="products")
*/
public function list(
Request $request,
Expand All @@ -44,7 +44,7 @@ public function list(
}

/**
* @Route("products/create", name="products-create")
* @Route("/admin/products/create", name="products-create")
*/
public function create(
Request $request,
Expand Down Expand Up @@ -75,7 +75,7 @@ public function create(
}

/**
* @Route("products/{productId}/edit", name="products-edit")
* @Route("/admin/products/{productId}/edit", name="products-edit")
*/
public function edit(
string $productId,
Expand Down Expand Up @@ -110,7 +110,7 @@ public function edit(
}

/**
* @Route("products/{productId}/delete", name="products-delete")
* @Route("/admin/products/{productId}/delete", name="products-delete")
*/
public function delete(
string $productId,
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class SettingsController
{
/**
* @Route("/settings", name="settings")
* @Route("/admin/settings", name="settings")
*/
public function logs(
Request $request,
Expand Down
244 changes: 244 additions & 0 deletions src/Controller/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
<?php declare(strict_types=1);

namespace App\Controller;

use App\Model\Order;
use App\Storage\OrderStorage;
use App\Storage\UserStorage;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;

class StatsController
{
private $orderStorage;

private $userStorage;

private $twig;

public function __construct(OrderStorage $orderStorage, UserStorage $userStorage, Environment $twig)
{
$this->orderStorage = $orderStorage;
$this->userStorage = $userStorage;
$this->twig = $twig;
}

/**
* @Route("/admin/stats", name="stats")
*/
public function __invoke(): Response
{
$threeMonthsAgo = time() - 60*60*24*90;

return new Response($this->twig->render('stats.html.twig', [
'finishedOrdersCountSum' => $this->calcFinishedOrdersSumStats($threeMonthsAgo),
'finishedOrdersCountStats' => $this->calcFinishedOrdersCount($threeMonthsAgo),
'canceledOrdersCountStats' => $this->calcCanceledOrdersCount($threeMonthsAgo),
]));
}

private function calcFinishedOrdersCount(int $from): array
{
$pipeline = [
[
'$match' => [
'state' => Order::STATE_FINISHED,
'createdAt.unix' => ['$gte' => $from],
],
],
[
'$project' => [
'points' => 1,
'updatedDate' => [
'$toDate' => [
'$multiply' => [1000, '$updatedAt.unix'],
],
],
],
],
[
'$group' => [
'_id' => [
'day' => [
'$dayOfMonth' => '$updatedDate',
],
'month' => [
'$month' => '$updatedDate',
],
'year' => [
'$year' => '$updatedDate',
],
],
'date' => [
'$first' => [
'$dateToString' => ['format' => '%Y-%m-%d', 'date' => '$updatedDate'],
],
],
'count' => ['$sum' => 1],
],
],
[
'$sort' => [
'_id.year' => 1,
'_id.month' => 1,
'_id.day' => 1,
],
],
];

$options = [
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
];

$data = [];
foreach ($this->orderStorage->getCollection()->aggregate($pipeline, $options) as $row) {
if (false == $row['date']) {
continue;
}

$data[] = [
'x' => $row['date'],
'y' => $row['count'],
];
}

return $data;
}

private function calcCanceledOrdersCount(int $from): array
{
$pipeline = [
[
'$match' => [
'state' => Order::STATE_CANCELED,
'createdAt.unix' => ['$gte' => $from],
],
],
[
'$project' => [
'points' => 1,
'finishProcessingDate' => [
'$toDate' => [
'$multiply' => [1000, '$finishProcessingAt.unix'],
],
],
],
],
[
'$group' => [
'_id' => [
'day' => [
'$dayOfMonth' => '$finishProcessingDate',
],
'month' => [
'$month' => '$finishProcessingDate',
],
'year' => [
'$year' => '$finishProcessingDate',
],
],
'date' => [
'$first' => [
'$dateToString' => ['format' => '%Y-%m-%d', 'date' => '$finishProcessingDate'],
],
],
'count' => ['$sum' => 1],
],
],
[
'$sort' => [
'_id.year' => 1,
'_id.month' => 1,
'_id.day' => 1,
],
],
];

$options = [
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
];

$data = [];
foreach ($this->orderStorage->getCollection()->aggregate($pipeline, $options) as $row) {
if (false == $row['date']) {
continue;
}

$data[] = [
'x' => $row['date'],
'y' => $row['count'],
];
}

return $data;
}

private function calcFinishedOrdersSumStats(int $from): array
{
$pipeline = [
[
'$match' => [
'state' => Order::STATE_FINISHED,
'createdAt.unix' => ['$gte' => $from],
],
],
[
'$project' => [
'totalPrice' => '$totalPrice',
'finishProcessingDate' => [
'$toDate' => [
'$multiply' => [1000, '$finishProcessingAt.unix'],
],
],
],
],
[
'$group' => [
'_id' => [
'day' => [
'$dayOfMonth' => '$finishProcessingDate',
],
'month' => [
'$month' => '$finishProcessingDate',
],
'year' => [
'$year' => '$finishProcessingDate',
],
],
'date' => [
'$first' => [
'$dateToString' => ['format' => '%Y-%m-%d', 'date' => '$finishProcessingDate'],
],
],
'sum' => ['$sum' => '$totalPrice'],
],
],
[
'$sort' => [
'_id.year' => 1,
'_id.month' => 1,
'_id.day' => 1,
],
],
];

$options = [
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
];
//echo '<pre>';
$data = [];
foreach ($this->orderStorage->getCollection()->aggregate($pipeline, $options) as $row) {
if (false == $row['date']) {
continue;
}
//var_dump($row);
$data[] = [
'x' => $row['date'],
'y' => round($row['sum'] / 100),
];
}
//echo '<pre />';
return $data;
}
}
2 changes: 2 additions & 0 deletions templates/menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
href="{{ path('orders-in-processing') }}">Заказы в обработке</a>
<a class="list-group-item list-group-item-action {% if app.request.get('_route') == 'settings' %}active{% endif %}"
href="{{ path('settings') }}">Настройки</a>
<a class="list-group-item list-group-item-action {% if app.request.get('_route') == 'stats' %}active{% endif %}"
href="{{ path('stats') }}">Статистика</a>
<a class="list-group-item list-group-item-action {% if app.request.get('_route') == 'logs' %}active{% endif %}"
href="{{ path('logs') }}">Ошибки</a>
</div>
Expand Down
Loading

0 comments on commit 467fad3

Please sign in to comment.