This commit is contained in:
Cayo Puigdefabregas 2022-07-08 12:21:07 +02:00
parent 5c5d1c1a64
commit fa45080a44

View file

@ -1,13 +1,18 @@
""" This file frame a correct row for csv report """ """ This file frame a correct row for csv report """
from collections import OrderedDict from collections import OrderedDict
from flask import url_for from flask import url_for
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 from ereuse_devicehub.resources.action import models as da
from ereuse_devicehub.resources.action.models import (BenchmarkDataStorage, RateComputer, from ereuse_devicehub.resources.action.models import (
TestDataStorage) BenchmarkDataStorage,
RateComputer,
TestDataStorage,
)
from ereuse_devicehub.resources.device import models as d
from ereuse_devicehub.resources.device import states
from ereuse_devicehub.resources.enums import Severity
class DeviceRow(OrderedDict): class DeviceRow(OrderedDict):
@ -40,20 +45,20 @@ class DeviceRow(OrderedDict):
software = '' software = ''
if snapshot: if snapshot:
software = "{software} {version}".format( software = "{software} {version}".format(
software=snapshot.software.name, version=snapshot.version) software=snapshot.software.name, version=snapshot.version
)
# General information about device # General information about device
self['DHID'] = device.devicehub_id self['DHID'] = device.devicehub_id
self['DocumentID'] = self.document_id self['DocumentID'] = self.document_id
self['Public Link'] = '{url}{id}'.format( self['Public Link'] = '{url}{id}'.format(
url=url_for('Device.main', _external=True), url=url_for('Device.main', _external=True), id=device.devicehub_id
id=device.devicehub_id) )
self['Lots'] = ', '.join([x.name for x in self.device.lots]) self['Lots'] = ', '.join([x.name for x in self.device.lots])
self['Tag 1 Type'] = self['Tag 1 ID'] = self['Tag 1 Organization'] = '' 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 2 Type'] = self['Tag 2 ID'] = self['Tag 2 Organization'] = ''
self['Tag 3 Type'] = self['Tag 3 ID'] = self['Tag 3 Organization'] = '' self['Tag 3 Type'] = self['Tag 3 ID'] = self['Tag 3 Organization'] = ''
for i, tag in zip(range(1, 3), device.tags): for i, tag in zip(range(1, 3), device.tags):
self['Tag {} Type'.format( self['Tag {} Type'.format(i)] = 'unamed' if tag.provider else 'named'
i)] = 'unamed' if tag.provider else 'named'
self['Tag {} ID'.format(i)] = tag.id self['Tag {} ID'.format(i)] = tag.id
self['Tag {} Organization'.format(i)] = tag.org.name self['Tag {} Organization'.format(i)] = tag.org.name
@ -79,8 +84,7 @@ class DeviceRow(OrderedDict):
self['Allocate state'] = device.allocated_status.type self['Allocate state'] = device.allocated_status.type
try: try:
self['Lifecycle state'] = device.last_action_of( self['Lifecycle state'] = device.last_action_of(*states.Status.actions()).t
*states.Trading.actions()).t
except LookupError: except LookupError:
self['Lifecycle state'] = '' self['Lifecycle state'] = ''
if isinstance(device, d.Computer): if isinstance(device, d.Computer):
@ -155,10 +159,12 @@ class DeviceRow(OrderedDict):
self['{} {} Serial Number'.format(ctype, i)] = '' self['{} {} Serial Number'.format(ctype, i)] = ''
else: else:
self['{} {} Manufacturer'.format(ctype, i)] = none2str( self['{} {} Manufacturer'.format(ctype, i)] = none2str(
component.manufacturer) component.manufacturer
)
self['{} {} Model'.format(ctype, i)] = none2str(component.model) self['{} {} Model'.format(ctype, i)] = none2str(component.model)
self['{} {} Serial Number'.format(ctype, i)] = none2str( self['{} {} Serial Number'.format(ctype, i)] = none2str(
component.serial_number) component.serial_number
)
if ctype == d.Processor.t: if ctype == d.Processor.t:
self.get_processor(ctype, i, component) self.get_processor(ctype, i, component)
@ -178,12 +184,10 @@ class DeviceRow(OrderedDict):
self['{} {} Number of cores'.format(ctype, i)] = '' self['{} {} Number of cores'.format(ctype, i)] = ''
self['{} {} Speed (GHz)'.format(ctype, i)] = '' self['{} {} Speed (GHz)'.format(ctype, i)] = ''
self['Benchmark {} {} (points)'.format(ctype, i)] = '' self['Benchmark {} {} (points)'.format(ctype, i)] = ''
self['Benchmark ProcessorSysbench {} {} (points)'.format( self['Benchmark ProcessorSysbench {} {} (points)'.format(ctype, i)] = ''
ctype, i)] = ''
return return
self['{} {} Number of cores'.format( self['{} {} Number of cores'.format(ctype, i)] = none2str(component.cores)
ctype, i)] = none2str(component.cores)
self['{} {} Speed (GHz)'.format(ctype, i)] = none2str(component.speed) self['{} {} Speed (GHz)'.format(ctype, i)] = none2str(component.speed)
benchmark = get_action(component, 'BenchmarkProcessor') benchmark = get_action(component, 'BenchmarkProcessor')
@ -194,11 +198,11 @@ class DeviceRow(OrderedDict):
sysbench = get_action(component, 'BenchmarkProcessorSysbench') sysbench = get_action(component, 'BenchmarkProcessorSysbench')
if not sysbench: if not sysbench:
self['Benchmark ProcessorSysbench {} {} (points)'.format( self['Benchmark ProcessorSysbench {} {} (points)'.format(ctype, i)] = ''
ctype, i)] = ''
return return
self['Benchmark ProcessorSysbench {} {} (points)'.format( self[
ctype, i)] = sysbench.rate 'Benchmark ProcessorSysbench {} {} (points)'.format(ctype, i)
] = sysbench.rate
def get_ram(self, ctype, i, component): def get_ram(self, ctype, i, component):
"""Particular fields for component Ram Module.""" """Particular fields for component Ram Module."""
@ -212,7 +216,7 @@ class DeviceRow(OrderedDict):
def get_datastorage(self, ctype, i, component): def get_datastorage(self, ctype, i, component):
"""Particular fields for component DataStorage. """Particular fields for component DataStorage.
A DataStorage can be HardDrive or SolidStateDrive. A DataStorage can be HardDrive or SolidStateDrive.
""" """
if component is None: if component is None:
@ -244,21 +248,23 @@ class DeviceRow(OrderedDict):
software = '' software = ''
if snapshot: if snapshot:
software = "{software} {version}".format( software = "{software} {version}".format(
software=snapshot.software.name, version=snapshot.version) software=snapshot.software.name, version=snapshot.version
)
self['{} {} Size (MB)'.format(ctype, i)] = none2str(component.size) self['{} {} Size (MB)'.format(ctype, i)] = none2str(component.size)
component_actions = sorted(component.actions, key=lambda x: x.created) component_actions = sorted(component.actions, key=lambda x: x.created)
erasures = [a for a in component_actions if a.type in [ erasures = [
'EraseBasic', 'EraseSectors', 'DataWipe']] a
for a in component_actions
if a.type in ['EraseBasic', 'EraseSectors', 'DataWipe']
]
erasure = erasures[-1] if erasures else None erasure = erasures[-1] if erasures else None
if not erasure: if not erasure:
self['Erasure {} {}'.format(ctype, i)] = none2str(component.hid) self['Erasure {} {}'.format(ctype, i)] = none2str(component.hid)
serial_number = none2str(component.serial_number) serial_number = none2str(component.serial_number)
self['Erasure {} {} Serial Number'.format( self['Erasure {} {} Serial Number'.format(ctype, i)] = serial_number
ctype, i)] = serial_number self['Erasure {} {} Size (MB)'.format(ctype, i)] = none2str(component.size)
self['Erasure {} {} Size (MB)'.format(
ctype, i)] = none2str(component.size)
self['Erasure {} {} Software'.format(ctype, i)] = '' self['Erasure {} {} Software'.format(ctype, i)] = ''
self['Erasure {} {} Result'.format(ctype, i)] = '' self['Erasure {} {} Result'.format(ctype, i)] = ''
self['Erasure {} {} Certificate URL'.format(ctype, i)] = '' self['Erasure {} {} Certificate URL'.format(ctype, i)] = ''
@ -272,15 +278,13 @@ class DeviceRow(OrderedDict):
elif hasattr(erasure, 'type') and erasure.type == 'DataWipe': elif hasattr(erasure, 'type') and erasure.type == 'DataWipe':
self['Erasure {} {}'.format(ctype, i)] = none2str(component.hid) self['Erasure {} {}'.format(ctype, i)] = none2str(component.hid)
serial_number = none2str(component.serial_number) serial_number = none2str(component.serial_number)
self['Erasure {} {} Serial Number'.format( self['Erasure {} {} Serial Number'.format(ctype, i)] = serial_number
ctype, i)] = serial_number self['Erasure {} {} Size (MB)'.format(ctype, i)] = none2str(component.size)
self['Erasure {} {} Size (MB)'.format( self['Erasure {} {} Software'.format(ctype, i)] = erasure.document.software
ctype, i)] = none2str(component.size)
self['Erasure {} {} Software'.format(
ctype, i)] = erasure.document.software
self['Erasure {} {} Result'.format(ctype, i)] = get_result(erasure) self['Erasure {} {} Result'.format(ctype, i)] = get_result(erasure)
self['Erasure {} {} Certificate URL'.format( self['Erasure {} {} Certificate URL'.format(ctype, i)] = (
ctype, i)] = erasure.document.url and erasure.document.url.to_text() or '' erasure.document.url and erasure.document.url.to_text() or ''
)
self['Erasure {} {} Type'.format(ctype, i)] = '' self['Erasure {} {} Type'.format(ctype, i)] = ''
self['Erasure {} {} Method'.format(ctype, i)] = '' self['Erasure {} {} Method'.format(ctype, i)] = ''
self['Erasure {} {} Elapsed (hours)'.format(ctype, i)] = '' self['Erasure {} {} Elapsed (hours)'.format(ctype, i)] = ''
@ -291,10 +295,8 @@ class DeviceRow(OrderedDict):
else: else:
self['Erasure {} {}'.format(ctype, i)] = none2str(component.hid) self['Erasure {} {}'.format(ctype, i)] = none2str(component.hid)
serial_number = none2str(component.serial_number) serial_number = none2str(component.serial_number)
self['Erasure {} {} Serial Number'.format( self['Erasure {} {} Serial Number'.format(ctype, i)] = serial_number
ctype, i)] = serial_number self['Erasure {} {} Size (MB)'.format(ctype, i)] = none2str(component.size)
self['Erasure {} {} Size (MB)'.format(
ctype, i)] = none2str(component.size)
self['Erasure {} {} Software'.format(ctype, i)] = software self['Erasure {} {} Software'.format(ctype, i)] = software
result = get_result(erasure) result = get_result(erasure)
@ -302,20 +304,16 @@ class DeviceRow(OrderedDict):
self['Erasure {} {} Certificate URL'.format(ctype, i)] = '' self['Erasure {} {} Certificate URL'.format(ctype, i)] = ''
self['Erasure {} {} Type'.format(ctype, i)] = erasure.type self['Erasure {} {} Type'.format(ctype, i)] = erasure.type
self['Erasure {} {} Method'.format(ctype, i)] = erasure.method self['Erasure {} {} Method'.format(ctype, i)] = erasure.method
self['Erasure {} {} Elapsed (hours)'.format( self['Erasure {} {} Elapsed (hours)'.format(ctype, i)] = format(
ctype, i)] = format(erasure.elapsed) erasure.elapsed
self['Erasure {} {} Date'.format( )
ctype, i)] = format(erasure.created) self['Erasure {} {} Date'.format(ctype, i)] = format(erasure.created)
steps = ','.join((format(x) for x in erasure.steps)) steps = ','.join((format(x) for x in erasure.steps))
self['Erasure {} {} Steps'.format(ctype, i)] = steps self['Erasure {} {} Steps'.format(ctype, i)] = steps
steps_start_time = ','.join( steps_start_time = ','.join((format(x.start_time) for x in erasure.steps))
(format(x.start_time) for x in erasure.steps)) self['Erasure {} {} Steps Start Time'.format(ctype, i)] = steps_start_time
self['Erasure {} {} Steps Start Time'.format( steps_end_time = ','.join((format(x.end_time) for x in erasure.steps))
ctype, i)] = steps_start_time self['Erasure {} {} Steps End Time'.format(ctype, i)] = steps_end_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') benchmark = get_action(component, 'BenchmarkDataStorage')
if not benchmark: if not benchmark:
@ -323,9 +321,11 @@ class DeviceRow(OrderedDict):
self['Benchmark {} {} Writing speed (MB/s)'.format(ctype, i)] = '' self['Benchmark {} {} Writing speed (MB/s)'.format(ctype, i)] = ''
else: else:
self['Benchmark {} {} Read Speed (MB/s)'.format(ctype, i)] = none2str( self['Benchmark {} {} Read Speed (MB/s)'.format(ctype, i)] = none2str(
benchmark.read_speed) benchmark.read_speed
)
self['Benchmark {} {} Writing speed (MB/s)'.format(ctype, i)] = none2str( self['Benchmark {} {} Writing speed (MB/s)'.format(ctype, i)] = none2str(
benchmark.write_speed) benchmark.write_speed
)
test_storage = get_action(component, 'TestDataStorage') test_storage = get_action(component, 'TestDataStorage')
if not test_storage: if not test_storage:
@ -339,14 +339,16 @@ class DeviceRow(OrderedDict):
self['Test {} {} Software'.format(ctype, i)] = software self['Test {} {} Software'.format(ctype, i)] = software
self['Test {} {} Type'.format(ctype, i)] = test_storage.length.value self['Test {} {} Type'.format(ctype, i)] = test_storage.length.value
self['Test {} {} Result'.format(ctype, i)] = get_result( self['Test {} {} Result'.format(ctype, i)] = get_result(test_storage)
test_storage)
self['Test {} {} Power cycle count'.format(ctype, i)] = none2str( self['Test {} {} Power cycle count'.format(ctype, i)] = none2str(
test_storage.power_cycle_count) test_storage.power_cycle_count
)
self['Test {} {} Lifetime (days)'.format(ctype, i)] = none2str( self['Test {} {} Lifetime (days)'.format(ctype, i)] = none2str(
test_storage.lifetime) test_storage.lifetime
)
self['Test {} {} Power on hours'.format(ctype, i)] = none2str( self['Test {} {} Power on hours'.format(ctype, i)] = none2str(
test_storage.power_on_hours) test_storage.power_on_hours
)
def get_graphic_card(self, ctype, i, component): def get_graphic_card(self, ctype, i, component):
"""Particular fields for component GraphicCard.""" """Particular fields for component GraphicCard."""
@ -400,39 +402,36 @@ class StockRow(OrderedDict):
def get_result(erasure): def get_result(erasure):
""" For the csv is necessary simplify the message of results """ """For the csv is necessary simplify the message of results"""
if hasattr(erasure, 'type') and erasure.type == 'DataWipe': if hasattr(erasure, 'type') and erasure.type == 'DataWipe':
if erasure.document.success: if erasure.document.success:
return 'Success' return 'Success'
return 'Failure' return 'Failure'
type_of_results = { type_of_results = {
Severity.Error: 'Failure', Severity.Error: 'Failure',
Severity.Warning: 'Success with Warnings', Severity.Warning: 'Success with Warnings',
Severity.Notice: 'Success', Severity.Notice: 'Success',
Severity.Info: 'Success' Severity.Info: 'Success',
} }
return type_of_results[erasure.severity] return type_of_results[erasure.severity]
def none2str(string): def none2str(string):
""" convert none to empty str """ """convert none to empty str"""
if string is None: if string is None:
return '' return ''
return format(string) return format(string)
def get_action(component, action): def get_action(component, action):
""" Filter one action from a component or return None """ """Filter one action from a component or return None"""
result = [a for a in component.actions if a.type == action] result = [a for a in component.actions if a.type == action]
return result[-1] if result else None return result[-1] if result else None
class ActionRow(OrderedDict): class ActionRow(OrderedDict):
def __init__(self, allocate):
def __init__(self, allocate):
super().__init__() super().__init__()
# General information about allocates, deallocate and lives # General information about allocates, deallocate and lives
self['DHID'] = allocate['devicehubID'] self['DHID'] = allocate['devicehubID']
@ -459,7 +458,6 @@ class ActionRow(OrderedDict):
class InternalStatsRow(OrderedDict): class InternalStatsRow(OrderedDict):
def __init__(self, user, create, actions): def __init__(self, user, create, actions):
super().__init__() super().__init__()
# General information about all internal stats # General information about all internal stats
@ -488,13 +486,7 @@ class InternalStatsRow(OrderedDict):
def count_actions(self): def count_actions(self):
for ac in self.actions: for ac in self.actions:
self.is_snapshot( self.is_snapshot(self.is_deallocate(self.is_live(self.is_allocate(ac))))
self.is_deallocate(
self.is_live(
self.is_allocate(ac)
)
)
)
def is_allocate(self, ac): def is_allocate(self, ac):
if ac.type == 'Allocate': if ac.type == 'Allocate':
@ -531,9 +523,18 @@ class InternalStatsRow(OrderedDict):
self['Snapshot (Registers)'] += 1 self['Snapshot (Registers)'] += 1
def quarter(self, month): def quarter(self, month):
q = {1: 'Q1', 2: 'Q1', 3: 'Q1', q = {
4: 'Q2', 5: 'Q2', 6: 'Q2', 1: 'Q1',
7: 'Q3', 8: 'Q3', 9: 'Q3', 2: 'Q1',
10: 'Q4', 11: 'Q4', 12: 'Q4', 3: 'Q1',
} 4: 'Q2',
5: 'Q2',
6: 'Q2',
7: 'Q3',
8: 'Q3',
9: 'Q3',
10: 'Q4',
11: 'Q4',
12: 'Q4',
}
return q[int(month)] return q[int(month)]