Skip to content

Commit

Permalink
Optimize admin queries
Browse files Browse the repository at this point in the history
Contributed by https://github.com/henribru in #110

Co-authored-by: henribru <6639509+henribru@users.noreply.github.com>
  • Loading branch information
Jens Diemer and henribru committed Dec 28, 2022
1 parent d077845 commit ed33679
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ You must change your Django settings and replace the app name:

* [dev](https://github.com/boxine/django-huey-monitor/compare/v0.5.0...master)
* Test against Django v3.2, v4.0 and Python v3.8 - v3.10
* Optimize Admin change list ([contributed by henribru](https://github.com/boxine/django-huey-monitor/pull/110))
* _tbc_
* [v0.5.0 - 10.02.2022](https://github.com/boxine/django-huey-monitor/compare/v0.4.6...v0.5.0)
* Refactor models: Remove `TaskProgressModel` and store progress information into `TaskModel`
Expand Down
25 changes: 22 additions & 3 deletions huey_monitor/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from bx_django_utils.templatetags.humanize_time import human_duration
from django.contrib import admin, messages
from django.contrib.admin.views.main import ChangeList
from django.db.models import OuterRef, Prefetch
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.urls import path, reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from huey.contrib.djhuey import HUEY
from huey.signals import SIGNAL_EXECUTING

from huey_monitor.models import SignalInfoModel, TaskModel

Expand All @@ -17,7 +19,21 @@ def get_queryset(self, request):
List only the main-tasks (sub-tasks will be inlined)
"""
qs = super().get_queryset(request)
qs = qs.filter(parent_task__isnull=True)
executing_dt = SignalInfoModel.objects.filter(
task_id=OuterRef("task_id"), signal_name=SIGNAL_EXECUTING
).values('create_dt')[:1]
qs = (
qs.filter(parent_task__isnull=True)
.prefetch_related(
Prefetch(
"sub_tasks",
queryset=TaskModel.objects.select_related("state")
.annotate(executing_dt=executing_dt)
.order_by('-create_dt'),
)
)
.annotate(executing_dt=executing_dt)
)
return qs


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

def column_name(self, obj):
qs = TaskModel.objects.filter(parent_task_id=obj.pk).order_by('-create_dt')
qs = obj.sub_tasks.all()
context = {
'main_task': obj,
'sub_tasks': qs
'sub_tasks': qs,
}
return render_to_string(
template_name='admin/huey_monitor/taskmodel/column_name.html',
Expand Down Expand Up @@ -175,3 +191,6 @@ def task_name(self, obj):

def has_change_permission(self, request, obj=None):
return False

def get_queryset(self, request):
return super().get_queryset(request).prefetch_related("task")
27 changes: 27 additions & 0 deletions huey_monitor/migrations/0010_alter_taskmodel_parent_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.14 on 2022-11-25 12:11

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('huey_monitor', '0009_fix_optinal_fields'),
]

operations = [
migrations.AlterField(
model_name='taskmodel',
name='parent_task',
field=models.ForeignKey(
blank=True,
editable=False,
help_text='Only set if this task is a sub task started from his parent.',
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name='sub_tasks',
to='huey_monitor.taskmodel',
verbose_name='Parent Task',
),
),
]
2 changes: 1 addition & 1 deletion huey_monitor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TaskModel(TimetrackingBaseModel):
to='self',
null=True, blank=True,
editable=False,
related_name='+',
related_name='sub_tasks',
on_delete=models.CASCADE,
verbose_name=_('Parent Task'),
help_text=_('Only set if this task is a sub task started from his parent.'),
Expand Down
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed33679

Please sign in to comment.