Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

massively speeds up the grants contribution api call #9642

Merged
merged 3 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/grants/templates/grants/detail/_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h3 class="pt-2 pb-3">
{% trans "Latest Contributions" %}
</h3>
<h5 class="mb-5 text-muted" v-if="!transactions.grantTransactions?.length && !loadingTx">{% trans "No Activity for this Grant!" %}</h5>
<div class="px-0" v-if="transactions.grantTransactions?.length && !loadingTx">
<div class="px-0" v-if="transactions.grantTransactions?.length">
<div class="mb-5 pb-2 px-0 mx-sm-0">
<div class="col-12 px-0">
<div id="contributions">
Expand Down
30 changes: 18 additions & 12 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,9 +1541,13 @@ def grant_details_contributors(request, grant_id):
def grant_details_contributions(request, grant_id):
page = int(request.GET.get('page', 1))
network = request.GET.get('network', 'mainnet')
limit = int(request.GET.get('limit', 10))
limit = int(request.GET.get('limit', 100))
max_page_size = 300
if limit > max_page_size:
limit = max_page_size

try:
grant = Grant.objects.prefetch_related('subscriptions').get(
grant = Grant.objects.get(
pk=grant_id
)
except Grant.DoesNotExist:
Expand All @@ -1554,19 +1558,24 @@ def grant_details_contributions(request, grant_id):

_contributions = Contribution.objects.filter(
subscription__grant=grant,
subscription__network=network,
subscription__is_postive_vote=True
).prefetch_related('subscription', 'subscription__contributor_profile')
contributions = list(_contributions.order_by('-created_on'))
)

contributions = _contributions.order_by('-created_on')
# print(contributions)
all_pages = Paginator(contributions, limit)
this_page = all_pages.page(page)
start_index = (page - 1) * limit
end_index = (page) * limit
this_page = contributions[start_index:end_index]
response = dict()

all_contributions = []
for contribution in this_page:
# print(contribution.subscription)
# print(contribution.subscription.tx_id)
subscription = contribution.subscription
if not subscription.is_postive_vote:
continue
if subscription.network != network:
continue

contribution_json = {
k: getattr(contribution, k) for k in
Expand All @@ -1586,10 +1595,7 @@ def grant_details_contributions(request, grant_id):
all_contributions.append(contribution_json)

response['contributions'] = json.loads(json.dumps(all_contributions, default=str))
response['has_next'] = all_pages.page(page).has_next()
response['count'] = all_pages.count
response['num_pages'] = all_pages.num_pages
response['next_page_number'] = all_pages.page(page).next_page_number() if all_pages.page(page).has_next() else None
response['next_page_number'] = page + 1

return JsonResponse(response)

Expand Down