241 lines
8.1 KiB
Python
241 lines
8.1 KiB
Python
import copy
|
|
|
|
|
|
class MetricsMix:
|
|
"""we want get the data metrics of one device"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.actions.sort(key=lambda x: x.created)
|
|
self.rows = []
|
|
self.lifetime = 0
|
|
self.last_trade = None
|
|
self.action_create_by = 'Receiver'
|
|
self.status_receiver = 'Use'
|
|
self.status_supplier = ''
|
|
self.act = None
|
|
self.end_users = 0
|
|
self.final_user_code = ''
|
|
|
|
def get_template_row(self):
|
|
"""
|
|
This is a template of a row.
|
|
"""
|
|
return {'type': '',
|
|
'action_type': 'Status',
|
|
'document_name': '',
|
|
'status_receiver': self.status_receiver,
|
|
'status_supplier': self.status_supplier,
|
|
'status_receiver_created': '',
|
|
'status_supplier_created': '',
|
|
'trade_supplier': '',
|
|
'trade_receiver': self.act.author.email,
|
|
'trade_confirmed': '',
|
|
'trade_weight': 0,
|
|
'action_create_by': self.action_create_by,
|
|
'devicehubID': self.devicehub_id,
|
|
'hid': self.hid,
|
|
'finalUserCode': '',
|
|
'numEndUsers': 0,
|
|
'liveCreate': 0,
|
|
'usageTimeHdd': self.lifetime,
|
|
'created': self.act.created,
|
|
'start': '',
|
|
'usageTimeAllocate': 0}
|
|
|
|
def get_metrics(self):
|
|
"""
|
|
This method get a list of values for calculate a metrics from a spreadsheet
|
|
"""
|
|
return self.rows
|
|
|
|
|
|
class Metrics(MetricsMix):
|
|
"""we want get the data metrics of one device"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.device = kwargs.pop('device')
|
|
self.actions = copy.copy(self.device.actions)
|
|
super().__init__(*args, **kwargs)
|
|
self.hid = self.device.hid
|
|
self.devicehub_id = self.device.devicehub_id
|
|
|
|
def get_action_status(self):
|
|
"""
|
|
Mark the status of one device.
|
|
If exist one trade before this action, then modify the trade action
|
|
else, create one row new.
|
|
"""
|
|
self.status_receiver = self.act.type
|
|
self.status_supplier = ''
|
|
if self.act.author != self.act.rol_user:
|
|
# It is neccesary exist one trade action before
|
|
self.last_trade['status_supplier'] = self.act.type
|
|
self.last_trade['status_supplier_created'] = self.act.created
|
|
return
|
|
|
|
self.action_create_by = 'Receiver'
|
|
if self.last_trade:
|
|
# if exist one trade action before
|
|
self.last_trade['status_receiver'] = self.act.type
|
|
self.last_trade['status_receiver_created'] = self.act.created
|
|
return
|
|
|
|
# If not exist any trade action for this device
|
|
row = self.get_template_row()
|
|
row['status_receiver_created'] = self.act.created
|
|
self.rows.append(row)
|
|
|
|
def get_snapshot(self):
|
|
"""
|
|
If there are one snapshot get the last lifetime for to do a calcul of time of use.
|
|
"""
|
|
lifestimes = self.act.get_last_lifetimes()
|
|
if lifestimes:
|
|
self.lifetime = lifestimes[0]['lifetime']
|
|
|
|
def get_allocate(self):
|
|
"""
|
|
If the action is one Allocate, need modify the row base.
|
|
"""
|
|
self.end_users = self.act.end_users
|
|
self.final_user_code = self.act.final_user_code
|
|
row = self.get_template_row()
|
|
row['type'] = 'Allocate'
|
|
row['trade_supplier'] = ''
|
|
row['finalUserCode'] = self.final_user_code
|
|
row['numEndUsers'] = self.end_users
|
|
row['start'] = self.act.start_time
|
|
row['usageTimeAllocate'] = self.lifetime
|
|
self.rows.append(row)
|
|
|
|
def get_live(self):
|
|
"""
|
|
If the action is one Live, need modify the row base.
|
|
"""
|
|
row = self.get_template_row()
|
|
row['type'] = 'Live'
|
|
row['finalUserCode'] = self.final_user_code
|
|
row['numEndUsers'] = self.end_users
|
|
row['start'] = self.act.start_time
|
|
row['usageTimeAllocate'] = self.lifetime
|
|
row['liveCreate'] = self.act.created
|
|
if self.act.usage_time_hdd:
|
|
row['usageTimeHdd'] = self.act.usage_time_hdd.total_seconds() / 3600
|
|
self.rows.append(row)
|
|
|
|
def get_deallocate(self):
|
|
"""
|
|
If the action is one Dellocate, need modify the row base.
|
|
"""
|
|
row = self.get_template_row()
|
|
row['type'] = 'Deallocate'
|
|
row['start'] = self.act.start_time
|
|
self.rows.append(row)
|
|
|
|
def get_confirms(self):
|
|
"""
|
|
if the action is one trade action, is possible than have a list of confirmations.
|
|
Get the doble confirm for to know if this trade is confirmed or not.
|
|
"""
|
|
if self.device.trading == 'TradeConfirmed':
|
|
return True
|
|
return False
|
|
|
|
def get_trade(self):
|
|
"""
|
|
If this action is a trade action modify the base row.
|
|
"""
|
|
if self.act.author == self.act.user_from:
|
|
self.action_create_by = 'Supplier'
|
|
row = self.get_template_row()
|
|
self.last_trade = row
|
|
row['type'] = 'Trade'
|
|
row['action_type'] = 'Trade'
|
|
row['trade_supplier'] = self.act.user_from.email
|
|
row['trade_receiver'] = self.act.user_to.email
|
|
row['self.status_receiver'] = self.status_receiver
|
|
row['self.status_supplier'] = self.status_supplier
|
|
row['trade_confirmed'] = self.get_confirms()
|
|
self.rows.append(row)
|
|
|
|
def get_metrics(self):
|
|
"""
|
|
This method get a list of values for calculate a metrics from a spreadsheet
|
|
"""
|
|
for act in self.actions:
|
|
self.act = act
|
|
if act.type in ['Use', 'Refurbish', 'Recycling', 'Management']:
|
|
self.get_action_status()
|
|
continue
|
|
|
|
if act.type == 'Snapshot':
|
|
self.get_snapshot()
|
|
continue
|
|
|
|
if act.type == 'Allocate':
|
|
self.get_allocate()
|
|
continue
|
|
|
|
if act.type == 'Live':
|
|
self.get_live()
|
|
continue
|
|
|
|
if act.type == 'Deallocate':
|
|
self.get_deallocate()
|
|
continue
|
|
|
|
if act.type == 'Trade':
|
|
self.get_trade()
|
|
continue
|
|
|
|
return self.rows
|
|
|
|
|
|
class TradeMetrics(MetricsMix):
|
|
"""we want get the data metrics of one device"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.document = kwargs.pop('document')
|
|
self.actions = copy.copy(self.document.actions)
|
|
self.hid = self.document.file_hash
|
|
self.devicehub_id = ''
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def get_metrics(self):
|
|
self.last_trade = next(x for x in self.actions if x.t == 'Trade')
|
|
self.act = self.last_trade
|
|
row = self.get_template_row()
|
|
|
|
row['type'] = 'Trade-Document'
|
|
row['action_type'] = 'Trade-Document'
|
|
if self.document.weight:
|
|
row['type'] = 'Trade-Container'
|
|
row['action_type'] = 'Trade-Container'
|
|
|
|
row['document_name'] = self.document.file_name
|
|
row['trade_supplier'] = self.last_trade.user_from.email
|
|
row['trade_receiver'] = self.last_trade.user_to.email
|
|
row['trade_confirmed'] = self.get_confirms()
|
|
row['status_receiver'] = ''
|
|
row['status_supplier'] = ''
|
|
row['trade_weight'] = self.document.weight
|
|
if self.last_trade.author == self.last_trade.user_from:
|
|
row['action_create_by'] = 'Supplier'
|
|
elif self.last_trade.author == self.last_trade.user_to:
|
|
row['action_create_by'] = 'Receiver'
|
|
|
|
self.rows.append(row)
|
|
|
|
return self.rows
|
|
|
|
def get_confirms(self):
|
|
"""
|
|
if the action is one trade action, is possible than have a list of confirmations.
|
|
Get the doble confirm for to know if this trade is confirmed or not.
|
|
"""
|
|
if hasattr(self.last_trade, 'acceptances_document'):
|
|
accept = self.last_trade.acceptances_document[-1]
|
|
if accept.t == 'Confirm' and accept.user == self.last_trade.user_to:
|
|
return True
|
|
return False
|