From 5588b5252a982b3970268a01a6ddc8b245dc7ab7 Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Sun, 12 Mar 2017 10:30:42 +0000 Subject: [PATCH] Minimise database queries when populating automatic worklists When populating automatic worklists we currently throw away queries representing the list of story and task objects to appear in the worklist, and then later on reacquire this data using one query per story or task. This commit makes use of the ability to resolve stories and tasks from a pre-loaded cache to reuse the queries constructed when first populating the list to get the story data with no additional database interaction. This commit also makes the population of the task or story's due dates optional, as this doesn't make sense for automatic worklists (wherein due dates cannot be applied). Change-Id: I76f29e9b9100693691187cffa64b3e6a02120ccb --- storyboard/api/v1/wmodels.py | 29 ++++++++++++++++++----------- storyboard/db/api/worklists.py | 2 +- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/storyboard/api/v1/wmodels.py b/storyboard/api/v1/wmodels.py index da0878f4..cdd05648 100644 --- a/storyboard/api/v1/wmodels.py +++ b/storyboard/api/v1/wmodels.py @@ -727,7 +727,7 @@ class WorklistItem(base.APIBase): self.resolved_due_date = resolved @nodoc - def resolve_item(self, item, story_cache, task_cache): + def resolve_item(self, item, story_cache, task_cache, due_dates=True): user_id = request.current_user_id if item.item_type == 'story': story = story_cache.get(item.item_id) or stories_api.story_get( @@ -735,18 +735,22 @@ class WorklistItem(base.APIBase): if story is None: return False self.story = Story.from_db_model(story) - due_dates = [date.id for date in story.due_dates - if due_dates_api.visible(date, user_id)] - self.story.due_dates = due_dates + if due_dates: + self.story.due_dates = [ + date.id for date in story.due_dates + if due_dates_api.visible(date, user_id) + ] elif item.item_type == 'task': task = task_cache.get(item.item_id) or tasks_api.task_get( item.item_id, current_user=request.current_user_id) if task is None or task.story is None: return False self.task = Task.from_db_model(task) - due_dates = [date.id for date in task.due_dates - if due_dates_api.visible(date, user_id)] - self.task.due_dates = due_dates + if due_dates: + self.task.due_dates = [ + date.id for date in task.due_dates + if due_dates_api.visible(date, user_id) + ] return True @@ -813,14 +817,17 @@ class Worklist(base.APIBase): @nodoc def _resolve_automatic_items(self, worklist, user_id): - for item in worklists_api.filter_items(worklist): + items, stories, tasks = worklists_api.filter_items(worklist) + story_cache = {story.id: story for story in stories} + task_cache = {task.id: task for task in tasks} + for item in items: item_model = WorklistItem(**item) - valid = item_model.resolve_item(item_model, {}, {}) + valid = item_model.resolve_item(item_model, story_cache, + task_cache, due_dates=False) if not valid: continue - item_model.resolve_due_date(item_model) + item_model.resolved_due_date = None self.items.append(item_model) - self.items.sort(key=lambda x: x.list_position) @nodoc def _resolve_set_items(self, worklist, user_id, story_cache, task_cache): diff --git a/storyboard/db/api/worklists.py b/storyboard/db/api/worklists.py index 51616579..87880453 100644 --- a/storyboard/db/api/worklists.py +++ b/storyboard/db/api/worklists.py @@ -664,4 +664,4 @@ def filter_items(worklist): 'display_due_date': None }) - return items + return items, filtered_stories, filtered_tasks