Merge pull request #2263 from wallabag/speed-up-count

Instead of selecting the whole data, just count it
This commit is contained in:
Jeremy Benoist 2016-09-04 20:07:45 +02:00 committed by GitHub
commit c3b53188d7
3 changed files with 51 additions and 26 deletions

View File

@ -49,7 +49,7 @@ class DeveloperController extends Controller
$this->get('session')->getFlashBag()->add(
'notice',
$this->get('translator')->trans('flashes.developer.notice.client_created', array('%name%' => $client->getName()))
$this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()])
);
return $this->render('WallabagCoreBundle:Developer:client_parameters.html.twig', [
@ -81,7 +81,7 @@ class DeveloperController extends Controller
$this->get('session')->getFlashBag()->add(
'notice',
$this->get('translator')->trans('flashes.developer.notice.client_deleted', array('%name%' => $client->getName()))
$this->get('translator')->trans('flashes.developer.notice.client_deleted', ['%name%' => $client->getName()])
);
return $this->redirect($this->generateUrl('developer'));

View File

@ -35,16 +35,16 @@
{% set currentRoute = app.request.attributes.get('_route') %}
<li class="bold {% if currentRoute == 'unread' or currentRoute == 'homepage' %}active{% endif %}">
<a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ unreadEntries }}</span></a>
<a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ count_entries('unread') }}</span></a>
</li>
<li class="bold {% if currentRoute == 'starred' %}active{% endif %}">
<a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ starredEntries }}</span></a>
<a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ count_entries('starred') }}</span></a>
</li>
<li class="bold {% if currentRoute == 'archive' %}active{% endif %}">
<a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ archivedEntries }}</span></a>
<a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ count_entries('archive') }}</span></a>
</li>
<li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}">
<a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ allEntries }}</span></a>
<a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ count_entries('all') }}</span></a>
</li>
<li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}">
<a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a>

View File

@ -23,41 +23,66 @@ 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;
if (null === $user || !is_object($user)) {
return array();
return [];
}
$unreadEntries = $this->repository->enableCache(
$this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()
);
switch ($type) {
case 'starred':
$qb = $this->repository->getBuilderForStarredByUser($user->getId());
break;
$starredEntries = $this->repository->enableCache(
$this->repository->getBuilderForStarredByUser($user->getId())->getQuery()
);
case 'archive':
$qb = $this->repository->getBuilderForArchiveByUser($user->getId());
break;
$archivedEntries = $this->repository->enableCache(
$this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()
);
case 'unread':
$qb = $this->repository->getBuilderForUnreadByUser($user->getId());
break;
$allEntries = $this->repository->enableCache(
$this->repository->getBuilderForAllByUser($user->getId())->getQuery()
);
case 'all':
$qb = $this->repository->getBuilderForAllByUser($user->getId());
break;
return array(
'unreadEntries' => count($unreadEntries->getResult()),
'starredEntries' => count($starredEntries->getResult()),
'archivedEntries' => count($archivedEntries->getResult()),
'allEntries' => count($allEntries->getResult()),
);
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()