mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-26 06:32:26 +00:00
Work on improving performance when filtering logs
This commit is contained in:
parent
e2660d0bee
commit
808f2e2790
1 changed files with 60 additions and 37 deletions
|
@ -198,9 +198,10 @@ class MachineHistorySearch:
|
||||||
except IpList.DoesNotExist:
|
except IpList.DoesNotExist:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
return filter(
|
return (
|
||||||
lambda x: x.field_dict["ipv4_id"] == ip_id,
|
Version.objects.get_for_model(Interface)
|
||||||
Version.objects.get_for_model(Interface).order_by("revision__date_created")
|
.filter(serialized_data__icontains='"ipv4": {}'.format(ip_id))
|
||||||
|
.order_by("revision__date_created")
|
||||||
)
|
)
|
||||||
|
|
||||||
def _get_interfaces_for_mac(self, mac):
|
def _get_interfaces_for_mac(self, mac):
|
||||||
|
@ -209,9 +210,10 @@ class MachineHistorySearch:
|
||||||
:return: An iterable object with the Version objects
|
:return: An iterable object with the Version objects
|
||||||
of Interfaces with the given MAC address
|
of Interfaces with the given MAC address
|
||||||
"""
|
"""
|
||||||
return filter(
|
return (
|
||||||
lambda x: str(x.field_dict["mac_address"]) == mac,
|
Version.objects.get_for_model(Interface)
|
||||||
Version.objects.get_for_model(Interface).order_by("revision__date_created")
|
.filter(serialized_data__icontains='"mac_address": "{}"'.format(mac))
|
||||||
|
.order_by("revision__date_created")
|
||||||
)
|
)
|
||||||
|
|
||||||
def _get_machines_for_interface(self, interface):
|
def _get_machines_for_interface(self, interface):
|
||||||
|
@ -221,9 +223,10 @@ class MachineHistorySearch:
|
||||||
which the given interface was attributed
|
which the given interface was attributed
|
||||||
"""
|
"""
|
||||||
machine_id = interface.field_dict["machine_id"]
|
machine_id = interface.field_dict["machine_id"]
|
||||||
return filter(
|
return (
|
||||||
lambda x: x.field_dict["id"] == machine_id,
|
Version.objects.get_for_model(Machine)
|
||||||
Version.objects.get_for_model(Machine).order_by("revision__date_created")
|
.filter(serialized_data__icontains='"pk": {}'.format(machine_id))
|
||||||
|
.order_by("revision__date_created")
|
||||||
)
|
)
|
||||||
|
|
||||||
def _get_user_for_machine(self, machine):
|
def _get_user_for_machine(self, machine):
|
||||||
|
@ -355,9 +358,10 @@ class History:
|
||||||
|
|
||||||
# Get all the versions for this instance, with the oldest first
|
# Get all the versions for this instance, with the oldest first
|
||||||
self._last_version = None
|
self._last_version = None
|
||||||
interface_versions = filter(
|
interface_versions = (
|
||||||
lambda x: x.field_dict["id"] == instance_id,
|
Version.objects.get_for_model(model)
|
||||||
Version.objects.get_for_model(model).order_by("revision__date_created")
|
.filter(serialized_data__icontains='"pk": {}'.format(instance_id))
|
||||||
|
.order_by("revision__date_created")
|
||||||
)
|
)
|
||||||
|
|
||||||
for version in interface_versions:
|
for version in interface_versions:
|
||||||
|
@ -440,11 +444,18 @@ class VersionAction(HistoryEvent):
|
||||||
def _previous_version(self):
|
def _previous_version(self):
|
||||||
model = self.object_type()
|
model = self.object_type()
|
||||||
try:
|
try:
|
||||||
return next(
|
query = (
|
||||||
filter(
|
Q(
|
||||||
lambda x: x.field_dict["id"] == self.object_id() and x.revision.date_created < self.version.revision.date_created,
|
serialized_data__icontains='"pk": {}'.format(self.object_id())
|
||||||
Version.objects.get_for_model(model).order_by("-revision__date_created")
|
|
||||||
)
|
)
|
||||||
|
& Q(
|
||||||
|
revision__date_created_lt=self.version.revision.date_created
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return next(
|
||||||
|
Version.objects.get_for_model(model)
|
||||||
|
.filter(query)
|
||||||
|
.order_by("-revision__date_created")
|
||||||
)
|
)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return None
|
return None
|
||||||
|
@ -584,21 +595,29 @@ class UserHistory(History):
|
||||||
self.events = []
|
self.events = []
|
||||||
|
|
||||||
# Try to find an Adherent object
|
# Try to find an Adherent object
|
||||||
adherents = filter(
|
# If it exists, its id will be the same as the user's
|
||||||
lambda x: x.field_dict["user_ptr_id"] == user_id,
|
adherents = (
|
||||||
Version.objects.get_for_model(Adherent)
|
Version.objects.get_for_model(Adherent)
|
||||||
|
.filter(serialized_data__icontains='"pk": {}'.format(user_id))
|
||||||
)
|
)
|
||||||
obj = next(adherents, None)
|
try:
|
||||||
|
obj = adherents[0]
|
||||||
model = Adherent
|
model = Adherent
|
||||||
|
except IndexError:
|
||||||
|
obj = None
|
||||||
|
|
||||||
# Fallback on a Club
|
# Fallback on a Club
|
||||||
if obj is None:
|
if obj is None:
|
||||||
clubs = filter(
|
clubs = (
|
||||||
lambda x: x.field_dict["user_ptr_id"] == user_id,
|
|
||||||
Version.objects.get_for_model(Club)
|
Version.objects.get_for_model(Club)
|
||||||
|
.filter(serialized_data__icontains='"pk": {}'.format(user_id))
|
||||||
)
|
)
|
||||||
obj = next(clubs, None)
|
|
||||||
|
try:
|
||||||
|
obj = clubs[0]
|
||||||
model = Club
|
model = Club
|
||||||
|
except IndexError:
|
||||||
|
obj = None
|
||||||
|
|
||||||
# If nothing was found, abort
|
# If nothing was found, abort
|
||||||
if obj is None:
|
if obj is None:
|
||||||
|
@ -606,9 +625,10 @@ class UserHistory(History):
|
||||||
|
|
||||||
# Add in "related" elements the list of Machine objects
|
# Add in "related" elements the list of Machine objects
|
||||||
# that were once owned by this user
|
# that were once owned by this user
|
||||||
self.related = filter(
|
self.related = (
|
||||||
lambda x: x.field_dict["user_id"] == user_id,
|
Version.objects.get_for_model(Machine)
|
||||||
Version.objects.get_for_model(Machine).order_by("revision__date_created")
|
.filter(serialized_data__icontains='"user": {}'.format(user_id))
|
||||||
|
.order_by("-revision__date_created")
|
||||||
)
|
)
|
||||||
self.related = [RelatedHistory(
|
self.related = [RelatedHistory(
|
||||||
m.field_dict["name"] or _("None"),
|
m.field_dict["name"] or _("None"),
|
||||||
|
@ -618,9 +638,10 @@ class UserHistory(History):
|
||||||
|
|
||||||
# Get all the versions for this user, with the oldest first
|
# Get all the versions for this user, with the oldest first
|
||||||
self._last_version = None
|
self._last_version = None
|
||||||
user_versions = filter(
|
user_versions = (
|
||||||
lambda x: x.field_dict["id"] == user_id,
|
Version.objects.get_for_model(User)
|
||||||
Version.objects.get_for_model(User).order_by("revision__date_created")
|
.filter(serialized_data__icontains='"pk": {}'.format(user_id))
|
||||||
|
.order_by("revision__date_created")
|
||||||
)
|
)
|
||||||
|
|
||||||
for version in user_versions:
|
for version in user_versions:
|
||||||
|
@ -631,9 +652,10 @@ class UserHistory(History):
|
||||||
|
|
||||||
# Do the same thing for the Adherent of Club
|
# Do the same thing for the Adherent of Club
|
||||||
self._last_version = None
|
self._last_version = None
|
||||||
obj_versions = filter(
|
obj_versions = (
|
||||||
lambda x: x.field_dict["id"] == user_id,
|
Version.objects.get_for_model(model)
|
||||||
Version.objects.get_for_model(model).order_by("revision__date_created")
|
.filter(serialized_data__icontains='"pk": {}'.format(user_id))
|
||||||
|
.order_by("revision__date_created")
|
||||||
)
|
)
|
||||||
|
|
||||||
for version in obj_versions:
|
for version in obj_versions:
|
||||||
|
@ -696,10 +718,11 @@ class MachineHistory(History):
|
||||||
def get(self, machine_id):
|
def get(self, machine_id):
|
||||||
# Add as "related" histories the list of Interface objects
|
# Add as "related" histories the list of Interface objects
|
||||||
# that were once assigned to this machine
|
# that were once assigned to this machine
|
||||||
self.related = list(filter(
|
self.related = list(
|
||||||
lambda x: x.field_dict["machine_id"] == machine_id,
|
Version.objects.get_for_model(Interface)
|
||||||
Version.objects.get_for_model(Interface).order_by("revision__date_created")
|
.filter(serialized_data__icontains='"machine": {}'.format(machine_id))
|
||||||
))
|
.order_by("-revision__date_created")
|
||||||
|
)
|
||||||
|
|
||||||
# Create RelatedHistory objects and remove duplicates
|
# Create RelatedHistory objects and remove duplicates
|
||||||
self.related = [RelatedHistory(
|
self.related = [RelatedHistory(
|
||||||
|
|
Loading…
Reference in a new issue