This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/documents/device_row.py

462 lines
19 KiB
Python
Raw Normal View History

2020-10-21 11:06:47 +00:00
""" This file frame a correct row for csv report """
2019-02-28 17:21:24 +00:00
2020-10-21 11:06:47 +00:00
from collections import OrderedDict
from flask import url_for
2019-02-28 17:21:24 +00:00
2020-10-21 11:06:47 +00:00
from ereuse_devicehub.resources.enums import Severity
from ereuse_devicehub.resources.device import models as d, states
from ereuse_devicehub.resources.action import models as da
2020-10-21 11:06:47 +00:00
from ereuse_devicehub.resources.action.models import (BenchmarkDataStorage, RateComputer,
TestDataStorage)
2019-02-28 17:21:24 +00:00
class DeviceRow(OrderedDict):
NUMS = {
d.Display.t: 1,
d.Processor.t: 2,
d.GraphicCard.t: 2,
d.Motherboard.t: 1,
d.NetworkAdapter.t: 2,
2020-10-21 11:06:47 +00:00
d.SoundCard.t: 2,
d.RamModule.t: 4,
d.DataStorage: 5,
2019-02-28 17:21:24 +00:00
}
2020-10-21 11:06:47 +00:00
ORDER_COMPONENTS = [
d.Processor.t,
d.RamModule.t,
d.DataStorage.t,
d.Motherboard.t,
d.Display.t,
d.GraphicCard.t,
d.NetworkAdapter.t,
d.SoundCard.t,
]
2019-02-28 17:21:24 +00:00
def __init__(self, device: d.Device, document_ids: dict) -> None:
2019-02-28 17:21:24 +00:00
super().__init__()
self.device = device
self.document_id = document_ids.get(device.id, '')
2020-10-22 13:54:01 +00:00
snapshot = get_action(device, 'Snapshot')
software = ''
if snapshot:
software = "{software} {version}".format(
software=snapshot.software.name, version=snapshot.version)
2019-02-28 17:21:24 +00:00
# General information about device
2021-03-08 21:43:24 +00:00
self['DevicehubID'] = device.devicehub_id
self['DocumentID'] = self.document_id
2020-10-21 11:06:47 +00:00
self['Public Link'] = '{url}{id}'.format(url=url_for('Device.main', _external=True),
2021-03-08 21:43:24 +00:00
id=device.devicehub_id)
2020-10-21 11:06:47 +00:00
self['Tag 1 Type'] = self['Tag 1 ID'] = self['Tag 1 Organization'] = ''
self['Tag 2 Type'] = self['Tag 2 ID'] = self['Tag 2 Organization'] = ''
self['Tag 3 Type'] = self['Tag 3 ID'] = self['Tag 3 Organization'] = ''
for i, tag in zip(range(1, 3), device.tags):
2021-03-16 14:43:42 +00:00
self['Tag {} Type'.format(i)] = 'unamed' if tag.provider else 'named'
2020-10-21 11:06:47 +00:00
self['Tag {} ID'.format(i)] = tag.id
self['Tag {} Organization'.format(i)] = tag.org.name
2020-10-22 13:54:01 +00:00
self['Device Hardware ID'] = device.hid
2020-10-21 11:06:47 +00:00
self['Device Type'] = device.t
self['Device Chassis'] = ''
2019-02-28 17:21:24 +00:00
if isinstance(device, d.Computer):
2020-10-21 20:11:55 +00:00
self['Device Chassis'] = device.chassis.name
2020-10-22 13:54:01 +00:00
self['Device Serial Number'] = none2str(device.serial_number)
self['Device Model'] = none2str(device.model)
self['Device Manufacturer'] = none2str(device.manufacturer)
2019-02-28 17:21:24 +00:00
self['Registered in'] = format(device.created, '%c')
2020-10-22 13:54:01 +00:00
self['Registered (process)'] = software
2020-10-21 11:06:47 +00:00
self['Updated in (software)'] = device.updated
self['Updated in (web)'] = ''
try:
self['Physical state'] = device.last_action_of(*states.Physical.actions()).t
except LookupError:
self['Physical state'] = ''
try:
self['Trading state'] = device.last_action_of(*states.Trading.actions()).t
except LookupError:
self['Trading state'] = ''
2019-02-28 17:21:24 +00:00
if isinstance(device, d.Computer):
2020-10-21 11:06:47 +00:00
self['Processor'] = none2str(device.processor_model)
self['RAM (MB)'] = none2str(device.ram_size)
self['Data Storage Size (MB)'] = none2str(device.data_storage_size)
# More specific information about components
if isinstance(device, d.Computer):
self.components()
else:
# TODO @cayop we need add columns as a component
pass
2019-02-28 17:21:24 +00:00
rate = device.rate
if rate:
2020-10-21 11:06:47 +00:00
self['Device Rate'] = rate.rating
2020-10-21 20:11:55 +00:00
self['Device Range'] = rate.rating_range.name
2019-05-08 17:12:05 +00:00
assert isinstance(rate, RateComputer)
2019-02-28 17:21:24 +00:00
self['Processor Rate'] = rate.processor
2020-10-21 20:11:55 +00:00
self['Processor Range'] = rate.processor_range.name
2019-02-28 17:21:24 +00:00
self['RAM Rate'] = rate.ram
2020-10-21 20:11:55 +00:00
self['RAM Range'] = rate.ram_range.name
2019-02-28 17:21:24 +00:00
self['Data Storage Rate'] = rate.data_storage
2020-10-21 20:11:55 +00:00
self['Data Storage Range'] = rate.data_storage_range.name
else:
self['Device Rate'] = ''
self['Device Range'] = ''
self['Processor Rate'] = ''
self['Processor Range'] = ''
self['RAM Rate'] = ''
self['RAM Range'] = ''
self['Data Storage Rate'] = ''
self['Data Storage Range'] = ''
2020-10-22 13:54:01 +00:00
self['Price'] = none2str(device.price)
2020-10-21 20:11:55 +00:00
benchram = get_action(device, 'BenchmarkRamSysbench')
if benchram:
self['Benchmark RamSysbench (points)'] = none2str(benchram.rate)
else:
self['Benchmark RamSysbench (points)'] = ''
2019-02-28 17:21:24 +00:00
def components(self):
"""Function to get all components information of a device."""
2019-02-28 17:21:24 +00:00
assert isinstance(self.device, d.Computer)
2020-10-21 11:06:47 +00:00
for ctype in self.ORDER_COMPONENTS: # ctype: str
cmax = self.NUMS.get(ctype, 4)
i = 1
l_ctype = [ctype]
if ctype == d.DataStorage.t:
l_ctype = [ctype, d.HardDrive.t, d.SolidStateDrive.t]
for component in (r for r in self.device.components if r.type in l_ctype):
self.fill_component(ctype, i, component)
i += 1
if i > cmax:
break
while i <= cmax:
self.fill_component(ctype, i)
i += 1
def fill_component(self, ctype, i, component=None):
"""Function to put specific information of components
in OrderedDict (csv)
2020-10-21 11:06:47 +00:00
:param ctype: type of component
2019-02-28 17:21:24 +00:00
:param component: device.components
"""
2020-10-21 11:06:47 +00:00
# Basic fields for all components
self['{} {}'.format(ctype, i)] = format(component) if component else ''
if component is None:
self['{} {} Manufacturer'.format(ctype, i)] = ''
self['{} {} Model'.format(ctype, i)] = ''
self['{} {} Serial Number'.format(ctype, i)] = ''
else:
self['{} {} Manufacturer'.format(ctype, i)] = none2str(component.manufacturer)
self['{} {} Model'.format(ctype, i)] = none2str(component.model)
self['{} {} Serial Number'.format(ctype, i)] = none2str(component.serial_number)
if ctype == d.Processor.t:
self.get_processor(ctype, i, component)
2020-10-21 20:11:55 +00:00
if ctype == d.RamModule.t:
self.get_ram(ctype, i, component)
2020-10-21 11:06:47 +00:00
if ctype == d.DataStorage.t:
self.get_datastorage(ctype, i, component)
2020-10-21 11:17:32 +00:00
if ctype == d.GraphicCard.t:
2020-10-21 20:11:55 +00:00
self.get_graphic_card(ctype, i, component)
2019-02-28 17:21:24 +00:00
2020-10-21 20:11:55 +00:00
def get_processor(self, ctype, i, component):
"""Particular fields for component Processor."""
if component is None:
self['{} {} Number of cores'.format(ctype, i)] = ''
self['{} {} Speed (GHz)'.format(ctype, i)] = ''
self['Benchmark {} {} (points)'.format(ctype, i)] = ''
self['Benchmark ProcessorSysbench {} {} (points)'.format(ctype, i)] = ''
return
2019-02-28 17:21:24 +00:00
2020-10-21 20:11:55 +00:00
self['{} {} Number of cores'.format(ctype, i)] = none2str(component.cores)
self['{} {} Speed (GHz)'.format(ctype, i)] = none2str(component.speed)
2019-02-28 17:21:24 +00:00
2020-10-21 20:11:55 +00:00
benchmark = get_action(component, 'BenchmarkProcessor')
if not benchmark:
self['Benchmark {} {} (points)'.format(ctype, i)] = ''
else:
self['Benchmark {} {} (points)'.format(ctype, i)] = benchmark.rate
2019-02-28 17:21:24 +00:00
2020-10-21 20:11:55 +00:00
sysbench = get_action(component, 'BenchmarkProcessorSysbench')
if not sysbench:
self['Benchmark ProcessorSysbench {} {} (points)'.format(ctype, i)] = ''
return
self['Benchmark ProcessorSysbench {} {} (points)'.format(ctype, i)] = sysbench.rate
2020-10-21 20:11:55 +00:00
def get_ram(self, ctype, i, component):
"""Particular fields for component Ram Module."""
if component is None:
self['{} {} Size (MB)'.format(ctype, i)] = ''
self['{} {} Speed (MHz)'.format(ctype, i)] = ''
return
2020-10-21 20:11:55 +00:00
self['{} {} Size (MB)'.format(ctype, i)] = none2str(component.size)
self['{} {} Speed (MHz)'.format(ctype, i)] = none2str(component.speed)
2020-10-21 11:06:47 +00:00
def get_datastorage(self, ctype, i, component):
"""Particular fields for component DataStorage.
A DataStorage can be HardDrive or SolidStateDrive.
"""
if component is None:
2020-10-22 13:54:01 +00:00
self['{} {} Size (MB)'.format(ctype, i)] = ''
2020-10-21 11:06:47 +00:00
self['Erasure {} {}'.format(ctype, i)] = ''
self['Erasure {} {} Serial Number'.format(ctype, i)] = ''
2020-10-22 13:54:01 +00:00
self['Erasure {} {} Size (MB)'.format(ctype, i)] = ''
2020-10-21 11:06:47 +00:00
self['Erasure {} {} Software'.format(ctype, i)] = ''
self['Erasure {} {} Result'.format(ctype, i)] = ''
self['Erasure {} {} Type'.format(ctype, i)] = ''
self['Erasure {} {} Method'.format(ctype, i)] = ''
2020-10-22 13:54:01 +00:00
self['Erasure {} {} Elapsed (hours)'.format(ctype, i)] = ''
2020-10-21 11:06:47 +00:00
self['Erasure {} {} Date'.format(ctype, i)] = ''
self['Erasure {} {} Steps'.format(ctype, i)] = ''
self['Erasure {} {} Steps Start Time'.format(ctype, i)] = ''
self['Erasure {} {} Steps End Time'.format(ctype, i)] = ''
self['Benchmark {} {} Read Speed (MB/s)'.format(ctype, i)] = ''
self['Benchmark {} {} Writing speed (MB/s)'.format(ctype, i)] = ''
2020-10-21 20:11:55 +00:00
self['Test {} {} Software'.format(ctype, i)] = ''
self['Test {} {} Type'.format(ctype, i)] = ''
self['Test {} {} Result'.format(ctype, i)] = ''
self['Test {} {} Power on (hours used)'.format(ctype, i)] = ''
self['Test {} {} Lifetime remaining (percentage)'.format(ctype, i)] = ''
2020-10-21 11:06:47 +00:00
return
2020-10-22 13:54:01 +00:00
snapshot = get_action(component, 'Snapshot')
software = ''
if snapshot:
software = "{software} {version}".format(
software=snapshot.software.name, version=snapshot.version)
self['{} {} Size (MB)'.format(ctype, i)] = none2str(component.size)
2020-10-21 11:06:47 +00:00
erasures = [a for a in component.actions if a.type in ['EraseBasic', 'EraseSectors']]
erasure = erasures[-1] if erasures else None
if not erasure:
self['Erasure {} {}'.format(ctype, i)] = none2str(component.hid)
serial_number = none2str(component.serial_number)
self['Erasure {} {} Serial Number'.format(ctype, i)] = serial_number
2020-10-22 13:54:01 +00:00
self['Erasure {} {} Size (MB)'.format(ctype, i)] = none2str(component.size)
2020-10-21 11:06:47 +00:00
self['Erasure {} {} Software'.format(ctype, i)] = ''
self['Erasure {} {} Result'.format(ctype, i)] = ''
self['Erasure {} {} Type'.format(ctype, i)] = ''
self['Erasure {} {} Method'.format(ctype, i)] = ''
2020-10-22 13:54:01 +00:00
self['Erasure {} {} Elapsed (hours)'.format(ctype, i)] = ''
2020-10-21 11:06:47 +00:00
self['Erasure {} {} Date'.format(ctype, i)] = ''
self['Erasure {} {} Steps'.format(ctype, i)] = ''
self['Erasure {} {} Steps Start Time'.format(ctype, i)] = ''
self['Erasure {} {} Steps End Time'.format(ctype, i)] = ''
2020-10-21 20:11:55 +00:00
else:
self['Erasure {} {}'.format(ctype, i)] = none2str(component.hid)
serial_number = none2str(component.serial_number)
self['Erasure {} {} Serial Number'.format(ctype, i)] = serial_number
2020-10-22 13:54:01 +00:00
self['Erasure {} {} Size (MB)'.format(ctype, i)] = none2str(component.size)
self['Erasure {} {} Software'.format(ctype, i)] = software
2020-10-21 20:11:55 +00:00
result = get_result(erasure.severity)
self['Erasure {} {} Result'.format(ctype, i)] = result
self['Erasure {} {} Type'.format(ctype, i)] = erasure.type
self['Erasure {} {} Method'.format(ctype, i)] = erasure.method
2020-10-22 13:54:01 +00:00
self['Erasure {} {} Elapsed (hours)'.format(ctype, i)] = format(erasure.elapsed)
2020-10-21 20:11:55 +00:00
self['Erasure {} {} Date'.format(ctype, i)] = format(erasure.created)
steps = ','.join((format(x) for x in erasure.steps))
self['Erasure {} {} Steps'.format(ctype, i)] = steps
steps_start_time = ','.join((format(x.start_time) for x in erasure.steps))
self['Erasure {} {} Steps Start Time'.format(ctype, i)] = steps_start_time
steps_end_time = ','.join((format(x.end_time) for x in erasure.steps))
self['Erasure {} {} Steps End Time'.format(ctype, i)] = steps_end_time
benchmark = get_action(component, 'BenchmarkDataStorage')
if not benchmark:
2020-10-21 11:06:47 +00:00
self['Benchmark {} {} Read Speed (MB/s)'.format(ctype, i)] = ''
self['Benchmark {} {} Writing speed (MB/s)'.format(ctype, i)] = ''
2020-10-21 20:11:55 +00:00
else:
self['Benchmark {} {} Read Speed (MB/s)'.format(ctype, i)] = none2str(
benchmark.read_speed)
self['Benchmark {} {} Writing speed (MB/s)'.format(ctype, i)] = none2str(
benchmark.write_speed)
test_storage = get_action(component, 'TestDataStorage')
if not test_storage:
self['Test {} {} Software'.format(ctype, i)] = ''
self['Test {} {} Type'.format(ctype, i)] = ''
self['Test {} {} Result'.format(ctype, i)] = ''
self['Test {} {} Power on (hours used)'.format(ctype, i)] = ''
self['Test {} {} Lifetime remaining (percentage)'.format(ctype, i)] = ''
2020-10-21 11:06:47 +00:00
return
2020-10-22 13:54:01 +00:00
self['Test {} {} Software'.format(ctype, i)] = software
2020-10-21 20:11:55 +00:00
self['Test {} {} Type'.format(ctype, i)] = test_storage.length.value
self['Test {} {} Result'.format(ctype, i)] = get_result(test_storage.severity)
self['Test {} {} Power on (hours used)'.format(ctype, i)] = none2str(
test_storage.power_cycle_count)
self['Test {} {} Lifetime remaining (percentage)'.format(ctype, i)] = none2str(
test_storage.lifetime)
2020-10-21 11:06:47 +00:00
2020-10-21 20:11:55 +00:00
def get_graphic_card(self, ctype, i, component):
"""Particular fields for component GraphicCard."""
if component is None:
self['{} {} Memory (MB)'.format(ctype, i)] = ''
2020-10-21 11:06:47 +00:00
return
2020-10-21 20:11:55 +00:00
self['{} {} Memory (MB)'.format(ctype, i)] = none2str(component.memory)
2020-10-21 11:17:32 +00:00
2020-10-21 11:06:47 +00:00
class StockRow(OrderedDict):
def __init__(self, device: d.Device) -> None:
super().__init__()
self.device = device
2020-10-21 11:06:47 +00:00
self['Type'] = none2str(device.t)
if isinstance(device, d.Computer):
self['Chassis'] = device.chassis
else:
self['Chassis'] = ''
2020-10-21 11:06:47 +00:00
self['Serial Number'] = none2str(device.serial_number)
self['Model'] = none2str(device.model)
self['Manufacturer'] = none2str(device.manufacturer)
self['Registered in'] = format(device.created, '%c')
try:
self['Physical state'] = device.last_action_of(*states.Physical.actions()).t
except LookupError:
self['Physical state'] = ''
try:
self['Trading state'] = device.last_action_of(*states.Trading.actions()).t
except LookupError:
self['Trading state'] = ''
2020-10-21 11:06:47 +00:00
self['Price'] = none2str(device.price)
self['Processor'] = none2str(device.processor_model)
self['RAM (MB)'] = none2str(device.ram_size)
self['Data Storage Size (MB)'] = none2str(device.data_storage_size)
rate = device.rate
if rate:
self['Rate'] = rate.rating
self['Range'] = rate.rating_range
assert isinstance(rate, RateComputer)
self['Processor Rate'] = rate.processor
self['Processor Range'] = rate.processor_range
self['RAM Rate'] = rate.ram
self['RAM Range'] = rate.ram_range
self['Data Storage Rate'] = rate.data_storage
self['Data Storage Range'] = rate.data_storage_range
2020-10-21 20:11:55 +00:00
def get_result(severity):
2020-10-21 11:06:47 +00:00
""" For the csv is necessary simplify the message of results """
type_of_results = {
Severity.Error: 'Failure',
Severity.Warning: 'Success with Warnings',
Severity.Notice: 'Success',
Severity.Info: 'Success'
}
return type_of_results[severity]
def none2str(string):
""" convert none to empty str """
if string is None:
return ''
2020-10-21 20:11:55 +00:00
return format(string)
def get_action(component, action):
""" Filter one action from a component or return None """
result = [a for a in component.actions if a.type == action]
return result[-1] if result else None
class ActionRow(OrderedDict):
2021-01-13 17:11:41 +00:00
def __init__(self, allocate):
super().__init__()
2021-01-13 17:11:41 +00:00
# General information about allocates, deallocate and lives
2021-03-08 21:43:24 +00:00
self['DevicehubID'] = allocate['devicehubID']
2021-01-13 17:11:41 +00:00
self['Hid'] = allocate['hid']
self['Start'] = allocate['start']
2021-01-20 09:39:02 +00:00
self['FinalUserCode'] = allocate['finalUserCode']
self['NumEndUsers'] = allocate['numEndUsers']
self['UsageTimeAllocate'] = allocate['usageTimeAllocate']
2021-01-13 17:11:41 +00:00
self['Type'] = allocate['type']
self['LiveCreate'] = allocate['liveCreate']
2021-01-20 09:39:02 +00:00
self['UsageTimeHdd'] = allocate['usageTimeHdd']
2021-02-22 14:49:13 +00:00
class InternalStatsRow(OrderedDict):
def __init__(self, user, create, actions):
super().__init__()
# General information about all internal stats
# user, quart, month, year:
# Snapshot (Registers)
# Snapshots (Update)
# Snapshots (All)
# Allocate
# Deallocate
# Live
self.actions = actions
year, month = create.split('-')
self['User'] = user
self['Year'] = year
self['Quarter'] = self.quarter(month)
self['Month'] = month
self['Snapshot (Registers)'] = 0
self['Snapshot (Update)'] = 0
self['Snapshot (All)'] = 0
self['Allocates'] = 0
self['Deallocates'] = 0
self['Lives'] = 0
self.count_actions()
def count_actions(self):
for ac in self.actions:
self.is_snapshot(
self.is_deallocate(
self.is_live(
self.is_allocate(ac)
)
2021-02-22 14:49:13 +00:00
)
)
def is_allocate(self, ac):
if ac.type == 'Allocate':
self['Allocates'] += 1
return ac
def is_live(self, ac):
if ac.type == 'Live':
self['Lives'] += 1
return ac
2021-02-22 14:49:13 +00:00
def is_deallocate(self, ac):
if ac.type == 'Deallocate':
self['Deallocates'] += 1
return ac
def is_snapshot(self, ac):
if not ac.type == 'Snapshot':
return
self['Snapshot (All)'] += 1
canary = False
for _ac in ac.device.actions:
2021-02-23 16:40:36 +00:00
if not _ac.type == 'Snapshot':
continue
2021-02-22 14:49:13 +00:00
if _ac.created < ac.created:
canary = True
break
if canary:
self['Snapshot (Update)'] += 1
else:
self['Snapshot (Registers)'] += 1
def quarter(self, month):
q = {1: 'Q1', 2: 'Q1', 3: 'Q1',
4: 'Q2', 5: 'Q2', 6: 'Q2',
7: 'Q3', 8: 'Q3', 9: 'Q3',
10: 'Q4', 11: 'Q4', 12: 'Q4',
}
return q[int(month)]