-
-
Notifications
You must be signed in to change notification settings - Fork 964
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
fix #1806: N+1 query issue on dashboard index page #1813
base: main
Are you sure you want to change the base?
Changes from 4 commits
eab41bd
785a17a
8fe1e01
f69cc41
14e2b13
ce82b7a
2288df6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import datetime | ||
import operator | ||
|
||
from django.contrib.contenttypes.models import ContentType | ||
from django.core.cache import cache | ||
from django.forms.models import model_to_dict | ||
from django.http.response import Http404, JsonResponse | ||
from django.shortcuts import render | ||
from django.utils.translation import gettext as _ | ||
|
||
from .models import Metric | ||
from .models import Datum, Metric | ||
from .utils import generation_key | ||
|
||
|
||
|
@@ -19,12 +19,28 @@ def index(request): | |
if data is None: | ||
metrics = [] | ||
for MC in Metric.__subclasses__(): | ||
metrics.extend(MC.objects.filter(show_on_dashboard=True)) | ||
metrics = sorted(metrics, key=operator.attrgetter("display_position")) | ||
metrics.extend( | ||
MC.objects.filter(show_on_dashboard=True).select_related("category") | ||
) | ||
|
||
content_types = ContentType.objects.get_for_models(*metrics) | ||
datum_queryset = Datum.objects.none() | ||
for metric, content_type in content_types.items(): | ||
datum_queryset = datum_queryset.union( | ||
Datum.objects.filter( | ||
content_type_id=content_type.id, object_id=metric.id | ||
).order_by("-timestamp")[0:1] | ||
) | ||
|
||
latest_datums = { | ||
(datum.object_id, datum.content_type_id): datum for datum in datum_queryset | ||
} | ||
|
||
data = [] | ||
for metric in metrics: | ||
data.append({"metric": metric, "latest": metric.data.latest()}) | ||
for metric, content_type in content_types.items(): | ||
if latest := latest_datums.get((metric.id, content_type.id)): | ||
data.append({"metric": metric, "latest": latest}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better way to write this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been trying to find an alternate approach (got pretty close with a Your original approach using I've made a suggestion for a slight rewrite that is a bit shorter and also closer to the original view implementation (it does so by pushing some logic onto the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! It's very interesting how you've moved most of the logic to The only concern is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, the current behavior for a metric with no data is for the view to crash, so it can't be worse than that 😁 |
||
data = sorted(data, key=lambda elem: elem["metric"].display_position) | ||
cache.set(key, data, 60 * 60, version=generation) | ||
|
||
return render(request, "dashboard/index.html", {"data": data}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding extra data to ensure the query will retrieve the latest datum for each metric.