Amélioration des preformances des requêtes

This commit is contained in:
Jean-Marie Favreau 2024-11-13 12:00:21 +01:00
parent 0bdd8693ec
commit ed2f530f0c
4 changed files with 47 additions and 40 deletions

View File

@ -242,7 +242,7 @@ class CalendarList:
Q(other_versions__isnull=True) | Q(other_versions__isnull=True) |
Q(other_versions__representative=F('pk')) | Q(other_versions__representative=F('pk')) |
Q(other_versions__representative__isnull=True) Q(other_versions__representative__isnull=True)
).order_by("start_time", "title__unaccent__lower").prefetch_related("exact_location").prefetch_related("category").prefetch_related("other_versions") ).order_by("start_time", "title__unaccent__lower").prefetch_related("exact_location").prefetch_related("category").prefetch_related("other_versions").prefetch_related("other_versions__representative")
firstdate = datetime.fromordinal(self.c_firstdate.toordinal()) firstdate = datetime.fromordinal(self.c_firstdate.toordinal())
if firstdate.tzinfo is None or firstdate.tzinfo.utcoffset(firstdate) is None: if firstdate.tzinfo is None or firstdate.tzinfo.utcoffset(firstdate) is None:

View File

@ -65,8 +65,7 @@
<li> <li>
<div> <div>
{% if perms.agenda_culturel.view_recurrentimport %} {% if perms.agenda_culturel.view_recurrentimport %}
{% show_badge_rimports "bottom" "failed" %} {% show_badges_rimports "bottom" %}
{% show_badge_rimports "bottom" "running" %}
{% endif %} {% endif %}
{% if perms.agenda_culturel.change_event %} {% if perms.agenda_culturel.change_event %}
{% show_badges_events "bottom" %} {% show_badges_events "bottom" %}

View File

@ -2,7 +2,7 @@ from django import template
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.template.defaultfilters import pluralize from django.template.defaultfilters import pluralize
from django.db.models import OuterRef, Subquery from django.db.models import OuterRef, Subquery, Count
from agenda_culturel.models import RecurrentImport, BatchImportation from agenda_culturel.models import RecurrentImport, BatchImportation
@ -11,32 +11,13 @@ from .utils_extra import picto_from_name
register = template.Library() register = template.Library()
import logging
@register.simple_tag logger = logging.getLogger(__name__)
def show_badge_rimports(placement, status):
newest = BatchImportation.objects.filter(recurrentImport=OuterRef("pk")).order_by(
"-created_date"
)
nb = (
RecurrentImport.objects.annotate(
last_run_status=Subquery(newest.values("status")[:1])
)
.filter(last_run_status=status)
.count()
)
if status == BatchImportation.STATUS.FAILED:
suffix = 'en erreur'
picto = "alert-triangle"
cl = "error"
else:
suffix = ' en cours'
picto = "refresh-cw"
cl = "simple"
if nb != 0: def badge_rimport(status, nb, suffix, picto, cl, placement):
return mark_safe( return ('<a href="'
'<a href="'
+ reverse_lazy("recurrent_imports_status", args=[status]) + reverse_lazy("recurrent_imports_status", args=[status])
+ '" class="badge ' + cl + '" data-placement="' + '" class="badge ' + cl + '" data-placement="'
+ placement + placement
@ -50,7 +31,34 @@ def show_badge_rimports(placement, status):
+ picto_from_name(picto) + picto_from_name(picto)
+ " " + " "
+ str(nb) + str(nb)
+ "</a>" + "</a>")
@register.simple_tag
def show_badges_rimports(placement):
newest = BatchImportation.objects.filter(recurrentImport=OuterRef("pk")).order_by(
"-created_date"
) )
request = RecurrentImport.objects.annotate(
last_run_status=Subquery(newest.values("status")[:1])
).values("last_run_status").annotate(nb_last_run_by_status=Count("last_run_status"))
nbs = {}
for res in request:
nbs[res["last_run_status"]] = res["nb_last_run_by_status"]
result = ""
for status in ["failed", "running"]:
if status in nbs and nbs[status] != 0:
if status == "failed":
suffix = 'en erreur'
picto = "alert-triangle"
cl = "error"
else: else:
return "" suffix = ' en cours'
picto = "refresh-cw"
cl = "simple"
result += badge_rimport(status, nbs[status], suffix, picto, cl, placement)
return mark_safe(result)

View File

@ -53,7 +53,7 @@ def tag_not_in_db(tag, tags):
@register.simple_tag @register.simple_tag
def show_suggested_tags(filter): def show_suggested_tags(filter):
filter.form.full_clean() filter.form.full_clean()
tags = Tag.objects.all().filter(principal=True).order_by("name") tags = Tag.objects.all().filter(principal=True).order_by("name").prefetch_related("category")
result = "Suggestion&nbsp;:" result = "Suggestion&nbsp;:"
for t in tags: for t in tags: