2018-04-27 17:16:43 +00:00
|
|
|
from distutils.version import StrictVersion
|
|
|
|
|
2018-05-11 16:58:48 +00:00
|
|
|
from flask import request
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.device.sync import Sync
|
|
|
|
from ereuse_devicehub.resources.event.enums import SoftwareType
|
|
|
|
from ereuse_devicehub.resources.event.models import Event, Snapshot, TestHardDrive
|
2018-04-10 15:06:39 +00:00
|
|
|
from teal.resource import View
|
|
|
|
|
|
|
|
|
|
|
|
class EventView(View):
|
2018-04-27 17:16:43 +00:00
|
|
|
def one(self, id: int):
|
2018-04-10 15:06:39 +00:00
|
|
|
"""Gets one event."""
|
2018-04-27 17:16:43 +00:00
|
|
|
return Event.query.filter_by(id=id).one()
|
|
|
|
|
|
|
|
|
|
|
|
SUPPORTED_WORKBENCH = StrictVersion('11.0')
|
|
|
|
|
|
|
|
|
|
|
|
class SnapshotView(View):
|
|
|
|
def post(self):
|
|
|
|
"""Creates a Snapshot."""
|
2018-04-30 17:58:19 +00:00
|
|
|
s = request.get_json()
|
|
|
|
# Note that if we set the device / components into the snapshot
|
|
|
|
# model object, when we flush them to the db we will flush
|
|
|
|
# snapshot, and we want to wait to flush snapshot at the end
|
|
|
|
device = s.pop('device')
|
|
|
|
components = s.pop('components') if s['software'] == SoftwareType.Workbench else None
|
2018-04-27 17:16:43 +00:00
|
|
|
# noinspection PyArgumentList
|
2018-04-30 17:58:19 +00:00
|
|
|
snapshot = Snapshot(**s)
|
|
|
|
snapshot.device, snapshot.events = Sync.run(device, components, snapshot.force_creation)
|
2018-05-11 16:58:48 +00:00
|
|
|
snapshot.components = snapshot.device.components
|
2018-04-27 17:16:43 +00:00
|
|
|
db.session.add(snapshot)
|
2018-05-11 16:58:48 +00:00
|
|
|
db.session.flush() # Take to DB so we get db-generated values
|
|
|
|
ret = self.schema.jsonify(snapshot) # transform it back
|
|
|
|
ret.status_code = 201
|
|
|
|
return ret
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestHardDriveView(View):
|
|
|
|
def post(self):
|
|
|
|
t = request.get_json() # type: dict
|
|
|
|
# noinspection PyArgumentList
|
|
|
|
test = TestHardDrive(snapshot_id=t.pop('snapshot'), device_id=t.pop('device'), **t)
|
|
|
|
return test
|
|
|
|
|
|
|
|
|
|
|
|
class StressTestView(View):
|
|
|
|
def post(self):
|
|
|
|
t = request.get_json() # type: dict
|