Améliorations des performances

This commit is contained in:
Jean-Marie Favreau 2024-12-11 20:16:08 +01:00
parent 526b83ec20
commit 504198b14f
7 changed files with 133 additions and 47 deletions

View File

@ -266,8 +266,7 @@ class CalendarList:
Q(other_versions__representative__isnull=True) Q(other_versions__representative__isnull=True)
).order_by("start_time", "title__unaccent__lower") ).order_by("start_time", "title__unaccent__lower")
if self.qs is None: qs = qs.select_related("exact_location").select_related("category").select_related("other_versions").select_related("other_versions__representative")
qs = qs.select_related("exact_location").select_related("category").select_related("other_versions").select_related("other_versions__representative")
self.events = qs self.events = qs
firstdate = datetime.fromordinal(self.c_firstdate.toordinal()) firstdate = datetime.fromordinal(self.c_firstdate.toordinal())

View File

@ -0,0 +1,25 @@
# Generated by Django 4.2.9 on 2024-12-11 19:04
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0126_message_user'),
]
operations = [
migrations.AddIndex(
model_name='event',
index=models.Index(fields=['end_day', 'end_time'], name='agenda_cult_end_day_4660a5_idx'),
),
migrations.AddIndex(
model_name='event',
index=models.Index(fields=['status'], name='agenda_cult_status_893243_idx'),
),
migrations.AddIndex(
model_name='event',
index=models.Index(fields=['recurrence_dtstart', 'recurrence_dtend'], name='agenda_cult_recurre_a8911c_idx'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.9 on 2024-12-11 19:12
from django.db import migrations, models
import django.db.models.functions.text
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0127_event_agenda_cult_end_day_4660a5_idx_and_more'),
]
operations = [
migrations.AddIndex(
model_name='event',
index=models.Index(models.F('start_time'), models.F('start_day'), models.F('end_day'), models.F('end_time'), django.db.models.functions.text.Lower('title'), name='datetimes title'),
),
]

View File

@ -0,0 +1,57 @@
# Generated by Django 4.2.9 on 2024-12-11 19:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0128_event_datetimes_title'),
]
operations = [
migrations.AddIndex(
model_name='batchimportation',
index=models.Index(fields=['created_date'], name='agenda_cult_created_a23990_idx'),
),
migrations.AddIndex(
model_name='batchimportation',
index=models.Index(fields=['status'], name='agenda_cult_status_54b205_idx'),
),
migrations.AddIndex(
model_name='batchimportation',
index=models.Index(fields=['created_date', 'recurrentImport'], name='agenda_cult_created_0296e4_idx'),
),
migrations.AddIndex(
model_name='duplicatedevents',
index=models.Index(fields=['representative'], name='agenda_cult_represe_9a4fa2_idx'),
),
migrations.AddIndex(
model_name='message',
index=models.Index(fields=['related_event'], name='agenda_cult_related_79de3c_idx'),
),
migrations.AddIndex(
model_name='message',
index=models.Index(fields=['user'], name='agenda_cult_user_id_42dc88_idx'),
),
migrations.AddIndex(
model_name='message',
index=models.Index(fields=['date'], name='agenda_cult_date_049c71_idx'),
),
migrations.AddIndex(
model_name='message',
index=models.Index(fields=['spam', 'closed'], name='agenda_cult_spam_22f9b3_idx'),
),
migrations.AddIndex(
model_name='place',
index=models.Index(fields=['name'], name='agenda_cult_name_222846_idx'),
),
migrations.AddIndex(
model_name='place',
index=models.Index(fields=['city'], name='agenda_cult_city_156dc7_idx'),
),
migrations.AddIndex(
model_name='place',
index=models.Index(fields=['location'], name='agenda_cult_locatio_6f3c05_idx'),
),
]

View File

@ -21,6 +21,7 @@ from django.utils import timezone
from django.contrib.postgres.search import TrigramSimilarity from django.contrib.postgres.search import TrigramSimilarity
from django.db.models import Q, Count, F, Subquery, OuterRef, Func from django.db.models import Q, Count, F, Subquery, OuterRef, Func
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.contrib.postgres.lookups import Unaccent
import recurrence.fields import recurrence.fields
import recurrence import recurrence
import copy import copy
@ -285,6 +286,10 @@ class DuplicatedEvents(models.Model):
class Meta: class Meta:
verbose_name = _("Duplicated events") verbose_name = _("Duplicated events")
verbose_name_plural = _("Duplicated events") verbose_name_plural = _("Duplicated events")
indexes = [
models.Index(fields=['representative']),
]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.events = None self.events = None
@ -460,6 +465,11 @@ class Place(models.Model):
verbose_name = _("Place") verbose_name = _("Place")
verbose_name_plural = _("Places") verbose_name_plural = _("Places")
ordering = ["name"] ordering = ["name"]
indexes = [
models.Index(fields=['name']),
models.Index(fields=['city']),
models.Index(fields=['location']),
]
def __str__(self): def __str__(self):
if self.address: if self.address:
@ -748,15 +758,6 @@ class Event(models.Model):
last = self.get_consolidated_end_day() last = self.get_consolidated_end_day()
return [first + timedelta(n) for n in range(int((last - first).days) + 1)] return [first + timedelta(n) for n in range(int((last - first).days) + 1)]
def get_nb_events_same_dates(self, remove_same_dup=True):
first = self.start_day
last = self.get_consolidated_end_day()
ignore_dup = None
if remove_same_dup:
ignore_dup = self.other_versions
calendar = CalendarList(first, last, exact=True, ignore_dup=ignore_dup)
return [(len(d.events), d.date) for dstr, d in calendar.get_calendar_days().items()]
def is_single_day(self, intuitive=True): def is_single_day(self, intuitive=True):
return self.start_day == self.get_consolidated_end_day(intuitive) return self.start_day == self.get_consolidated_end_day(intuitive)
@ -803,7 +804,11 @@ class Event(models.Model):
permissions = [("set_duplicated_event", "Can set an event as duplicated")] permissions = [("set_duplicated_event", "Can set an event as duplicated")]
indexes = [ indexes = [
models.Index(fields=["start_day", "start_time"]), models.Index(fields=["start_day", "start_time"]),
models.Index("start_time", Lower("title"), name="start_time title") models.Index(fields=["end_day", "end_time"]),
models.Index(fields=["status"]),
models.Index(fields=["recurrence_dtstart", "recurrence_dtend"]),
models.Index("start_time", Lower("title"), name="start_time title"),
models.Index("start_time", "start_day", "end_day", "end_time", Lower("title"), name="datetimes title")
] ]
def sorted_tags(self): def sorted_tags(self):
@ -1630,13 +1635,12 @@ class Event(models.Model):
def get_concurrent_events(self, remove_same_dup=True): def get_concurrent_events(self, remove_same_dup=True):
day = self.current_date if hasattr(self, "current_date") else self.start_day day = self.current_date if hasattr(self, "current_date") else self.start_day
day_events = CalendarDay(self.start_day).get_events() day_events = CalendarDay(day, qs = Event.objects.filter(status=Event.STATUS.PUBLISHED)).get_events()
return [ return [
e e
for e in day_events for e in day_events
if e != self if e != self
and self.is_concurrent_event(e, day) and self.is_concurrent_event(e, day)
and e.status == Event.STATUS.PUBLISHED
and (e.other_versions is None or e.other_versions != self.other_versions) and (e.other_versions is None or e.other_versions != self.other_versions)
] ]
@ -1745,6 +1749,14 @@ class Message(models.Model):
class Meta: class Meta:
verbose_name = _("Message") verbose_name = _("Message")
verbose_name_plural = _("Messages") verbose_name_plural = _("Messages")
indexes = [
models.Index(fields=['related_event']),
models.Index(fields=['user']),
models.Index(fields=['date']),
models.Index(fields=['spam', 'closed']),
]
subject = models.CharField( subject = models.CharField(
verbose_name=_("Subject"), verbose_name=_("Subject"),
@ -1961,6 +1973,11 @@ class BatchImportation(models.Model):
verbose_name = _("Batch importation") verbose_name = _("Batch importation")
verbose_name_plural = _("Batch importations") verbose_name_plural = _("Batch importations")
permissions = [("run_batchimportation", "Can run a batch importation")] permissions = [("run_batchimportation", "Can run a batch importation")]
indexes = [
models.Index(fields=['created_date']),
models.Index(fields=['status']),
models.Index(fields=['created_date', 'recurrentImport']),
]
created_date = models.DateTimeField(auto_now_add=True) created_date = models.DateTimeField(auto_now_add=True)

View File

@ -55,39 +55,7 @@
</article> </article>
{% endif %} {% endif %}
{% endwith %} {% endwith %}
<article>
{% with event.get_nb_events_same_dates as nb_events_same_dates %}
{% with nb_events_same_dates|length as c_dates %}
<header>
<h2>Voir aussi</h2>
{% if c_dates != 1 %}
<p class="remarque">
Retrouvez ci-dessous tous les événements
{% if event.is_single_day %}
à la même date
{% else %}
aux mêmes dates
{% endif %}
que l'événement affiché.
</p>
{% endif %}
</header>
<nav>
{% if c_dates == 1 %}
<a role="button" href="{% url 'day_view' nb_events_same_dates.0.1.year nb_events_same_dates.0.1.month nb_events_same_dates.0.1.day %}">Toute la journée</a>
{% else %}
<ul>
{% for nbevents_date in nb_events_same_dates %}
<li>
<a href="{% url 'day_view' nbevents_date.1.year nbevents_date.1.month nbevents_date.1.day %}">{{ nbevents_date.0 }} événement{{ nbevents_date.0 | pluralize }} le {{ nbevents_date.1 }}</a>
</li>
{% endfor %}
</ul>
{% endif %}
</nav>
{% endwith %}
{% endwith %}
</article>
{% if event.other_versions and not event.other_versions.fixed %} {% if event.other_versions and not event.other_versions.fixed %}
{% with poss_dup=event.get_other_versions|only_allowed:user.is_authenticated %} {% with poss_dup=event.get_other_versions|only_allowed:user.is_authenticated %}
{% if poss_dup|length > 0 %} {% if poss_dup|length > 0 %}

View File

@ -472,6 +472,8 @@ class EventDeleteView(
class EventDetailView(UserPassesTestMixin, DetailView): class EventDetailView(UserPassesTestMixin, DetailView):
model = Event model = Event
template_name = "agenda_culturel/page-event.html" template_name = "agenda_culturel/page-event.html"
queryset = Event.objects.select_related("exact_location").select_related("category").select_related("other_versions").select_related("other_versions__representative")
def test_func(self): def test_func(self):
return ( return (