Skip to content

Commit 6c8cf1d

Browse files
authored
Merge pull request #114 from boxine/new#110
Optimize admin queries
2 parents d077845 + ed33679 commit 6c8cf1d

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ You must change your Django settings and replace the app name:
196196

197197
* [dev](https://github.com/boxine/django-huey-monitor/compare/v0.5.0...master)
198198
* Test against Django v3.2, v4.0 and Python v3.8 - v3.10
199+
* Optimize Admin change list ([contributed by henribru](https://github.com/boxine/django-huey-monitor/pull/110))
199200
* _tbc_
200201
* [v0.5.0 - 10.02.2022](https://github.com/boxine/django-huey-monitor/compare/v0.4.6...v0.5.0)
201202
* Refactor models: Remove `TaskProgressModel` and store progress information into `TaskModel`

huey_monitor/admin.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from bx_django_utils.templatetags.humanize_time import human_duration
22
from django.contrib import admin, messages
33
from django.contrib.admin.views.main import ChangeList
4+
from django.db.models import OuterRef, Prefetch
45
from django.shortcuts import redirect
56
from django.template.loader import render_to_string
67
from django.urls import path, reverse
78
from django.utils import timezone
89
from django.utils.translation import gettext_lazy as _
910
from huey.contrib.djhuey import HUEY
11+
from huey.signals import SIGNAL_EXECUTING
1012

1113
from huey_monitor.models import SignalInfoModel, TaskModel
1214

@@ -17,7 +19,21 @@ def get_queryset(self, request):
1719
List only the main-tasks (sub-tasks will be inlined)
1820
"""
1921
qs = super().get_queryset(request)
20-
qs = qs.filter(parent_task__isnull=True)
22+
executing_dt = SignalInfoModel.objects.filter(
23+
task_id=OuterRef("task_id"), signal_name=SIGNAL_EXECUTING
24+
).values('create_dt')[:1]
25+
qs = (
26+
qs.filter(parent_task__isnull=True)
27+
.prefetch_related(
28+
Prefetch(
29+
"sub_tasks",
30+
queryset=TaskModel.objects.select_related("state")
31+
.annotate(executing_dt=executing_dt)
32+
.order_by('-create_dt'),
33+
)
34+
)
35+
.annotate(executing_dt=executing_dt)
36+
)
2137
return qs
2238

2339

@@ -27,10 +43,10 @@ def get_changelist(self, request, **kwargs):
2743
return TaskModelChangeList
2844

2945
def column_name(self, obj):
30-
qs = TaskModel.objects.filter(parent_task_id=obj.pk).order_by('-create_dt')
46+
qs = obj.sub_tasks.all()
3147
context = {
3248
'main_task': obj,
33-
'sub_tasks': qs
49+
'sub_tasks': qs,
3450
}
3551
return render_to_string(
3652
template_name='admin/huey_monitor/taskmodel/column_name.html',
@@ -175,3 +191,6 @@ def task_name(self, obj):
175191

176192
def has_change_permission(self, request, obj=None):
177193
return False
194+
195+
def get_queryset(self, request):
196+
return super().get_queryset(request).prefetch_related("task")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Django 3.2.14 on 2022-11-25 12:11
2+
3+
import django.db.models.deletion
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
('huey_monitor', '0009_fix_optinal_fields'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='taskmodel',
15+
name='parent_task',
16+
field=models.ForeignKey(
17+
blank=True,
18+
editable=False,
19+
help_text='Only set if this task is a sub task started from his parent.',
20+
null=True,
21+
on_delete=django.db.models.deletion.CASCADE,
22+
related_name='sub_tasks',
23+
to='huey_monitor.taskmodel',
24+
verbose_name='Parent Task',
25+
),
26+
),
27+
]

huey_monitor/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TaskModel(TimetrackingBaseModel):
4343
to='self',
4444
null=True, blank=True,
4545
editable=False,
46-
related_name='+',
46+
related_name='sub_tasks',
4747
on_delete=models.CASCADE,
4848
verbose_name=_('Parent Task'),
4949
help_text=_('Only set if this task is a sub task started from his parent.'),

poetry.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)