2018-04-27 17:16:43 +00:00
|
|
|
from contextlib import suppress
|
2018-05-30 10:49:40 +00:00
|
|
|
from itertools import chain
|
2018-05-13 13:13:12 +00:00
|
|
|
from operator import attrgetter
|
2018-04-27 17:16:43 +00:00
|
|
|
from typing import Dict, Set
|
|
|
|
|
2018-06-14 13:14:23 +00:00
|
|
|
from sqlalchemy import BigInteger, Column, Enum as DBEnum, Float, ForeignKey, Integer, Sequence, \
|
|
|
|
SmallInteger, Unicode, inspect
|
2018-04-10 15:06:39 +00:00
|
|
|
from sqlalchemy.ext.declarative import declared_attr
|
2018-04-30 17:58:19 +00:00
|
|
|
from sqlalchemy.orm import ColumnProperty, backref, relationship
|
2018-05-30 10:49:40 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
2018-06-10 16:47:49 +00:00
|
|
|
from sqlalchemy_utils import ColorType
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-06-20 21:18:15 +00:00
|
|
|
from ereuse_devicehub.resources.enums import ComputerMonitorTechnologies, DataStorageInterface, \
|
|
|
|
RamFormat, RamInterface
|
2018-05-30 10:49:40 +00:00
|
|
|
from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE, STR_SM_SIZE, Thing
|
2018-06-10 16:47:49 +00:00
|
|
|
from ereuse_utils.naming import Naming
|
2018-05-30 10:49:40 +00:00
|
|
|
from teal.db import CASCADE, POLYMORPHIC_ID, POLYMORPHIC_ON, ResourceNotFound, check_range
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Device(Thing):
|
2018-06-10 16:47:49 +00:00
|
|
|
id = Column(BigInteger, Sequence('device_seq'), primary_key=True)
|
2018-06-20 21:18:15 +00:00
|
|
|
id.comment = """
|
|
|
|
The identifier of the device for this database.
|
|
|
|
"""
|
2018-04-27 17:16:43 +00:00
|
|
|
type = Column(Unicode(STR_SM_SIZE), nullable=False)
|
2018-06-10 16:47:49 +00:00
|
|
|
hid = Column(Unicode(STR_BIG_SIZE), unique=True)
|
2018-06-20 21:18:15 +00:00
|
|
|
hid.comment = """
|
|
|
|
The Hardware ID (HID) is the unique ID traceability systems
|
|
|
|
use to ID a device globally.
|
|
|
|
"""
|
2018-06-10 16:47:49 +00:00
|
|
|
model = Column(Unicode(STR_BIG_SIZE))
|
|
|
|
manufacturer = Column(Unicode(STR_SIZE))
|
|
|
|
serial_number = Column(Unicode(STR_SIZE))
|
|
|
|
weight = Column(Float(decimal_return_scale=3), check_range('weight', 0.1, 3))
|
2018-06-20 21:18:15 +00:00
|
|
|
weight.comment = """
|
|
|
|
The weight of the device in Kgm.
|
|
|
|
"""
|
2018-06-10 16:47:49 +00:00
|
|
|
width = Column(Float(decimal_return_scale=3), check_range('width', 0.1, 3))
|
2018-06-20 21:18:15 +00:00
|
|
|
width.comment = """
|
|
|
|
The width of the device in meters.
|
|
|
|
"""
|
2018-06-10 16:47:49 +00:00
|
|
|
height = Column(Float(decimal_return_scale=3), check_range('height', 0.1, 3))
|
2018-06-20 21:18:15 +00:00
|
|
|
height.comment = """
|
|
|
|
The height of the device in meters.
|
|
|
|
"""
|
2018-06-10 16:47:49 +00:00
|
|
|
depth = Column(Float(decimal_return_scale=3), check_range('depth', 0.1, 3))
|
|
|
|
color = Column(ColorType)
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2018-05-13 13:13:12 +00:00
|
|
|
@property
|
|
|
|
def events(self) -> list:
|
2018-06-10 16:47:49 +00:00
|
|
|
"""
|
|
|
|
All the events performed to the device,
|
|
|
|
ordered by ascending creation time.
|
|
|
|
"""
|
|
|
|
return sorted(chain(self.events_multiple, self.events_one), key=attrgetter('created'))
|
2018-05-13 13:13:12 +00:00
|
|
|
|
2018-06-15 13:31:03 +00:00
|
|
|
def __init__(self, **kw) -> None:
|
|
|
|
super().__init__(**kw)
|
2018-04-30 17:58:19 +00:00
|
|
|
with suppress(TypeError):
|
2018-06-15 13:31:03 +00:00
|
|
|
self.hid = Naming.hid(self.manufacturer, self.serial_number, self.model)
|
2018-04-30 17:58:19 +00:00
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
@property
|
2018-04-30 17:58:19 +00:00
|
|
|
def physical_properties(self) -> Dict[str, object or None]:
|
2018-04-27 17:16:43 +00:00
|
|
|
"""
|
|
|
|
Fields that describe the physical properties of a device.
|
|
|
|
|
|
|
|
:return A generator where each value is a tuple with tho fields:
|
|
|
|
- Column.
|
|
|
|
- Actual value of the column or None.
|
|
|
|
"""
|
|
|
|
# todo ensure to remove materialized values when start using them
|
|
|
|
# todo or self.__table__.columns if inspect fails
|
2018-04-30 17:58:19 +00:00
|
|
|
return {c.key: getattr(self, c.key, None)
|
2018-04-27 17:16:43 +00:00
|
|
|
for c in inspect(self.__class__).attrs
|
2018-04-30 17:58:19 +00:00
|
|
|
if isinstance(c, ColumnProperty)
|
|
|
|
and not getattr(c, 'foreign_keys', None)
|
|
|
|
and c.key not in {'id', 'type', 'created', 'updated', 'parent_id', 'hid'}}
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
@declared_attr
|
|
|
|
def __mapper_args__(cls):
|
|
|
|
"""
|
|
|
|
Defines inheritance.
|
|
|
|
|
|
|
|
From `the guide <http://docs.sqlalchemy.org/en/latest/orm/
|
|
|
|
extensions/declarative/api.html
|
|
|
|
#sqlalchemy.ext.declarative.declared_attr>`_
|
|
|
|
"""
|
2018-05-13 13:13:12 +00:00
|
|
|
args = {POLYMORPHIC_ID: cls.t}
|
|
|
|
if cls.t == 'Device':
|
2018-04-10 15:06:39 +00:00
|
|
|
args[POLYMORPHIC_ON] = cls.type
|
|
|
|
return args
|
|
|
|
|
2018-04-30 17:58:19 +00:00
|
|
|
def __lt__(self, other):
|
|
|
|
return self.id < other.id
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2018-05-13 13:13:12 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return '<{0.t} {0.id!r} model={0.model!r} S/N={0.serial_number!r}>'.format(self)
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
class Computer(Device):
|
2018-06-10 16:47:49 +00:00
|
|
|
id = Column(BigInteger, ForeignKey(Device.id), primary_key=True)
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-06-16 10:41:12 +00:00
|
|
|
@property
|
|
|
|
def events(self) -> list:
|
|
|
|
return sorted(chain(super().events, self.events_parent), key=attrgetter('created'))
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
class Desktop(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Laptop(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Netbook(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Server(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Microtower(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-06-20 21:18:15 +00:00
|
|
|
class ComputerMonitor(Device):
|
|
|
|
id = Column(BigInteger, ForeignKey(Device.id), primary_key=True)
|
|
|
|
size = Column(Float(decimal_return_scale=2), check_range('size', 2, 150))
|
|
|
|
size.comment = """
|
|
|
|
The size of the monitor in inches.
|
|
|
|
"""
|
|
|
|
technology = Column(DBEnum(ComputerMonitorTechnologies))
|
|
|
|
technology.comment = """
|
|
|
|
The technology the monitor uses to display the image.
|
|
|
|
"""
|
|
|
|
resolution_width = Column(SmallInteger, check_range('resolution_width', 10, 20000))
|
|
|
|
resolution_width.comment = """
|
|
|
|
The maximum horizontal resolution the monitor can natively support
|
|
|
|
in pixels.
|
|
|
|
"""
|
|
|
|
resolution_height = Column(SmallInteger, check_range('resolution_height', 10, 20000))
|
|
|
|
resolution_height.comment = """
|
|
|
|
The maximum vertical resolution the monitor can natively support
|
|
|
|
in pixels.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
class Component(Device):
|
2018-06-10 16:47:49 +00:00
|
|
|
id = Column(BigInteger, ForeignKey(Device.id), primary_key=True)
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-05-30 10:49:40 +00:00
|
|
|
parent_id = Column(BigInteger, ForeignKey(Computer.id))
|
2018-04-10 15:06:39 +00:00
|
|
|
parent = relationship(Computer,
|
2018-05-13 13:13:12 +00:00
|
|
|
backref=backref('components',
|
|
|
|
lazy=True,
|
|
|
|
cascade=CASCADE,
|
2018-05-30 10:49:40 +00:00
|
|
|
order_by=lambda: Component.id,
|
|
|
|
collection_class=OrderedSet),
|
2018-06-15 13:31:03 +00:00
|
|
|
primaryjoin=parent_id == Computer.id)
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
def similar_one(self, parent: Computer, blacklist: Set[int]) -> 'Component':
|
|
|
|
"""
|
|
|
|
Gets a component that:
|
|
|
|
- has the same parent.
|
|
|
|
- Doesn't generate HID.
|
|
|
|
- Has same physical properties.
|
|
|
|
:param parent:
|
|
|
|
:param blacklist: A set of components to not to consider
|
|
|
|
when looking for similar ones.
|
|
|
|
"""
|
|
|
|
assert self.hid is None, 'Don\'t use this method with a component that has HID'
|
|
|
|
component = self.__class__.query \
|
|
|
|
.filter_by(parent=parent, hid=None, **self.physical_properties) \
|
|
|
|
.filter(~Component.id.in_(blacklist)) \
|
|
|
|
.first()
|
|
|
|
if not component:
|
|
|
|
raise ResourceNotFound(self.type)
|
|
|
|
return component
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-05-13 13:13:12 +00:00
|
|
|
@property
|
|
|
|
def events(self) -> list:
|
2018-06-15 13:31:03 +00:00
|
|
|
return sorted(chain(super().events, self.events_components), key=attrgetter('created'))
|
2018-05-13 13:13:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JoinedComponentTableMixin:
|
|
|
|
@declared_attr
|
|
|
|
def id(cls):
|
|
|
|
return Column(BigInteger, ForeignKey(Component.id), primary_key=True)
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-05-13 13:13:12 +00:00
|
|
|
class GraphicCard(JoinedComponentTableMixin, Component):
|
2018-06-10 16:47:49 +00:00
|
|
|
memory = Column(SmallInteger, check_range('memory', min=1, max=10000))
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-06-10 16:47:49 +00:00
|
|
|
class DataStorage(JoinedComponentTableMixin, Component):
|
|
|
|
size = Column(Integer, check_range('size', min=1, max=10 ** 8))
|
2018-06-12 14:50:05 +00:00
|
|
|
interface = Column(DBEnum(DataStorageInterface))
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-06-10 16:47:49 +00:00
|
|
|
|
|
|
|
class HardDrive(DataStorage):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SolidStateDrive(DataStorage):
|
|
|
|
pass
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
2018-05-13 13:13:12 +00:00
|
|
|
class Motherboard(JoinedComponentTableMixin, Component):
|
2018-06-10 16:47:49 +00:00
|
|
|
slots = Column(SmallInteger, check_range('slots'))
|
|
|
|
usb = Column(SmallInteger, check_range('usb'))
|
|
|
|
firewire = Column(SmallInteger, check_range('firewire'))
|
|
|
|
serial = Column(SmallInteger, check_range('serial'))
|
|
|
|
pcmcia = Column(SmallInteger, check_range('pcmcia'))
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
2018-05-13 13:13:12 +00:00
|
|
|
class NetworkAdapter(JoinedComponentTableMixin, Component):
|
2018-06-10 16:47:49 +00:00
|
|
|
speed = Column(SmallInteger, check_range('speed', min=10, max=10000))
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
2018-05-13 13:13:12 +00:00
|
|
|
class Processor(JoinedComponentTableMixin, Component):
|
2018-05-11 16:58:48 +00:00
|
|
|
speed = Column(Float, check_range('speed', 0.1, 15))
|
|
|
|
cores = Column(SmallInteger, check_range('cores', 1, 10))
|
|
|
|
address = Column(SmallInteger, check_range('address', 8, 256))
|
|
|
|
|
|
|
|
|
2018-05-13 13:13:12 +00:00
|
|
|
class RamModule(JoinedComponentTableMixin, Component):
|
2018-04-27 17:16:43 +00:00
|
|
|
size = Column(SmallInteger, check_range('size', min=128, max=17000))
|
|
|
|
speed = Column(Float, check_range('speed', min=100, max=10000))
|
2018-06-12 14:50:05 +00:00
|
|
|
interface = Column(DBEnum(RamInterface))
|
|
|
|
format = Column(DBEnum(RamFormat))
|