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

[IMPROVEMENT] Various page query optimizations #127

Merged
merged 4 commits into from
Feb 27, 2019
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
4 changes: 2 additions & 2 deletions promgen/templates/promgen/shard_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ <h2><a href="{% url 'shard-detail' shard.id %}">{{ shard.name }}</a></h2>
{% endfor %}
</table>
</div>
{% endfor %}

{% endfor %}
</div>
{% endblock %}
72 changes: 50 additions & 22 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,34 @@ class ShardDetail(LoginRequiredMixin, DetailView):
queryset = models.Shard.objects\
.prefetch_related(
'service_set',
'service_set__owner',
'service_set__notifiers',
'service_set__notifiers__owner',
'service_set__rule_set',
'service_set__project_set',
'service_set__project_set__owner',
'service_set__project_set__farm',
'service_set__project_set__exporter_set',
'service_set__project_set__notifiers')
'service_set__project_set__notifiers',
'service_set__project_set__notifiers__owner'
)


class ServiceList(LoginRequiredMixin, ListView):
queryset = models.Service.objects\
.prefetch_related(
'notifiers',
'rule_set',
'rule_set__parent',
'project_set',
'project_set__farm',
'project_set__exporter_set',
'project_set__notifiers',
'shard',
)
queryset = models.Service.objects.prefetch_related(
"shard",
"rule_set",
"rule_set__parent",
"project_set",
"project_set__owner",
"project_set__notifiers",
"project_set__notifiers__owner",
"project_set__farm",
"project_set__exporter_set",
"owner",
"notifiers",
"notifiers__owner",
)


class HomeList(LoginRequiredMixin, ListView):
Expand All @@ -131,12 +139,16 @@ def get_queryset(self):
# and return just our list of services
return models.Service.objects.filter(pk__in=senders).prefetch_related(
'notifiers',
'notifiers__owner',
'owner',
'rule_set',
'rule_set__parent',
'project_set',
'project_set__farm',
'project_set__exporter_set',
'project_set__notifiers',
'project_set__owner',
'project_set__notifiers__owner',
'shard',
)

Expand Down Expand Up @@ -457,26 +469,42 @@ def post(self, request, pk):


class RulesList(LoginRequiredMixin, ListView, ServiceMixin):
template_name = 'promgen/rule_list.html'
queryset = models.Rule.objects\
.prefetch_related('content_type', 'content_object')
template_name = "promgen/rule_list.html"
queryset = models.Rule.objects.prefetch_related("content_type", "content_object")

def get_context_data(self, **kwargs):
context = super(RulesList, self).get_context_data(**kwargs)

site_rules = models.Rule.objects.filter(
content_type__model='site', content_type__app_label='sites'
).prefetch_related('content_object', 'rulelabel_set', 'ruleannotation_set')
content_type__model="site", content_type__app_label="sites"
).prefetch_related(
"content_object",
"rulelabel_set",
"ruleannotation_set",
)

service_rules = models.Rule.objects.filter(
content_type__model='service', content_type__app_label='promgen'
).prefetch_related('content_object', 'content_object__shard', 'rulelabel_set', 'ruleannotation_set', 'parent')
content_type__model="service", content_type__app_label="promgen"
).prefetch_related(
"content_object",
"content_object__shard",
"rulelabel_set",
"ruleannotation_set",
"parent",
)

project_rules = models.Rule.objects.filter(
content_type__model='project', content_type__app_label='promgen'
).prefetch_related('content_object', 'content_object__service', 'rulelabel_set', 'ruleannotation_set', 'parent')
content_type__model="project", content_type__app_label="promgen"
).prefetch_related(
"content_object",
"content_object__service",
"content_object__service__shard",
"rulelabel_set",
"ruleannotation_set",
"parent",
)

context['rule_list'] = chain(site_rules, service_rules, project_rules)
context["rule_list"] = chain(site_rules, service_rules, project_rules)

return context

Expand Down