Optimize certificate filtering by name

This commit is contained in:
Curtis Castrapel 2018-11-07 15:34:25 -08:00
parent a2b22a7d09
commit 08a2a2b0e5
2 changed files with 6 additions and 5 deletions

View File

@ -20,6 +20,7 @@ from lemur.common.utils import generate_private_key, truthiness
from lemur.destinations.models import Destination from lemur.destinations.models import Destination
from lemur.domains.models import Domain from lemur.domains.models import Domain
from lemur.extensions import metrics, sentry, signals from lemur.extensions import metrics, sentry, signals
from lemur.models import certificate_associations
from lemur.notifications.models import Notification from lemur.notifications.models import Notification
from lemur.pending_certificates.models import PendingCertificate from lemur.pending_certificates.models import PendingCertificate
from lemur.plugins.base import plugins from lemur.plugins.base import plugins
@ -332,13 +333,13 @@ def render(args):
elif 'id' in terms: elif 'id' in terms:
query = query.filter(Certificate.id == cast(terms[1], Integer)) query = query.filter(Certificate.id == cast(terms[1], Integer))
elif 'name' in terms: elif 'name' in terms:
query = query.filter( query = query.join(certificate_associations).join(Domain).filter(
or_( or_(
Certificate.name.ilike(term), Certificate.name.ilike(term),
Certificate.domains.any(Domain.name.ilike(term)), Certificate.domains.any(Domain.name.ilike(term)),
Certificate.cn.ilike(term), Certificate.cn.ilike(term),
) )
) ).group_by(Certificate.id)
else: else:
query = database.filter(query, Certificate, terms) query = database.filter(query, Certificate, terms)

View File

@ -11,11 +11,11 @@
""" """
from inflection import underscore from inflection import underscore
from sqlalchemy import exc, func from sqlalchemy import exc, func
from sqlalchemy.sql import and_, or_
from sqlalchemy.orm import make_transient from sqlalchemy.orm import make_transient
from sqlalchemy.sql import and_, or_
from lemur.extensions import db
from lemur.exceptions import AttrNotFound, DuplicateError from lemur.exceptions import AttrNotFound, DuplicateError
from lemur.extensions import db
def filter_none(kwargs): def filter_none(kwargs):
@ -273,7 +273,7 @@ def get_count(q):
:param q: :param q:
:return: :return:
""" """
count_q = q.statement.with_only_columns([func.count()]).order_by(None) count_q = q.statement.with_only_columns([func.count()]).group_by(None).order_by(None)
count = q.session.execute(count_q).scalar() count = q.session.execute(count_q).scalar()
return count return count