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-05-30 10:49:40 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
2018-06-10 16:47:49 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Computer
|
|
|
|
from ereuse_devicehub.resources.enums import SnapshotSoftware
|
|
|
|
from ereuse_devicehub.resources.event.models import Event, Snapshot, TestDataStorage
|
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-05-13 13:13:12 +00:00
|
|
|
event = Event.query.filter_by(id=id).one()
|
|
|
|
return self.schema.jsonify(event)
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
SUPPORTED_WORKBENCH = StrictVersion('11.0')
|
|
|
|
|
|
|
|
|
|
|
|
class SnapshotView(View):
|
|
|
|
def post(self):
|
2018-05-13 13:13:12 +00:00
|
|
|
"""
|
|
|
|
Performs a Snapshot.
|
|
|
|
|
|
|
|
See `Snapshot` section in docs for more info.
|
|
|
|
"""
|
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
|
2018-06-10 16:47:49 +00:00
|
|
|
device = s.pop('device') # type: Computer
|
|
|
|
components = s.pop('components') if s['software'] == SnapshotSoftware.Workbench else None
|
|
|
|
if 'events' in s:
|
|
|
|
events = s.pop('events')
|
|
|
|
# todo perform events
|
2018-04-27 17:16:43 +00:00
|
|
|
# noinspection PyArgumentList
|
2018-04-30 17:58:19 +00:00
|
|
|
snapshot = Snapshot(**s)
|
2018-05-30 10:49:40 +00:00
|
|
|
snapshot.device, snapshot.events = self.resource_def.sync.run(device, components)
|
2018-05-11 16:58:48 +00:00
|
|
|
snapshot.components = snapshot.device.components
|
2018-06-10 16:47:49 +00:00
|
|
|
# todo compute rating
|
2018-05-13 13:13:12 +00:00
|
|
|
# commit will change the order of the components by what
|
2018-05-30 10:49:40 +00:00
|
|
|
# the DB wants. Let's get a copy of the list so we preserve order
|
|
|
|
ordered_components = OrderedSet(x for x in snapshot.components)
|
2018-04-27 17:16:43 +00:00
|
|
|
db.session.add(snapshot)
|
2018-05-13 13:13:12 +00:00
|
|
|
db.session.commit()
|
|
|
|
# todo we are setting snapshot dirty again with this components but
|
|
|
|
# we do not want to update it.
|
|
|
|
# The real solution is https://stackoverflow.com/questions/
|
|
|
|
# 24480581/set-the-insert-order-of-a-many-to-many-sqlalchemy-
|
|
|
|
# flask-app-sqlite-db?noredirect=1&lq=1
|
|
|
|
snapshot.components = ordered_components
|
2018-05-11 16:58:48 +00:00
|
|
|
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
|
2018-06-10 16:47:49 +00:00
|
|
|
test = TestDataStorage(snapshot_id=t.pop('snapshot'), device_id=t.pop('device'), **t)
|
2018-04-27 17:16:43 +00:00
|
|
|
return test
|
|
|
|
|
|
|
|
|
|
|
|
class StressTestView(View):
|
|
|
|
def post(self):
|
|
|
|
t = request.get_json() # type: dict
|