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/event/schemas.py

285 lines
11 KiB
Python
Raw Normal View History

2018-06-20 21:18:15 +00:00
from flask import current_app as app
from marshmallow import ValidationError, validates_schema
from marshmallow.fields import Boolean, DateTime, Float, Integer, List, Nested, String, TimeDelta, \
UUID
from marshmallow.validate import Length, Range
2018-04-27 17:16:43 +00:00
from ereuse_devicehub.marshmallow import NestedOn
from ereuse_devicehub.resources.device.schemas import Component, Device
2018-06-10 16:47:49 +00:00
from ereuse_devicehub.resources.enums import AppearanceRange, Bios, FunctionalityRange, \
RATE_POSITIVE, RatingSoftware, SnapshotExpectedEvents, SnapshotSoftware, TestHardDriveLength
2018-06-12 14:50:05 +00:00
from ereuse_devicehub.resources.event import models as m
2018-04-27 17:16:43 +00:00
from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE
from ereuse_devicehub.resources.schemas import Thing
from ereuse_devicehub.resources.user.schemas import User
from teal.marshmallow import EnumField, Version
2018-04-27 17:16:43 +00:00
from teal.resource import Schema
class Event(Thing):
2018-06-15 13:31:03 +00:00
id = UUID(dump_only=True)
2018-06-26 13:35:13 +00:00
name = String(default='', validate=Length(STR_BIG_SIZE), description=m.Event.name.comment)
date = DateTime('iso', description=m.Event.date.comment)
error = Boolean(default=False, description=m.Event.error.comment)
incidence = Boolean(default=False, description=m.Event.incidence.comment)
snapshot = NestedOn('Snapshot', dump_only=True)
components = NestedOn(Component, dump_only=True, many=True)
2018-06-26 13:35:13 +00:00
description = String(default='', description=m.Event.description.comment)
2018-06-12 14:50:05 +00:00
author = NestedOn(User, dump_only=True, exclude=('token',))
2018-06-26 13:35:13 +00:00
closed = Boolean(missing=True, description=m.Event.closed.comment)
2018-04-27 17:16:43 +00:00
class EventWithOneDevice(Event):
device = NestedOn(Device)
2018-04-27 17:16:43 +00:00
class EventWithMultipleDevices(Event):
devices = NestedOn(Device, many=True)
2018-04-27 17:16:43 +00:00
class Add(EventWithOneDevice):
pass
class Remove(EventWithOneDevice):
pass
class Allocate(EventWithMultipleDevices):
to = NestedOn(User,
description='The user the devices are allocated to.')
2018-04-27 17:16:43 +00:00
organization = String(validate=Length(STR_SIZE),
description='The organization where the user was when this happened.')
class Deallocate(EventWithMultipleDevices):
from_rel = Nested(User,
2018-04-27 17:16:43 +00:00
data_key='from',
description='The user where the devices are not allocated to anymore.')
organization = String(validate=Length(STR_SIZE),
description='The organization where the user was when this happened.')
class EraseBasic(EventWithOneDevice):
2018-06-10 16:47:49 +00:00
start_time = DateTime(required=True, data_key='startTime')
end_time = DateTime(required=True, data_key='endTime')
2018-04-27 17:16:43 +00:00
secure_random_steps = Integer(validate=Range(min=0), required=True,
data_key='secureRandomSteps')
clean_with_zeros = Boolean(required=True, data_key='cleanWithZeros')
2018-06-10 16:47:49 +00:00
steps = NestedOn('Step', many=True, required=True)
2018-04-27 17:16:43 +00:00
class EraseSectors(EraseBasic):
pass
class Step(Schema):
2018-06-10 16:47:49 +00:00
type = String(description='Only required when it is nested.')
start_time = DateTime(required=True, data_key='startTime')
end_time = DateTime(required=True, data_key='endTime')
2018-04-27 17:16:43 +00:00
secure_random_steps = Integer(validate=Range(min=0),
required=True,
data_key='secureRandomSteps')
clean_with_zeros = Boolean(required=True, data_key='cleanWithZeros')
2018-06-10 16:47:49 +00:00
error = Boolean(default=False, description='Did the event fail?')
class StepZero(Step):
pass
class StepRandom(Step):
pass
class Rate(EventWithOneDevice):
rating = Integer(validate=Range(*RATE_POSITIVE),
dump_only=True,
data_key='ratingValue',
description='The rating for the content.')
algorithm_software = EnumField(RatingSoftware,
dump_only=True,
data_key='algorithmSoftware',
description='The algorithm used to produce this rating.')
algorithm_version = Version(dump_only=True,
data_key='algorithmVersion',
description='The algorithm_version of the algorithm_software.')
appearance = Integer(validate=Range(-3, 5), dump_only=True)
functionality = Integer(validate=Range(-3, 5),
dump_only=True,
data_key='functionalityScore')
class IndividualRate(Rate):
pass
class AggregateRate(Rate):
ratings = NestedOn(IndividualRate, many=True)
class PhotoboxRate(IndividualRate):
num = Integer(dump_only=True)
# todo Image
2018-06-20 21:18:15 +00:00
class PhotoboxUserRate(IndividualRate):
2018-06-10 16:47:49 +00:00
assembling = Integer()
parts = Integer()
buttons = Integer()
dents = Integer()
decolorization = Integer()
scratches = Integer()
tag_adhesive = Integer()
dirt = Integer()
2018-04-27 17:16:43 +00:00
2018-06-20 21:18:15 +00:00
class PhotoboxSystemRate(IndividualRate):
2018-06-10 16:47:49 +00:00
pass
2018-06-20 21:18:15 +00:00
class ManualRate(IndividualRate):
2018-06-10 16:47:49 +00:00
appearance_range = EnumField(AppearanceRange,
required=True,
data_key='appearanceRange',
description='Grades the imperfections that aesthetically '
'affect the device, but not its usage.')
functionality_range = EnumField(FunctionalityRange,
required=True,
data_key='functionalityRange',
description='Grades the defects of a device that affect its usage.')
2018-06-20 21:18:15 +00:00
labelling = Boolean(description='Sets if there are labels stuck that should be removed.')
class AppRate(ManualRate):
pass
class WorkbenchRate(ManualRate):
processor = Float()
ram = Float()
data_storage = Float()
graphic_card = Float()
bios = EnumField(Bios, description='How difficult it has been to set the bios to '
'boot from the network.')
2018-06-10 16:47:49 +00:00
class Install(EventWithOneDevice):
2018-06-19 16:38:42 +00:00
name = String(validate=Length(min=4, max=STR_BIG_SIZE),
2018-04-27 17:16:43 +00:00
required=True,
description='The name of the OS installed.')
elapsed = TimeDelta(precision=TimeDelta.SECONDS, required=True)
class Snapshot(EventWithOneDevice):
"""
The Snapshot updates the state of the device with information about
its components and events performed at them.
See docs for more info.
"""
2018-06-20 21:18:15 +00:00
uuid = UUID()
2018-06-10 16:47:49 +00:00
software = EnumField(SnapshotSoftware,
required=True,
description='The software that generated this Snapshot.')
version = Version(required=True, description='The version of the software.')
events = NestedOn(Event, many=True, dump_only=True)
2018-06-19 16:38:42 +00:00
expected_events = List(EnumField(SnapshotExpectedEvents),
data_key='expectedEvents',
description='Keep open this Snapshot until the following events'
'are performed. Setting this value will activate'
'the async Snapshot.')
2018-06-10 16:47:49 +00:00
device = NestedOn(Device)
2018-06-20 21:18:15 +00:00
elapsed = TimeDelta(precision=TimeDelta.SECONDS)
components = NestedOn(Component,
many=True,
description='A list of components that are inside of the device'
'at the moment of this Snapshot.'
'Order is preserved, so the component num 0 when'
'submitting is the component num 0 when returning it back.')
2018-04-27 17:16:43 +00:00
@validates_schema
def validate_workbench_version(self, data: dict):
2018-06-10 16:47:49 +00:00
if data['software'] == SnapshotSoftware.Workbench:
2018-04-27 17:16:43 +00:00
if data['version'] < app.config['MIN_WORKBENCH']:
raise ValidationError(
2018-06-10 16:47:49 +00:00
'Min. supported Workbench algorithm_version is '
'{}'.format(app.config['MIN_WORKBENCH']),
2018-04-27 17:16:43 +00:00
field_names=['version']
)
@validates_schema
def validate_components_only_workbench(self, data: dict):
2018-06-10 16:47:49 +00:00
if data['software'] != SnapshotSoftware.Workbench:
2018-06-20 21:18:15 +00:00
if data.get('components', None) is not None:
2018-04-27 17:16:43 +00:00
raise ValidationError('Only Workbench can add component info',
field_names=['components'])
2018-06-20 21:18:15 +00:00
@validates_schema
def validate_only_workbench_fields(self, data: dict):
"""Ensures workbench has ``elapsed`` and ``uuid`` and no others."""
# todo test
if data['software'] == SnapshotSoftware.Workbench:
if not data.get('uuid', None):
raise ValidationError('Snapshots from Workbench must have uuid',
field_names=['uuid'])
if not data.get('elapsed', None):
raise ValidationError('Snapshots from Workbench must have elapsed',
field_names=['elapsed'])
else:
if data.get('uuid', None):
raise ValidationError('Only Snapshots from Workbench can have uuid',
field_names=['uuid'])
if data.get('elapsed', None):
raise ValidationError('Only Snapshots from Workbench can have elapsed',
field_names=['elapsed'])
2018-04-27 17:16:43 +00:00
class Test(EventWithOneDevice):
elapsed = TimeDelta(precision=TimeDelta.SECONDS, required=True)
2018-06-10 16:47:49 +00:00
class TestDataStorage(Test):
2018-04-27 17:16:43 +00:00
length = EnumField(TestHardDriveLength, required=True)
status = String(validate=Length(max=STR_SIZE), required=True)
lifetime = TimeDelta(precision=TimeDelta.DAYS, required=True)
2018-06-19 16:38:42 +00:00
first_error = Integer(missing=0, data_key='firstError')
assessment = Boolean()
reallocated_sector_count = Integer(data_key='reallocatedSectorCount')
power_cycle_count = Integer(data_key='powerCycleCount')
reported_uncorrectable_errors = Integer(data_key='reportedUncorrectableErrors')
command_timeout = Integer(data_key='commandTimeout')
current_pending_sector_count = Integer(data_key='currentPendingSectorCount')
offline_uncorrectable = Integer(data_key='offlineUncorrectable')
remaining_lifetime_percentage = Integer(data_key='remainingLifetimePercentage')
2018-04-27 17:16:43 +00:00
class StressTest(Test):
pass
2018-06-19 16:38:42 +00:00
class Benchmark(EventWithOneDevice):
elapsed = TimeDelta(precision=TimeDelta.SECONDS)
class BenchmarkDataStorage(Benchmark):
read_speed = Float(required=True, data_key='readSpeed')
write_speed = Float(required=True, data_key='writeSpeed')
class BenchmarkWithRate(Benchmark):
rate = Integer(required=True)
class BenchmarkProcessor(BenchmarkWithRate):
pass
class BenchmarkProcessorSysbench(BenchmarkProcessor):
pass
class BenchmarkRamSysbench(BenchmarkWithRate):
pass