adding metrics for documents

This commit is contained in:
Cayo Puigdefabregas 2021-10-15 15:04:58 +02:00
parent 17f8aebb94
commit 0eed8330ce
5 changed files with 60 additions and 6 deletions

View File

@ -49,6 +49,7 @@ from ereuse_devicehub.resources.enums import AppearanceRange, BatteryHealth, Bio
from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing
from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
from ereuse_devicehub.resources.device.metrics import TradeMetrics
class JoinedTableMixin:
@ -1633,6 +1634,16 @@ class Trade(JoinedTableMixin, ActionWithMultipleTradeDocuments):
cascade=CASCADE_OWN),
primaryjoin='Trade.lot_id == Lot.id')
def get_metrics(self):
"""
This method get a list of values for calculate a metrics from a spreadsheet
"""
metrics = []
for doc in self.documents:
m = TradeMetrics(document=doc, Trade=self)
metrics.append(m.get_metrics())
return metrics
def __repr__(self) -> str:
return '<{0.t} {0.id} executed by {0.author}>'.format(self)

View File

@ -1,12 +1,10 @@
import copy
class Metrics:
class MetricsMix:
"""we want get the data metrics of one device"""
def __init__(self, device):
self.hid = device.hid
self.devicehub_id = device.devicehub_id
def __init__(self, *args, **kwargs):
self.actions = copy.copy(device.actions)
self.actions.sort(key=lambda x: x.created)
self.rows = []
@ -44,6 +42,22 @@ class Metrics:
'start': self.act.created,
'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):
device = kwargs.pop('device')
super().__init__(*args, **kwargs)
self.hid = device.hid
self.devicehub_id = device.devicehub_id
def get_action_status(self):
"""
Mark the status of one device.
@ -176,3 +190,14 @@ class Metrics:
continue
return self.rows
class TradeMetrics(MetricsMix):
"""we want get the data metrics of one device"""
def __init__(self, *args, **kwargs):
document = kwargs.pop('document')
super().__init__(*args, **kwargs)
self.hid = document.hash
self.devicehub_id = ''

View File

@ -506,7 +506,7 @@ class Device(Thing):
"""
This method get a list of values for calculate a metrics from a spreadsheet
"""
metrics = Metrics(self)
metrics = Metrics(device=self)
return metrics.get_metrics()
def __lt__(self, other):

View File

@ -159,13 +159,31 @@ class ActionsDocumentView(DeviceView):
data = StringIO()
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
first = True
devs_id = []
for device in query:
devs_id.append(device.id)
for allocate in device.get_metrics():
d = ActionRow(allocate)
if first:
cw.writerow(d.keys())
first = False
cw.writerow(d.values())
from ereuse_devicehub.resources.action.models import Trade
from ereuse_devicehub.resources.device.models import Device
# import pdb; pdb.set_trace()
query_trade = Trade.query.filter(Trade.devices.any(Device.id.in_(devs_id))).all()
doc_metrics = []
for trade in query_trade:
doc_metrics.extend(trade.get_metrics())
for data_row in doc_metrics:
row = ActionRow(data_row)
if first:
cw.writerow(row.keys())
first = False
cw.writerow(row.values())
bfile = data.getvalue().encode('utf-8')
output = make_response(bfile)
insert_hash(bfile)

View File

@ -160,7 +160,7 @@ def test_complet_metrics(user: UserClient, user2: UserClient):
res=Lot,
item='{}/devices'.format(lot['id']),
query=devices)
import pdb; pdb.set_trace()
# import pdb; pdb.set_trace()
# request_post = {
# 'type': 'Trade',
# 'devices': [span1['device']['id'], snap2['device']['id']],