forked from gernest/apidemic
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
436 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.