Skip to content

Commit

Permalink
Added new PreviousNextPaginator paginator, because of `VisualPagina…
Browse files Browse the repository at this point in the history
…tor` performance issues

remp/crm#2512
+ refactored all modules to use new `PreviousNextPaginator` instead of `VisualPaginator`
  • Loading branch information
zoldia committed Jul 26, 2022
1 parent 227531f commit ca0cf86
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 33 deletions.
21 changes: 21 additions & 0 deletions src/Models/Repository/OrdersRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Crm\ProductsModule\Repository;

use Crm\ApplicationModule\Cache\CacheRepository;
use Crm\ApplicationModule\Hermes\HermesMessage;
use Crm\ApplicationModule\Repository;
use Crm\ApplicationModule\Repository\AuditLogRepository;
Expand All @@ -28,19 +29,23 @@ class OrdersRepository extends Repository

protected $tableName = 'orders';

private $cacheRepository;

private $emitter;

private $hermesEmitter;

public function __construct(
Explorer $database,
AuditLogRepository $auditLogRepository,
CacheRepository $cacheRepository,
Emitter $emitter,
\Tomaj\Hermes\Emitter $hermesEmitter
) {
parent::__construct($database);
$this->auditLogRepository = $auditLogRepository;
$this->database = $database;
$this->cacheRepository = $cacheRepository;
$this->emitter = $emitter;
$this->hermesEmitter = $hermesEmitter;
}
Expand Down Expand Up @@ -138,4 +143,20 @@ final public function hasOrderAfter(int $userId, DateTime $after): bool
])
->count('*') > 0;
}

final public function totalCount($allowCached = false, $forceCacheUpdate = false)
{
$callable = function () {
return parent::totalCount();
};
if ($allowCached) {
return $this->cacheRepository->loadAndUpdate(
'orders_count',
$callable,
\Nette\Utils\DateTime::from(CacheRepository::REFRESH_TIME_5_MINUTES),
$forceCacheUpdate
);
}
return $callable();
}
}
23 changes: 22 additions & 1 deletion src/Models/Repository/ProductsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Crm\ProductsModule\Repository;

use Crm\ApplicationModule\Cache\CacheRepository;
use Crm\ApplicationModule\Repository;
use Crm\ApplicationModule\Repository\AuditLogRepository;
use Crm\PaymentsModule\Repository\PaymentsRepository;
Expand All @@ -27,20 +28,24 @@ class ProductsRepository extends Repository

private $productShopCountsDistribution;

private $cacheRepository;

public function __construct(
Explorer $database,
AuditLogRepository $auditLogRepository,
AmountSpentDistribution $amountSpentDistribution,
PaymentCountsDistribution $paymentCountDistribution,
ProductDaysFromLastOrderDistribution $productDaysFromLastOrderDistribution,
ProductShopCountsDistribution $productShopCountsDistribution
ProductShopCountsDistribution $productShopCountsDistribution,
CacheRepository $cacheRepository
) {
parent::__construct($database);
$this->auditLogRepository = $auditLogRepository;
$this->amountSpentDistribution = $amountSpentDistribution;
$this->paymentCountDistribution = $paymentCountDistribution;
$this->productDaysFromLastOrderDistribution = $productDaysFromLastOrderDistribution;
$this->productShopCountsDistribution = $productShopCountsDistribution;
$this->cacheRepository = $cacheRepository;
}

final public function find($id)
Expand Down Expand Up @@ -246,4 +251,20 @@ final public function softDelete(ActiveRow $segment)
'modified_at' => new \DateTime(),
]);
}

final public function totalCount($allowCached = false, $forceCacheUpdate = false)
{
$callable = function () {
return parent::totalCount();
};
if ($allowCached) {
return $this->cacheRepository->loadAndUpdate(
'products_count',
$callable,
\Nette\Utils\DateTime::from(CacheRepository::REFRESH_TIME_5_MINUTES),
$forceCacheUpdate
);
}
return $callable();
}
}
20 changes: 9 additions & 11 deletions src/Presenters/OrdersAdminPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Crm\ProductsModule\Presenters;

use Crm\AdminModule\Presenters\AdminPresenter;
use Crm\ApplicationModule\Components\VisualPaginator;
use Crm\ApplicationModule\Components\PreviousNextPaginator;
use Crm\PaymentsModule\Repository\PaymentsRepository;
use Crm\ProductsModule\Forms\CheckoutFormFactory;
use Crm\ProductsModule\PaymentItem\ProductPaymentItem;
Expand Down Expand Up @@ -54,19 +54,17 @@ public function __construct(
public function renderDefault()
{
$orders = $this->getFilteredOrders();
$filteredCount = $orders->count('*');
$totalCount = $this->ordersRepository->totalCount();

$vp = new VisualPaginator();
$this->addComponent($vp, 'vp');
$paginator = $vp->getPaginator();
$paginator->setItemCount($filteredCount);
$pnp = new PreviousNextPaginator();
$this->addComponent($pnp, 'paginator');
$paginator = $pnp->getPaginator();
$paginator->setItemsPerPage($this->onPage);

$this->template->vp = $vp;
$this->template->orders = $orders->limit($paginator->getLength(), $paginator->getOffset());
$this->template->filteredCount = $filteredCount;
$this->template->totalCount = $totalCount;
$orders = $orders->limit($paginator->getLength(), $paginator->getOffset())->fetchAll();
$pnp->setActualItemCount(count($orders));

$this->template->orders = $orders;
$this->template->totalCount = $this->ordersRepository->totalCount(true);
}

/**
Expand Down
18 changes: 8 additions & 10 deletions src/Presenters/ProductsAdminPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Crm\AdminModule\Presenters\AdminPresenter;
use Crm\ApplicationModule\Components\Graphs\GoogleLineGraphGroupControlFactoryInterface;
use Crm\ApplicationModule\Components\VisualPaginator;
use Crm\ApplicationModule\Components\PreviousNextPaginator;
use Crm\ApplicationModule\Config\Repository\ConfigsRepository;
use Crm\ApplicationModule\Graphs\Criteria;
use Crm\ApplicationModule\Graphs\GraphDataItem;
Expand Down Expand Up @@ -84,19 +84,17 @@ public function setDistributionConfiguration(string $key, array $distributionLev
public function renderDefault()
{
$products = $this->productsRepository->all($this->text, $this->tags);
$filteredCount = $products->count();

$vp = new VisualPaginator();
$this->addComponent($vp, 'products_vp');
$paginator = $vp->getPaginator();
$paginator->setItemCount($filteredCount);
$pnp = new PreviousNextPaginator();
$this->addComponent($pnp, 'paginator');
$paginator = $pnp->getPaginator();
$paginator->setItemsPerPage($this->onPage);

$this->template->vp = $vp;
$this->template->products = $products->limit($paginator->getLength(), $paginator->getOffset());
$products = $products->limit($paginator->getLength(), $paginator->getOffset())->fetchAll();
$pnp->setActualItemCount(count($products));

$this->template->allProductsCount = $this->productsRepository->all()->count();
$this->template->filteredProductsCount = $filteredCount;
$this->template->allProductsCount = $this->productsRepository->totalCount(true);
$this->template->products = $products;
}

public function renderSortShopProducts()
Expand Down
9 changes: 3 additions & 6 deletions src/templates/OrdersAdmin/default.latte
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
<h1>
{_products.admin.orders.default.header}
<small>
/ {_products.admin.orders.default.totalCount} {$totalCount}
{if $totalCount != $filteredCount}
{_products.admin.orders.default.filteredCount} {$filteredCount}
{/if}
/ {_system.total} {$totalCount}
</small>
</h1>
</div>
Expand All @@ -23,7 +20,7 @@
</div>

<div class="col-md-12">
{if $filteredCount > 0}
{if count($orders)}
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
Expand Down Expand Up @@ -86,7 +83,7 @@
</tr>
</tbody>
</table>
{control vp}
{control paginator}
{else}
{_products.admin.orders.default.empty}
{/if}
Expand Down
8 changes: 3 additions & 5 deletions src/templates/ProductsAdmin/default.latte
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
<h1>
{_'products.admin.products.default.header'}
<small>
/ {_'system.total'} {$allProductsCount}
{if $allProductsCount != $filteredProductsCount}
, {_'system.filtered'} {$filteredProductsCount}
{/if}
/ {_system.total} {$allProductsCount}
</small>

<a n:href="new" class="btn btn-success btn-lg pull-right"><i
class="fa fa-sliders-h"></i> {_'products.admin.products.default.new'}</a>
</h1>
Expand Down Expand Up @@ -64,7 +62,7 @@
</tbody>
</table>

{control products_vp}
{control paginator}
</div>

</div>

0 comments on commit ca0cf86

Please sign in to comment.