From d8ad882effcb9fd8264f9f75e330d99c7360e43f Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 6 Aug 2019 04:11:17 +0200 Subject: [PATCH 01/10] =?UTF-8?q?xOptimisation=20des=20requ=C3=A8tes=20SQL?= =?UTF-8?q?=20chargement=20des=20switchs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/views.py | 59 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 201c8eb4..558666c6 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -129,7 +129,9 @@ def index(request): .select_related('ipv4__ip_type__extension') .select_related('domain__extension')) )) - .select_related('stack')) + .select_related('stack') + .select_related('switchbay__building__dormitory') + .select_related('model__constructor')) switch_list = SortTable.sort( switch_list, request.GET.get('col'), @@ -1265,7 +1267,14 @@ def make_machine_graph(): 'head_server': "#1c3777" } } - missing = list(Switch.objects.all()) + missing = list(Switch.objects.prefetch_related(Prefetch( + 'interface_set', + queryset=( + Interface.objects + .select_related('ipv4__ip_type__extension') + .select_related('domain__extension') + ) + ))) detected = [] for building in Building.objects.all(): # Visit all buildings @@ -1279,9 +1288,16 @@ def make_machine_graph(): } ) # Visit all switchs in this building - for switch in Switch.objects.filter(switchbay__building=building): + for switch in Switch.objects.filter(switchbay__building=building).prefetch_related( + Prefetch( + 'interface_set', + queryset=( + Interface.objects + .select_related('ipv4__ip_type__extension') + .select_related('domain__extension') + ))).select_related('switchbay__building').select_related('switchbay__building__dormitory').select_related('model__constructor'): dico['subs'][-1]['switchs'].append({ - 'name': switch.main_interface().domain.name, + 'name': switch.get_name, 'nombre': switch.number, 'model': switch.model, 'id': switch.id, @@ -1289,27 +1305,42 @@ def make_machine_graph(): 'ports': [] }) # visit all ports of this switch and add the switchs linked to it - for port in switch.ports.filter(related__isnull=False): + for port in switch.ports.filter(related__isnull=False).select_related('related__switch'): dico['subs'][-1]['switchs'][-1]['ports'].append({ 'numero': port.port, - 'related': port.related.switch.main_interface().domain.name + 'related': port.related.switch.get_name, }) - for ap in AccessPoint.all_ap_in(building): + for ap in AccessPoint.all_ap_in(building).prefetch_related(Prefetch( + 'interface_set', + queryset=( + Interface.objects + .select_related('ipv4__ip_type__extension') + .select_related('domain__extension') + )) + ): + switch = ap.switch().first() dico['subs'][-1]['bornes'].append({ 'name': ap.short_name, - 'switch': ap.switch()[0].main_interface().domain.name, - 'port': ap.switch()[0].ports.filter( + 'switch': switch.get_name, + 'port': switch.ports.filter( machine_interface__machine=ap - )[0].port + ).first().port }) - for server in Server.all_server_in(building): + for server in Server.all_server_in(building).prefetch_related(Prefetch( + 'interface_set', + queryset=( + Interface.objects + .select_related('ipv4__ip_type__extension') + .select_related('domain__extension') + )) + ): dico['subs'][-1]['machines'].append({ 'name': server.short_name, - 'switch': server.switch()[0].main_interface().domain.name, + 'switch': server.switch().first().get_name, 'port': Port.objects.filter( machine_interface__machine=server - )[0].port + ).first().port }) # While the list of forgotten ones is not empty @@ -1330,7 +1361,7 @@ def make_machine_graph(): switchbay__isnull=True).exclude(ports__related__isnull=False): dico['alone'].append({ 'id': switch.id, - 'name': switch.main_interface().domain.name + 'name': switch.get_name, }) # generate the dot file From 99fd3031430b378990eb86ffb2af7cbd3f3310b6 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 6 Aug 2019 04:20:53 +0200 Subject: [PATCH 02/10] Optimisation chargement des profils de ports --- topologie/views.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 558666c6..59532b99 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -165,8 +165,10 @@ def index(request): @can_view_all(PortProfile) def index_port_profile(request): pagination_number = GeneralOption.get_cached_value('pagination_number') - port_profile_list = PortProfile.objects.all().select_related( - 'vlan_untagged') + port_profile_list = PortProfile.objects.all()\ + .select_related('vlan_untagged')\ + .select_related('on_dormitory')\ + .prefetch_related('vlan_tagged') port_profile_list = re2o_paginator( request, port_profile_list, pagination_number) vlan_list = Vlan.objects.all().order_by('vlan_id') From 5034c2656ccf5d808726210c98144b7bb9d05e7f Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 6 Aug 2019 04:30:04 +0200 Subject: [PATCH 03/10] Optimisation chargement des modules switchs --- topologie/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/topologie/views.py b/topologie/views.py index 59532b99..c8be33b7 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -339,7 +339,8 @@ def index_model_switch(request): def index_module(request): """Display all modules of switchs""" module_list = ModuleSwitch.objects.all() - modular_switchs = Switch.objects.filter(model__is_modular=True) + modular_switchs = Switch.objects.filter(model__is_modular=True)\ + .select_related('model').prefetch_related('moduleonswitch_set__module') pagination_number = GeneralOption.get_cached_value('pagination_number') module_list = re2o_paginator(request, module_list, pagination_number) return render( From e17b28cae0369df3304136a86d4feef0434a791a Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 6 Aug 2019 17:46:13 +0200 Subject: [PATCH 04/10] Optimise le chargement des chambres --- topologie/models.py | 13 ++++++++----- topologie/views.py | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/topologie/models.py b/topologie/models.py index 2eaa3364..954fa9c4 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -611,16 +611,19 @@ class Building(AclMixin, RevMixin, models.Model): """Returns all ap of the building""" return AccessPoint.all_ap_in(self) - @cached_property - def cached_name(self): - return self.__str__() - - def __str__(self): + def get_name(self): if Dormitory.objects.count() > 1: return self.dormitory.name + " : " + self.name else: return self.name + @cached_property + def cached_name(self): + return self.get_name() + + def __str__(self): + return self.cached_name + class Port(AclMixin, RevMixin, models.Model): """ Definition d'un port. Relié à un switch(foreign_key), diff --git a/topologie/views.py b/topologie/views.py index c8be33b7..9781a7d2 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -217,7 +217,7 @@ def index_port(request, switch, switchid): @can_view_all(Room) def index_room(request): """ Affichage de l'ensemble des chambres""" - room_list = Room.objects + room_list = Room.objects.select_related('building__dormitory') room_list = SortTable.sort( room_list, request.GET.get('col'), From 84cab79d6f784b69a14b28373bede27f3219f80a Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 6 Aug 2019 17:50:27 +0200 Subject: [PATCH 05/10] Optimisation de l'affichage des switchs --- topologie/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/topologie/views.py b/topologie/views.py index 9781a7d2..97b430fc 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -310,7 +310,7 @@ def index_physical_grouping(request): @can_view_all(ModelSwitch, ConstructorSwitch) def index_model_switch(request): """ Affichage de l'ensemble des modèles de switches""" - model_switch_list = ModelSwitch.objects.select_related('constructor') + model_switch_list = ModelSwitch.objects.select_related('constructor').prefetch_related('switch_set__interface_set__domain') constructor_switch_list = ConstructorSwitch.objects model_switch_list = SortTable.sort( model_switch_list, From 0794056d19c1dce8cef79a656b47f50e4b05de8d Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 7 Aug 2019 00:08:19 +0200 Subject: [PATCH 06/10] Optimisation chargement des locaux --- topologie/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 97b430fc..01c21cfb 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -267,9 +267,9 @@ def index_physical_grouping(request): .prefetch_related( 'switch_set__interface_set__domain__extension' )) - building_list = Building.objects.all() - dormitory_list = Dormitory.objects.all() - switch_bay_list = SwitchBay.objects.select_related('building') + building_list = Building.objects.all().select_related('dormitory') + dormitory_list = Dormitory.objects.all().prefetch_related('building_set') + switch_bay_list = SwitchBay.objects.select_related('building__dormitory').prefetch_related('switch_set__interface_set__domain') stack_list = SortTable.sort( stack_list, request.GET.get('col'), From 08d9e04a9e38ff5b4eaa5c3ce0c016abb1264efb Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 7 Aug 2019 00:29:21 +0200 Subject: [PATCH 07/10] =?UTF-8?q?Optimisation=20affichage=20d=C3=A9taill?= =?UTF-8?q?=C3=A9=20d'un=20switch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 01c21cfb..d3acdff2 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -186,16 +186,18 @@ def index_port(request, switch, switchid): """ Affichage de l'ensemble des ports reliés à un switch particulier""" port_list = (Port.objects .filter(switch=switch) - .select_related('room') + .select_related('room__building__dormitory') .select_related('machine_interface__domain__extension') .select_related('machine_interface__machine__user') - .select_related('related__switch') + .select_related('machine_interface__machine__accesspoint') + .select_related('related__switch__switchbay__building__dormitory') .prefetch_related(Prefetch( 'related__switch__interface_set', queryset=(Interface.objects .select_related('domain__extension')) )) - .select_related('switch')) + .select_related('switch__switchbay__building__dormitory') + .select_related('switch__model__constructor')) port_list = SortTable.sort( port_list, request.GET.get('col'), From 0770da51d1ba9e1739f00df92544e881838e2041 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 7 Aug 2019 00:43:47 +0200 Subject: [PATCH 08/10] Correction de l'affichage des chambres/locaux --- topologie/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/topologie/models.py b/topologie/models.py index 954fa9c4..c4e0591b 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -802,7 +802,7 @@ class Room(AclMixin, RevMixin, models.Model): unique_together = ('name', 'building') def __str__(self): - return self.building.cached_name + self.name + return self.building.cached_name + ' ' + self.name class PortProfile(AclMixin, RevMixin, models.Model): From 105348b73aa79908909f183f08e8e45847a7795b Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 7 Aug 2019 00:44:44 +0200 Subject: [PATCH 09/10] =?UTF-8?q?Prechargement=20des=20donn=C3=A9es=20bati?= =?UTF-8?q?ment/r=C3=A9sidences=20pour=20selection=20des=20chambres?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/forms.py | 1 + 1 file changed, 1 insertion(+) diff --git a/topologie/forms.py b/topologie/forms.py index bdc8ba4e..72b499d4 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -93,6 +93,7 @@ class EditPortForm(FormRevMixin, ModelForm): Interface.objects.all().select_related('domain__extension') ) self.fields['related'].queryset = Port.objects.all().prefetch_related('switch__machine_ptr__interface_set__domain__extension') + self.fields['room'].queryset = Room.objects.all().select_related('building__dormitory') class AddPortForm(FormRevMixin, ModelForm): From 3f82adb720ad91e6caae16b23e54bc4b8e59be49 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 7 Aug 2019 02:09:57 +0200 Subject: [PATCH 10/10] =?UTF-8?q?Mise=20en=20cache=20affichage=20r=C3=A9si?= =?UTF-8?q?dence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/models.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/topologie/models.py b/topologie/models.py index c4e0591b..250e52a9 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -42,6 +42,7 @@ import itertools from django.db import models from django.db.models.signals import post_save, post_delete from django.utils.functional import cached_property +from django.core.cache import cache from django.dispatch import receiver from django.core.exceptions import ValidationError from django.db import IntegrityError @@ -586,6 +587,14 @@ class Dormitory(AclMixin, RevMixin, models.Model): """Returns all ap of the dorms""" return AccessPoint.all_ap_in(self.building_set.all()) + @classmethod + def is_multiple_dorms(cls): + multiple_dorms = cache.get('multiple_dorms') + if multiple_dorms: + return multiple_dorms + else: + return cache.get_or_set('multiple_dorms', cls.objects.count() > 1) + def __str__(self): return self.name @@ -612,7 +621,7 @@ class Building(AclMixin, RevMixin, models.Model): return AccessPoint.all_ap_in(self) def get_name(self): - if Dormitory.objects.count() > 1: + if Dormitory.is_multiple_dorms(): return self.dormitory.name + " : " + self.name else: return self.name