Skip to content

Commit

Permalink
Optimize admin queries
Browse files Browse the repository at this point in the history
  • Loading branch information
henribru committed Nov 25, 2022
1 parent c5a741e commit 28e728c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
25 changes: 19 additions & 6 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,11 +43,7 @@ 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')
context = {
'main_task': obj,
'sub_tasks': qs
}
context = {'main_task': obj, 'sub_tasks': obj.sub_tasks.all()}
return render_to_string(
template_name='admin/huey_monitor/taskmodel/column_name.html',
context=context,
Expand Down Expand Up @@ -168,6 +180,7 @@ def task_name(self, obj):
)
readonly_fields = ('create_dt',)
list_display_links = ('task_name',)
list_select_related = ('task',)
ordering = ('-create_dt',)
date_hierarchy = 'create_dt'
list_filter = ('task__name', 'signal_name', 'hostname')
Expand Down
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',
),
),
]
4 changes: 3 additions & 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 Expand Up @@ -106,6 +106,8 @@ class TaskModel(TimetrackingBaseModel):

@cached_property
def executing_dt(self):
if hasattr(self, "_executing_dt"):
return self._executing_dt
executing_signal = SignalInfoModel.objects.filter(
task_id=self.task_id,
signal_name=SIGNAL_EXECUTING
Expand Down

0 comments on commit 28e728c

Please sign in to comment.