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/device/metrics.py

289 lines
9.6 KiB
Python
Raw Normal View History

2021-10-14 10:56:33 +00:00
import copy
2021-10-15 13:04:58 +00:00
class MetricsMix:
2021-10-14 10:56:33 +00:00
"""we want get the data metrics of one device"""
2021-10-15 13:04:58 +00:00
def __init__(self, *args, **kwargs):
# self.actions.sort(key=lambda x: x.created)
2021-10-14 10:56:33 +00:00
self.rows = []
self.lifetime = 0
self.last_trade = None
self.action_create_by = 'Receiver'
2021-11-03 09:37:15 +00:00
self.status_receiver = ''
2021-10-14 10:56:33 +00:00
self.status_supplier = ''
self.act = None
self.end_users = 0
self.final_user_code = ''
self.trades = {}
2021-10-14 10:56:33 +00:00
def get_template_row(self):
"""
This is a template of a row.
"""
2022-12-19 19:03:34 +00:00
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,
}
2021-10-14 10:56:33 +00:00
2021-10-15 13:04:58 +00:00
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):
2021-10-19 16:19:25 +00:00
self.device = kwargs.pop('device')
self.actions = copy.copy(self.device.actions)
2021-10-15 13:04:58 +00:00
super().__init__(*args, **kwargs)
2022-12-19 19:03:34 +00:00
self.hid = self.device.chid
2021-10-19 16:19:25 +00:00
self.devicehub_id = self.device.devicehub_id
2021-10-15 13:04:58 +00:00
2021-10-14 10:56:33 +00:00
def get_action_status(self):
"""
Mark the status of one device.
If exist one trade before this action, then modify the trade action
2021-11-03 09:37:15 +00:00
else, create one new row.
2021-10-14 10:56:33 +00:00
"""
2021-11-05 12:00:01 +00:00
if self.act.trade not in self.trades:
2021-11-02 11:47:23 +00:00
# If not exist one trade, the status is of the Receive
self.action_create_by = 'Receiver'
self.status_receiver = self.act.type
self.status_supplier = ''
row = self.get_template_row()
2021-11-03 09:37:15 +00:00
row['status_supplier_created'] = ''
2021-11-02 11:47:23 +00:00
row['status_receiver_created'] = self.act.created
self.rows.append(row)
return
2021-11-05 12:00:01 +00:00
trade = self.trades[self.act.trade]
if trade['trade_supplier'] == self.act.author.email:
trade['status_supplier'] = self.act.type
trade['status_supplier_created'] = self.act.created
2021-10-14 10:56:33 +00:00
return
2021-11-05 12:00:01 +00:00
if trade['trade_receiver'] == self.act.author.email:
trade['status_receiver'] = self.act.type
trade['status_receiver_created'] = self.act.created
2021-10-14 10:56:33 +00:00
return
2021-11-05 11:35:10 +00:00
# necesitamos poder poner un cambio de estado de un trade mas antiguo que last_trade
# lo mismo con confirm
2021-10-14 10:56:33 +00:00
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.action_create_by = 'Receiver'
2021-10-14 10:56:33 +00:00
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.
"""
self.action_create_by = 'Receiver'
2021-10-14 10:56:33 +00:00
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.
"""
self.action_create_by = 'Receiver'
2021-10-14 10:56:33 +00:00
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.
"""
return self.device.trading(self.act.lot, simple=True)
2021-10-14 10:56:33 +00:00
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'
self.status_receiver = ''
2021-10-14 10:56:33 +00:00
row = self.get_template_row()
self.last_trade = row
row['type'] = 'Trade'
row['action_type'] = 'Trade'
2021-10-14 16:13:20 +00:00
row['trade_supplier'] = self.act.user_from.email
row['trade_receiver'] = self.act.user_to.email
row['status_receiver'] = self.status_receiver
2021-11-03 09:37:15 +00:00
row['status_supplier'] = ''
2021-10-14 10:56:33 +00:00
row['trade_confirmed'] = self.get_confirms()
2021-11-05 12:00:01 +00:00
self.trades[self.act] = row
2021-10-14 10:56:33 +00:00
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
2021-10-15 13:04:58 +00:00
class TradeMetrics(MetricsMix):
"""we want get the data metrics of one device"""
def __init__(self, *args, **kwargs):
2021-10-19 16:19:25 +00:00
self.document = kwargs.pop('document')
self.actions = copy.copy(self.document.actions)
2021-11-23 10:08:48 +00:00
self.reversed_actions = copy.copy(self.document.actions)
2021-10-19 16:19:25 +00:00
self.hid = self.document.file_hash
2021-10-15 13:04:58 +00:00
self.devicehub_id = ''
2021-10-19 16:19:25 +00:00
super().__init__(*args, **kwargs)
2021-11-23 10:08:48 +00:00
self.reversed_actions.reverse()
2021-10-19 16:19:25 +00:00
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'
2021-11-23 10:08:48 +00:00
if self.document.total_weight or self.document.weight:
2021-10-19 16:19:25 +00:00
row['type'] = 'Trade-Container'
2021-10-19 17:25:17 +00:00
row['action_type'] = 'Trade-Container'
2021-10-19 16:19:25 +00:00
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()
2021-10-19 17:25:17 +00:00
row['status_receiver'] = ''
row['status_supplier'] = ''
2021-11-22 09:16:39 +00:00
row['trade_weight'] = self.document.total_weight
2021-11-08 16:33:22 +00:00
if self.document.owner == self.last_trade.user_from:
2021-10-19 16:19:25 +00:00
row['action_create_by'] = 'Supplier'
2021-11-08 16:33:22 +00:00
elif self.document.owner == self.last_trade.user_to:
2021-10-19 16:19:25 +00:00
row['action_create_by'] = 'Receiver'
2021-11-23 10:08:48 +00:00
self.get_status(row)
2021-10-19 16:19:25 +00:00
self.rows.append(row)
2021-10-15 13:04:58 +00:00
2021-10-19 16:19:25 +00:00
return self.rows
2021-11-23 10:08:48 +00:00
def get_status(self, row):
"""
We want to know if receiver or supplier do some action that change the status
of the container.
"""
if not (self.document.total_weight and self.document.weight):
return ''
for ac in self.reversed_actions:
if ac.type not in ['Use', 'Refurbish', 'Recycling', 'Management']:
continue
if ac.author == self.last_trade.user_from:
row['status_supplier'] = ac.type
row['status_supplier_created'] = ac.created
if ac.author == self.last_trade.user_to:
row['status_receiver'] = ac.type
row['status_receiver_created'] = ac.created
return ''
2021-10-19 16:19:25 +00:00
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.
"""
2021-11-08 16:33:22 +00:00
trade = None
confirmations = []
confirms = []
for ac in self.document.actions:
if ac.t == 'Trade':
trade = ac
elif ac.t == 'ConfirmDocument':
confirms.append(ac.author)
confirmations.append(ac)
elif ac.t in ['RevokeDocument', 'ConfirmDocumentRevoke']:
confirmations.append(ac)
if confirmations and confirmations[-1].t == 'ConfirmDocument':
if trade.user_from in confirms and trade.user_to in confirms:
2021-10-19 16:19:25 +00:00
return True
2021-11-08 16:33:22 +00:00
2021-10-19 16:19:25 +00:00
return False