Fix the heavy time cost of event-list

Acorrding the current event-list API, an admin user is allowed to query
all events which have traits.project_id value that match it's own project
OR any event without any project_id trait. In get_events() in
impl_sqlalchemy.py, this current query clause is implemented in a
inefficient way. This change make it more efficient.

Co-Authored-By: gordon chung<gord@live.ca>
Change-Id: I645a7def0552e2574da540e180b4f57d8770e96e
Closes-Bug: #1494440
This commit is contained in:
unknown 2015-09-14 15:16:52 +08:00 committed by Sergey Skripnick
parent 72eb6985c4
commit d5860a7072

View File

@ -266,14 +266,18 @@ class Connection(base.Connection):
query = query.join(trait_subq, query = query.join(trait_subq,
trait_subq.c.ev_id == models.Event.id) trait_subq.c.ev_id == models.Event.id)
if event_filter.admin_proj: if event_filter.admin_proj:
admin_q = session.query(models.TraitText).filter( no_proj_q = session.query(models.TraitText.event_id).filter(
models.TraitText.key == 'project_id') models.TraitText.key == 'project_id')
query = query.filter(sa.or_(~sa.exists().where( admin_q = (session.query(models.TraitText.event_id).filter(
models.Event.id == admin_q.subquery().c.event_id), ~sa.exists().where(models.TraitText.event_id ==
sa.and_( no_proj_q.subquery().c.event_id)).union(
session.query(models.TraitText.event_id).filter(sa.and_(
models.TraitText.key == 'project_id', models.TraitText.key == 'project_id',
models.TraitText.value == event_filter.admin_proj, models.TraitText.value == event_filter.admin_proj,
models.Event.id == models.TraitText.event_id))) models.Event.id == models.TraitText.event_id))))
query = query.filter(sa.exists().where(
models.Event.id ==
admin_q.subquery().c.trait_text_event_id))
if event_filter_conditions: if event_filter_conditions:
query = query.filter(sa.and_(*event_filter_conditions)) query = query.filter(sa.and_(*event_filter_conditions))
@ -283,7 +287,7 @@ class Connection(base.Connection):
for (id_, generated, message_id, for (id_, generated, message_id,
desc, raw) in query.add_columns( desc, raw) in query.add_columns(
models.Event.generated, models.Event.message_id, models.Event.generated, models.Event.message_id,
models.EventType.desc, models.Event.raw).distinct().all(): models.EventType.desc, models.Event.raw).all():
event_list[id_] = api_models.Event(message_id, desc, event_list[id_] = api_models.Event(message_id, desc,
generated, [], raw) generated, [], raw)
# Query all traits related to events. # Query all traits related to events.