From 543da3e0b7592b1a00a7c5baec1554460609d63f Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 3 Sep 2016 18:11:07 +0200 Subject: Instead of selecting the whole data, just count it Instead of performing a complex select (to retrieve all data for entry, etc...) just select the counter and retrieve it. Down from ~50ms to ~30ms on the unread page (with 500 items) --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 5c475d61..93640dc6 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -33,31 +33,31 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; if (null === $user || !is_object($user)) { - return array(); + return []; } $unreadEntries = $this->repository->enableCache( - $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery() + $this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery() ); $starredEntries = $this->repository->enableCache( - $this->repository->getBuilderForStarredByUser($user->getId())->getQuery() + $this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery() ); $archivedEntries = $this->repository->enableCache( - $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery() + $this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery() ); $allEntries = $this->repository->enableCache( - $this->repository->getBuilderForAllByUser($user->getId())->getQuery() + $this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery() ); - return array( - 'unreadEntries' => count($unreadEntries->getResult()), - 'starredEntries' => count($starredEntries->getResult()), - 'archivedEntries' => count($archivedEntries->getResult()), - 'allEntries' => count($allEntries->getResult()), - ); + return [ + 'unreadEntries' => $unreadEntries->getSingleScalarResult(), + 'starredEntries' => $starredEntries->getSingleScalarResult(), + 'archivedEntries' => $archivedEntries->getSingleScalarResult(), + 'allEntries' => $allEntries->getSingleScalarResult(), + ]; } public function getName() -- cgit v1.2.3 From 59ddb9ae99b97a1a8fa3aa3770a4a2afef333699 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 3 Sep 2016 19:09:28 +0200 Subject: Remove Twig globals Twig Global function are called globally. This means even on a query to the api. Using a function we can decide when we want to call it. Also, remove previous `COUNT(e.id)` since it doesn't work on PostgreSQL ... --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 63 +++++++++++++++------- 1 file changed, 44 insertions(+), 19 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 93640dc6..d6ac6117 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -23,12 +23,26 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa ]; } + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), + ); + } + public function removeWww($url) { return preg_replace('/^www\./i', '', $url); } - public function getGlobals() + /** + * Return number of entries depending of the type (unread, archive, starred or all) + * + * @param string $type Type of entries to count + * + * @return int + */ + public function countEntries($type) { $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; @@ -36,28 +50,39 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return []; } - $unreadEntries = $this->repository->enableCache( - $this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery() - ); + switch ($type) { + case 'starred': + $qb = $this->repository->getBuilderForStarredByUser($user->getId()); + break; - $starredEntries = $this->repository->enableCache( - $this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery() - ); + case 'archive': + $qb = $this->repository->getBuilderForArchiveByUser($user->getId()); + break; - $archivedEntries = $this->repository->enableCache( - $this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery() - ); + case 'unread': + $qb = $this->repository->getBuilderForUnreadByUser($user->getId()); + break; - $allEntries = $this->repository->enableCache( - $this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery() - ); + case 'all': + $qb = $this->repository->getBuilderForAllByUser($user->getId()); + break; - return [ - 'unreadEntries' => $unreadEntries->getSingleScalarResult(), - 'starredEntries' => $starredEntries->getSingleScalarResult(), - 'archivedEntries' => $archivedEntries->getSingleScalarResult(), - 'allEntries' => $allEntries->getSingleScalarResult(), - ]; + default: + throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); + } + + // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) + // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function + $query = $qb + ->select('e.id') + ->groupBy('e.id') + ->getQuery(); + + $data =$this->repository + ->enableCache($query) + ->getArrayResult(); + + return count($data); } public function getName() -- cgit v1.2.3 From 234ad944534cf51118f63f0b48c206b5d9a70fe5 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 3 Sep 2016 19:26:23 +0200 Subject: CS --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index d6ac6117..974b86a9 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -36,9 +36,9 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa } /** - * Return number of entries depending of the type (unread, archive, starred or all) + * Return number of entries depending of the type (unread, archive, starred or all). * - * @param string $type Type of entries to count + * @param string $type Type of entries to count * * @return int */ @@ -78,7 +78,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa ->groupBy('e.id') ->getQuery(); - $data =$this->repository + $data = $this->repository ->enableCache($query) ->getArrayResult(); -- cgit v1.2.3