This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/device/sync.py

180 lines
8.3 KiB
Python
Raw Normal View History

2018-04-30 17:58:19 +00:00
import re
2018-04-27 17:16:43 +00:00
from contextlib import suppress
from itertools import groupby
from typing import Iterable, List, Set
from psycopg2.errorcodes import UNIQUE_VIOLATION
from sqlalchemy.exc import IntegrityError
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.device.exceptions import NeedsId
from ereuse_devicehub.resources.device.models import Component, Computer, Device
from ereuse_devicehub.resources.event.models import Add, Remove
from teal.db import ResourceNotFound
class Sync:
"""Synchronizes the device and components with the database."""
@classmethod
def run(cls, device: Device,
components: Iterable[Component] or None,
2018-04-30 17:58:19 +00:00
force_creation: bool = False) -> (Device, List[Add or Remove]):
2018-04-27 17:16:43 +00:00
"""
Synchronizes the device and components with the database.
Identifies if the device and components exist in the database
and updates / inserts them as necessary.
This performs Add / Remove as necessary.
:param device: The device to add / update to the database.
:param components: Components that are inside of the device.
This method performs Add and Remove events
so the device ends up with these components.
Components are added / updated accordingly.
If this is empty, all components are removed.
If this is None, it means that there is
no info about components and the already
existing components of the device (in case
the device already exists) won't be touch.
:param force_creation: Shall we create the device even if
it doesn't generate HID or have an ID?
Only for the device param.
:return: A tuple of:
2018-04-30 17:58:19 +00:00
1. The device from the database (with an ID) whose
``components`` field contain the db version
of the passed-in components.
2. A list of Add / Remove (not yet added to session).
2018-04-27 17:16:43 +00:00
"""
2018-04-30 17:58:19 +00:00
db_device, _ = cls.execute_register(device, force_creation=force_creation)
db_components, events = [], []
if components is not None: # We have component info (see above)
blacklist = set() # type: Set[int]
not_new_components = set()
for component in components:
db_component, is_new = cls.execute_register(component, blacklist, parent=db_device)
2018-04-27 17:16:43 +00:00
db_components.append(db_component)
2018-04-30 17:58:19 +00:00
if not is_new:
not_new_components.add(db_component)
# We only want to perform Add/Remove to not new components
events = cls.add_remove(db_device, not_new_components)
db_device.components = db_components
return db_device, events
@classmethod
def execute_register(cls, device: Device,
blacklist: Set[int] = None,
2018-04-27 17:16:43 +00:00
force_creation: bool = False,
2018-04-30 17:58:19 +00:00
parent: Computer = None) -> (Device, bool):
2018-04-27 17:16:43 +00:00
"""
Synchronizes one device to the DB.
2018-04-30 17:58:19 +00:00
This method tries to create a device into the database, and
if it already exists it returns a "local synced version",
this is the same ``device`` you passed-in but with updated
values from the database one (like the id value).
When we say "local" we mean that if, the device existed on the
database, we do not "touch" any of its values on the DB.
2018-04-27 17:16:43 +00:00
:param device: The device to synchronize to the DB.
:param blacklist: A set of components already found by
Component.similar_one(). Pass-in an empty Set.
:param force_creation: Allow creating a device even if it
doesn't generate HID or doesn't have an
ID. Only valid for non-components.
Usually used when creating non-branded
custom computers (as they don't have
S/N).
:param parent: For components, the computer that contains them.
Helper used by Component.similar_one().
2018-04-30 17:58:19 +00:00
:return: A tuple with:
1. A synchronized device with the DB. It can be a new
device or an already existing one.
2. A flag stating if the device is new or it existed
already in the DB.
2018-04-27 17:16:43 +00:00
:raise NeedsId: The device has not any identifier we can use.
To still create the device use
``force_creation``.
:raise DatabaseError: Any other error from the DB.
"""
# Let's try to create the device
if not device.hid and not device.id:
# We won't be able to surely identify this device
if isinstance(device, Component):
with suppress(ResourceNotFound):
# Is there a component similar to ours?
db_component = device.similar_one(parent, blacklist)
# We blacklist this component so we
# ensure we don't get it again for another component
# with the same physical properties
blacklist.add(db_component.id)
2018-04-30 17:58:19 +00:00
return cls.merge(device, db_component), False
2018-04-27 17:16:43 +00:00
elif not force_creation:
raise NeedsId()
try:
2018-04-30 17:58:19 +00:00
with db.session.begin_nested():
# Create transaction savepoint to auto-rollback on insertion err
# Let's try to insert or update
db.session.add(device)
db.session.flush()
2018-04-27 17:16:43 +00:00
except IntegrityError as e:
if e.orig.diag.sqlstate == UNIQUE_VIOLATION:
2018-04-30 17:58:19 +00:00
db.session.rollback()
2018-04-27 17:16:43 +00:00
# This device already exists in the DB
2018-04-30 17:58:19 +00:00
field, value = re.findall('\(.*?\)', e.orig.diag.message_detail) # type: str
field = field.replace('(', '').replace(')', '')
value = value.replace('(', '').replace(')', '')
db_device = Device.query.filter(getattr(device.__class__, field) == value).one()
return cls.merge(device, db_device), False
2018-04-27 17:16:43 +00:00
else:
raise e
else:
2018-04-30 17:58:19 +00:00
return device, True # Our device is new
2018-04-27 17:16:43 +00:00
@classmethod
def merge(cls, device: Device, db_device: Device):
"""
Copies the physical properties of the device to the db_device.
"""
2018-04-30 17:58:19 +00:00
for field_name, value in device.physical_properties.items():
2018-04-27 17:16:43 +00:00
if value is not None:
2018-04-30 17:58:19 +00:00
setattr(db_device, field_name, value)
2018-04-27 17:16:43 +00:00
return db_device
@classmethod
def add_remove(cls, device: Device,
2018-04-30 17:58:19 +00:00
components: Set[Component]) -> List[Add or Remove]:
2018-04-27 17:16:43 +00:00
"""
2018-04-30 17:58:19 +00:00
Generates the Add and Remove events (but doesn't add them to
session).
:param device: A device which ``components`` attribute contains
the old list of components. The components that
are not in ``components`` will be Removed.
:param components: List of components that are potentially to
be Added. Some of them can already exist
on the device, in which case they won't
be re-added.
:return: A list of Add / Remove events.
2018-04-27 17:16:43 +00:00
"""
2018-04-30 17:58:19 +00:00
events = []
2018-04-27 17:16:43 +00:00
old_components = set(device.components)
2018-04-30 17:58:19 +00:00
adding = components - old_components
if adding:
add = Add(device=device, components=list(adding))
# For the components we are adding, let's remove them from their old parents
def g_parent(component: Component) -> int:
return component.parent or Computer(id=0) # Computer with id 0 is our Identity
for parent, _components in groupby(sorted(add.components, key=g_parent), key=g_parent):
if parent.id != 0:
events.append(Remove(device=parent, components=list(_components)))
events.append(add)
removing = old_components - components
if removing:
events.append(Remove(device=device, components=list(removing)))
2018-04-27 17:16:43 +00:00
return events