2020-10-13 13:37:21 +00:00
|
|
|
""" This is the view for Snapshots """
|
|
|
|
|
2020-10-07 16:28:57 +00:00
|
|
|
import os
|
2020-10-07 16:37:23 +00:00
|
|
|
import json
|
2020-10-29 15:09:02 +00:00
|
|
|
import shutil
|
2020-12-04 15:58:53 +00:00
|
|
|
from datetime import datetime, timedelta
|
2018-04-27 17:16:43 +00:00
|
|
|
from distutils.version import StrictVersion
|
2018-06-15 13:31:03 +00:00
|
|
|
from uuid import UUID
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2020-12-29 16:36:28 +00:00
|
|
|
from flask import current_app as app, request, g
|
2018-05-30 10:49:40 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
2019-01-16 19:40:27 +00:00
|
|
|
from teal.marshmallow import ValidationError
|
2018-09-07 10:35:32 +00:00
|
|
|
from teal.resource import View
|
2020-12-04 15:58:53 +00:00
|
|
|
from teal.db import ResourceNotFound
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
2020-11-19 20:04:14 +00:00
|
|
|
from ereuse_devicehub.query import things_response
|
2020-12-29 16:36:28 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest,
|
2021-03-18 09:59:38 +00:00
|
|
|
InitTransfer, Live, Allocate, Deallocate,
|
2021-04-19 17:34:22 +00:00
|
|
|
Trade, Confirm)
|
2020-12-02 11:53:15 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device, Computer, DataStorage
|
2019-06-12 14:49:55 +00:00
|
|
|
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
|
2020-07-07 15:17:19 +00:00
|
|
|
from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity
|
2020-05-13 17:52:09 +00:00
|
|
|
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
|
2021-03-18 09:59:38 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2020-05-13 17:52:09 +00:00
|
|
|
|
2019-02-18 11:43:50 +00:00
|
|
|
SUPPORTED_WORKBENCH = StrictVersion('11.0')
|
|
|
|
|
2020-10-07 16:28:57 +00:00
|
|
|
|
2020-12-29 16:36:28 +00:00
|
|
|
def save_json(req_json, tmp_snapshots, user, live=False):
|
2020-10-07 16:28:57 +00:00
|
|
|
"""
|
|
|
|
This function allow save a snapshot in json format un a TMP_SNAPSHOTS directory
|
|
|
|
The file need to be saved with one name format with the stamptime and uuid joins
|
|
|
|
"""
|
2020-10-13 13:59:39 +00:00
|
|
|
uuid = req_json.get('uuid', '')
|
|
|
|
now = datetime.now()
|
|
|
|
year = now.year
|
|
|
|
month = now.month
|
|
|
|
day = now.day
|
2020-10-15 09:00:11 +00:00
|
|
|
hour = now.hour
|
2020-10-29 11:10:12 +00:00
|
|
|
minutes = now.minute
|
2020-10-13 13:59:39 +00:00
|
|
|
|
2020-10-15 09:00:11 +00:00
|
|
|
name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{user}_{uuid}.json"
|
2020-10-29 15:09:02 +00:00
|
|
|
path_dir_base = os.path.join(tmp_snapshots, user)
|
2020-12-29 16:36:28 +00:00
|
|
|
if live:
|
|
|
|
path_dir_base = tmp_snapshots
|
2020-10-29 15:09:02 +00:00
|
|
|
path_errors = os.path.join(path_dir_base, 'errors')
|
|
|
|
path_fixeds = os.path.join(path_dir_base, 'fixeds')
|
|
|
|
path_name = os.path.join(path_errors, name_file)
|
2020-10-07 17:58:09 +00:00
|
|
|
|
2020-10-29 15:09:02 +00:00
|
|
|
if not os.path.isdir(path_dir_base):
|
|
|
|
os.system(f'mkdir -p {path_errors}')
|
|
|
|
os.system(f'mkdir -p {path_fixeds}')
|
2020-10-07 16:28:57 +00:00
|
|
|
|
2020-10-09 18:54:12 +00:00
|
|
|
with open(path_name, 'w') as snapshot_file:
|
|
|
|
snapshot_file.write(json.dumps(req_json))
|
|
|
|
|
2020-10-08 12:47:11 +00:00
|
|
|
return path_name
|
2020-10-07 16:28:57 +00:00
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2020-12-29 16:36:28 +00:00
|
|
|
def move_json(tmp_snapshots, path_name, user, live=False):
|
2020-10-29 15:09:02 +00:00
|
|
|
"""
|
|
|
|
This function move the json than it's correct
|
|
|
|
"""
|
|
|
|
path_dir_base = os.path.join(tmp_snapshots, user)
|
2020-12-29 16:36:28 +00:00
|
|
|
if live:
|
|
|
|
path_dir_base = tmp_snapshots
|
2020-10-29 15:09:02 +00:00
|
|
|
if os.path.isfile(path_name):
|
|
|
|
shutil.copy(path_name, path_dir_base)
|
|
|
|
os.remove(path_name)
|
|
|
|
|
|
|
|
|
2020-11-27 17:10:51 +00:00
|
|
|
class AllocateMix():
|
|
|
|
model = None
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
""" Create one res_obj """
|
|
|
|
res_json = request.get_json()
|
|
|
|
res_obj = self.model(**res_json)
|
|
|
|
db.session.add(res_obj)
|
|
|
|
db.session().final_flush()
|
|
|
|
ret = self.schema.jsonify(res_obj)
|
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def find(self, args: dict):
|
|
|
|
res_objs = self.model.query.filter_by(author=g.user) \
|
|
|
|
.order_by(self.model.created.desc()) \
|
|
|
|
.paginate(per_page=200)
|
|
|
|
return things_response(
|
|
|
|
self.schema.dump(res_objs.items, many=True, nested=0),
|
|
|
|
res_objs.page, res_objs.per_page, res_objs.total,
|
|
|
|
res_objs.prev_num, res_objs.next_num
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class AllocateView(AllocateMix, View):
|
|
|
|
model = Allocate
|
|
|
|
|
2020-12-28 14:31:57 +00:00
|
|
|
|
2020-11-27 17:10:51 +00:00
|
|
|
class DeallocateView(AllocateMix, View):
|
|
|
|
model = Deallocate
|
|
|
|
|
|
|
|
|
2020-12-28 14:31:57 +00:00
|
|
|
class LiveView(View):
|
|
|
|
def post(self):
|
|
|
|
"""Posts an action."""
|
|
|
|
res_json = request.get_json(validate=False)
|
2020-12-29 16:36:28 +00:00
|
|
|
tmp_snapshots = app.config['TMP_LIVES']
|
|
|
|
path_live = save_json(res_json, tmp_snapshots, '', live=True)
|
|
|
|
res_json.pop('debug', None)
|
2021-01-04 14:53:58 +00:00
|
|
|
res_json.pop('elapsed', None)
|
|
|
|
res_json.pop('os', None)
|
2020-12-29 16:36:28 +00:00
|
|
|
res_json_valid = self.schema.load(res_json)
|
2020-12-28 14:31:57 +00:00
|
|
|
live = self.live(res_json_valid)
|
|
|
|
db.session.add(live)
|
|
|
|
db.session().final_flush()
|
|
|
|
ret = self.schema.jsonify(live)
|
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
2020-12-29 16:36:28 +00:00
|
|
|
move_json(tmp_snapshots, path_live, '', live=True)
|
2020-12-28 14:31:57 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_hdd_details(self, snapshot, device):
|
|
|
|
"""We get the liftime and serial_number of the disk"""
|
|
|
|
usage_time_hdd = None
|
|
|
|
serial_number = None
|
2021-01-13 17:11:41 +00:00
|
|
|
components = [c for c in snapshot['components']]
|
|
|
|
components.sort(key=lambda x: x.created)
|
|
|
|
for hd in components:
|
2020-12-28 14:31:57 +00:00
|
|
|
if not isinstance(hd, DataStorage):
|
|
|
|
continue
|
|
|
|
|
|
|
|
serial_number = hd.serial_number
|
|
|
|
for act in hd.actions:
|
|
|
|
if not act.type == "TestDataStorage":
|
|
|
|
continue
|
|
|
|
usage_time_hdd = act.lifetime
|
|
|
|
break
|
|
|
|
|
|
|
|
if usage_time_hdd:
|
|
|
|
break
|
|
|
|
|
|
|
|
if not serial_number:
|
|
|
|
"""There aren't any disk"""
|
|
|
|
raise ResourceNotFound("There aren't any disk in this device {}".format(device))
|
|
|
|
return usage_time_hdd, serial_number
|
|
|
|
|
|
|
|
def get_hid(self, snapshot):
|
|
|
|
device = snapshot.get('device') # type: Computer
|
|
|
|
components = snapshot.get('components')
|
|
|
|
if not device:
|
|
|
|
return None
|
|
|
|
if not components:
|
|
|
|
return device.hid
|
|
|
|
macs = [c.serial_number for c in components
|
|
|
|
if c.type == 'NetworkAdapter' and c.serial_number is not None]
|
|
|
|
macs.sort()
|
|
|
|
mac = ''
|
|
|
|
hid = device.hid
|
|
|
|
if not hid:
|
|
|
|
return hid
|
|
|
|
if macs:
|
|
|
|
mac = "-{mac}".format(mac=macs[0])
|
|
|
|
hid += mac
|
|
|
|
return hid
|
|
|
|
|
|
|
|
def live(self, snapshot):
|
|
|
|
"""If the device.allocated == True, then this snapshot create an action live."""
|
|
|
|
hid = self.get_hid(snapshot)
|
|
|
|
if not hid or not Device.query.filter(
|
2020-12-29 16:36:28 +00:00
|
|
|
Device.hid==hid).count():
|
2020-12-28 14:31:57 +00:00
|
|
|
raise ValidationError('Device not exist.')
|
|
|
|
|
|
|
|
device = Device.query.filter(
|
2021-01-08 09:46:34 +00:00
|
|
|
Device.hid==hid, Device.allocated==True).one()
|
2020-12-28 14:31:57 +00:00
|
|
|
# Is not necessary
|
|
|
|
if not device:
|
|
|
|
raise ValidationError('Device not exist.')
|
|
|
|
if not device.allocated:
|
|
|
|
raise ValidationError('Sorry this device is not allocated.')
|
|
|
|
|
|
|
|
usage_time_hdd, serial_number = self.get_hdd_details(snapshot, device)
|
|
|
|
|
|
|
|
data_live = {'usage_time_hdd': usage_time_hdd,
|
|
|
|
'serial_number': serial_number,
|
|
|
|
'snapshot_uuid': snapshot['uuid'],
|
|
|
|
'description': '',
|
|
|
|
'software': snapshot['software'],
|
|
|
|
'software_version': snapshot['version'],
|
|
|
|
'licence_version': snapshot['licence_version'],
|
2020-12-29 16:36:28 +00:00
|
|
|
'author_id': device.owner_id,
|
|
|
|
'agent_id': device.owner.individual.id,
|
2020-12-28 14:31:57 +00:00
|
|
|
'device': device}
|
|
|
|
|
|
|
|
live = Live(**data_live)
|
|
|
|
|
|
|
|
if not usage_time_hdd:
|
|
|
|
warning = f"We don't found any TestDataStorage for disk sn: {serial_number}"
|
|
|
|
live.severity = Severity.Warning
|
|
|
|
live.description = warning
|
|
|
|
return live
|
|
|
|
|
|
|
|
live.sort_actions()
|
|
|
|
diff_time = live.diff_time()
|
|
|
|
if diff_time is None:
|
|
|
|
warning = "Don't exist one previous live or snapshot as reference"
|
|
|
|
live.description += warning
|
|
|
|
live.severity = Severity.Warning
|
|
|
|
elif diff_time < timedelta(0):
|
|
|
|
warning = "The difference with the last live/snapshot is negative"
|
|
|
|
live.description += warning
|
|
|
|
live.severity = Severity.Warning
|
|
|
|
return live
|
|
|
|
|
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
class ActionView(View):
|
2018-08-03 16:15:08 +00:00
|
|
|
def post(self):
|
2019-05-11 14:27:22 +00:00
|
|
|
"""Posts an action."""
|
2018-08-03 16:15:08 +00:00
|
|
|
json = request.get_json(validate=False)
|
2019-02-18 11:43:50 +00:00
|
|
|
if not json or 'type' not in json:
|
2019-01-16 19:40:27 +00:00
|
|
|
raise ValidationError('Resource needs a type.')
|
2019-02-18 11:43:50 +00:00
|
|
|
# todo there should be a way to better get subclassess resource
|
|
|
|
# defs
|
|
|
|
resource_def = app.resources[json['type']]
|
|
|
|
if json['type'] == Snapshot.t:
|
2020-11-28 17:47:35 +00:00
|
|
|
tmp_snapshots = app.config['TMP_SNAPSHOTS']
|
|
|
|
path_snapshot = save_json(json, tmp_snapshots, g.user.email)
|
|
|
|
json.pop('debug', None)
|
|
|
|
a = resource_def.schema.load(json)
|
2020-10-08 12:47:11 +00:00
|
|
|
response = self.snapshot(a, resource_def)
|
2020-10-29 15:09:02 +00:00
|
|
|
move_json(tmp_snapshots, path_snapshot, g.user.email)
|
2020-10-08 12:47:11 +00:00
|
|
|
return response
|
2019-05-14 18:31:43 +00:00
|
|
|
if json['type'] == VisualTest.t:
|
|
|
|
pass
|
|
|
|
# TODO JN add compute rate with new visual test and old components device
|
2019-12-12 20:17:35 +00:00
|
|
|
if json['type'] == InitTransfer.t:
|
|
|
|
return self.transfer_ownership()
|
2020-11-28 17:47:35 +00:00
|
|
|
a = resource_def.schema.load(json)
|
2018-08-03 16:15:08 +00:00
|
|
|
Model = db.Model._decl_class_registry.data[json['type']]()
|
2019-05-11 14:27:22 +00:00
|
|
|
action = Model(**a)
|
2021-04-19 17:34:22 +00:00
|
|
|
if json['type'] == Trade.t:
|
2021-04-08 19:21:29 +00:00
|
|
|
return self.offer(action)
|
2019-05-11 14:27:22 +00:00
|
|
|
db.session.add(action)
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session().final_flush()
|
2019-05-11 14:27:22 +00:00
|
|
|
ret = self.schema.jsonify(action)
|
2018-08-03 16:15:08 +00:00
|
|
|
ret.status_code = 201
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session.commit()
|
2018-08-03 16:15:08 +00:00
|
|
|
return ret
|
|
|
|
|
2018-06-15 13:31:03 +00:00
|
|
|
def one(self, id: UUID):
|
2019-05-11 14:27:22 +00:00
|
|
|
"""Gets one action."""
|
|
|
|
action = Action.query.filter_by(id=id).one()
|
|
|
|
return self.schema.jsonify(action)
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2019-02-18 11:43:50 +00:00
|
|
|
def snapshot(self, snapshot_json: dict, resource_def):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Performs a Snapshot.
|
2018-05-13 13:13:12 +00:00
|
|
|
|
|
|
|
See `Snapshot` section in docs for more info.
|
|
|
|
"""
|
2018-04-30 17:58:19 +00:00
|
|
|
# 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
|
2020-05-13 17:52:09 +00:00
|
|
|
|
2020-10-08 12:47:11 +00:00
|
|
|
device = snapshot_json.pop('device') # type: Computer
|
|
|
|
components = None
|
|
|
|
if snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid):
|
|
|
|
components = snapshot_json.pop('components', None) # type: List[Component]
|
2020-11-12 20:00:07 +00:00
|
|
|
if isinstance(device, Computer) and device.hid:
|
|
|
|
device.add_mac_to_hid(components_snap=components)
|
2020-10-08 12:47:11 +00:00
|
|
|
snapshot = Snapshot(**snapshot_json)
|
|
|
|
|
|
|
|
# Remove new actions from devices so they don't interfere with sync
|
|
|
|
actions_device = set(e for e in device.actions_one)
|
|
|
|
device.actions_one.clear()
|
|
|
|
if components:
|
|
|
|
actions_components = tuple(set(e for e in c.actions_one) for c in components)
|
|
|
|
for component in components:
|
|
|
|
component.actions_one.clear()
|
|
|
|
|
|
|
|
assert not device.actions_one
|
|
|
|
assert all(not c.actions_one for c in components) if components else True
|
|
|
|
db_device, remove_actions = resource_def.sync.run(device, components)
|
|
|
|
|
|
|
|
del device # Do not use device anymore
|
|
|
|
snapshot.device = db_device
|
|
|
|
snapshot.actions |= remove_actions | actions_device # Set actions to snapshot
|
|
|
|
# commit will change the order of the components by what
|
|
|
|
# the DB wants. Let's get a copy of the list so we preserve order
|
|
|
|
ordered_components = OrderedSet(x for x in snapshot.components)
|
|
|
|
|
|
|
|
# Add the new actions to the db-existing devices and components
|
|
|
|
db_device.actions_one |= actions_device
|
|
|
|
if components:
|
|
|
|
for component, actions in zip(ordered_components, actions_components):
|
|
|
|
component.actions_one |= actions
|
|
|
|
snapshot.actions |= actions
|
|
|
|
|
|
|
|
if snapshot.software == SnapshotSoftware.Workbench:
|
|
|
|
# Check ownership of (non-component) device to from current.user
|
|
|
|
if db_device.owner_id != g.user.id:
|
|
|
|
raise InsufficientPermission()
|
|
|
|
# Compute ratings
|
|
|
|
try:
|
|
|
|
rate_computer, price = RateComputer.compute(db_device)
|
|
|
|
except CannotRate:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
snapshot.actions.add(rate_computer)
|
|
|
|
if price:
|
|
|
|
snapshot.actions.add(price)
|
|
|
|
elif snapshot.software == SnapshotSoftware.WorkbenchAndroid:
|
|
|
|
pass # TODO try except to compute RateMobile
|
|
|
|
# Check if HID is null and add Severity:Warning to Snapshot
|
|
|
|
if snapshot.device.hid is None:
|
|
|
|
snapshot.severity = Severity.Warning
|
2020-12-02 11:53:15 +00:00
|
|
|
|
2020-10-08 12:47:11 +00:00
|
|
|
db.session.add(snapshot)
|
|
|
|
db.session().final_flush()
|
|
|
|
ret = self.schema.jsonify(snapshot) # transform it back
|
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
2019-12-12 20:17:35 +00:00
|
|
|
|
|
|
|
def transfer_ownership(self):
|
|
|
|
"""Perform a InitTransfer action to change author_id of device"""
|
|
|
|
pass
|
2021-04-08 19:21:29 +00:00
|
|
|
|
2021-04-19 17:34:22 +00:00
|
|
|
def offer(self, offer: Trade):
|
|
|
|
self.create_first_confirmation(offer)
|
2021-04-08 19:21:29 +00:00
|
|
|
self.create_phantom_account(offer)
|
|
|
|
db.session.add(offer)
|
|
|
|
self.create_automatic_trade(offer)
|
|
|
|
|
|
|
|
db.session().final_flush()
|
|
|
|
ret = self.schema.jsonify(offer)
|
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
|
|
|
|
2021-04-19 17:34:22 +00:00
|
|
|
def create_first_confirmation(self, offer: Trade) -> None:
|
|
|
|
"""Do the first confirmation for the user than do the action"""
|
|
|
|
|
|
|
|
# check than the user than want to do the action is one of the users
|
|
|
|
# involved in the action
|
|
|
|
assert g.user.id in [offer.user_from_id, offer.user_to_id]
|
|
|
|
|
|
|
|
confirm = Confirm(user=g.user, trade=offer, devices=offer.devices)
|
|
|
|
db.session.add(confirm)
|
|
|
|
|
|
|
|
def create_phantom_account(self, offer) -> None:
|
2021-04-08 19:21:29 +00:00
|
|
|
"""
|
|
|
|
If exist both users not to do nothing
|
|
|
|
If exist from but not to:
|
|
|
|
search if exist in the DB
|
|
|
|
if exist use it
|
|
|
|
else create new one
|
|
|
|
The same if exist to but not from
|
|
|
|
|
|
|
|
"""
|
|
|
|
if offer.user_from_id and offer.user_to_id:
|
|
|
|
return
|
|
|
|
|
|
|
|
if offer.user_from_id and not offer.user_to_id:
|
2021-04-09 20:21:13 +00:00
|
|
|
assert g.user.id == offer.user_from_id
|
2021-04-08 19:21:29 +00:00
|
|
|
email = "{}_{}@dhub.com".format(str(offer.user_from_id), offer.code)
|
|
|
|
users = User.query.filter_by(email=email)
|
|
|
|
if users.first():
|
|
|
|
user = users.first()
|
2021-04-09 20:21:13 +00:00
|
|
|
offer.user_to = user
|
2021-04-08 19:21:29 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
user = User(email=email, password='', active=False, phantom=True)
|
|
|
|
db.session.add(user)
|
|
|
|
offer.user_to = user
|
|
|
|
|
|
|
|
if not offer.user_from_id and offer.user_to_id:
|
|
|
|
email = "{}_{}@dhub.com".format(str(offer.user_to_id), offer.code)
|
|
|
|
users = User.query.filter_by(email=email)
|
|
|
|
if users.first():
|
|
|
|
user = users.first()
|
2021-04-09 20:21:13 +00:00
|
|
|
offer.user_from = user
|
2021-04-08 19:21:29 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
user = User(email=email, password='', active=False, phantom=True)
|
|
|
|
db.session.add(user)
|
|
|
|
offer.user_from = user
|
|
|
|
|
2021-04-19 17:34:22 +00:00
|
|
|
def create_automatic_trade(self, offer: Trade) -> None:
|
|
|
|
# not do nothing if it's neccesary confirmation explicity
|
2021-04-08 19:21:29 +00:00
|
|
|
if offer.confirm:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Change the owner for every devices
|
|
|
|
for dev in offer.devices:
|
|
|
|
dev.owner = offer.user_to
|
|
|
|
if hasattr(dev, 'components'):
|
|
|
|
for c in dev.components:
|
|
|
|
c.owner = offer.user_to
|
|
|
|
|
2021-04-19 17:34:22 +00:00
|
|
|
# Create a new Confirmation for the user who does not perform the action
|
|
|
|
user = offer.user_from
|
|
|
|
if g.user == offer.user_from:
|
|
|
|
user = offer.user_to
|
|
|
|
confirm = Confirm(user=user, trade=offer, devices=offer.devices)
|
|
|
|
db.session.add(confirm)
|