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

Optimize admin queries #110

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
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,9 +43,9 @@ 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,
'main_task': obj,
'sub_tasks': qs
}
return render_to_string(
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")
28 changes: 28 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,28 @@
# 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