modify live action

This commit is contained in:
Cayo Puigdefabregas 2020-12-02 12:53:15 +01:00
parent 4d9420d1e9
commit 8a508ebc0e
3 changed files with 46 additions and 20 deletions

View File

@ -1317,7 +1317,6 @@ class Live(JoinedWithOneDeviceMixin, ActionWithOneDevice):
"""Show how many hours is used one device from the last check""" """Show how many hours is used one device from the last check"""
actions = self.device.actions actions = self.device.actions
actions.sort(key=lambda x: x.created) actions.sort(key=lambda x: x.created)
# import pdb; pdb.set_trace()
for e in reversed(actions): for e in reversed(actions):
if isinstance(e, Snapshot) and e.created < self.created: if isinstance(e, Snapshot) and e.created < self.created:
return self.time - self.get_last_power_cycle(e) return self.time - self.get_last_power_cycle(e)
@ -1326,11 +1325,11 @@ class Live(JoinedWithOneDeviceMixin, ActionWithOneDevice):
return self.time - e.time return self.time - e.time
def get_last_power_cycle(self, snapshot): def get_last_power_cycle(self, snapshot):
test_hdd= [a for a in snapshot.actions if a.type == "TestDataStorage"] for a in snapshot.actions:
if not (test_hdd and snapshot.device.allocated): if a.type == 'TestDataStorage' and a.device.serial_number == self.serial_number:
return 0 return a.power_cycle_count
return test_hdd[0].power_cycle_count return 0
class Organize(JoinedTableMixin, ActionWithMultipleDevices): class Organize(JoinedTableMixin, ActionWithMultipleDevices):

View File

@ -17,6 +17,7 @@ from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest, from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest,
InitTransfer, Live, Allocate, Deallocate) InitTransfer, Live, Allocate, Deallocate)
from ereuse_devicehub.resources.device.models import Device, Computer, DataStorage
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
@ -142,6 +143,16 @@ class ActionView(View):
# model object, when we flush them to the db we will flush # model object, when we flush them to the db we will flush
# snapshot, and we want to wait to flush snapshot at the end # snapshot, and we want to wait to flush snapshot at the end
# If the device is allocated, then snapshot is a live
live = self.live(snapshot_json)
if live:
db.session.add(live)
db.session().final_flush()
ret = self.schema.jsonify(live) # transform it back
ret.status_code = 201
db.session.commit()
return ret
device = snapshot_json.pop('device') # type: Computer device = snapshot_json.pop('device') # type: Computer
components = None components = None
if snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid): if snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid):
@ -192,7 +203,7 @@ class ActionView(View):
# Check if HID is null and add Severity:Warning to Snapshot # Check if HID is null and add Severity:Warning to Snapshot
if snapshot.device.hid is None: if snapshot.device.hid is None:
snapshot.severity = Severity.Warning snapshot.severity = Severity.Warning
self.live(snapshot)
db.session.add(snapshot) db.session.add(snapshot)
db.session().final_flush() db.session().final_flush()
ret = self.schema.jsonify(snapshot) # transform it back ret = self.schema.jsonify(snapshot) # transform it back
@ -201,22 +212,38 @@ class ActionView(View):
return ret return ret
def live(self, snapshot): def live(self, snapshot):
test_hdd= [a for a in snapshot.actions if a.type == "TestDataStorage"] """If the device.allocated == True, then this snapshot create an action live."""
if not (test_hdd and snapshot.device.allocated): device = snapshot.get('device') # type: Computer
# TODO @cayop dependency of pulls 85 and 83
# if the pr/85 and pr/83 is merged, then you need change this way for get the device
if not Device.query.filter(Device.hid==device.hid).count():
return return
test_hdd = test_hdd[0] device = Device.query.filter(Device.hid==device.hid).one()
time = test_hdd.power_cycle_count
if time:
data_live = {'time': time,
'serial_number': test_hdd.device.serial_number,
'device': snapshot.device
}
live = Live(**data_live)
snapshot.actions.add(live)
if not device.allocated:
return
time = 0
serial_number = ''
for hd in snapshot['components']:
if not isinstance(hd, DataStorage):
continue
for act in hd.actions:
if not act.type == "TestDataStorage":
continue
time = act.power_cycle_count
serial_number = hd.serial_number
if not serial_number:
return
data_live = {'time': time,
'serial_number': serial_number,
'device': device}
return Live(**data_live)
def transfer_ownership(self): def transfer_ownership(self):
"""Perform a InitTransfer action to change author_id of device""" """Perform a InitTransfer action to change author_id of device"""
pass pass

View File

@ -270,7 +270,7 @@ def test_live(user: UserClient, app: Devicehub):
action_live = [a for a in db_device.actions if a.type == 'Live'] action_live = [a for a in db_device.actions if a.type == 'Live']
assert len(action_live) == 1 assert len(action_live) == 1
assert action_live[0].time == 6293 assert action_live[0].time == 6293
assert action_live[0].hours_of_use == 0 assert action_live[0].hours_of_use == 1000
assert action_live[0].final_user_code == post_request['finalUserCode'] assert action_live[0].final_user_code == post_request['finalUserCode']
assert action_live[0].serial_number == 'wd-wx11a80w7430' assert action_live[0].serial_number == 'wd-wx11a80w7430'