diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index dcebaa05..6d7832ab 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -559,7 +559,7 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): continue if not act.lifetime: continue - data['lifetime'] = act.lifetime.total_seconds() + data['lifetime'] = act.lifetime.total_seconds()/3600 break hdds.append(data) @@ -1371,6 +1371,17 @@ class Live(JoinedWithOneDeviceMixin, ActionWithOneDevice): return e.usage_time_allocate return timedelta(0) + def get_last_snapshot_lifetime(self): + for e in self.actions: + if e.created > self.created: + continue + + if isinstance(e, Snapshot): + last_time = self.get_last_lifetime(e) + if not last_time: + continue + return last_time + def diff_time(self): for e in self.actions: if e.created > self.created: diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 7081ce49..3737fe68 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -335,36 +335,31 @@ class Device(Thing): if act.type == 'Allocate': allo = {'type': 'Allocate', - 'userCode': act.final_user_code, - 'numUsers': act.end_users, + 'finalUserCode': act.final_user_code, + 'numEndUsers': act.end_users, 'hid': self.hid, 'liveCreate': 0, - 'liveLifetime': 0, + 'usageTimeHdd': 0, 'start': act.start_time, - 'allocateLifetime': lifetime} + 'usageTimeAllocate': lifetime} allocates.append(allo) if act.type == 'Live': allocate = copy.copy(allo) - lifetime = act.usage_time_hdd.total_seconds() allocate['type'] = 'Live' allocate['liveCreate'] = act.created - for hd in lifestimes: - if hd['serial_number'] == act.serial_number: - lifetime = hd['lifetime'] - break - allocate['liveLifetime'] = lifetime + allocate['usageTimeHdd'] = act.usage_time_hdd.total_seconds()/3600 allocates.append(allocate) if act.type == 'Deallocate': deallo = {'type': 'Deallocate', - 'userCode': '', - 'numUsers': '', + 'finalUserCode': '', + 'numEndUsers': '', 'hid': self.hid, 'liveCreate': 0, - 'liveLifetime': lifetime, + 'usageTimeHdd': lifetime, 'start': act.start_time, - 'allocateLifetime': 0} + 'usageTimeAllocate': 0} allocates.append(deallo) return allocates diff --git a/ereuse_devicehub/resources/documents/device_row.py b/ereuse_devicehub/resources/documents/device_row.py index 82f0c628..7e5da1b4 100644 --- a/ereuse_devicehub/resources/documents/device_row.py +++ b/ereuse_devicehub/resources/documents/device_row.py @@ -370,9 +370,9 @@ class ActionRow(OrderedDict): # General information about allocates, deallocate and lives self['Hid'] = allocate['hid'] self['Start'] = allocate['start'] - self['UserCode'] = allocate['userCode'] - self['NumUsers'] = allocate['numUsers'] - self['AllocateLifetime'] = allocate['allocateLifetime'] + self['finalUserCode'] = allocate['finalUserCode'] + self['numEndUsers'] = allocate['numEndUsers'] + self['usageTimeAllocate'] = allocate['usageTimeAllocate'] self['Type'] = allocate['type'] self['LiveCreate'] = allocate['liveCreate'] - self['LiveLifetime'] = allocate['liveLifetime'] + self['usageTimeHdd'] = allocate['usageTimeHdd'] diff --git a/tests/files/acer-happy.live-test1.yaml b/tests/files/acer-happy.live-test1.yaml new file mode 100644 index 00000000..50fe3722 --- /dev/null +++ b/tests/files/acer-happy.live-test1.yaml @@ -0,0 +1,185 @@ +{ + "components": [ + { + "size": 10.030411318500475, + "technology": "LCD", + "resolutionWidth": 1024, + "model": "AUO LCD Monitor", + "actions": [], + "type": "Display", + "refreshRate": 60, + "productionDate": "2009-01-04T00:00:00", + "manufacturer": "AUO \"AUO\"", + "serialNumber": null, + "resolutionHeight": 600 + }, + { + "generation": null, + "actions": [ + { + "rate": 164.4981, + "type": "BenchmarkProcessorSysbench", + "elapsed": 165 + }, + { + "rate": 6650.48, + "type": "BenchmarkProcessor", + "elapsed": 0 + } + ], + "speed": 1.0, + "cores": 1, + "model": "Intel Atom CPU N450 @ 1.66GHz", + "address": 64, + "type": "Processor", + "threads": 2, + "manufacturer": "Intel Corp.", + "serialNumber": null, + "brand": "Atom" + }, + { + "memory": null, + "model": "Atom Processor D4xx/D5xx/N4xx/N5xx Integrated Graphics Controller", + "actions": [], + "type": "GraphicCard", + "manufacturer": "Intel Corporation", + "serialNumber": null + }, + { + "type": "SoundCard", + "actions": [], + "manufacturer": "Intel Corporation", + "serialNumber": null, + "model": "NM10/ICH7 Family High Definition Audio Controller" + }, + { + "type": "SoundCard", + "actions": [], + "manufacturer": "XPA970VW0", + "serialNumber": null, + "model": "1.3M WebCam" + }, + { + "size": 1024.0, + "actions": [], + "format": "SODIMM", + "model": "48594D503131325336344350362D53362020", + "interface": "DDR2", + "type": "RamModule", + "manufacturer": "Hynix Semiconductor", + "serialNumber": "4F43487B", + "speed": 667.0 + }, + { + "size": 160041.88569599998, + "variant": "1A01", + "actions": [ + { + "type": "EraseBasic", + "steps": [ + { + "type": "StepRandom", + "endTime": "2019-10-23T08:35:31.400587+00:00", + "severity": "Info", + "startTime": "2019-10-23T07:49:54.410830+00:00" + } + ], + "endTime": "2019-10-23T08:35:31.400988+00:00", + "severity": "Info", + "startTime": "2019-10-23T07:49:54.410193+00:00" + }, + { + "elapsed": 22, + "writeSpeed": 17.3, + "readSpeed": 41.6, + "type": "BenchmarkDataStorage" + }, + { + "status": "Completed without error", + "reallocatedSectorCount": 0, + "currentPendingSectorCount": 0, + "assessment": true, + "severity": "Info", + "offlineUncorrectable": 0, + "lifetime": 8692, + "type": "TestDataStorage", + "length": "Short", + "elapsed": 118, + "powerCycleCount": 5293 + } + ], + "model": "WDC WD1600BEVT-2", + "interface": "ATA", + "type": "HardDrive", + "manufacturer": "Western Digital", + "serialNumber": "WD-WX11A80W7430" + }, + { + "variant": "c1", + "actions": [], + "speed": 100.0, + "model": "AR8152 v1.1 Fast Ethernet", + "wireless": false, + "type": "NetworkAdapter", + "serialNumber": "88:ae:1d:a6:f3:d1", + "manufacturer": "Qualcomm Atheros" + }, + { + "variant": "00", + "actions": [], + "speed": null, + "model": "Centrino Wireless-N 1000 Condor Peak", + "wireless": true, + "type": "NetworkAdapter", + "serialNumber": "00:26:c7:8e:cb:89", + "manufacturer": "Intel Corporation" + }, + { + "ramMaxSize": 4, + "slots": 1, + "model": "AOHAPPY", + "pcmcia": 0, + "type": "Motherboard", + "version": "V3.05(DDR2)", + "ramSlots": 2, + "serialNumber": "Base Board Serial Number", + "manufacturer": "Acer", + "serial": 1, + "actions": [], + "biosDate": "2010-08-12T00:00:00", + "firewire": 0, + "usb": 5 + } + ], + "software": "Workbench", + "device": { + "sku": null, + "chassis": "Netbook", + "actions": [ + { + "type": "StressTest", + "elapsed": 60, + "severity": "Info" + }, + { + "rate": 19.2726, + "type": "BenchmarkRamSysbench", + "elapsed": 19 + } + ], + "model": "AOHAPPY Test 1 Live", + "type": "Laptop", + "version": "V3.05", + "manufacturer": "Acer", + "serialNumber": "LUSEA0D010038879A01601" + }, + "software": "DesktopApp", + "uuid": "0973fda0-589a-11eb-ae93-0242ac130004", + "type": "Live", + "version": "11.0b9", + "endTime": "2019-10-23T07:43:13.625104+00:00", + "closed": true, + "usageTimeAllocate": 8692, + "finalUserCode": "aaa_code_user", + "licence_version": "1.0.0" +} diff --git a/tests/files/acer-happy.snapshot-test1.yaml b/tests/files/acer-happy.snapshot-test1.yaml new file mode 100644 index 00000000..e39c990e --- /dev/null +++ b/tests/files/acer-happy.snapshot-test1.yaml @@ -0,0 +1,182 @@ +{ + "components": [ + { + "size": 10.030411318500475, + "technology": "LCD", + "resolutionWidth": 1024, + "model": "AUO LCD Monitor", + "actions": [], + "type": "Display", + "refreshRate": 60, + "productionDate": "2009-01-04T00:00:00", + "manufacturer": "AUO \"AUO\"", + "serialNumber": null, + "resolutionHeight": 600 + }, + { + "generation": null, + "actions": [ + { + "rate": 164.4981, + "type": "BenchmarkProcessorSysbench", + "elapsed": 165 + }, + { + "rate": 6650.48, + "type": "BenchmarkProcessor", + "elapsed": 0 + } + ], + "speed": 1.0, + "cores": 1, + "model": "Intel Atom CPU N450 @ 1.66GHz", + "address": 64, + "type": "Processor", + "threads": 2, + "manufacturer": "Intel Corp.", + "serialNumber": null, + "brand": "Atom" + }, + { + "memory": null, + "model": "Atom Processor D4xx/D5xx/N4xx/N5xx Integrated Graphics Controller", + "actions": [], + "type": "GraphicCard", + "manufacturer": "Intel Corporation", + "serialNumber": null + }, + { + "type": "SoundCard", + "actions": [], + "manufacturer": "Intel Corporation", + "serialNumber": null, + "model": "NM10/ICH7 Family High Definition Audio Controller" + }, + { + "type": "SoundCard", + "actions": [], + "manufacturer": "XPA970VW0", + "serialNumber": null, + "model": "1.3M WebCam" + }, + { + "size": 1024.0, + "actions": [], + "format": "SODIMM", + "model": "48594D503131325336344350362D53362020", + "interface": "DDR2", + "type": "RamModule", + "manufacturer": "Hynix Semiconductor", + "serialNumber": "4F43487B", + "speed": 667.0 + }, + { + "size": 160041.88569599998, + "variant": "1A01", + "actions": [ + { + "type": "EraseBasic", + "steps": [ + { + "type": "StepRandom", + "endTime": "2019-10-23T08:35:31.400587+00:00", + "severity": "Info", + "startTime": "2019-10-23T07:49:54.410830+00:00" + } + ], + "endTime": "2019-10-23T08:35:31.400988+00:00", + "severity": "Info", + "startTime": "2019-10-23T07:49:54.410193+00:00" + }, + { + "elapsed": 22, + "writeSpeed": 17.3, + "readSpeed": 41.6, + "type": "BenchmarkDataStorage" + }, + { + "status": "Completed without error", + "reallocatedSectorCount": 0, + "currentPendingSectorCount": 0, + "assessment": true, + "severity": "Info", + "offlineUncorrectable": 0, + "lifetime": 4692, + "type": "TestDataStorage", + "length": "Short", + "elapsed": 118, + "powerCycleCount": 5293 + } + ], + "model": "WDC WD1600BEVT-2", + "interface": "ATA", + "type": "HardDrive", + "manufacturer": "Western Digital", + "serialNumber": "WD-WX11A80W7430" + }, + { + "variant": "c1", + "actions": [], + "speed": 100.0, + "model": "AR8152 v1.1 Fast Ethernet", + "wireless": false, + "type": "NetworkAdapter", + "serialNumber": "88:ae:1d:a6:f3:d1", + "manufacturer": "Qualcomm Atheros" + }, + { + "variant": "00", + "actions": [], + "speed": null, + "model": "Centrino Wireless-N 1000 Condor Peak", + "wireless": true, + "type": "NetworkAdapter", + "serialNumber": "00:26:c7:8e:cb:89", + "manufacturer": "Intel Corporation" + }, + { + "ramMaxSize": 4, + "slots": 1, + "model": "AOHAPPY", + "pcmcia": 0, + "type": "Motherboard", + "version": "V3.05(DDR2)", + "ramSlots": 2, + "serialNumber": "Base Board Serial Number", + "manufacturer": "Acer", + "serial": 1, + "actions": [], + "biosDate": "2010-08-12T00:00:00", + "firewire": 0, + "usb": 5 + } + ], + "software": "Workbench", + "device": { + "sku": null, + "chassis": "Netbook", + "actions": [ + { + "type": "StressTest", + "elapsed": 60, + "severity": "Info" + }, + { + "rate": 19.2726, + "type": "BenchmarkRamSysbench", + "elapsed": 19 + } + ], + "model": "AOHAPPY Test 1 Live", + "type": "Laptop", + "version": "V3.05", + "manufacturer": "Acer", + "serialNumber": "LUSEA0D010038879A01601" + }, + "uuid": "0973fda0-589a-11eb-ae93-0242ac130002", + "type": "Snapshot", + "version": "11.0b9", + "endTime": "2019-10-23T07:43:13.625104+00:00", + "elapsed": 3138, + "closed": true +} diff --git a/tests/test_documents.py b/tests/test_documents.py index f76ca0ed..fd07663f 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -148,6 +148,56 @@ def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client) assert len(csv_user2) == 0 +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_live_export_csv2(user: UserClient, client: Client, app: Devicehub): + """Tests inserting a Live into the database and GETting it.""" + acer = file('acer-happy.snapshot-test1') + snapshot, _ = user.post(acer, res=Snapshot) + device_id = snapshot['device']['id'] + post_request = {"transaction": "ccc", "name": "John", "endUsers": 1, + "devices": [device_id], "description": "aaa", + "finalUserCode": "abcdefjhi", + "startTime": "2020-11-01T02:00:00+00:00", + "endTime": "2020-12-01T02:00:00+00:00" + } + + user.post(res=Allocate, data=post_request) + + acer = file('acer-happy.live-test1') + live, _ = client.post(acer, res=Live) + csv_user, _ = user.get(res=documents.DocumentDef.t, + item='actions/', + accept='text/csv', + query=[('filter', {'type': ['Computer']})]) + + assert "4692" in csv_user + assert "8692" in csv_user + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_live_example2(user: UserClient, client: Client, app: Devicehub): + """Tests inserting a Live into the database and GETting it.""" + acer = file('acer-happy.snapshot-test1') + snapshot, _ = user.post(acer, res=models.Snapshot) + device_id = snapshot['device']['id'] + post_request = {"transaction": "ccc", "name": "John", "endUsers": 1, + "devices": [device_id], "description": "aaa", + "finalUserCode": "abcdefjhi", + "startTime": "2020-11-01T02:00:00+00:00", + "endTime": "2020-12-01T02:00:00+00:00" + } + + user.post(res=models.Allocate, data=post_request) + + acer = file('acer-happy.live-test1') + live, _ = client.post(acer, res=models.Live) + db_device = Device.query.filter_by(id=device_id).one() + action_live = [a for a in db_device.actions if a.type == 'Live'] + assert len(action_live) == 1 + assert str(action_live[0].snapshot_uuid) == acer['uuid'] + + @pytest.mark.mvp def test_export_basic_snapshot(user: UserClient): """Test export device information in a csv file."""