From 2a5298d9324eced7e2e7d61ecf3197856a468846 Mon Sep 17 00:00:00 2001 From: Stephen LaPorte Date: Sat, 7 Oct 2017 22:30:30 -0700 Subject: [PATCH] backend: speeding up get_index for admins --- montage/docs/api.md | 1 - montage/rdb.py | 19 ++++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/montage/docs/api.md b/montage/docs/api.md index 1d895915..49c6e182 100644 --- a/montage/docs/api.md +++ b/montage/docs/api.md @@ -1015,7 +1015,6 @@ Abbreviated information about a round for a coordinator: - `cancelled` (basically, it's deleted) - `finalized` (all done) - `config`: a dictionary of various settings for the round - - `is_closable`: this is true if the round has all the votes necessary to advance to the next round. For ranking rounds, you cannot preview results until all the ballots are submitted. ## round details Complete information about a round for a coordinator (similar to [`juror round details`](#juror-round-details): diff --git a/montage/rdb.py b/montage/rdb.py index 23feaf3c..fe94592e 100644 --- a/montage/rdb.py +++ b/montage/rdb.py @@ -311,16 +311,18 @@ def _get_rdb_session(self): return rdb_session - def _get_open_task_count(self): - rdb_session = self._get_rdb_session() + def _get_open_task_count(self, rdb_session=None): + if not rdb_session: + rdb_session = self._get_rdb_session() ret = (rdb_session.query(Vote) .filter(Vote.round_entry.has(round_id=self.id), Vote.status == ACTIVE_STATUS) .count()) return ret - def _get_task_count(self): - rdb_session = self._get_rdb_session() + def _get_task_count(self, rdb_session=None): + if not rdb_session: + rdb_session = self._get_rdb_session() ret = (rdb_session.query(Vote) .filter(Vote.round_entry.has(round_id=self.id), Vote.status != CANCELLED_STATUS) @@ -336,8 +338,8 @@ def get_count_map(self): rdb_session = self._get_rdb_session() re_count = len(self.round_entries) - open_task_count = self._get_open_task_count() - task_count = self._get_task_count() + open_task_count = self._get_open_task_count(rdb_session=rdb_session) + task_count = self._get_task_count(rdb_session=rdb_session) cancelled_task_count = rdb_session.query(Vote)\ .filter(Vote.round_entry.has(round_id=self.id), Vote.status == CANCELLED_STATUS)\ @@ -400,7 +402,6 @@ def is_closeable(self): return not active_votes return False - def to_info_dict(self): ret = {'id': self.id, 'name': self.name, @@ -413,12 +414,12 @@ def to_info_dict(self): 'jurors': [rj.to_info_dict() for rj in self.round_jurors], 'status': self.status, 'config': self.config, - 'round_sources': [], - 'is_closable': self.check_closability()} + 'round_sources': []} return ret def to_details_dict(self): ret = self.to_info_dict() + ret['is_closable'] = self.check_closability() ret['campaign'] = self.campaign.to_info_dict() ret['quorum'] = self.quorum ret['total_round_entries'] = len(self.round_entries)