From 0eed8330cef4fdcde30aa85f9a57de7835f70acd Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 15 Oct 2021 15:04:58 +0200 Subject: [PATCH] adding metrics for documents --- ereuse_devicehub/resources/action/models.py | 11 +++++++ ereuse_devicehub/resources/device/metrics.py | 33 ++++++++++++++++--- ereuse_devicehub/resources/device/models.py | 2 +- .../resources/documents/documents.py | 18 ++++++++++ tests/test_metrics.py | 2 +- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 853fd49b..f2df237d 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -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) diff --git a/ereuse_devicehub/resources/device/metrics.py b/ereuse_devicehub/resources/device/metrics.py index 68a790b4..80840b87 100644 --- a/ereuse_devicehub/resources/device/metrics.py +++ b/ereuse_devicehub/resources/device/metrics.py @@ -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 = '' + diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index d940534d..2322326a 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -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): diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index f6de8884..41606d0b 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -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) diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 46c2a20c..70c728d2 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -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']],