Skip to content

Commit

Permalink
Merge pull request #4763 from gitcoinco/kevin/profile_performance_5
Browse files Browse the repository at this point in the history
profile performance - saves 0.25 seconds by hitting the DB once total for activity tabs per profile load instead of once per activity type
  • Loading branch information
owocki authored Jul 11, 2019
2 parents 6f38d50 + d39770d commit 7e23f9f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions app/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1987,18 +1987,29 @@ def profile(request, handle):
context['ratings'] = range(0,5)
tabs = []

counts = all_activities.values('activity_type').order_by('activity_type').annotate(the_count=Count('activity_type'))
counts = {ele['activity_type']: ele['the_count'] for ele in counts}
for tab, name in activity_tabs:
activities = profile_filter_activities(all_activities, tab)
activities_count = activities.count()

# this functions as profile_filter_activities does
# except w. aggregate counts

if tab == 'all':
activities_count = sum([val for key, val in counts.items()])
else:
activities_count = counts.get(tab, 0)
if tab == 'start_work':
activities_count += counts.get("worker_approved", 0)


# dont draw a tab where the activities count is 0
if activities_count == 0:
continue

paginator = Paginator(activities, 10)

# buidl dict
obj = {'id': tab,
'name': name,
'objects': paginator.get_page(1),
'objects': [],
'count': activities_count,
'type': 'activity'
}
Expand Down

0 comments on commit 7e23f9f

Please sign in to comment.