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,46 +11,54 @@ from .utils_extra import picto_from_name
register = template.Library() register = template.Library()
import logging
logger = logging.getLogger(__name__)
def badge_rimport(status, nb, suffix, picto, cl, placement):
return ('<a href="'
+ reverse_lazy("recurrent_imports_status", args=[status])
+ '" class="badge ' + cl + '" data-placement="'
+ placement
+ '" data-tooltip="'
+ str(nb)
+ " importation"
+ pluralize(nb)
+ " récurrente"
+ pluralize(nb)
+ ' ' + suffix + '">'
+ picto_from_name(picto)
+ " "
+ str(nb)
+ "</a>")
@register.simple_tag @register.simple_tag
def show_badge_rimports(placement, status): def show_badges_rimports(placement):
newest = BatchImportation.objects.filter(recurrentImport=OuterRef("pk")).order_by( newest = BatchImportation.objects.filter(recurrentImport=OuterRef("pk")).order_by(
"-created_date" "-created_date"
) )
nb = ( request = RecurrentImport.objects.annotate(
RecurrentImport.objects.annotate(
last_run_status=Subquery(newest.values("status")[:1]) last_run_status=Subquery(newest.values("status")[:1])
) ).values("last_run_status").annotate(nb_last_run_by_status=Count("last_run_status"))
.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: nbs = {}
return mark_safe( for res in request:
'<a href="' nbs[res["last_run_status"]] = res["nb_last_run_by_status"]
+ reverse_lazy("recurrent_imports_status", args=[status])
+ '" class="badge ' + cl + '" data-placement="' result = ""
+ placement
+ '" data-tooltip="' for status in ["failed", "running"]:
+ str(nb) if status in nbs and nbs[status] != 0:
+ " importation" if status == "failed":
+ pluralize(nb) suffix = 'en erreur'
+ " récurrente" picto = "alert-triangle"
+ pluralize(nb) cl = "error"
+ ' ' + suffix + '">' else:
+ picto_from_name(picto) suffix = ' en cours'
+ " " picto = "refresh-cw"
+ str(nb) cl = "simple"
+ "</a>" result += badge_rimport(status, nbs[status], suffix, picto, cl, placement)
)
else: return mark_safe(result)
return ""

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: